From: Daniel Gruno Date: Sat, 20 Apr 2013 11:20:22 +0000 (+0000) Subject: use ap_varbuf instead of allocating new strings each time we override an old one. X-Git-Tag: 2.5.0-alpha~5558 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=35f9ad9165354c8fb0e066530e14da09ebd45727;p=apache use ap_varbuf instead of allocating new strings each time we override an old one. This uses leaks less memory, but it's still not perfect (but it's a start - maybe I need to use a mutex for this, to override the original object without running into race conditions) git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1470155 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/modules/lua/lua_request.c b/modules/lua/lua_request.c index 6af2184f99..8a2ac91636 100644 --- a/modules/lua/lua_request.c +++ b/modules/lua/lua_request.c @@ -26,6 +26,7 @@ #include "scoreboard.h" #include "lua_dbd.h" #include "util_md5.h" +#include "util_varbuf.h" APLOG_USE_MODULE(lua); #define POST_MAX_VARS 500 @@ -1661,7 +1662,7 @@ static int lua_ivm_get(lua_State *L) { if (object) { if (object->type == LUA_TBOOLEAN) lua_pushboolean(L, object->number); else if (object->type == LUA_TNUMBER) lua_pushnumber(L, object->number); - else if (object->type == LUA_TSTRING) lua_pushlstring(L, object->string, object->size); + else if (object->type == LUA_TSTRING) lua_pushlstring(L, object->vb.buf, object->size); return 1; } else { @@ -1674,22 +1675,27 @@ static int lua_ivm_set(lua_State *L) { const char *key, *raw_key; const char *value = NULL; lua_ivm_object *object = NULL; + lua_ivm_object *old_object = NULL; + request_rec *r = ap_lua_check_request_rec(L, 1); key = luaL_checkstring(L, 2); luaL_checkany(L, 3); raw_key = apr_pstrcat(r->pool, "lua_ivm_", key, NULL); - /* This will require MaxConnectionsPerChild to be > 0, since it's - * essentially leaking memory as values are being overridden */ + apr_pool_userdata_get((void **)&old_object, raw_key, r->server->process->pool); object = apr_pcalloc(r->server->process->pool, sizeof(lua_ivm_object)); object->type = lua_type(L, 3); if (object->type == LUA_TNUMBER) object->number = lua_tonumber(L, 3); else if (object->type == LUA_TBOOLEAN) object->number = lua_tonumber(L, 3); else if (object->type == LUA_TSTRING) { value = lua_tolstring(L, 3, &object->size); - object->string = apr_pstrmemdup(r->server->process->pool, value, object->size); + ap_varbuf_init(r->server->process->pool, &object->vb, object->size); + ap_varbuf_strmemcat(&object->vb, value, object->size); } apr_pool_userdata_set(object, raw_key, NULL, r->server->process->pool); + if (old_object && old_object->type == LUA_TSTRING) { + ap_varbuf_free(&old_object->vb); + } return 0; } diff --git a/modules/lua/lua_request.h b/modules/lua/lua_request.h index 460e2c1824..903af004f8 100644 --- a/modules/lua/lua_request.h +++ b/modules/lua/lua_request.h @@ -16,7 +16,7 @@ */ #include "mod_lua.h" - +#include "util_varbuf.h" #ifndef _LUA_REQUEST_H_ #define _LUA_REQUEST_H_ @@ -40,8 +40,8 @@ typedef struct typedef struct { int type; size_t size; - void* string; lua_Number number; + struct ap_varbuf vb; } lua_ivm_object; #endif /* !_LUA_REQUEST_H_ */