ucx/mempool.h

changeset 17
11dffb40cd91
parent 5
88625853ae74
child 40
a95ee94b9204
equal deleted inserted replaced
16:5dbef9e07376 17:11dffb40cd91
57 * UCX mempool structure. 57 * UCX mempool structure.
58 */ 58 */
59 typedef struct { 59 typedef struct {
60 /** List of pointers to pooled memory. */ 60 /** List of pointers to pooled memory. */
61 void **data; 61 void **data;
62
62 /** Count of pooled memory items. */ 63 /** Count of pooled memory items. */
63 size_t ndata; 64 size_t ndata;
65
64 /** Memory pool size. */ 66 /** Memory pool size. */
65 size_t size; 67 size_t size;
66 } UcxMempool; 68 } UcxMempool;
67 69
68 /** Shorthand for a new default memory pool with a capacity of 16 elements. */ 70 /** Shorthand for a new default memory pool with a capacity of 16 elements. */
90 * <code>EXIT_FAILURE</code> on failure 92 * <code>EXIT_FAILURE</code> on failure
91 */ 93 */
92 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); 94 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
93 95
94 /** 96 /**
97 * Changes the pool size to the next smallest multiple of 16.
98 *
99 * You may use this macro, to reduce the pool size after freeing
100 * many pooled memory items.
101 *
102 * @param pool the pool to clamp
103 * @return <code>EXIT_SUCCESS</code> on success or
104 * <code>EXIT_FAILURE</code> on failure
105 */
106 #define ucx_mempool_clamp(pool) ucx_mempool_chcap(pool, \
107 (pool->ndata & ~0xF)+0x10)
108
109 /**
95 * Allocates pooled memory. 110 * Allocates pooled memory.
96 * 111 *
97 * @param pool the memory pool 112 * @param pool the memory pool
98 * @param n amount of memory to allocate 113 * @param n amount of memory to allocate
99 * @return a pointer to the allocated memory 114 * @return a pointer to the allocated memory
110 * @param elsize amount of memory per element 125 * @param elsize amount of memory per element
111 * @return a pointer to the allocated memory 126 * @return a pointer to the allocated memory
112 * @see ucx_allocator_calloc() 127 * @see ucx_allocator_calloc()
113 */ 128 */
114 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); 129 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);
130
115 /** 131 /**
116 * Reallocates pooled memory. 132 * Reallocates pooled memory.
133 *
134 * If the memory to be reallocated is not contained by the specified pool, this
135 * function will possibly fail. In case the memory had to be moved to another
136 * location, this function will print out a message to <code>stderr</code>
137 * and exit the program with error code <code>EXIT_FAILURE</code>.
117 * 138 *
118 * @param pool the memory pool 139 * @param pool the memory pool
119 * @param ptr a pointer to the memory that shall be reallocated 140 * @param ptr a pointer to the memory that shall be reallocated
120 * @param n the new size of the memory 141 * @param n the new size of the memory
121 * @return a pointer to the the location of the memory 142 * @return a pointer to the the location of the memory
122 * @see ucx_allocator_realloc() 143 * @see ucx_allocator_realloc()
123 */ 144 */
124 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); 145 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
146
125 /** 147 /**
126 * Frees pooled memory. 148 * Frees pooled memory.
127 * 149 *
128 * Before freeing the memory, the specified destructor function (if any) 150 * Before freeing the memory, the specified destructor function (if any)
129 * is called. 151 * is called.

mercurial