diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-09-27 19:22:59 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-09-27 19:22:59 +0300 |
commit | bfa808d68997d74bfe0d54196bbbb649646787c1 (patch) | |
tree | 16751ca3da7dfd1c3c7830b3203fffdaea55b0cf /src/nxt_event_engine.c | |
parent | 752038eea857988045ce72252e0d1c19509981b3 (diff) | |
download | unit-bfa808d68997d74bfe0d54196bbbb649646787c1.tar.gz unit-bfa808d68997d74bfe0d54196bbbb649646787c1.tar.bz2 |
Event engine memory cache for nxt_sockaddr_t.
Introducing event engine memory cache and using the cache for
nxt_sockaddr_t structures.
Diffstat (limited to 'src/nxt_event_engine.c')
-rw-r--r-- | src/nxt_event_engine.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/nxt_event_engine.c b/src/nxt_event_engine.c index 7f20c4c8..e275571c 100644 --- a/src/nxt_event_engine.c +++ b/src/nxt_event_engine.c @@ -7,6 +7,20 @@ #include <nxt_main.h> +typedef struct nxt_mem_cache_block_s nxt_mem_cache_block_t; + +struct nxt_mem_cache_block_s { + nxt_mem_cache_block_t *next; +}; + + +typedef struct { + nxt_mem_cache_block_t *free; + uint32_t size; + uint32_t count; +} nxt_mem_cache_t; + + static nxt_int_t nxt_event_engine_post_init(nxt_event_engine_t *engine); static nxt_int_t nxt_event_engine_signal_pipe_create( nxt_event_engine_t *engine); @@ -541,6 +555,100 @@ nxt_event_engine_start(nxt_event_engine_t *engine) } +void * +nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint32_t *slot, + size_t size) +{ + 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; + + if (n == (uint32_t) -1) { + + if (mem_cache == NULL) { + /* IPv4 nxt_sockaddr_t and HTTP/1 and HTTP/2 buffers. */ + items = 3; +#if (NXT_INET6) + items++; +#endif +#if (NXT_HAVE_UNIX_DOMAIN) + items++; +#endif + + mem_cache = nxt_array_create(engine->mem_pool, items, + sizeof(nxt_mem_cache_t)); + if (nxt_slow_path(mem_cache == NULL)) { + return mem_cache; + } + + engine->mem_cache = mem_cache; + } + + cache = mem_cache->elts; + for (n = 0; n < mem_cache->nelts; n++) { + if (cache[n].size == size) { + goto found; + } + } + + cache = nxt_array_add(mem_cache); + if (nxt_slow_path(cache == NULL)) { + return cache; + } + + cache->free = NULL; + cache->size = size; + cache->count = 0; + + found: + + *slot = n; + } + + cache = mem_cache->elts; + cache = cache + n; + + block = cache->free; + + if (block != NULL) { + cache->free = block->next; + cache->count--; + + return block; + } + + return nxt_mp_alloc(engine->mem_pool, size); +} + + +void +nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint32_t *slot, void *p) +{ + nxt_mem_cache_t *cache; + nxt_mem_cache_block_t *block; + + block = p; + + cache = engine->mem_cache->elts; + cache = cache + *slot; + + if (cache->count < 16) { + cache->count++; + block->next = cache->free; + cache->free = block; + + return; + } + + nxt_mp_free(engine->mem_pool, p); +} + + #if (NXT_DEBUG) void nxt_event_engine_thread_adopt(nxt_event_engine_t *engine) |