From bba8f4af26227c9af3289e12058797190fe381f8 Mon Sep 17 00:00:00 2001 From: Konstantin Osipov <kostja@tarantool.org> Date: Mon, 8 Oct 2012 23:25:33 +0400 Subject: [PATCH] Dont' malloc() struct coio. Dont' malloc() struct coio. It's tiny and safe (unless ev_io_start() is done) to pass around by value. --- mod/box/memcached.m | 9 ++++----- src/admin.m | 7 +++---- src/admin.rl | 7 +++---- src/coio.m | 13 ++++--------- src/iproto.m | 13 ++++++------- 5 files changed, 20 insertions(+), 29 deletions(-) diff --git a/mod/box/memcached.m b/mod/box/memcached.m index 71a5ee5c5f..2e1ac538bb 100644 --- a/mod/box/memcached.m +++ b/mod/box/memcached.m @@ -378,13 +378,13 @@ memcached_loop(struct coio *coio) void memcached_handler(va_list ap) { - struct coio *coio = va_arg(ap, struct coio *); + struct coio coio = va_arg(ap, struct coio); stats.total_connections++; stats.curr_connections++; @try { - memcached_loop(coio); - iov_flush(coio); + memcached_loop(&coio); + iov_flush(&coio); } @catch (FiberCancelException *e) { @throw; } @catch (tnt_Exception *e) { @@ -393,8 +393,7 @@ void memcached_handler(va_list ap) iov_reset(); fiber_sleep(0.01); stats.curr_connections--; - coio_close(coio); - free(coio); + coio_close(&coio); } } diff --git a/src/admin.m b/src/admin.m index 5236363682..cfc9d66955 100644 --- a/src/admin.m +++ b/src/admin.m @@ -1910,19 +1910,18 @@ case 134: static void admin_handler(va_list ap) { - struct coio *coio = va_arg(ap, struct coio *); + struct coio coio = va_arg(ap, struct coio); lua_State *L = lua_newthread(tarantool_L); int coro_ref = luaL_ref(tarantool_L, LUA_REGISTRYINDEX); @try { for (;;) { - if (admin_dispatch(coio, L) < 0) + if (admin_dispatch(&coio, L) < 0) return; fiber_gc(); } } @finally { luaL_unref(tarantool_L, LUA_REGISTRYINDEX, coro_ref); - coio_close(coio); - free(coio); + coio_close(&coio); } } diff --git a/src/admin.rl b/src/admin.rl index 66847749ef..f1deecde6a 100644 --- a/src/admin.rl +++ b/src/admin.rl @@ -339,19 +339,18 @@ admin_dispatch(struct coio *coio, lua_State *L) static void admin_handler(va_list ap) { - struct coio *coio = va_arg(ap, struct coio *); + struct coio coio = va_arg(ap, struct coio); lua_State *L = lua_newthread(tarantool_L); int coro_ref = luaL_ref(tarantool_L, LUA_REGISTRYINDEX); @try { for (;;) { - if (admin_dispatch(coio, L) < 0) + if (admin_dispatch(&coio, L) < 0) return; fiber_gc(); } } @finally { luaL_unref(tarantool_L, LUA_REGISTRYINDEX, coro_ref); - coio_close(coio); - free(coio); + coio_close(&coio); } } diff --git a/src/coio.m b/src/coio.m index 8e1ee0d4d2..e4cb895f58 100644 --- a/src/coio.m +++ b/src/coio.m @@ -254,12 +254,9 @@ coio_service_on_accept(struct evio_service *evio_service, int fd, struct sockaddr_in *addr) { struct coio_service *service = evio_service->on_accept_param; - struct coio *coio = malloc(sizeof(struct coio)); + struct coio coio; - if (coio == NULL) - goto error; - - coio_init(coio, fd); + coio_init(&coio, fd); /* Set connection name. */ char name[SERVICE_NAME_MAXLEN]; @@ -268,15 +265,13 @@ coio_service_on_accept(struct evio_service *evio_service, /* Create the worker fiber. */ struct fiber *f = fiber_create(name, service->handler); - if (f == NULL) { - free(coio); + if (f == NULL) goto error; - } /* * The coio is passed on to the created fiber, reset the * libev callback param to point at it. */ - coio->ev.data = f; + coio.ev.data = f; /* * Start the created fiber. It becomes the coio object owner * and will have to close it and free before termination. diff --git a/src/iproto.m b/src/iproto.m index 4495dd8e04..cb6ea44cfd 100644 --- a/src/iproto.m +++ b/src/iproto.m @@ -62,13 +62,13 @@ iproto_flush(struct coio *coio, ssize_t to_read) void iproto_interact(va_list ap) { - struct coio *coio = va_arg(ap, struct coio *); + struct coio coio = va_arg(ap, struct coio); iproto_callback callback = va_arg(ap, iproto_callback); struct tbuf *in = &fiber->rbuf; ssize_t to_read = sizeof(struct iproto_header); @try { for (;;) { - if (to_read > 0 && coio_bread(coio, in, to_read) <= 0) + if (to_read > 0 && coio_bread(&coio, in, to_read) <= 0) break; /* validating iproto package header */ @@ -78,20 +78,19 @@ iproto_interact(va_list ap) + iproto(in)->len; to_read = request_len - in->size; - iproto_flush(coio, to_read); + iproto_flush(&coio, to_read); - if (to_read > 0 && coio_bread(coio, in, to_read) <= 0) + if (to_read > 0 && coio_bread(&coio, in, to_read) <= 0) break; struct tbuf *request = tbuf_split(in, request_len); iproto_reply(callback, request); to_read = sizeof(struct iproto_header) - in->size; - iproto_flush(coio, to_read); + iproto_flush(&coio, to_read); } } @finally { - coio_close(coio); - free(coio); + coio_close(&coio); } } -- GitLab