Browse Source

Prepare data for server status (Step 2/?) (See #243)

bel2125 8 years ago
parent
commit
b1e46087a8
5 changed files with 94 additions and 47 deletions
  1. 46 32
      src/civetweb.c
  2. 1 1
      src/handle_form.inl
  3. 2 2
      src/mod_duktape.inl
  4. 43 11
      src/mod_lua.inl
  5. 2 1
      src/timer.inl

+ 46 - 32
src/civetweb.c

@@ -6232,7 +6232,7 @@ interpret_uri(struct mg_connection *conn,    /* in: request (must be valid) */
 	/* Step 9: Script resources may handle sub-resources */
 	/* Support PATH_INFO for CGI scripts. */
 	tmp_str_len = strlen(filename);
-	tmp_str = mg_malloc(tmp_str_len + PATH_MAX + 1);
+	tmp_str = mg_malloc_ctx(tmp_str_len + PATH_MAX + 1, conn->ctx);
 	if (!tmp_str) {
 		/* Out of memory */
 		goto interpret_cleanup;
@@ -8800,7 +8800,7 @@ addenv(struct cgi_environment *env, const char *fmt, ...)
 		if (space <= n) {
 			/* Allocate new buffer */
 			n = env->buflen + CGI_ENVIRONMENT_SIZE;
-			added = (char *)mg_realloc(env->buf, n);
+			added = (char *)mg_realloc_ctx(env->buf, n, env->conn->ctx);
 			if (!added) {
 				/* Out of memory */
 				mg_cry(env->conn,
@@ -8867,10 +8867,10 @@ prepare_cgi_environment(struct mg_connection *conn,
 	env->conn = conn;
 	env->buflen = CGI_ENVIRONMENT_SIZE;
 	env->bufused = 0;
-	env->buf = (char *)mg_malloc(env->buflen);
+	env->buf = (char *)mg_malloc_ctx(env->buflen, conn->ctx);
 	env->varlen = MAX_CGI_ENVIR_VARS;
 	env->varused = 0;
-	env->var = (char **)mg_malloc(env->buflen * sizeof(char *));
+	env->var = (char **)mg_malloc_ctx(env->buflen * sizeof(char *), conn->ctx);
 
 	addenv(env, "SERVER_NAME=%s", conn->ctx->config[AUTHENTICATION_DOMAIN]);
 	addenv(env, "SERVER_ROOT=%s", conn->ctx->config[DOCUMENT_ROOT]);
@@ -9183,7 +9183,7 @@ 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 = (char *)mg_malloc(buflen);
+	buf = (char *)mg_malloc_ctx(buflen, conn->ctx);
 	if (buf == NULL) {
 		mg_send_http_error(conn,
 		                   500,
@@ -10163,7 +10163,7 @@ read_websocket(struct mg_connection *conn,
 			/* Allocate space to hold websocket payload */
 			data = mem;
 			if (data_len > sizeof(mem)) {
-				data = (unsigned char *)mg_malloc(data_len);
+				data = (unsigned char *)mg_malloc_ctx(data_len, conn->ctx);
 				if (data == NULL) {
 					/* Allocation failed, exit the loop and then close the
 					 * connection */
@@ -10399,7 +10399,8 @@ mg_websocket_client_write(struct mg_connection *conn,
                           size_t dataLen)
 {
 	int retval = -1;
-	char *masked_data = (char *)mg_malloc(((dataLen + 7) / 4) * 4);
+	char *masked_data =
+	    (char *)mg_malloc_ctx(((dataLen + 7) / 4) * 4, conn->ctx);
 	uint32_t masking_key = (uint32_t)get_random();
 
 	if (masked_data == NULL) {
@@ -11014,7 +11015,9 @@ mg_set_handler_type(struct mg_context *ctx,
 	}
 
 	tmp_rh =
-	    (struct mg_handler_info *)mg_calloc(sizeof(struct mg_handler_info), 1);
+	    (struct mg_handler_info *)mg_calloc_ctx(sizeof(struct mg_handler_info),
+	                                            1,
+	                                            ctx);
 	if (tmp_rh == NULL) {
 		mg_unlock_context(ctx);
 		mg_cry(fc(ctx), "%s", "Cannot create new request handler struct, OOM");
@@ -12121,9 +12124,10 @@ set_ports_option(struct mg_context *ctx)
 		}
 
 		if ((ptr = (struct socket *)
-		         mg_realloc(ctx->listening_sockets,
-		                    (ctx->num_listening_sockets + 1)
-		                        * sizeof(ctx->listening_sockets[0]))) == NULL) {
+		         mg_realloc_ctx(ctx->listening_sockets,
+		                        (ctx->num_listening_sockets + 1)
+		                            * sizeof(ctx->listening_sockets[0]),
+		                        ctx)) == NULL) {
 
 			mg_cry(fc(ctx), "%s", "Out of memory");
 			closesocket(so.sock);
@@ -12131,10 +12135,11 @@ set_ports_option(struct mg_context *ctx)
 			continue;
 		}
 
-		if ((pfd = (struct pollfd *)mg_realloc(
-		         ctx->listening_socket_fds,
-		         (ctx->num_listening_sockets + 1)
-		             * sizeof(ctx->listening_socket_fds[0]))) == NULL) {
+		if ((pfd = (struct pollfd *)
+		         mg_realloc_ctx(ctx->listening_socket_fds,
+		                        (ctx->num_listening_sockets + 1)
+		                            * sizeof(ctx->listening_socket_fds[0]),
+		                        ctx)) == NULL) {
 
 			mg_cry(fc(ctx), "%s", "Out of memory");
 			closesocket(so.sock);
@@ -12624,7 +12629,9 @@ ssl_get_client_cert_info(struct mg_connection *conn)
 		 * using EVP_Digest. */
 		ilen = i2d_X509((void *)cert, NULL);
 		tmp_buf =
-		    (ilen > 0) ? (unsigned char *)mg_malloc((unsigned)ilen + 1) : NULL;
+		    (ilen > 0)
+		        ? (unsigned char *)mg_malloc_ctx((unsigned)ilen + 1, conn->ctx)
+		        : NULL;
 		if (tmp_buf) {
 			tmp_p = tmp_buf;
 			(void)i2d_X509((void *)cert, &tmp_p);
@@ -12641,7 +12648,8 @@ ssl_get_client_cert_info(struct mg_connection *conn)
 		}
 
 		conn->request_info.client_cert =
-		    (struct client_cert *)mg_malloc(sizeof(struct client_cert));
+		    (struct client_cert *)mg_malloc_ctx(sizeof(struct client_cert),
+		                                        conn->ctx);
 		if (conn->request_info.client_cert) {
 			conn->request_info.client_cert->subject = mg_strdup(str_subject);
 			conn->request_info.client_cert->issuer = mg_strdup(str_issuer);
@@ -13503,8 +13511,8 @@ mg_connect_client_impl(const struct mg_client_options *client_options,
 	                    &sa)) {
 		return NULL;
 	}
-	if ((conn = (struct mg_connection *)
-	         mg_calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE)) == NULL) {
+	if ((conn = (struct mg_connection *)mg_calloc_ctx(
+	         1, sizeof(*conn) + MAX_REQUEST_SIZE, conn->ctx)) == NULL) {
 		mg_snprintf(NULL,
 		            NULL, /* No truncation check for ebuf */
 		            ebuf,
@@ -14232,10 +14240,12 @@ mg_connect_websocket_client(const char *host,
 	newctx->context_type = 2;       /* ws/wss client context type */
 	newctx->cfg_worker_threads = 1; /* one worker thread will be created */
 	newctx->worker_threadids =
-	    (pthread_t *)mg_calloc(newctx->cfg_worker_threads, sizeof(pthread_t));
+	    (pthread_t *)mg_calloc_ctx(newctx->cfg_worker_threads,
+	                               sizeof(pthread_t),
+	                               newctx);
 	conn->ctx = newctx;
 	thread_data = (struct websocket_client_thread_data *)
-	    mg_calloc(sizeof(struct websocket_client_thread_data), 1);
+	    mg_calloc_ctx(sizeof(struct websocket_client_thread_data), 1, newctx);
 	thread_data->conn = conn;
 	thread_data->data_handler = data_func;
 	thread_data->close_handler = close_func;
@@ -14600,7 +14610,7 @@ worker_thread_run(struct worker_thread_args *thread_args)
 	/* Request buffers are not pre-allocated. They are private to the
 	 * request and do not contain any state information that might be
 	 * of interest to anyone observing a server status.  */
-	conn->buf = (char *)mg_malloc(MAX_REQUEST_SIZE);
+	conn->buf = (char *)mg_malloc_ctx(MAX_REQUEST_SIZE, conn->ctx);
 	if (conn->buf == NULL) {
 		mg_cry(fc(ctx),
 		       "Out of memory: Cannot allocate buffer for worker %i",
@@ -15329,8 +15339,9 @@ mg_start(const struct mg_callbacks *callbacks,
 #endif /* !_WIN32 && !__SYMBIAN32__ */
 
 	ctx->cfg_worker_threads = ((unsigned int)(workerthreadcount));
-	ctx->worker_threadids =
-	    (pthread_t *)mg_calloc(ctx->cfg_worker_threads, sizeof(pthread_t));
+	ctx->worker_threadids = (pthread_t *)mg_calloc_ctx(ctx->cfg_worker_threads,
+	                                                   sizeof(pthread_t),
+	                                                   ctx);
 	if (ctx->worker_threadids == NULL) {
 		mg_cry(fc(ctx), "Not enough memory for worker thread ID array");
 		free_context(ctx);
@@ -15338,8 +15349,9 @@ mg_start(const struct mg_callbacks *callbacks,
 		return NULL;
 	}
 	ctx->worker_connections =
-	    (struct mg_connection *)mg_calloc(ctx->cfg_worker_threads,
-	                                      sizeof(struct mg_connection));
+	    (struct mg_connection *)mg_calloc_ctx(ctx->cfg_worker_threads,
+	                                          sizeof(struct mg_connection),
+	                                          ctx);
 	if (ctx->worker_connections == NULL) {
 		mg_cry(fc(ctx), "Not enough memory for worker thread connection array");
 		free_context(ctx);
@@ -15349,8 +15361,9 @@ mg_start(const struct mg_callbacks *callbacks,
 
 
 #if defined(ALTERNATIVE_QUEUE)
-	ctx->client_wait_events =
-	    mg_calloc(sizeof(ctx->client_wait_events[0]), ctx->cfg_worker_threads);
+	ctx->client_wait_events = mg_calloc_ctx(sizeof(ctx->client_wait_events[0]),
+	                                        ctx->cfg_worker_threads,
+	                                        ctx);
 	if (ctx->client_wait_events == NULL) {
 		mg_cry(fc(ctx), "Not enough memory for worker event array");
 		mg_free(ctx->worker_threadids);
@@ -15359,8 +15372,9 @@ mg_start(const struct mg_callbacks *callbacks,
 		return NULL;
 	}
 
-	ctx->client_socks =
-	    mg_calloc(sizeof(ctx->client_socks[0]), ctx->cfg_worker_threads);
+	ctx->client_socks = mg_calloc_ctx(sizeof(ctx->client_socks[0]),
+	                                  ctx->cfg_worker_threads,
+	                                  ctx);
 	if (ctx->client_wait_events == NULL) {
 		mg_cry(fc(ctx), "Not enough memory for worker socket array");
 		mg_free(ctx->client_socks);
@@ -15409,8 +15423,8 @@ mg_start(const struct mg_callbacks *callbacks,
 
 	/* Start worker threads */
 	for (i = 0; i < ctx->cfg_worker_threads; i++) {
-		struct worker_thread_args *wta = (struct worker_thread_args *)mg_malloc(
-		    sizeof(struct worker_thread_args));
+		struct worker_thread_args *wta = (struct worker_thread_args *)
+		    mg_malloc_ctx(sizeof(struct worker_thread_args), ctx);
 		if (wta) {
 			wta->ctx = ctx;
 			wta->index = (int)i;

+ 1 - 1
src/handle_form.inl

@@ -90,7 +90,7 @@ url_encoded_field_get(const struct mg_connection *conn,
 {
 	char key_dec[1024];
 
-	char *value_dec = (char *)mg_malloc(value_len + 1);
+	char *value_dec = (char *)mg_malloc_ctx(value_len + 1, conn->ctx);
 	int value_dec_len, ret;
 
 	if (!value_dec) {

+ 2 - 2
src/mod_duktape.inl

@@ -22,14 +22,14 @@ static const char *civetweb_ctx_id = "\xFF"
 static void *
 mg_duk_mem_alloc(void *udata, duk_size_t size)
 {
-	return mg_malloc(size);
+	return mg_malloc_ctx(size, ???);
 }
 
 
 static void *
 mg_duk_mem_realloc(void *udata, void *ptr, duk_size_t newsize)
 {
-	return mg_realloc(ptr, newsize);
+	return mg_realloc_ctx(ptr, newsize, ???);
 }
 
 

+ 43 - 11
src/mod_lua.inl

@@ -654,6 +654,11 @@ lsp_get_var(lua_State *L)
 	const char *data, *var_name;
 	size_t data_len, occurrence;
 	int ret;
+	struct mg_context *ctx;
+
+	lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
+	lua_gettable(L, LUA_REGISTRYINDEX);
+	ctx = (struct mg_context *)lua_touserdata(L, -1);
 
 	if (num_args >= 2 && num_args <= 3) {
 		char *dst;
@@ -662,7 +667,7 @@ lsp_get_var(lua_State *L)
 		occurrence = (num_args > 2) ? (long)lua_tonumber(L, 3) : 0;
 
 		/* Allocate dynamically, so there is no internal limit for get_var */
-		dst = (char *)mg_malloc(data_len + 1);
+		dst = (char *)mg_malloc_ctx(data_len + 1, ctx);
 		if (!dst) {
 			return luaL_error(L, "out of memory in get_var() call");
 		}
@@ -727,6 +732,11 @@ lsp_get_cookie(lua_State *L)
 	const char *cookie;
 	const char *var_name;
 	int ret;
+	struct mg_context *ctx;
+
+	lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
+	lua_gettable(L, LUA_REGISTRYINDEX);
+	ctx = (struct mg_context *)lua_touserdata(L, -1);
 
 	if (num_args == 2) {
 		/* Correct number of arguments */
@@ -741,7 +751,7 @@ lsp_get_cookie(lua_State *L)
 			return luaL_error(L, "invalid get_cookie() call");
 		}
 
-		dst = (char *)mg_malloc(data_len + 1);
+		dst = (char *)mg_malloc_ctx(data_len + 1, ctx);
 		if (!dst) {
 			return luaL_error(L, "out of memory in get_cookie() call");
 		}
@@ -802,12 +812,17 @@ lsp_url_encode(lua_State *L)
 	size_t text_len;
 	char *dst;
 	int dst_len;
+	struct mg_context *ctx;
+
+	lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
+	lua_gettable(L, LUA_REGISTRYINDEX);
+	ctx = (struct mg_context *)lua_touserdata(L, -1);
 
 	if (num_args == 1) {
 		text = lua_tolstring(L, 1, &text_len);
 		if (text) {
 			dst_len = 3 * (int)text_len + 1;
-			dst = ((text_len < 0x2AAAAAAA) ? (char *)mg_malloc(dst_len)
+			dst = ((text_len < 0x2AAAAAAA) ? (char *)mg_malloc_ctx(dst_len, ctx)
 			                               : (char *)NULL);
 			if (dst) {
 				mg_url_encode(text, dst, dst_len);
@@ -837,13 +852,18 @@ lsp_url_decode(lua_State *L)
 	int is_form;
 	char *dst;
 	int dst_len;
+	struct mg_context *ctx;
+
+	lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
+	lua_gettable(L, LUA_REGISTRYINDEX);
+	ctx = (struct mg_context *)lua_touserdata(L, -1);
 
 	if (num_args == 1 || (num_args == 2 && lua_isboolean(L, 2))) {
 		text = lua_tolstring(L, 1, &text_len);
 		is_form = (num_args == 2) ? lua_isboolean(L, 2) : 0;
 		if (text) {
 			dst_len = (int)text_len + 1;
-			dst = ((text_len < 0x7FFFFFFF) ? (char *)mg_malloc(dst_len)
+			dst = ((text_len < 0x7FFFFFFF) ? (char *)mg_malloc_ctx(dst_len, ctx)
 			                               : (char *)NULL);
 			if (dst) {
 				mg_url_decode(text, (int)text_len, dst, dst_len, is_form);
@@ -871,11 +891,16 @@ lsp_base64_encode(lua_State *L)
 	const char *text;
 	size_t text_len;
 	char *dst;
+	struct mg_context *ctx;
+
+	lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
+	lua_gettable(L, LUA_REGISTRYINDEX);
+	ctx = (struct mg_context *)lua_touserdata(L, -1);
 
 	if (num_args == 1) {
 		text = lua_tolstring(L, 1, &text_len);
 		if (text) {
-			dst = (char *)mg_malloc(text_len * 8 / 6 + 4);
+			dst = (char *)mg_malloc_ctx(text_len * 8 / 6 + 4, ctx);
 			if (dst) {
 				base64_encode((const unsigned char *)text, (int)text_len, dst);
 				lua_pushstring(L, dst);
@@ -903,11 +928,16 @@ lsp_base64_decode(lua_State *L)
 	size_t text_len, dst_len;
 	int ret;
 	char *dst;
+	struct mg_context *ctx;
+
+	lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
+	lua_gettable(L, LUA_REGISTRYINDEX);
+	ctx = (struct mg_context *)lua_touserdata(L, -1);
 
 	if (num_args == 1) {
 		text = lua_tolstring(L, 1, &text_len);
 		if (text) {
-			dst = (char *)mg_malloc(text_len);
+			dst = (char *)mg_malloc_ctx(text_len, ctx);
 			if (dst) {
 				ret = base64_decode((const unsigned char *)text,
 				                    (int)text_len,
@@ -1250,8 +1280,9 @@ lwebsocket_set_timer(lua_State *L, int is_periodic)
 		timediff = (double)lua_tonumber(L, 2);
 		txt = lua_tostring(L, 1);
 		txt_len = strlen(txt);
-		arg = (struct laction_arg *)mg_malloc(sizeof(struct laction_arg)
-		                                      + txt_len + 10);
+		arg = (struct laction_arg *)mg_malloc_ctx(sizeof(struct laction_arg)
+		                                              + txt_len + 10,
+		                                          ctx);
 		arg->state = L;
 		arg->script = ws->script;
 		arg->pmutex = &(ws->ws_mutex);
@@ -1590,7 +1621,7 @@ lua_allocator(void *ud, void *ptr, size_t osize, size_t nsize)
 		mg_free(ptr);
 		return NULL;
 	}
-	return mg_realloc(ptr, nsize);
+	return mg_realloc_ctx(ptr, nsize, ???);
 }
 
 
@@ -1767,8 +1798,9 @@ lua_websocket_new(const char *script, struct mg_connection *conn)
 
 	if (*shared_websock_list == NULL) {
 		/* add ws to list */
-		*shared_websock_list = (struct mg_shared_lua_websocket_list *)
-		    mg_calloc(sizeof(struct mg_shared_lua_websocket_list), 1);
+		*shared_websock_list =
+		    (struct mg_shared_lua_websocket_list *)mg_calloc_ctx(
+		        sizeof(struct mg_shared_lua_websocket_list), 1, conn->ctx);
 		if (*shared_websock_list == NULL) {
 			mg_unlock_context(conn->ctx);
 			mg_cry(conn, "Cannot create shared websocket struct, OOM");

+ 2 - 1
src/timer.inl

@@ -192,7 +192,8 @@ timer_thread(void *thread_func_param)
 TIMER_API int
 timers_init(struct mg_context *ctx)
 {
-	ctx->timers = (struct ttimers *)mg_calloc(sizeof(struct ttimers), 1);
+	ctx->timers =
+	    (struct ttimers *)mg_calloc_ctx(sizeof(struct ttimers), 1, ctx);
 	(void)pthread_mutex_init(&ctx->timers->mutex, NULL);
 
 	(void)timer_getcurrenttime();