xref: /unit/src/nxt_mp.h (revision 128)
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 
4463Sigor@sysoev.ru /* nxt_mp_test_sizes() tests validity of memory pool parameters. */
4563Sigor@sysoev.ru NXT_EXPORT nxt_bool_t nxt_mp_test_sizes(size_t cluster_size,
4663Sigor@sysoev.ru     size_t page_alignment, size_t page_size, size_t min_chunk_size);
4763Sigor@sysoev.ru 
4863Sigor@sysoev.ru /* nxt_mp_is_empty() tests that pool is empty. */
4963Sigor@sysoev.ru NXT_EXPORT nxt_bool_t nxt_mp_is_empty(nxt_mp_t *mp);
5063Sigor@sysoev.ru 
5163Sigor@sysoev.ru 
5263Sigor@sysoev.ru /*
5363Sigor@sysoev.ru  * nxt_mp_alloc() returns aligned freeable memory.
5463Sigor@sysoev.ru  * The alignment is sutiable to allocate structures.
5563Sigor@sysoev.ru  */
5663Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_alloc(nxt_mp_t *mp, size_t size)
5763Sigor@sysoev.ru     NXT_MALLOC_LIKE;
5863Sigor@sysoev.ru 
5963Sigor@sysoev.ru 
6063Sigor@sysoev.ru /*
6163Sigor@sysoev.ru  * nxt_mp_zalloc() returns zeroed aligned freeable memory.
6263Sigor@sysoev.ru  * The alignment is sutiable to allocate structures.
6363Sigor@sysoev.ru  */
6463Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_zalloc(nxt_mp_t *mp, size_t size)
6563Sigor@sysoev.ru     NXT_MALLOC_LIKE;
6663Sigor@sysoev.ru 
6763Sigor@sysoev.ru /* nxt_mp_align() returns aligned freeable memory. */
6863Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size)
6963Sigor@sysoev.ru     NXT_MALLOC_LIKE;
7063Sigor@sysoev.ru 
7163Sigor@sysoev.ru /* nxt_mp_zalign() returns zeroed aligned freeable memory. */
7263Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size)
7363Sigor@sysoev.ru     NXT_MALLOC_LIKE;
7463Sigor@sysoev.ru 
7563Sigor@sysoev.ru /* nxt_mp_free() frees freeable memory. */
7663Sigor@sysoev.ru NXT_EXPORT void nxt_mp_free(nxt_mp_t *mp, void *p);
7763Sigor@sysoev.ru 
7863Sigor@sysoev.ru 
7963Sigor@sysoev.ru /*
8063Sigor@sysoev.ru  * nxt_mp_retain() returns aligned freeable memory and increases memory
8163Sigor@sysoev.ru  * pool retention counter.
8263Sigor@sysoev.ru  */
8363Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_retain(nxt_mp_t *mp, size_t size)
8463Sigor@sysoev.ru     NXT_MALLOC_LIKE;
8563Sigor@sysoev.ru 
8663Sigor@sysoev.ru /*
8763Sigor@sysoev.ru  * nxt_mp_release() returns freeable memory and decreases memory pool
8863Sigor@sysoev.ru  * retention counter.  If the counter becomes zero the pool is destroyed.
8963Sigor@sysoev.ru  */
9063Sigor@sysoev.ru NXT_EXPORT void nxt_mp_release(nxt_mp_t *mp, void *p);
9163Sigor@sysoev.ru 
9263Sigor@sysoev.ru 
9363Sigor@sysoev.ru /* nxt_mp_nget() returns non-aligned non-freeable memory. */
9463Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_nget(nxt_mp_t *mp, size_t size)
9563Sigor@sysoev.ru     NXT_MALLOC_LIKE;
9663Sigor@sysoev.ru 
9763Sigor@sysoev.ru /*
9863Sigor@sysoev.ru  * nxt_mp_get() returns aligned non-freeable memory.
9963Sigor@sysoev.ru  * The alignment is sutiable to allocate structures.
10063Sigor@sysoev.ru  */
10163Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_get(nxt_mp_t *mp, size_t size)
10263Sigor@sysoev.ru     NXT_MALLOC_LIKE;
10363Sigor@sysoev.ru 
10463Sigor@sysoev.ru /*
10563Sigor@sysoev.ru  * nxt_mp_zget() returns zeroed aligned non-freeable memory.
10663Sigor@sysoev.ru  * The alignment is sutiable to allocate structures.
10763Sigor@sysoev.ru  */
10863Sigor@sysoev.ru NXT_EXPORT void *nxt_mp_zget(nxt_mp_t *mp, size_t size)
10963Sigor@sysoev.ru     NXT_MALLOC_LIKE;
11063Sigor@sysoev.ru 
11163Sigor@sysoev.ru 
112*128Smax.romanov@nginx.com NXT_EXPORT void nxt_mp_thread_adopt(nxt_mp_t *mp);
113*128Smax.romanov@nginx.com 
11463Sigor@sysoev.ru #endif /* _NXT_MP_H_INCLUDED_ */
115