Просмотр исходного кода

All memory allocation should use mg local functions

bel 11 лет назад
Родитель
Сommit
6cfa667500
2 измененных файлов с 96 добавлено и 72 удалено
  1. 67 46
      src/civetweb.c
  2. 29 26
      src/mod_lua.inl

+ 67 - 46
src/civetweb.c

@@ -319,6 +319,27 @@ typedef int SOCKET;
 #endif
 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
 
+void * mg_malloc(size_t size) {
+    return malloc(size);
+}
+
+void * mg_calloc(size_t count, size_t size) {
+    return calloc(count, size);
+}
+
+void * mg_realloc(void * memory, size_t newsize) {
+    return realloc(memory, newsize);
+}
+
+void mg_free(void * memory) {
+    free(memory);
+}
+
+#define malloc  DO_NOT_USE_THIS_FUNCTION__USE_mg_malloc
+#define calloc  DO_NOT_USE_THIS_FUNCTION__USE_mg_calloc
+#define realloc DO_NOT_USE_THIS_FUNCTION__USE_mg_realloc
+#define free    DO_NOT_USE_THIS_FUNCTION__USE_mg_free
+
 #ifdef _WIN32
 static CRITICAL_SECTION global_log_file_lock;
 static DWORD pthread_self(void)
@@ -935,7 +956,7 @@ static char * mg_strndup(const char *ptr, size_t len)
 {
     char *p;
 
-    if ((p = (char *) malloc(len + 1)) != NULL) {
+    if ((p = (char *) mg_malloc(len + 1)) != NULL) {
         mg_strlcpy(p, ptr, len + 1);
     }
 
@@ -1288,7 +1309,7 @@ static int pthread_cond_init(pthread_cond_t *cv, const void *unused)
     (void) unused;
     InitializeCriticalSection(&cv->threadIdSec);
     cv->waitingthreadcount = 0;
-    cv->waitingthreadhdls = calloc(MAX_WORKER_THREADS, sizeof(pthread_t));
+    cv->waitingthreadhdls = mg_calloc(MAX_WORKER_THREADS, sizeof(pthread_t));
     return (cv->waitingthreadhdls!=NULL) ? 0 : -1;
 }
 
@@ -1367,7 +1388,7 @@ static int pthread_cond_destroy(pthread_cond_t *cv)
 {
     EnterCriticalSection(&cv->threadIdSec);
     assert(cv->waitingthreadcount==0);
-    free(cv->waitingthreadhdls);
+    mg_free(cv->waitingthreadhdls);
     cv->waitingthreadhdls = 0;
     LeaveCriticalSection(&cv->threadIdSec);
     DeleteCriticalSection(&cv->threadIdSec);
@@ -1539,7 +1560,7 @@ static DIR * opendir(const char *name)
 
     if (name == NULL) {
         SetLastError(ERROR_BAD_ARGUMENTS);
-    } else if ((dir = (DIR *) malloc(sizeof(*dir))) == NULL) {
+    } else if ((dir = (DIR *) mg_malloc(sizeof(*dir))) == NULL) {
         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
     } else {
         to_unicode(name, wpath, ARRAY_SIZE(wpath));
@@ -1550,7 +1571,7 @@ static DIR * opendir(const char *name)
             dir->handle = FindFirstFileW(wpath, &dir->info);
             dir->result.d_name[0] = '\0';
         } else {
-            free(dir);
+            mg_free(dir);
             dir = NULL;
         }
     }
@@ -1566,7 +1587,7 @@ static int closedir(DIR *dir)
         if (dir->handle != INVALID_HANDLE_VALUE)
             result = FindClose(dir->handle) ? 0 : -1;
 
-        free(dir);
+        mg_free(dir);
     } else {
         result = -1;
         SetLastError(ERROR_BAD_ARGUMENTS);
@@ -2126,8 +2147,8 @@ static int alloc_vprintf2(char **buf, const char *fmt, va_list ap)
 
     *buf = NULL;
     while (len == -1) {
-        if (*buf) free(*buf);
-        *buf = (char *)malloc(size *= 4);
+        if (*buf) mg_free(*buf);
+        *buf = (char *)mg_malloc(size *= 4);
         if (!*buf) break;
         va_copy(ap_copy, ap);
         len = vsnprintf(*buf, size, fmt, ap_copy);
@@ -2164,7 +2185,7 @@ static int alloc_vprintf(char **buf, size_t size, const char *fmt, va_list ap)
         va_end(ap_copy);
     } else if (len > (int) size &&
                (size = len + 1) > 0 &&
-               (*buf = (char *) malloc(size)) == NULL) {
+               (*buf = (char *) mg_malloc(size)) == NULL) {
         len = -1;  /* Allocation failed, mark failure */
     } else {
         va_copy(ap_copy, ap);
@@ -2186,7 +2207,7 @@ int mg_vprintf(struct mg_connection *conn, const char *fmt, va_list ap)
         len = mg_write(conn, buf, (size_t) len);
     }
     if (buf != mem && buf != NULL) {
-        free(buf);
+        mg_free(buf);
     }
 
     return len;
@@ -3335,9 +3356,9 @@ struct dir_scan_data {
 /* Behaves like realloc(), but frees original pointer on failure */
 static void *realloc2(void *ptr, size_t size)
 {
-    void *new_ptr = realloc(ptr, size);
+    void *new_ptr = mg_realloc(ptr, size);
     if (new_ptr == NULL) {
-        free(ptr);
+        mg_free(ptr);
     }
     return new_ptr;
 }
@@ -3411,9 +3432,9 @@ static void handle_directory_request(struct mg_connection *conn,
               sizeof(data.entries[0]), compare_dir_entries);
         for (i = 0; i < data.num_entries; i++) {
             print_dir_entry(&data.entries[i]);
-            free(data.entries[i].file_name);
+            mg_free(data.entries[i].file_name);
         }
-        free(data.entries);
+        mg_free(data.entries);
     }
 
     conn->num_bytes_sent += mg_printf(conn, "%s", "</table></body></html>");
@@ -4053,7 +4074,7 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog)
        Do not send anything back to client, until we buffer in all
        HTTP headers. */
     data_len = 0;
-    buf = malloc(buflen);
+    buf = mg_malloc(buflen);
     if (buf == NULL) {
         send_http_error(conn, 500, http_500_error,
                         "Not enough memory for buffer (%u bytes)",
@@ -4136,7 +4157,7 @@ done:
         close(fdout[0]);
     }
     if (buf != NULL) {
-        free(buf);
+        mg_free(buf);
     }
 }
 #endif /* !NO_CGI */
@@ -4815,11 +4836,11 @@ static void read_websocket(struct mg_connection *conn)
             /* Allocate space to hold websocket payload */
             data = mem;
             if (data_len > sizeof(mem)) {
-                data = (char *)malloc(data_len);
+                data = (char *)mg_malloc(data_len);
                 if (data == NULL) {
                     /* Allocation failed, exit the loop and then close the
                        connection */
-                    mg_cry(conn, "websocket malloc() failed; closing connection");
+                    mg_cry(conn, "websocket out of memory; closing connection");
                     break;
                 }
             }
@@ -4893,7 +4914,7 @@ static void read_websocket(struct mg_connection *conn)
             }
 
             if (data != mem) {
-                free(data);
+                mg_free(data);
             }
             /* Not breaking the loop, process next websocket frame. */
         } else {
@@ -4972,7 +4993,7 @@ static void handle_websocket_request(struct mg_connection *conn, const char *pat
                                        path) : 0;
 
         if (lua_websock || shared_lua_websock) {
-            /* TODO */ shared_lua_websock=1;
+            /* TODO */ shared_lua_websock = 1;
             conn->lua_websocket_state = lua_websocket_new(path, conn, !!shared_lua_websock);
             if (conn->lua_websocket_state) {
                 send_websocket_handshake(conn);
@@ -5252,8 +5273,8 @@ void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_
                     lastref->next = tmp_rh->next;
                 else
                     ctx->request_handlers = tmp_rh->next;
-                free(tmp_rh->uri);
-                free(tmp_rh);
+                mg_free(tmp_rh->uri);
+                mg_free(tmp_rh);
             }
             return;
         }
@@ -5274,7 +5295,7 @@ void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_
         return;
     }
 
-    tmp_rh = (struct mg_request_handler_info *)malloc(sizeof(struct mg_request_handler_info));
+    tmp_rh = (struct mg_request_handler_info *)mg_malloc(sizeof(struct mg_request_handler_info));
     if (tmp_rh == NULL) {
         mg_cry(fc(ctx), "%s", "Cannot create new request handler struct, OOM");
         return;
@@ -5470,7 +5491,7 @@ static void close_all_listening_sockets(struct mg_context *ctx)
     for (i = 0; i < ctx->num_listening_sockets; i++) {
         closesocket(ctx->listening_sockets[i].sock);
     }
-    free(ctx->listening_sockets);
+    mg_free(ctx->listening_sockets);
 }
 
 static int is_valid_port(unsigned int port)
@@ -5568,12 +5589,12 @@ static int set_ports_option(struct mg_context *ctx)
                 closesocket(so.sock);
             }
             success = 0;
-        } else if ((ptr = (struct socket *) realloc(ctx->listening_sockets,
+        } else if ((ptr = (struct socket *) mg_realloc(ctx->listening_sockets,
                           (ctx->num_listening_sockets + 1) *
                           sizeof(ctx->listening_sockets[0]))) == NULL) {
             closesocket(so.sock);
             success = 0;
-        } else if ((portPtr = (in_port_t*) realloc(ctx->listening_ports,
+        } else if ((portPtr = (in_port_t*) mg_realloc(ctx->listening_ports,
                           (ctx->num_listening_sockets + 1) *
                           sizeof(ctx->listening_ports[0]))) == NULL) {
             closesocket(so.sock);
@@ -5822,7 +5843,7 @@ static int set_ssl_option(struct mg_context *ctx)
     /* Initialize locking callbacks, needed for thread safety.
        http://www.openssl.org/support/faq.html#PROG1 */
     size = sizeof(pthread_mutex_t) * CRYPTO_num_locks();
-    if ((ssl_mutexes = (pthread_mutex_t *) malloc((size_t)size)) == NULL) {
+    if ((ssl_mutexes = (pthread_mutex_t *) mg_malloc((size_t)size)) == NULL) {
         mg_cry(fc(ctx), "%s: cannot allocate mutexes: %s", __func__, ssl_error());
         return 0;
     }
@@ -5953,7 +5974,7 @@ void mg_close_connection(struct mg_connection *conn)
 #endif
     close_connection(conn);
     (void) pthread_mutex_destroy(&conn->mutex);
-    free(conn);
+    mg_free(conn);
 }
 
 struct mg_connection *mg_connect(const char *host, int port, int use_ssl,
@@ -5966,7 +5987,7 @@ struct mg_connection *mg_connect(const char *host, int port, int use_ssl,
     if ((sock = conn2(&fake_ctx, host, port, use_ssl, ebuf,
                       ebuf_len)) == INVALID_SOCKET) {
     } else if ((conn = (struct mg_connection *)
-                       calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE)) == NULL) {
+                       mg_calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE)) == NULL) {
         snprintf(ebuf, ebuf_len, "calloc(): %s", strerror(ERRNO));
         closesocket(sock);
 #ifndef NO_SSL
@@ -5974,7 +5995,7 @@ struct mg_connection *mg_connect(const char *host, int port, int use_ssl,
                                SSL_CTX_new(SSLv23_client_method())) == NULL) {
         snprintf(ebuf, ebuf_len, "SSL_CTX_new error");
         closesocket(sock);
-        free(conn);
+        mg_free(conn);
         conn = NULL;
 #endif /* NO_SSL */
     } else {
@@ -6100,7 +6121,7 @@ static void process_new_connection(struct mg_connection *conn)
             log_access(conn);
         }
         if (ri->remote_user != NULL) {
-            free((void *) ri->remote_user);
+            mg_free((void *) ri->remote_user);
             /* Important! When having connections with and without auth
                would cause double free and then crash */
             ri->remote_user = NULL;
@@ -6167,7 +6188,7 @@ static void *worker_thread_run(void *thread_func_param)
     tls.pthread_cond_helper_mutex = CreateEvent(NULL, FALSE, FALSE, NULL);
 #endif
 
-    conn = (struct mg_connection *) calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE);
+    conn = (struct mg_connection *) mg_calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE);
     if (conn == NULL) {
         mg_cry(fc(ctx), "%s", "Cannot create new connection struct, OOM");
     } else {
@@ -6219,7 +6240,7 @@ static void *worker_thread_run(void *thread_func_param)
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
     CloseHandle(tls.pthread_cond_helper_mutex);
 #endif
-    free(conn);
+    mg_free(conn);
 
     DEBUG_TRACE(("exiting"));
     return NULL;
@@ -6351,7 +6372,7 @@ static void master_thread_run(void *thread_func_param)
     ctx->start_time = (unsigned long)time(NULL);
 
     /* Allocate memory for the listening sockets, and start the server */
-    pfd = (struct pollfd *) calloc(ctx->num_listening_sockets, sizeof(pfd[0]));
+    pfd = (struct pollfd *) mg_calloc(ctx->num_listening_sockets, sizeof(pfd[0]));
     while (pfd != NULL && ctx->stop_flag == 0) {
         for (i = 0; i < ctx->num_listening_sockets; i++) {
             pfd[i].fd = ctx->listening_sockets[i].sock;
@@ -6371,7 +6392,7 @@ static void master_thread_run(void *thread_func_param)
             }
         }
     }
-    free(pfd);
+    mg_free(pfd);
     DEBUG_TRACE(("stopping workers"));
 
     /* Stop signal received: somebody called mg_stop. Quit. */
@@ -6445,15 +6466,15 @@ static void free_context(struct mg_context *ctx)
 #ifdef WIN32
 #pragma warning(suppress: 6001)
 #endif
-            free(ctx->config[i]);
+            mg_free(ctx->config[i]);
     }
 
     /* Deallocate request handlers */
     while (ctx->request_handlers) {
         tmp_rh = ctx->request_handlers;
         ctx->request_handlers = tmp_rh->next;
-        free(tmp_rh->uri);
-        free(tmp_rh);
+        mg_free(tmp_rh->uri);
+        mg_free(tmp_rh);
     }
 
 #ifndef NO_SSL
@@ -6462,14 +6483,14 @@ static void free_context(struct mg_context *ctx)
         SSL_CTX_free(ctx->ssl_ctx);
     }
     if (ssl_mutexes != NULL) {
-        free(ssl_mutexes);
+        mg_free(ssl_mutexes);
         ssl_mutexes = NULL;
     }
 #endif /* !NO_SSL */
 
     /* Deallocate worker thread ID array */
     if (ctx->workerthreadids != NULL) {
-        free(ctx->workerthreadids);
+        mg_free(ctx->workerthreadids);
     }
 
     /* Deallocate the tls variable */
@@ -6479,10 +6500,10 @@ static void free_context(struct mg_context *ctx)
     }
 
     /* deallocate system name string */
-    free(ctx->systemName);
+    mg_free(ctx->systemName);
 
     /* Deallocate context itself */
-    free(ctx);
+    mg_free(ctx);
 }
 
 void mg_stop(struct mg_context *ctx)
@@ -6552,14 +6573,14 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 
     /* Allocate context and initialize reasonable general case defaults.
        TODO(lsm): do proper error handling here. */
-    if ((ctx = (struct mg_context *) calloc(1, sizeof(*ctx))) == NULL) {
+    if ((ctx = (struct mg_context *) mg_calloc(1, sizeof(*ctx))) == NULL) {
         return NULL;
     }
 
     if (sTlsInit==0) {
         if (0 != pthread_key_create(&sTlsKey, NULL)) {
             mg_cry(fc(ctx), "Cannot initialize thread local storage");
-            free(ctx);
+            mg_free(ctx);
             return NULL;
         }
         sTlsInit++;
@@ -6585,7 +6606,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
         }
         if (ctx->config[i] != NULL) {
             mg_cry(fc(ctx), "warning: %s: duplicate option", name);
-            free(ctx->config[i]);
+            mg_free(ctx->config[i]);
         }
         ctx->config[i] = mg_strdup(value);
         DEBUG_TRACE(("[%s] -> [%s]", name, value));
@@ -6637,7 +6658,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 
     if (workerthreadcount > 0) {
         ctx->workerthreadcount = workerthreadcount;
-        ctx->workerthreadids = calloc(workerthreadcount, sizeof(pthread_t));
+        ctx->workerthreadids = mg_calloc(workerthreadcount, sizeof(pthread_t));
         if (ctx->workerthreadids == NULL) {
             mg_cry(fc(ctx), "Not enough memory for worker thread ID array");
             free_context(ctx);

+ 29 - 26
src/mod_lua.inl

@@ -570,11 +570,11 @@ static int lsp_base64_encode(lua_State *L)
     if (num_args==1) {
         text = lua_tolstring(L, 1, &text_len);
         if (text) {
-            dst = malloc(text_len*8/6+4);
+            dst = mg_malloc(text_len*8/6+4);
             if (dst) {
                 base64_encode(text, text_len, dst);
                 lua_pushstring(L, dst);
-                free(dst);
+                mg_free(dst);
             } else {
                 return luaL_error(L, "out of memory in base64_encode() call");
             }
@@ -600,15 +600,15 @@ static int lsp_base64_decode(lua_State *L)
     if (num_args==1) {
         text = lua_tolstring(L, 1, &text_len);
         if (text) {
-            dst = malloc(text_len);
+            dst = mg_malloc(text_len);
             if (dst) {
                 ret = base64_decode(text, text_len, dst, &dst_len);
                 if (ret != -1) {
-                    free(dst);
+                    mg_free(dst);
                     return luaL_error(L, "illegal character in lsp_base64_decode() call");
                 } else {
                     lua_pushlstring(L, dst, dst_len);
-                    free(dst);
+                    mg_free(dst);
                 }
             } else {
                 return luaL_error(L, "out of memory in lsp_base64_decode() call");
@@ -970,29 +970,30 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn,
     }
 
     if (!found) {
-        lws_data = (struct lua_websock_data *) malloc(sizeof(*lws_data));
-        lws_data->shared = is_shared;
+        lws_data = (struct lua_websock_data *) mg_malloc(sizeof(*lws_data));
     }
 
     if (lws_data) {
-        lws_data->conn = conn;
-        lws_data->script = mg_strdup(script);
-
-        if (is_shared && !found) {
-            (void)pthread_mutex_lock(&conn->ctx->mutex);
-            shared_websock_list = &(conn->ctx->shared_lua_websockets);
-            while (*shared_websock_list) {
-                shared_websock_list = &((*shared_websock_list)->next);
-            }
-            *shared_websock_list = (struct mg_shared_lua_websocket *)malloc(sizeof(struct mg_shared_lua_websocket));
-            if (*shared_websock_list) {
-                (*shared_websock_list)->sock = lws_data;
-                (*shared_websock_list)->next = 0;
+        if (!found) {
+            lws_data->shared = is_shared;
+            lws_data->conn = conn;
+            lws_data->script = mg_strdup(script);
+            lws_data->main = luaL_newstate();
+            if (is_shared) {
+                (void)pthread_mutex_lock(&conn->ctx->mutex);
+                shared_websock_list = &(conn->ctx->shared_lua_websockets);
+                while (*shared_websock_list) {
+                    shared_websock_list = &((*shared_websock_list)->next);
+                }
+                *shared_websock_list = (struct mg_shared_lua_websocket *)mg_malloc(sizeof(struct mg_shared_lua_websocket));
+                if (*shared_websock_list) {
+                    (*shared_websock_list)->sock = lws_data;
+                    (*shared_websock_list)->next = 0;
+                }
+                (void)pthread_mutex_unlock(&conn->ctx->mutex);
             }
-            (void)pthread_mutex_unlock(&conn->ctx->mutex);
         }
 
-        lws_data->main = luaL_newstate();
         if (lws_data->main) {
             prepare_lua_environment(conn, lws_data->main, script, LUA_ENV_TYPE_LUA_WEBSOCKET);
             if (conn->ctx->callbacks.init_lua != NULL) {
@@ -1019,7 +1020,7 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn,
 
         if (!ok) {
             if (lws_data->main) lua_close(lws_data->main);
-            free(lws_data);
+            mg_free(lws_data);
             lws_data=0;
         }
     } else {
@@ -1103,6 +1104,7 @@ static void lua_websocket_close(struct mg_connection *conn)
         (void)pthread_mutex_lock(&conn->ctx->mutex);
         lws_data->shared--;
         if (lws_data->shared==0) {
+        /*
             shared_websock_list = &(conn->ctx->shared_lua_websockets);
             while (*shared_websock_list) {
                 if ((*shared_websock_list)->sock == lws_data) {
@@ -1113,14 +1115,15 @@ static void lua_websocket_close(struct mg_connection *conn)
             }
 
             lua_close(lws_data->main);
-            free(lws_data->script);
+            mg_free(lws_data->script);
             lws_data->script=0;
-            free(lws_data);
+            mg_free(lws_data);
+         */
         }
         (void)pthread_mutex_unlock(&conn->ctx->mutex);
     } else {
         lua_close(lws_data->main);
-        free(lws_data);
+        mg_free(lws_data);
     }
     conn->lua_websocket_state = NULL;
 }