ucx/mempool.c

branch
dav-2
changeset 889
42cdbf9bbd49
parent 886
da79af4baec8
equal deleted inserted replaced
887:26541c37b619 889:42cdbf9bbd49
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) {
182 const bool has_destr = pool->destr; 185 cx_destructor_func destr = pool->destr;
183 const bool has_destr2 = pool->destr2; 186 cx_destructor_func2 destr2 = pool->destr2;
184 for (size_t i = 0; i < pool->size; i++) { 187 for (size_t i = 0; i < pool->size; i++) {
185 struct cx_mempool_memory_s *mem = pool->data[i]; 188 struct cx_mempool_memory_s *mem = pool->data[i];
186 if (mem->destructor) { 189 if (mem->destructor) {
187 mem->destructor(mem->c); 190 mem->destructor(mem->c);
188 } 191 }
189 if (has_destr) { 192 if (destr != NULL) {
190 pool->destr(mem->c); 193 destr(mem->c);
191 } 194 }
192 if (has_destr2) { 195 if (destr2 != NULL) {
193 pool->destr2(pool->destr2_data, mem->c); 196 destr2(pool->destr2_data, mem->c);
194 } 197 }
195 cxFree(pool->base_allocator, mem); 198 cxFree(pool->base_allocator, mem);
196 } 199 }
197 } 200 }
198 201
245 void *ptr 248 void *ptr
246 ) { 249 ) {
247 if (!ptr) return; 250 if (!ptr) return;
248 struct cx_mempool_s *pool = p; 251 struct cx_mempool_s *pool = p;
249 252
253 cx_destructor_func destr = pool->destr;
254 cx_destructor_func2 destr2 = pool->destr2;
255
250 struct cx_mempool_memory2_s *mem = 256 struct cx_mempool_memory2_s *mem =
251 (void*) ((char *) ptr - sizeof(struct cx_mempool_memory2_s)); 257 (void*) ((char *) ptr - sizeof(struct cx_mempool_memory2_s));
252 258
253 for (size_t i = 0; i < pool->size; i++) { 259 for (size_t i = 0; i < pool->size; i++) {
254 if (mem == pool->data[i]) { 260 if (mem == pool->data[i]) {
255 if (mem->destructor) { 261 if (mem->destructor) {
256 mem->destructor(mem->data, mem->c); 262 mem->destructor(mem->data, mem->c);
257 } 263 }
258 if (pool->destr) { 264 if (destr != NULL) {
259 pool->destr(mem->c); 265 destr(mem->c);
260 } 266 }
261 if (pool->destr2) { 267 if (destr2 != NULL) {
262 pool->destr2(pool->destr2_data, mem->c); 268 destr2(pool->destr2_data, mem->c);
263 } 269 }
264 cxFree(pool->base_allocator, mem); 270 cxFree(pool->base_allocator, mem);
265 size_t last_index = pool->size - 1; 271 size_t last_index = pool->size - 1;
266 if (i != last_index) { 272 if (i != last_index) {
267 pool->data[i] = pool->data[last_index]; 273 pool->data[i] = pool->data[last_index];
308 return ptr; // LCOV_EXCL_LINE 314 return ptr; // LCOV_EXCL_LINE
309 } 315 }
310 } 316 }
311 317
312 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) {
313 const bool has_destr = pool->destr; 319 cx_destructor_func destr = pool->destr;
314 const bool has_destr2 = pool->destr2; 320 cx_destructor_func2 destr2 = pool->destr2;
315 for (size_t i = 0; i < pool->size; i++) { 321 for (size_t i = 0; i < pool->size; i++) {
316 struct cx_mempool_memory2_s *mem = pool->data[i]; 322 struct cx_mempool_memory2_s *mem = pool->data[i];
317 if (mem->destructor) { 323 if (mem->destructor) {
318 mem->destructor(mem->data, mem->c); 324 mem->destructor(mem->data, mem->c);
319 } 325 }
320 if (has_destr) { 326 if (destr != NULL) {
321 pool->destr(mem->c); 327 destr(mem->c);
322 } 328 }
323 if (has_destr2) { 329 if (destr2 != NULL) {
324 pool->destr2(pool->destr2_data, mem->c); 330 destr2(pool->destr2_data, mem->c);
325 } 331 }
326 cxFree(pool->base_allocator, mem); 332 cxFree(pool->base_allocator, mem);
327 } 333 }
328 } 334 }
329 335
374 void *ptr 380 void *ptr
375 ) { 381 ) {
376 if (!ptr) return; 382 if (!ptr) return;
377 struct cx_mempool_s *pool = p; 383 struct cx_mempool_s *pool = p;
378 384
385 cx_destructor_func destr = pool->destr;
386 cx_destructor_func2 destr2 = pool->destr2;
387
379 for (size_t i = 0; i < pool->size; i++) { 388 for (size_t i = 0; i < pool->size; i++) {
380 if (ptr == pool->data[i]) { 389 if (ptr == pool->data[i]) {
381 if (pool->destr) { 390 if (destr != NULL) {
382 pool->destr(ptr); 391 destr(ptr);
383 } 392 }
384 if (pool->destr2) { 393 if (destr2 != NULL) {
385 pool->destr2(pool->destr2_data, ptr); 394 destr2(pool->destr2_data, ptr);
386 } 395 }
387 cxFree(pool->base_allocator, ptr); 396 cxFree(pool->base_allocator, ptr);
388 size_t last_index = pool->size - 1; 397 size_t last_index = pool->size - 1;
389 if (i != last_index) { 398 if (i != last_index) {
390 pool->data[i] = pool->data[last_index]; 399 pool->data[i] = pool->data[last_index];
425 return ptr; // LCOV_EXCL_LINE 434 return ptr; // LCOV_EXCL_LINE
426 } 435 }
427 } 436 }
428 437
429 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) {
430 const bool has_destr = pool->destr; 439 cx_destructor_func destr = pool->destr;
431 const bool has_destr2 = pool->destr2; 440 cx_destructor_func2 destr2 = pool->destr2;
432 for (size_t i = 0; i < pool->size; i++) { 441 for (size_t i = 0; i < pool->size; i++) {
433 void *mem = pool->data[i]; 442 void *mem = pool->data[i];
434 if (has_destr) { 443 if (destr != NULL) {
435 pool->destr(mem); 444 destr(mem);
436 } 445 }
437 if (has_destr2) { 446 if (destr2 != NULL) {
438 pool->destr2(pool->destr2_data, mem); 447 destr2(pool->destr2_data, mem);
439 } 448 }
440 cxFree(pool->base_allocator, mem); 449 cxFree(pool->base_allocator, mem);
441 } 450 }
442 } 451 }
443 452

mercurial