1 /* |
1 /* |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * |
3 * |
4 * Copyright 2013 Olaf Wintermann. All rights reserved. |
4 * Copyright 2015 Olaf Wintermann. All rights reserved. |
5 * |
5 * |
6 * Redistribution and use in source and binary forms, with or without |
6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: |
7 * modification, are permitted provided that the following conditions are met: |
8 * |
8 * |
9 * 1. Redistributions of source code must retain the above copyright |
9 * 1. Redistributions of source code must retain the above copyright |
45 #ifdef __cplusplus |
45 #ifdef __cplusplus |
46 extern "C" { |
46 extern "C" { |
47 #endif |
47 #endif |
48 |
48 |
49 /** |
49 /** |
50 * A function pointer to a destructor function. |
|
51 * @see ucx_mempool_setdestr() |
|
52 * @see ucx_mempool_regdestr() |
|
53 */ |
|
54 typedef void(*ucx_destructor)(void*); |
|
55 |
|
56 /** |
|
57 * UCX mempool structure. |
50 * UCX mempool structure. |
58 */ |
51 */ |
59 typedef struct { |
52 typedef struct { |
|
53 /** UcxAllocator based on this pool */ |
|
54 UcxAllocator *allocator; |
|
55 |
60 /** List of pointers to pooled memory. */ |
56 /** List of pointers to pooled memory. */ |
61 void **data; |
57 void **data; |
62 |
58 |
63 /** Count of pooled memory items. */ |
59 /** Count of pooled memory items. */ |
64 size_t ndata; |
60 size_t ndata; |
65 |
61 |
66 /** Memory pool size. */ |
62 /** Memory pool size. */ |
67 size_t size; |
63 size_t size; |
68 } UcxMempool; |
64 } UcxMempool; |
69 |
65 |
70 /** Shorthand for a new default memory pool with a capacity of 16 elements. */ |
66 /** Shorthand for a new default memory pool with a capacity of 16 elements. */ |
71 #define ucx_mempool_new_default() ucx_mempool_new(16) |
67 #define ucx_mempool_new_default() ucx_mempool_new(16) |
72 |
68 |
116 */ |
112 */ |
117 void *ucx_mempool_malloc(UcxMempool *pool, size_t n); |
113 void *ucx_mempool_malloc(UcxMempool *pool, size_t n); |
118 /** |
114 /** |
119 * Allocates a pooled memory array. |
115 * Allocates a pooled memory array. |
120 * |
116 * |
121 * The contents of the allocated memory is set to zero. |
117 * The content of the allocated memory is set to zero. |
122 * |
118 * |
123 * @param pool the memory pool |
119 * @param pool the memory pool |
124 * @param nelem amount of elements to allocate |
120 * @param nelem amount of elements to allocate |
125 * @param elsize amount of memory per element |
121 * @param elsize amount of memory per element |
126 * @return a pointer to the allocated memory |
122 * @return a pointer to the allocated memory |
137 * and exit the program with error code <code>EXIT_FAILURE</code>. |
133 * and exit the program with error code <code>EXIT_FAILURE</code>. |
138 * |
134 * |
139 * @param pool the memory pool |
135 * @param pool the memory pool |
140 * @param ptr a pointer to the memory that shall be reallocated |
136 * @param ptr a pointer to the memory that shall be reallocated |
141 * @param n the new size of the memory |
137 * @param n the new size of the memory |
142 * @return a pointer to the the location of the memory |
138 * @return a pointer to the new location of the memory |
143 * @see ucx_allocator_realloc() |
139 * @see ucx_allocator_realloc() |
144 */ |
140 */ |
145 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); |
141 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); |
146 |
142 |
147 /** |
143 /** |
208 * @param ptr data the destructor is registered for |
204 * @param ptr data the destructor is registered for |
209 * @param destr a pointer to the destructor function |
205 * @param destr a pointer to the destructor function |
210 */ |
206 */ |
211 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); |
207 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); |
212 |
208 |
213 /** |
|
214 * Creates an UcxAllocator based on an UcxMempool. |
|
215 * |
|
216 * @param pool the mempool to create the UcxAllocator for |
|
217 * @return a new UcxAllocator based on the specified pool |
|
218 */ |
|
219 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool); |
|
220 |
|
221 #ifdef __cplusplus |
209 #ifdef __cplusplus |
222 } |
210 } |
223 #endif |
211 #endif |
224 |
212 |
225 #endif /* UCX_MEMPOOL_H */ |
213 #endif /* UCX_MEMPOOL_H */ |