src/mempool.c

changeset 1452
26e006ba651d
parent 1451
b9a384b1226e
equal deleted inserted replaced
1451:b9a384b1226e 1452:26e006ba651d
114 void *ptr 114 void *ptr
115 ) { 115 ) {
116 if (!ptr) return; 116 if (!ptr) return;
117 struct cx_mempool_s *pool = p; 117 struct cx_mempool_s *pool = p;
118 118
119 cx_destructor_func destr = pool->destr;
120 cx_destructor_func2 destr2 = pool->destr2;
121
119 struct cx_mempool_memory_s *mem = 122 struct cx_mempool_memory_s *mem =
120 (void*) ((char *) ptr - sizeof(struct cx_mempool_memory_s)); 123 (void*) ((char *) ptr - sizeof(struct cx_mempool_memory_s));
121 124
122 for (size_t i = 0; i < pool->size; i++) { 125 for (size_t i = 0; i < pool->size; i++) {
123 if (mem == pool->data[i]) { 126 if (mem == pool->data[i]) {
124 if (mem->destructor) { 127 if (mem->destructor) {
125 mem->destructor(mem->c); 128 mem->destructor(mem->c);
126 } 129 }
127 if (pool->destr) { 130 if (destr != NULL) {
128 pool->destr(mem->c); 131 destr(mem->c);
129 } 132 }
130 if (pool->destr2) { 133 if (destr2 != NULL) {
131 pool->destr2(pool->destr2_data, mem->c); 134 destr2(pool->destr2_data, mem->c);
132 } 135 }
133 cxFree(pool->base_allocator, mem); 136 cxFree(pool->base_allocator, mem);
134 size_t last_index = pool->size - 1; 137 size_t last_index = pool->size - 1;
135 if (i != last_index) { 138 if (i != last_index) {
136 pool->data[i] = pool->data[last_index]; 139 pool->data[i] = pool->data[last_index];
177 return ptr; // LCOV_EXCL_LINE 180 return ptr; // LCOV_EXCL_LINE
178 } 181 }
179 } 182 }
180 183
181 static void cx_mempool_free_all_simple(const struct cx_mempool_s *pool) { 184 static void cx_mempool_free_all_simple(const struct cx_mempool_s *pool) {
185 cx_destructor_func destr = pool->destr;
186 cx_destructor_func2 destr2 = pool->destr2;
182 for (size_t i = 0; i < pool->size; i++) { 187 for (size_t i = 0; i < pool->size; i++) {
183 struct cx_mempool_memory_s *mem = pool->data[i]; 188 struct cx_mempool_memory_s *mem = pool->data[i];
184 if (mem->destructor) { 189 if (mem->destructor) {
185 mem->destructor(mem->c); 190 mem->destructor(mem->c);
186 } 191 }
187 if (pool->destr != NULL) { 192 if (destr != NULL) {
188 pool->destr(mem->c); 193 destr(mem->c);
189 } 194 }
190 if (pool->destr2 != NULL) { 195 if (destr2 != NULL) {
191 pool->destr2(pool->destr2_data, mem->c); 196 destr2(pool->destr2_data, mem->c);
192 } 197 }
193 cxFree(pool->base_allocator, mem); 198 cxFree(pool->base_allocator, mem);
194 } 199 }
195 } 200 }
196 201
243 void *ptr 248 void *ptr
244 ) { 249 ) {
245 if (!ptr) return; 250 if (!ptr) return;
246 struct cx_mempool_s *pool = p; 251 struct cx_mempool_s *pool = p;
247 252
253 cx_destructor_func destr = pool->destr;
254 cx_destructor_func2 destr2 = pool->destr2;
255
248 struct cx_mempool_memory2_s *mem = 256 struct cx_mempool_memory2_s *mem =
249 (void*) ((char *) ptr - sizeof(struct cx_mempool_memory2_s)); 257 (void*) ((char *) ptr - sizeof(struct cx_mempool_memory2_s));
250 258
251 for (size_t i = 0; i < pool->size; i++) { 259 for (size_t i = 0; i < pool->size; i++) {
252 if (mem == pool->data[i]) { 260 if (mem == pool->data[i]) {
253 if (mem->destructor) { 261 if (mem->destructor) {
254 mem->destructor(mem->data, mem->c); 262 mem->destructor(mem->data, mem->c);
255 } 263 }
256 if (pool->destr) { 264 if (destr != NULL) {
257 pool->destr(mem->c); 265 destr(mem->c);
258 } 266 }
259 if (pool->destr2) { 267 if (destr2 != NULL) {
260 pool->destr2(pool->destr2_data, mem->c); 268 destr2(pool->destr2_data, mem->c);
261 } 269 }
262 cxFree(pool->base_allocator, mem); 270 cxFree(pool->base_allocator, mem);
263 size_t last_index = pool->size - 1; 271 size_t last_index = pool->size - 1;
264 if (i != last_index) { 272 if (i != last_index) {
265 pool->data[i] = pool->data[last_index]; 273 pool->data[i] = pool->data[last_index];
306 return ptr; // LCOV_EXCL_LINE 314 return ptr; // LCOV_EXCL_LINE
307 } 315 }
308 } 316 }
309 317
310 static void cx_mempool_free_all_advanced(const struct cx_mempool_s *pool) { 318 static void cx_mempool_free_all_advanced(const struct cx_mempool_s *pool) {
311 const bool has_destr = pool->destr; 319 cx_destructor_func destr = pool->destr;
312 const bool has_destr2 = pool->destr2; 320 cx_destructor_func2 destr2 = pool->destr2;
313 for (size_t i = 0; i < pool->size; i++) { 321 for (size_t i = 0; i < pool->size; i++) {
314 struct cx_mempool_memory2_s *mem = pool->data[i]; 322 struct cx_mempool_memory2_s *mem = pool->data[i];
315 if (mem->destructor) { 323 if (mem->destructor) {
316 mem->destructor(mem->data, mem->c); 324 mem->destructor(mem->data, mem->c);
317 } 325 }
318 if (has_destr) { 326 if (destr != NULL) {
319 pool->destr(mem->c); 327 destr(mem->c);
320 } 328 }
321 if (has_destr2) { 329 if (destr2 != NULL) {
322 pool->destr2(pool->destr2_data, mem->c); 330 destr2(pool->destr2_data, mem->c);
323 } 331 }
324 cxFree(pool->base_allocator, mem); 332 cxFree(pool->base_allocator, mem);
325 } 333 }
326 } 334 }
327 335
372 void *ptr 380 void *ptr
373 ) { 381 ) {
374 if (!ptr) return; 382 if (!ptr) return;
375 struct cx_mempool_s *pool = p; 383 struct cx_mempool_s *pool = p;
376 384
385 cx_destructor_func destr = pool->destr;
386 cx_destructor_func2 destr2 = pool->destr2;
387
377 for (size_t i = 0; i < pool->size; i++) { 388 for (size_t i = 0; i < pool->size; i++) {
378 if (ptr == pool->data[i]) { 389 if (ptr == pool->data[i]) {
379 if (pool->destr) { 390 if (destr != NULL) {
380 pool->destr(ptr); 391 destr(ptr);
381 } 392 }
382 if (pool->destr2) { 393 if (destr2 != NULL) {
383 pool->destr2(pool->destr2_data, ptr); 394 destr2(pool->destr2_data, ptr);
384 } 395 }
385 cxFree(pool->base_allocator, ptr); 396 cxFree(pool->base_allocator, ptr);
386 size_t last_index = pool->size - 1; 397 size_t last_index = pool->size - 1;
387 if (i != last_index) { 398 if (i != last_index) {
388 pool->data[i] = pool->data[last_index]; 399 pool->data[i] = pool->data[last_index];
423 return ptr; // LCOV_EXCL_LINE 434 return ptr; // LCOV_EXCL_LINE
424 } 435 }
425 } 436 }
426 437
427 static void cx_mempool_free_all_pure(const struct cx_mempool_s *pool) { 438 static void cx_mempool_free_all_pure(const struct cx_mempool_s *pool) {
428 const bool has_destr = pool->destr; 439 cx_destructor_func destr = pool->destr;
429 const bool has_destr2 = pool->destr2; 440 cx_destructor_func2 destr2 = pool->destr2;
430 for (size_t i = 0; i < pool->size; i++) { 441 for (size_t i = 0; i < pool->size; i++) {
431 void *mem = pool->data[i]; 442 void *mem = pool->data[i];
432 if (has_destr) { 443 if (destr != NULL) {
433 pool->destr(mem); 444 destr(mem);
434 } 445 }
435 if (has_destr2) { 446 if (destr2 != NULL) {
436 pool->destr2(pool->destr2_data, mem); 447 destr2(pool->destr2_data, mem);
437 } 448 }
438 cxFree(pool->base_allocator, mem); 449 cxFree(pool->base_allocator, mem);
439 } 450 }
440 } 451 }
441 452

mercurial