ext/libuv/src/win/getaddrinfo.c in libuv-1.2.0 vs ext/libuv/src/win/getaddrinfo.c in libuv-1.3.0

- old
+ new

@@ -18,11 +18,10 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ #include <assert.h> -#include <malloc.h> #include "uv.h" #include "internal.h" #include "req-inl.h" @@ -75,14 +74,17 @@ #define ALIGNED_SIZE(X) ((((X) + 3) >> 2) << 2) static void uv__getaddrinfo_work(struct uv__work* w) { uv_getaddrinfo_t* req; + struct addrinfoW* hints; int err; req = container_of(w, uv_getaddrinfo_t, work_req); - err = GetAddrInfoW(req->node, req->service, req->hints, &req->res); + hints = req->addrinfow; + req->addrinfow = NULL; + err = GetAddrInfoW(req->node, req->service, hints, &req->addrinfow); req->retcode = uv__getaddrinfo_translate_error(err); } /* @@ -106,28 +108,24 @@ req = container_of(w, uv_getaddrinfo_t, work_req); /* release input parameter memory */ if (req->alloc != NULL) { - free(req->alloc); + uv__free(req->alloc); req->alloc = NULL; } if (status == UV_ECANCELED) { assert(req->retcode == 0); req->retcode = UV_EAI_CANCELED; - if (req->res != NULL) { - FreeAddrInfoW(req->res); - req->res = NULL; - } goto complete; } if (req->retcode == 0) { /* convert addrinfoW to addrinfo */ /* first calculate required length */ - addrinfow_ptr = req->res; + addrinfow_ptr = req->addrinfow; while (addrinfow_ptr != NULL) { addrinfo_len += addrinfo_struct_len + ALIGNED_SIZE(addrinfow_ptr->ai_addrlen); if (addrinfow_ptr->ai_canonname != NULL) { name_len = uv_utf16_to_utf8(addrinfow_ptr->ai_canonname, -1, NULL, 0); @@ -139,16 +137,16 @@ } addrinfow_ptr = addrinfow_ptr->ai_next; } /* allocate memory for addrinfo results */ - alloc_ptr = (char*)malloc(addrinfo_len); + alloc_ptr = (char*)uv__malloc(addrinfo_len); /* do conversions */ if (alloc_ptr != NULL) { cur_ptr = alloc_ptr; - addrinfow_ptr = req->res; + addrinfow_ptr = req->addrinfow; while (addrinfow_ptr != NULL) { /* copy addrinfo struct data */ assert(cur_ptr + addrinfo_struct_len <= alloc_ptr + addrinfo_len); addrinfo_ptr = (struct addrinfo*)cur_ptr; @@ -194,35 +192,37 @@ addrinfow_ptr = addrinfow_ptr->ai_next; if (addrinfow_ptr != NULL) { addrinfo_ptr->ai_next = (struct addrinfo*)cur_ptr; } } + req->addrinfo = (struct addrinfo*)alloc_ptr; } else { req->retcode = UV_EAI_MEMORY; } } /* return memory to system */ - if (req->res != NULL) { - FreeAddrInfoW(req->res); - req->res = NULL; + if (req->addrinfow != NULL) { + FreeAddrInfoW(req->addrinfow); + req->addrinfow = NULL; } complete: uv__req_unregister(req->loop, req); /* finally do callback with converted result */ - req->getaddrinfo_cb(req, req->retcode, (struct addrinfo*)alloc_ptr); + if (req->getaddrinfo_cb) + req->getaddrinfo_cb(req, req->retcode, req->addrinfo); } void uv_freeaddrinfo(struct addrinfo* ai) { char* alloc_ptr = (char*)ai; /* release copied result memory */ if (alloc_ptr != NULL) { - free(alloc_ptr); + uv__free(alloc_ptr); } } /* @@ -248,20 +248,19 @@ int servicesize = 0; int hintssize = 0; char* alloc_ptr = NULL; int err; - if (req == NULL || getaddrinfo_cb == NULL || - (node == NULL && service == NULL)) { + if (req == NULL || (node == NULL && service == NULL)) { err = WSAEINVAL; goto error; } uv_req_init(loop, (uv_req_t*)req); req->getaddrinfo_cb = getaddrinfo_cb; - req->res = NULL; + req->addrinfo = NULL; req->type = UV_GETADDRINFO; req->loop = loop; req->retcode = 0; /* calculate required memory size for all input values */ @@ -284,11 +283,11 @@ if (hints != NULL) { hintssize = ALIGNED_SIZE(sizeof(struct addrinfoW)); } /* allocate memory for inputs, and partition it as needed */ - alloc_ptr = (char*)malloc(nodesize + servicesize + hintssize); + alloc_ptr = (char*)uv__malloc(nodesize + servicesize + hintssize); if (!alloc_ptr) { err = WSAENOBUFS; goto error; } @@ -325,33 +324,38 @@ req->service = NULL; } /* copy hints to allocated memory and save pointer in req */ if (hints != NULL) { - req->hints = (struct addrinfoW*)alloc_ptr; - req->hints->ai_family = hints->ai_family; - req->hints->ai_socktype = hints->ai_socktype; - req->hints->ai_protocol = hints->ai_protocol; - req->hints->ai_flags = hints->ai_flags; - req->hints->ai_addrlen = 0; - req->hints->ai_canonname = NULL; - req->hints->ai_addr = NULL; - req->hints->ai_next = NULL; + req->addrinfow = (struct addrinfoW*)alloc_ptr; + req->addrinfow->ai_family = hints->ai_family; + req->addrinfow->ai_socktype = hints->ai_socktype; + req->addrinfow->ai_protocol = hints->ai_protocol; + req->addrinfow->ai_flags = hints->ai_flags; + req->addrinfow->ai_addrlen = 0; + req->addrinfow->ai_canonname = NULL; + req->addrinfow->ai_addr = NULL; + req->addrinfow->ai_next = NULL; } else { - req->hints = NULL; + req->addrinfow = NULL; } - uv__work_submit(loop, - &req->work_req, - uv__getaddrinfo_work, - uv__getaddrinfo_done); - uv__req_register(loop, req); - return 0; + if (getaddrinfo_cb) { + uv__work_submit(loop, + &req->work_req, + uv__getaddrinfo_work, + uv__getaddrinfo_done); + return 0; + } else { + uv__getaddrinfo_work(&req->work_req); + uv__getaddrinfo_done(&req->work_req, 0); + return req->retcode; + } error: if (req != NULL && req->alloc != NULL) { - free(req->alloc); + uv__free(req->alloc); } return uv_translate_sys_error(err); }