1 /* sl_malloc.c - malloc routines using a per-thread slab */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
24 static struct slab_object * slap_replenish_sopool(struct slab_heap* sh);
26 static void print_slheap(int level, void *ctx);
35 struct slab_heap *sh = data;
36 int pad = 2*sizeof(int)-1, pad_shift;
37 int order_start = -1, i;
38 struct slab_object *so;
41 ber_memfree_x(sh->sh_base, NULL);
42 ber_memfree_x(sh, NULL);
47 } while (pad_shift >>= 1);
49 for (i = 0; i <= sh->sh_maxorder - order_start; i++) {
50 so = LDAP_LIST_FIRST(&sh->sh_free[i]);
52 struct slab_object *so_tmp = so;
53 so = LDAP_LIST_NEXT(so, so_link);
54 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_tmp, so_link);
56 ch_free(sh->sh_map[i]);
61 so = LDAP_LIST_FIRST(&sh->sh_sopool);
63 struct slab_object *so_tmp = so;
64 so = LDAP_LIST_NEXT(so, so_link);
65 if (!so_tmp->so_blockhead) {
66 LDAP_LIST_REMOVE(so_tmp, so_link);
69 so = LDAP_LIST_FIRST(&sh->sh_sopool);
71 struct slab_object *so_tmp = so;
72 so = LDAP_LIST_NEXT(so, so_link);
75 ber_memfree_x(sh->sh_base, NULL);
76 ber_memfree_x(sh, NULL);
80 BerMemoryFunctions slap_sl_mfuncs =
81 { slap_sl_malloc, slap_sl_calloc, slap_sl_realloc, slap_sl_free };
86 ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &slap_sl_mfuncs );
90 static struct slab_heap *slheap;
100 struct slab_heap *sh;
101 ber_len_t size_shift;
102 int pad = 2*sizeof(int)-1, pad_shift;
103 int order = -1, order_start = -1, order_end = -1;
105 struct slab_object *so;
111 ldap_pvt_thread_pool_getkey(
112 ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL );
116 /* round up to doubleword boundary */
122 sh = ch_malloc(sizeof(struct slab_heap));
123 sh->sh_base = ch_malloc(size);
127 ldap_pvt_thread_pool_setkey(ctx, (void *)slap_sl_mem_init,
128 (void *)sh, slap_sl_mem_destroy);
130 } else if ( size > (char *)sh->sh_end - (char *)sh->sh_base ) {
133 newptr = ch_realloc( sh->sh_base, size );
134 if ( newptr == NULL ) return NULL;
135 sh->sh_base = newptr;
137 sh->sh_last = sh->sh_base;
138 sh->sh_end = (char *) sh->sh_base + size;
139 sh->sh_stack = stack;
142 size_shift = size - 1;
145 } while (size_shift >>= 1);
150 } while (pad_shift >>= 1);
152 order = order_end - order_start + 1;
155 sh = (struct slab_heap *) ch_malloc(sizeof(struct slab_heap));
156 sh->sh_base = ch_malloc(size);
160 ldap_pvt_thread_pool_setkey(ctx, (void *)slap_sl_mem_init,
161 (void *)sh, slap_sl_mem_destroy);
164 for (i = 0; i <= sh->sh_maxorder - order_start; i++) {
165 so = LDAP_LIST_FIRST(&sh->sh_free[i]);
167 struct slab_object *so_tmp = so;
168 so = LDAP_LIST_NEXT(so, so_link);
169 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_tmp, so_link);
171 ch_free(sh->sh_map[i]);
173 ch_free(sh->sh_free);
176 so = LDAP_LIST_FIRST(&sh->sh_sopool);
178 struct slab_object *so_tmp = so;
179 so = LDAP_LIST_NEXT(so, so_link);
180 if (!so_tmp->so_blockhead) {
181 LDAP_LIST_REMOVE(so_tmp, so_link);
184 so = LDAP_LIST_FIRST(&sh->sh_sopool);
186 struct slab_object *so_tmp = so;
187 so = LDAP_LIST_NEXT(so, so_link);
191 if (size > (char *)sh->sh_end - (char *)sh->sh_base) {
194 newptr = realloc( sh->sh_base, size );
195 if ( newptr == NULL ) return NULL;
196 sh->sh_base = newptr;
199 sh->sh_end = (char *)sh->sh_base + size;
200 sh->sh_maxorder = order_end;
202 sh->sh_free = (struct sh_freelist *)
203 ch_malloc(order * sizeof(struct sh_freelist));
204 for (i = 0; i < order; i++) {
205 LDAP_LIST_INIT(&sh->sh_free[i]);
208 LDAP_LIST_INIT(&sh->sh_sopool);
210 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
211 slap_replenish_sopool(sh);
213 so = LDAP_LIST_FIRST(&sh->sh_sopool);
214 LDAP_LIST_REMOVE(so, so_link);
215 so->so_ptr = sh->sh_base;
217 LDAP_LIST_INSERT_HEAD(&sh->sh_free[order-1], so, so_link);
219 sh->sh_map = (unsigned char **)
220 ch_malloc(order * sizeof(unsigned char *));
221 for (i = 0; i < order; i++) {
222 int shiftamt = order_start + 1 + i;
223 int nummaps = size >> shiftamt;
226 if (!nummaps) nummaps = 1;
227 sh->sh_map[i] = (unsigned char *) ch_malloc(nummaps);
228 memset(sh->sh_map[i], 0, nummaps);
230 sh->sh_stack = stack;
244 /* separate from context */
245 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_sl_mem_init, NULL, NULL );
255 struct slab_heap *sh = ctx;
256 ber_len_t size_shift;
257 int pad = 2*sizeof(int)-1, pad_shift;
258 int order = -1, order_start = -1;
259 struct slab_object *so_new, *so_left, *so_right;
260 ber_len_t *ptr, *newptr;
264 /* ber_set_option calls us like this */
265 if (!ctx) return ber_memalloc_x(size, NULL);
267 /* round up to doubleword boundary */
268 size += sizeof(ber_len_t) + pad;
272 if ((char *)sh->sh_last + size >= (char *)sh->sh_end) {
273 Debug(LDAP_DEBUG_TRACE,
274 "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
276 return ch_malloc(size);
278 newptr = sh->sh_last;
279 *newptr++ = size - sizeof(ber_len_t);
280 sh->sh_last = (char *) sh->sh_last + size;
281 return( (void *)newptr );
283 size_shift = size - 1;
286 } while (size_shift >>= 1);
291 } while (pad_shift >>= 1);
293 for (i = order; i <= sh->sh_maxorder &&
294 LDAP_LIST_EMPTY(&sh->sh_free[i-order_start]); i++);
297 so_new = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
298 LDAP_LIST_REMOVE(so_new, so_link);
299 ptr = so_new->so_ptr;
300 diff = (unsigned long)((char*)ptr -
301 (char*)sh->sh_base) >> (order + 1);
302 sh->sh_map[order-order_start][diff>>3] |= (1 << (diff & 0x7));
303 *ptr++ = size - sizeof(ber_len_t);
304 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_new, so_link);
306 } else if (i <= sh->sh_maxorder) {
307 for (j = i; j > order; j--) {
308 so_left = LDAP_LIST_FIRST(&sh->sh_free[j-order_start]);
309 LDAP_LIST_REMOVE(so_left, so_link);
310 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
311 slap_replenish_sopool(sh);
313 so_right = LDAP_LIST_FIRST(&sh->sh_sopool);
314 LDAP_LIST_REMOVE(so_right, so_link);
315 so_right->so_ptr = (void *)((char *)so_left->so_ptr + (1 << j));
316 if (j == order + 1) {
317 ptr = so_left->so_ptr;
318 diff = (unsigned long)((char*)ptr -
319 (char*)sh->sh_base) >> (order+1);
320 sh->sh_map[order-order_start][diff>>3] |=
322 *ptr++ = size - sizeof(ber_len_t);
323 LDAP_LIST_INSERT_HEAD(
324 &sh->sh_free[j-1-order_start], so_right, so_link);
325 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_left, so_link);
328 LDAP_LIST_INSERT_HEAD(
329 &sh->sh_free[j-1-order_start], so_right, so_link);
330 LDAP_LIST_INSERT_HEAD(
331 &sh->sh_free[j-1-order_start], so_left, so_link);
335 Debug( LDAP_DEBUG_TRACE,
336 "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
338 return (void*)ch_malloc(size);
342 /* FIXME: missing return; guessing... */
347 slap_sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
351 newptr = slap_sl_malloc( n*size, ctx );
353 memset( newptr, 0, n*size );
359 slap_sl_realloc(void *ptr, ber_len_t size, void *ctx)
361 struct slab_heap *sh = ctx;
362 int pad = 2*sizeof(int) -1;
363 ber_len_t *p = (ber_len_t *)ptr, *newptr;
366 return slap_sl_malloc(size, ctx);
368 /* Not our memory? */
369 if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
370 /* duplicate of realloc behavior, oh well */
371 newptr = ber_memrealloc_x(ptr, size, NULL);
375 Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
378 exit( EXIT_FAILURE );
382 slap_sl_free(ptr, ctx);
387 /* round up to doubleword boundary */
388 size += pad + sizeof( ber_len_t );
391 /* Never shrink blocks */
395 /* If reallocing the last block, we can grow it */
396 } else if ((char *)ptr + p[-1] == sh->sh_last &&
397 (char *)ptr + size < (char *)sh->sh_end ) {
399 sh->sh_last = (char *)sh->sh_last + size - p[-1];
402 /* Nowhere to grow, need to alloc and copy */
404 newptr = slap_sl_malloc(size, ctx);
405 AC_MEMCPY(newptr, ptr, p[-1]);
411 newptr2 = slap_sl_malloc(size, ctx);
413 AC_MEMCPY(newptr2, ptr, size);
415 AC_MEMCPY(newptr2, ptr, p[-1]);
417 slap_sl_free(ptr, ctx);
423 slap_sl_free(void *ptr, void *ctx)
425 struct slab_heap *sh = ctx;
426 int size, size_shift, order_size;
427 int pad = 2*sizeof(int)-1, pad_shift;
428 ber_len_t *p = (ber_len_t *)ptr, *tmpp;
429 int order_start = -1, order = -1;
430 struct slab_object *so;
437 if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
438 ber_memfree_x(ptr, NULL);
439 } else if (sh->sh_stack && (char *)ptr + p[-1] == sh->sh_last) {
442 } else if (!sh->sh_stack) {
444 size_shift = size + sizeof(ber_len_t) - 1;
447 } while (size_shift >>= 1);
452 } while (pad_shift >>= 1);
454 for (i = order, tmpp = p; i <= sh->sh_maxorder; i++) {
455 order_size = 1 << (i+1);
456 diff = (unsigned long)((char*)tmpp - (char*)sh->sh_base) >> (i+1);
457 sh->sh_map[i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
458 if (diff == ((diff>>1)<<1)) {
459 if (!(sh->sh_map[i-order_start][(diff+1)>>3] &
460 (1<<((diff+1)&0x7)))) {
461 so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
463 if ((char*)so->so_ptr == (char*)tmpp) {
464 LDAP_LIST_REMOVE( so, so_link );
465 } else if ((char*)so->so_ptr ==
466 (char*)tmpp + order_size) {
467 LDAP_LIST_REMOVE(so, so_link);
470 so = LDAP_LIST_NEXT(so, so_link);
473 if (i < sh->sh_maxorder) {
476 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1],
481 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
482 slap_replenish_sopool(sh);
484 so = LDAP_LIST_FIRST(&sh->sh_sopool);
485 LDAP_LIST_REMOVE(so, so_link);
487 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
491 Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
492 "free object not found while bit is clear.\n",
499 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
500 slap_replenish_sopool(sh);
502 so = LDAP_LIST_FIRST(&sh->sh_sopool);
503 LDAP_LIST_REMOVE(so, so_link);
505 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
511 if (!(sh->sh_map[i-order_start][(diff-1)>>3] &
512 (1<<((diff-1)&0x7)))) {
513 so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
515 if ((char*)so->so_ptr == (char*)tmpp) {
516 LDAP_LIST_REMOVE(so, so_link);
517 } else if ((char*)tmpp == (char *)so->so_ptr + order_size) {
518 LDAP_LIST_REMOVE(so, so_link);
522 so = LDAP_LIST_NEXT(so, so_link);
525 if (i < sh->sh_maxorder) {
527 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1], so, so_link);
531 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
532 slap_replenish_sopool(sh);
534 so = LDAP_LIST_FIRST(&sh->sh_sopool);
535 LDAP_LIST_REMOVE(so, so_link);
537 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
541 Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
542 "free object not found while bit is clear.\n",
549 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
550 slap_replenish_sopool(sh);
552 so = LDAP_LIST_FIRST(&sh->sh_sopool);
553 LDAP_LIST_REMOVE(so, so_link);
555 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
566 slap_sl_context( void *ptr )
568 struct slab_heap *sh;
571 if ( slapMode & SLAP_TOOL_MODE ) return NULL;
576 ctx = ldap_pvt_thread_pool_context();
579 ldap_pvt_thread_pool_getkey(
580 ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL);
584 if (sh && ptr >= sh->sh_base && ptr <= sh->sh_end) {
590 static struct slab_object *
591 slap_replenish_sopool(
595 struct slab_object *so_block;
598 so_block = (struct slab_object *)ch_malloc(
599 SLAP_SLAB_SOBLOCK * sizeof(struct slab_object));
601 if ( so_block == NULL ) {
605 so_block[0].so_blockhead = 1;
606 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[0], so_link);
607 for (i = 1; i < SLAP_SLAB_SOBLOCK; i++) {
608 so_block[i].so_blockhead = 0;
609 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[i], so_link );
617 print_slheap(int level, void *ctx)
619 struct slab_heap *sh = ctx;
620 int order_start = -1;
621 int pad = 2*sizeof(int)-1, pad_shift;
622 struct slab_object *so;
626 Debug(level, "NULL memctx\n", 0, 0, 0);
633 } while (pad_shift >>= 1);
635 Debug(level, "sh->sh_maxorder=%d\n", sh->sh_maxorder, 0, 0);
637 for (i = order_start; i <= sh->sh_maxorder; i++) {
639 Debug(level, "order=%d\n", i, 0, 0);
640 for (j = 0; j < (1<<(sh->sh_maxorder-i))/8; j++) {
641 Debug(level, "%02x ", sh->sh_map[i-order_start][j], 0, 0);
645 Debug(level, "%02x ", sh->sh_map[i-order_start][0], 0, 0);
647 Debug(level, "\n", 0, 0, 0);
648 Debug(level, "free list:\n", 0, 0, 0);
649 so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
651 Debug(level, "%lx\n", (unsigned long) so->so_ptr, 0, 0);
652 so = LDAP_LIST_NEXT(so, so_link);