Skip to content

Commit

Permalink
Handle luv_handle_t extra be gc correctly
Browse files Browse the repository at this point in the history
other clibs can manage how to gc extra data in luv_handle_t
  • Loading branch information
zhaozg committed Mar 15, 2020
1 parent 02237cd commit f206c52
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/async.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static int luv_new_async(lua_State* L) {
}
data = luv_setup_handle(L, ctx);
data->extra = (luv_thread_arg_t*)malloc(sizeof(luv_thread_arg_t));
data->extra_gc = free;
memset(data->extra, 0, sizeof(luv_thread_arg_t));
handle->data = data;
luv_check_callback(L, (luv_handle_t*)handle->data, LUV_ASYNC, 1);
Expand Down
4 changes: 2 additions & 2 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ static int luv_close(lua_State* L) {
static void luv_handle_free(uv_handle_t* handle) {
luv_handle_t* data = (luv_handle_t*)handle->data;
if (data) {
if (data->extra)
free(data->extra);
if (data->extra_gc)
data->extra_gc(data->extra);
free(data);
}
free(handle);
Expand Down
1 change: 1 addition & 0 deletions src/lhandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static luv_handle_t* luv_setup_handle(lua_State* L, luv_ctx_t* ctx) {
data->callbacks[1] = LUA_NOREF;
data->ctx = ctx;
data->extra = NULL;
data->extra_gc = NULL;
return data;
}

Expand Down
3 changes: 3 additions & 0 deletions src/lhandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@
#define LUV_FS_POLL 1
#define LUV_RESET 1

typedef int (*luv_handle_extra_gc) (void* ptr);

/* Ref for userdata and event callbacks */
typedef struct {
int ref;
int callbacks[2];
luv_ctx_t* ctx;
void* extra;
luv_handle_extra_gc extra_gc;
} luv_handle_t;

#ifdef LUV_SOURCE
Expand Down

0 comments on commit f206c52

Please sign in to comment.