summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/nxt_event_engine.c44
-rw-r--r--src/nxt_event_engine.h8
-rw-r--r--src/nxt_sockaddr.c10
3 files changed, 48 insertions, 14 deletions
diff --git a/src/nxt_event_engine.c b/src/nxt_event_engine.c
index 31a35f6d..c2c66c82 100644
--- a/src/nxt_event_engine.c
+++ b/src/nxt_event_engine.c
@@ -556,19 +556,19 @@ nxt_event_engine_start(nxt_event_engine_t *engine)
void *
-nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *slot,
+nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint,
size_t size)
{
- uint8_t n;
+ uint32_t n;
nxt_uint_t items;
nxt_array_t *mem_cache;
nxt_mem_cache_t *cache;
nxt_mem_cache_block_t *block;
mem_cache = engine->mem_cache;
- n = *slot;
+ n = *hint;
- if (n == (uint8_t) -1) {
+ if (n == NXT_EVENT_ENGINE_NO_MEM_HINT) {
if (mem_cache == NULL) {
/* IPv4 nxt_sockaddr_t and HTTP/1 and HTTP/2 buffers. */
@@ -607,7 +607,9 @@ nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *slot,
found:
- *slot = n;
+ if (n < NXT_EVENT_ENGINE_NO_MEM_HINT) {
+ *hint = (uint8_t) n;
+ }
}
cache = mem_cache->elts;
@@ -626,15 +628,39 @@ nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *slot,
void
-nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t *slot, void *p)
+nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t hint, void *p,
+ size_t size)
{
+ uint32_t n;
+ nxt_array_t *mem_cache;
nxt_mem_cache_t *cache;
nxt_mem_cache_block_t *block;
block = p;
+ mem_cache = engine->mem_cache;
+ cache = mem_cache->elts;
- cache = engine->mem_cache->elts;
- cache = cache + *slot;
+ n = hint;
+
+ if (nxt_slow_path(n == NXT_EVENT_ENGINE_NO_MEM_HINT)) {
+
+ if (size != 0) {
+ for (n = 0; n < mem_cache->nelts; n++) {
+ if (cache[n].size == size) {
+ goto found;
+ }
+ }
+
+ nxt_alert(&engine->task,
+ "event engine mem free(%p, %z) not found", p, size);
+ }
+
+ goto done;
+ }
+
+found:
+
+ cache = cache + n;
if (cache->count < 16) {
cache->count++;
@@ -644,6 +670,8 @@ nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t *slot, void *p)
return;
}
+done:
+
nxt_mp_free(engine->mem_pool, p);
}
diff --git a/src/nxt_event_engine.h b/src/nxt_event_engine.h
index 365d9e89..0c349f7e 100644
--- a/src/nxt_event_engine.h
+++ b/src/nxt_event_engine.h
@@ -514,10 +514,12 @@ NXT_EXPORT void nxt_event_engine_post(nxt_event_engine_t *engine,
NXT_EXPORT void nxt_event_engine_signal(nxt_event_engine_t *engine,
nxt_uint_t signo);
-void *nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *slot,
+#define NXT_EVENT_ENGINE_NO_MEM_HINT 255
+
+void *nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint,
size_t size);
-void nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t *slot,
- void *p);
+void nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t hint,
+ void *p, size_t size);
nxt_inline nxt_event_engine_t *
diff --git a/src/nxt_sockaddr.c b/src/nxt_sockaddr.c
index 99cf54b4..57dfbfa6 100644
--- a/src/nxt_sockaddr.c
+++ b/src/nxt_sockaddr.c
@@ -23,11 +23,11 @@ static nxt_int_t nxt_job_sockaddr_inet_parse(nxt_job_sockaddr_parse_t *jbs);
nxt_sockaddr_t *
nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls)
{
- uint8_t hint;
size_t size;
+ uint8_t hint;
nxt_sockaddr_t *sa;
- hint = (uint8_t) -1;
+ hint = NXT_EVENT_ENGINE_NO_MEM_HINT;
size = offsetof(nxt_sockaddr_t, u) + ls->socklen + ls->address_length;
sa = nxt_event_engine_mem_alloc(engine, &hint, size);
@@ -56,7 +56,11 @@ nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls)
void
nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c)
{
- nxt_event_engine_mem_free(engine, &c->remote->cache_hint, c->remote);
+ nxt_sockaddr_t *sa;
+
+ sa = c->remote;
+
+ nxt_event_engine_mem_free(engine, sa->cache_hint, sa, 0);
}