]> granicus.if.org Git - apache/commitdiff
use ap_varbuf instead of allocating new strings each time we override an old one.
authorDaniel Gruno <humbedooh@apache.org>
Sat, 20 Apr 2013 11:20:22 +0000 (11:20 +0000)
committerDaniel Gruno <humbedooh@apache.org>
Sat, 20 Apr 2013 11:20:22 +0000 (11:20 +0000)
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

modules/lua/lua_request.c
modules/lua/lua_request.h

index 6af2184f99c05aaf7d5e3400b04b3f523b5d9e91..8a2ac916366b3ffed24a12fb94fb160247f2b0c6 100644 (file)
@@ -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;
 }
 
index 460e2c18245ca143a7eca663baf72b550f2f230b..903af004f8185b31f15aabe7ddb898705ba0c4ce 100644 (file)
@@ -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_ */