163Sigor@sysoev.ru 263Sigor@sysoev.ru /* 363Sigor@sysoev.ru * Copyright (C) Igor Sysoev 463Sigor@sysoev.ru * Copyright (C) NGINX, Inc. 563Sigor@sysoev.ru */ 663Sigor@sysoev.ru 763Sigor@sysoev.ru #ifndef _NXT_MP_H_INCLUDED_ 863Sigor@sysoev.ru #define _NXT_MP_H_INCLUDED_ 963Sigor@sysoev.ru 1063Sigor@sysoev.ru 1163Sigor@sysoev.ru /* 1263Sigor@sysoev.ru * Memory pool keeps track of all allocations so they can be freed at once 1363Sigor@sysoev.ru * on pool destruction. A memory pool is not thread safe, so only one thread 1463Sigor@sysoev.ru * must work with the pool. If an allocation should be passed to another 1563Sigor@sysoev.ru * thread, it should be allocated with nxt_mp_retain() and then should be 1663Sigor@sysoev.ru * freed with nxt_mp_release(). These functions updates pool retention 1763Sigor@sysoev.ru * counter. Memory pools decrease number of malloc() and free() calls and 1863Sigor@sysoev.ru * thus reduces thread contention on locks in malloc library. Memory pools 1963Sigor@sysoev.ru * allow to make both freeable and non-freeable allocations. The freeable 2063Sigor@sysoev.ru * memory is allocated in fixed size chunks to decrease memory fragmentaiton 2163Sigor@sysoev.ru * on reallocations. The non-freeable memory is intended to allocate 2263Sigor@sysoev.ru * structures and other items which should be available until memory pool 2363Sigor@sysoev.ru * destruction. Due to allocation strategy described in nxt_mp.c memory pools 2463Sigor@sysoev.ru * may also improve data cache locality. 2563Sigor@sysoev.ru */ 2663Sigor@sysoev.ru 2763Sigor@sysoev.ru typedef struct nxt_mp_s nxt_mp_t; 2863Sigor@sysoev.ru 2963Sigor@sysoev.ru 3063Sigor@sysoev.ru /* 3163Sigor@sysoev.ru * nxt_mp_create() creates a memory pool and sets the pool's retention 3263Sigor@sysoev.ru * counter to 1. 3363Sigor@sysoev.ru */ 3463Sigor@sysoev.ru NXT_EXPORT nxt_mp_t *nxt_mp_create(size_t cluster_size, size_t page_alignment, 3563Sigor@sysoev.ru size_t page_size, size_t min_chunk_size) 3663Sigor@sysoev.ru NXT_MALLOC_LIKE; 3763Sigor@sysoev.ru 3863Sigor@sysoev.ru /* 3963Sigor@sysoev.ru * nxt_mp_destroy() destroys memory pool in spite of the pool's retention 4063Sigor@sysoev.ru * counter. 4163Sigor@sysoev.ru */ 4263Sigor@sysoev.ru NXT_EXPORT void nxt_mp_destroy(nxt_mp_t *mp); 4363Sigor@sysoev.ru 44*430Sigor@sysoev.ru /* 45*430Sigor@sysoev.ru * nxt_mp_retain() increases memory pool retention counter. 46*430Sigor@sysoev.ru */ 47*430Sigor@sysoev.ru NXT_EXPORT void nxt_mp_retain(nxt_mp_t *mp); 48*430Sigor@sysoev.ru 49*430Sigor@sysoev.ru /* 50*430Sigor@sysoev.ru * nxt_mp_release() decreases memory pool retention counter. 51*430Sigor@sysoev.ru * If the counter becomes zero the pool is destroyed. 52*430Sigor@sysoev.ru */ 53*430Sigor@sysoev.ru NXT_EXPORT void nxt_mp_release(nxt_mp_t *mp); 54*430Sigor@sysoev.ru 5563Sigor@sysoev.ru /* nxt_mp_test_sizes() tests validity of memory pool parameters. */ 5663Sigor@sysoev.ru NXT_EXPORT nxt_bool_t nxt_mp_test_sizes(size_t cluster_size, 5763Sigor@sysoev.ru size_t page_alignment, size_t page_size, size_t min_chunk_size); 5863Sigor@sysoev.ru 5963Sigor@sysoev.ru /* nxt_mp_is_empty() tests that pool is empty. */ 6063Sigor@sysoev.ru NXT_EXPORT nxt_bool_t nxt_mp_is_empty(nxt_mp_t *mp); 6163Sigor@sysoev.ru 6263Sigor@sysoev.ru 6363Sigor@sysoev.ru /* 6463Sigor@sysoev.ru * nxt_mp_alloc() returns aligned freeable memory. 6563Sigor@sysoev.ru * The alignment is sutiable to allocate structures. 6663Sigor@sysoev.ru */ 6763Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_alloc(nxt_mp_t *mp, size_t size) 6863Sigor@sysoev.ru NXT_MALLOC_LIKE; 6963Sigor@sysoev.ru 7063Sigor@sysoev.ru 7163Sigor@sysoev.ru /* 7263Sigor@sysoev.ru * nxt_mp_zalloc() returns zeroed aligned freeable memory. 7363Sigor@sysoev.ru * The alignment is sutiable to allocate structures. 7463Sigor@sysoev.ru */ 7563Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_zalloc(nxt_mp_t *mp, size_t size) 7663Sigor@sysoev.ru NXT_MALLOC_LIKE; 7763Sigor@sysoev.ru 7863Sigor@sysoev.ru /* nxt_mp_align() returns aligned freeable memory. */ 7963Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size) 8063Sigor@sysoev.ru NXT_MALLOC_LIKE; 8163Sigor@sysoev.ru 8263Sigor@sysoev.ru /* nxt_mp_zalign() returns zeroed aligned freeable memory. */ 8363Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size) 8463Sigor@sysoev.ru NXT_MALLOC_LIKE; 8563Sigor@sysoev.ru 8663Sigor@sysoev.ru /* nxt_mp_free() frees freeable memory. */ 8763Sigor@sysoev.ru NXT_EXPORT void nxt_mp_free(nxt_mp_t *mp, void *p); 8863Sigor@sysoev.ru 8963Sigor@sysoev.ru 9063Sigor@sysoev.ru /* nxt_mp_nget() returns non-aligned non-freeable memory. */ 9163Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_nget(nxt_mp_t *mp, size_t size) 9263Sigor@sysoev.ru NXT_MALLOC_LIKE; 9363Sigor@sysoev.ru 9463Sigor@sysoev.ru /* 9563Sigor@sysoev.ru * nxt_mp_get() returns aligned non-freeable memory. 9663Sigor@sysoev.ru * The alignment is sutiable to allocate structures. 9763Sigor@sysoev.ru */ 9863Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_get(nxt_mp_t *mp, size_t size) 9963Sigor@sysoev.ru NXT_MALLOC_LIKE; 10063Sigor@sysoev.ru 10163Sigor@sysoev.ru /* 10263Sigor@sysoev.ru * nxt_mp_zget() returns zeroed aligned non-freeable memory. 10363Sigor@sysoev.ru * The alignment is sutiable to allocate structures. 10463Sigor@sysoev.ru */ 10563Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_zget(nxt_mp_t *mp, size_t size) 10663Sigor@sysoev.ru NXT_MALLOC_LIKE; 10763Sigor@sysoev.ru 10863Sigor@sysoev.ru 109164Smax.romanov@nginx.com NXT_EXPORT nxt_int_t nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler, 110164Smax.romanov@nginx.com nxt_task_t *task, void *obj, void *data); 111164Smax.romanov@nginx.com 112164Smax.romanov@nginx.com 113128Smax.romanov@nginx.com NXT_EXPORT void nxt_mp_thread_adopt(nxt_mp_t *mp); 114128Smax.romanov@nginx.com 11563Sigor@sysoev.ru #endif /* _NXT_MP_H_INCLUDED_ */ 116