Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Subclass uv_getaddrinfo_t from uv_req_t.
Browse files Browse the repository at this point in the history
This patch also fixes #155. Since we no longer
memset clear the uv_getaddrinfo_t, the user can
now set the `uv_getaddrinfo_t->data` field without
problems.
  • Loading branch information
erickt authored and ry committed Sep 10, 2011
1 parent eb987bc commit efa1b54
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
10 changes: 5 additions & 5 deletions include/uv.h
Expand Up @@ -200,7 +200,6 @@ typedef enum {
UV_ASYNC,
UV_ARES_TASK,
UV_ARES_EVENT,
UV_GETADDRINFO,
UV_PROCESS
} uv_handle_type;

Expand All @@ -215,6 +214,7 @@ typedef enum {
UV_UDP_SEND,
UV_FS,
UV_WORK,
UV_GETADDRINFO,
UV_REQ_TYPE_PRIVATE
} uv_req_type;

Expand Down Expand Up @@ -737,14 +737,14 @@ void uv_ares_destroy(uv_loop_t*, ares_channel channel);


/*
* uv_getaddrinfo_t is a subclass of uv_handle_t
*
* TODO this should be a subclass of uv_req_t
* uv_getaddrinfo_t is a subclass of uv_req_t
*
* Request object for uv_getaddrinfo.
*/
struct uv_getaddrinfo_s {
UV_HANDLE_FIELDS
UV_REQ_FIELDS
/* read-only */
uv_loop_t* loop; \
UV_GETADDRINFO_PRIVATE_FIELDS
};

Expand Down
11 changes: 7 additions & 4 deletions src/unix/core.c
Expand Up @@ -607,7 +607,7 @@ static void getaddrinfo_thread_proc(eio_req *req) {


/* stub implementation of uv_getaddrinfo */
int uv_getaddrinfo(uv_loop_t* loop,
int uv_getaddrinfo(uv_loop_t* loop,
uv_getaddrinfo_t* handle,
uv_getaddrinfo_cb cb,
const char* hostname,
Expand All @@ -622,7 +622,10 @@ int uv_getaddrinfo(uv_loop_t* loop,
return -1;
}

memset(handle, 0, sizeof(uv_getaddrinfo_t));
uv__req_init((uv_req_t*)handle);
handle->type = UV_GETADDRINFO;
handle->loop = loop;
handle->cb = cb;

/* TODO don't alloc so much. */

Expand All @@ -633,10 +636,10 @@ int uv_getaddrinfo(uv_loop_t* loop,

/* TODO security! check lengths, check return values. */

handle->loop = loop;
handle->cb = cb;
handle->hostname = hostname ? strdup(hostname) : NULL;
handle->service = service ? strdup(service) : NULL;
handle->res = NULL;
handle->retcode = 0;

/* TODO check handle->hostname == NULL */
/* TODO check handle->service == NULL */
Expand Down
2 changes: 2 additions & 0 deletions src/win/getaddrinfo.c
Expand Up @@ -255,6 +255,8 @@ int uv_getaddrinfo(uv_loop_t* loop,
goto error;
}

uv_req_init((uv_req_init*)handle);

handle->getaddrinfo_cb = getaddrinfo_cb;
handle->res = NULL;
handle->type = UV_GETADDRINFO;
Expand Down
10 changes: 10 additions & 0 deletions test/test-getaddrinfo.c
Expand Up @@ -51,15 +51,20 @@ static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle,
int status,
struct addrinfo* res) {
int i;
int* data = (int*)handle->data;

for (i = 0; i < CONCURRENT_COUNT; i++) {
if (&getaddrinfo_handles[i] == handle) {
ASSERT(i == *data);

callback_counts[i]++;
break;
}
}
ASSERT (i < CONCURRENT_COUNT);

free(data);

getaddrinfo_cbs++;
}

Expand Down Expand Up @@ -88,12 +93,17 @@ TEST_IMPL(getaddrinfo_basic) {

TEST_IMPL(getaddrinfo_concurrent) {
int i, r;
int* data;

uv_init();

for (i = 0; i < CONCURRENT_COUNT; i++) {
callback_counts[i] = 0;

data = (int*)malloc(sizeof(int));
*data = i;
getaddrinfo_handles[i].data = data;

r = uv_getaddrinfo(uv_default_loop(),
&getaddrinfo_handles[i],
&getaddrinfo_cuncurrent_cb,
Expand Down

0 comments on commit efa1b54

Please sign in to comment.