1 2 /* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) NGINX, Inc. 5 */ 6 7 #ifndef _NXT_MP_H_INCLUDED_ 8 #define _NXT_MP_H_INCLUDED_ 9 10 11 /* 12 * Memory pool keeps track of all allocations so they can be freed at once 13 * on pool destruction. A memory pool is not thread safe, so only one thread 14 * must work with the pool. If an allocation should be passed to another 15 * thread, it should be allocated with nxt_mp_retain() and then should be 16 * freed with nxt_mp_release(). These functions updates pool retention 17 * counter. Memory pools decrease number of malloc() and free() calls and 18 * thus reduces thread contention on locks in malloc library. Memory pools 19 * allow to make both freeable and non-freeable allocations. The freeable 20 * memory is allocated in fixed size chunks to decrease memory fragmentaiton 21 * on reallocations. The non-freeable memory is intended to allocate 22 * structures and other items which should be available until memory pool 23 * destruction. Due to allocation strategy described in nxt_mp.c memory pools 24 * may also improve data cache locality. 25 */ 26 27 typedef struct nxt_mp_s nxt_mp_t; 28 29 30 /* 31 * nxt_mp_create() creates a memory pool and sets the pool's retention 32 * counter to 1. 33 */ 34 NXT_EXPORT nxt_mp_t *nxt_mp_create(size_t cluster_size, size_t page_alignment, 35 size_t page_size, size_t min_chunk_size) 36 NXT_MALLOC_LIKE; 37 38 /* 39 * nxt_mp_destroy() destroys memory pool in spite of the pool's retention 40 * counter. 41 */ 42 NXT_EXPORT void nxt_mp_destroy(nxt_mp_t *mp); 43 44 /* nxt_mp_test_sizes() tests validity of memory pool parameters. */ 45 NXT_EXPORT nxt_bool_t nxt_mp_test_sizes(size_t cluster_size, 46 size_t page_alignment, size_t page_size, size_t min_chunk_size); 47 48 /* nxt_mp_is_empty() tests that pool is empty. */ 49 NXT_EXPORT nxt_bool_t nxt_mp_is_empty(nxt_mp_t *mp); 50 51 52 /* 53 * nxt_mp_alloc() returns aligned freeable memory. 54 * The alignment is sutiable to allocate structures. 55 */ 56 NXT_EXPORT void *nxt_mp_alloc(nxt_mp_t *mp, size_t size) 57 NXT_MALLOC_LIKE; 58 59 60 /* 61 * nxt_mp_zalloc() returns zeroed aligned freeable memory. 62 * The alignment is sutiable to allocate structures. 63 */ 64 NXT_EXPORT void *nxt_mp_zalloc(nxt_mp_t *mp, size_t size) 65 NXT_MALLOC_LIKE; 66 67 /* nxt_mp_align() returns aligned freeable memory. */ 68 NXT_EXPORT void *nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size) 69 NXT_MALLOC_LIKE; 70 71 /* nxt_mp_zalign() returns zeroed aligned freeable memory. */ 72 NXT_EXPORT void *nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size) 73 NXT_MALLOC_LIKE; 74 75 /* nxt_mp_free() frees freeable memory. */ 76 NXT_EXPORT void nxt_mp_free(nxt_mp_t *mp, void *p); 77 78 79 /* 80 * nxt_mp_retain() returns aligned freeable memory and increases memory 81 * pool retention counter. 82 */ 83 NXT_EXPORT void *nxt_mp_retain(nxt_mp_t *mp, size_t size) 84 NXT_MALLOC_LIKE; 85 86 /* 87 * nxt_mp_release() returns freeable memory and decreases memory pool 88 * retention counter. If the counter becomes zero the pool is destroyed. 89 */ 90 NXT_EXPORT uint32_t nxt_mp_release(nxt_mp_t *mp, void *p); 91 92 93 /* nxt_mp_nget() returns non-aligned non-freeable memory. */ 94 NXT_EXPORT void *nxt_mp_nget(nxt_mp_t *mp, size_t size) 95 NXT_MALLOC_LIKE; 96 97 /* 98 * nxt_mp_get() returns aligned non-freeable memory. 99 * The alignment is sutiable to allocate structures. 100 */ 101 NXT_EXPORT void *nxt_mp_get(nxt_mp_t *mp, size_t size) 102 NXT_MALLOC_LIKE; 103 104 /* 105 * nxt_mp_zget() returns zeroed aligned non-freeable memory. 106 * The alignment is sutiable to allocate structures. 107 */ 108 NXT_EXPORT void *nxt_mp_zget(nxt_mp_t *mp, size_t size) 109 NXT_MALLOC_LIKE; 110 111 112 NXT_EXPORT nxt_int_t nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler, 113 nxt_task_t *task, void *obj, void *data); 114 115 116 NXT_EXPORT void nxt_mp_thread_adopt(nxt_mp_t *mp); 117 118 #endif /* _NXT_MP_H_INCLUDED_ */ 119