]> git.sur5r.net Git - openldap/blob - servers/slapd/sl_malloc.c
ITS#6437, save space: Do not allocate the tail, except if size==0.
[openldap] / servers / slapd / sl_malloc.c
1 /* sl_malloc.c - malloc routines using a per-thread slab */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2010 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "slap.h"
23
24 /*
25  * This allocator returns temporary memory from a slab in a given memory
26  * context, aligned on a 2-int boundary.  It cannot be used for data
27  * which will outlive the task allocating it.
28  *
29  * A new memory context attaches to the creator's thread context, if any.
30  * Threads cannot use other threads' memory contexts; there are no locks.
31  *
32  * The caller of slap_sl_malloc, usually a thread pool task, must
33  * slap_sl_free the memory before finishing: New tasks reuse the context
34  * and normally reset it, reclaiming memory left over from last task.
35  *
36  * The allocator helps memory fragmentation, speed and memory leaks.
37  * It is not (yet) reliable as a garbage collector:
38  *
39  * It falls back to context NULL - plain ber_memalloc() - when the
40  * context's slab is full.  A reset does not reclaim such memory.
41  * Conversely, free/realloc of data not from the given context assumes
42  * context NULL.  The data must not belong to another memory context.
43  *
44  * Code which has lost track of the current memory context can try
45  * slap_sl_context() or ch_malloc.c:ch_free/ch_realloc().
46  *
47  * Allocations cannot yet return failure.  Like ch_malloc, they succeed
48  * or abort slapd.  This will change, do fix code which assumes success.
49  */
50
51 /*
52  * The stack-based allocator stores (ber_len_t)sizeof(head+block) at
53  * allocated blocks' head - and in freed blocks also at the tail, marked
54  * by ORing *next* block's head with 1.  Freed blocks are only reclaimed
55  * from the last block forward.  This is fast, but when a block is never
56  * freed, older blocks will not be reclaimed until the slab is reset...
57  */
58
59 #ifdef SLAP_NO_SL_MALLOC /* Useful with memory debuggers like Valgrind */
60 enum { No_sl_malloc = 1 };
61 #else
62 enum { No_sl_malloc = 0 };
63 #endif
64
65 enum {
66         Align = sizeof(ber_len_t) > 2*sizeof(int)
67                 ? sizeof(ber_len_t) : 2*sizeof(int),
68         Align_log2 = 1 + (Align>2) + (Align>4) + (Align>8) + (Align>16),
69         order_start = Align_log2 - 1,
70         pad = Align - 1
71 };
72
73 static struct slab_object * slap_replenish_sopool(struct slab_heap* sh);
74 #ifdef SLAPD_UNUSED
75 static void print_slheap(int level, void *ctx);
76 #endif
77
78 /* Keep memory context in a thread-local var, or in a global when no threads */
79 #ifdef NO_THREADS
80 static struct slab_heap *slheap;
81 # define SET_MEMCTX(thrctx, memctx, sfree)      ((void) (slheap = (memctx)))
82 # define GET_MEMCTX(thrctx, memctxp)            (*(memctxp) = slheap))
83 #else
84 # define memctx_key ((void *) slap_sl_mem_init)
85 # define SET_MEMCTX(thrctx, memctx, kfree) \
86         ldap_pvt_thread_pool_setkey(thrctx,memctx_key, memctx,kfree, NULL,NULL)
87 # define GET_MEMCTX(thrctx, memctxp) \
88         ((void) (*(memctxp) = NULL), \
89          (void) ldap_pvt_thread_pool_getkey(thrctx,memctx_key, memctxp,NULL), \
90          *(memctxp))
91 #endif /* NO_THREADS */
92
93
94 /* Destroy the context, or if key==NULL clean it up for reuse. */
95 void
96 slap_sl_mem_destroy(
97         void *key,
98         void *data
99 )
100 {
101         struct slab_heap *sh = data;
102         struct slab_object *so;
103         int i;
104
105         if (!sh->sh_stack) {
106                 for (i = 0; i <= sh->sh_maxorder - order_start; i++) {
107                         so = LDAP_LIST_FIRST(&sh->sh_free[i]);
108                         while (so) {
109                                 struct slab_object *so_tmp = so;
110                                 so = LDAP_LIST_NEXT(so, so_link);
111                                 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_tmp, so_link);
112                         }
113                         ch_free(sh->sh_map[i]);
114                 }
115                 ch_free(sh->sh_free);
116                 ch_free(sh->sh_map);
117
118                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
119                 while (so) {
120                         struct slab_object *so_tmp = so;
121                         so = LDAP_LIST_NEXT(so, so_link);
122                         if (!so_tmp->so_blockhead) {
123                                 LDAP_LIST_REMOVE(so_tmp, so_link);
124                         }
125                 }
126                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
127                 while (so) {
128                         struct slab_object *so_tmp = so;
129                         so = LDAP_LIST_NEXT(so, so_link);
130                         ch_free(so_tmp);
131                 }
132         }
133
134         if (key != NULL) {
135                 ber_memfree_x(sh->sh_base, NULL);
136                 ber_memfree_x(sh, NULL);
137         }
138 }
139
140 BerMemoryFunctions slap_sl_mfuncs =
141         { slap_sl_malloc, slap_sl_calloc, slap_sl_realloc, slap_sl_free };
142
143 void
144 slap_sl_mem_init()
145 {
146         assert( Align == 1 << Align_log2 );
147
148         ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &slap_sl_mfuncs );
149 }
150
151 /* Create, reset or just return the memory context of the current thread. */
152 void *
153 slap_sl_mem_create(
154         ber_len_t size,
155         int stack,
156         void *thrctx,
157         int new
158 )
159 {
160         void *memctx;
161         struct slab_heap *sh;
162         ber_len_t size_shift;
163         struct slab_object *so;
164         enum { Base_offset = (unsigned) -sizeof(ber_len_t) % Align };
165
166         sh = GET_MEMCTX(thrctx, &memctx);
167         if ( sh && !new )
168                 return sh;
169
170         /* round up to doubleword boundary */
171         size = (size + Align-1) & -Align;
172
173         if (!sh) {
174                 sh = ch_malloc(sizeof(struct slab_heap));
175                 sh->sh_base = ch_malloc(size);
176                 SET_MEMCTX(thrctx, sh, slap_sl_mem_destroy);
177         } else {
178                 slap_sl_mem_destroy(NULL, sh);
179                 if ( size > (char *)sh->sh_end - (char *)sh->sh_base ) {
180                         void    *newptr;
181
182                         newptr = ch_realloc( sh->sh_base, size );
183                         if ( newptr == NULL ) return NULL;
184                         sh->sh_base = newptr;
185                 }
186         }
187         sh->sh_end = (char *) sh->sh_base + size;
188
189         sh->sh_stack = stack;
190         if (stack) {
191                 /* Align first returned block (sh_last + head) */
192                 sh->sh_last = (char *) sh->sh_base + Base_offset;
193
194         } else {
195                 int i, order = -1, order_end = -1;
196
197                 size_shift = size - 1;
198                 do {
199                         order_end++;
200                 } while (size_shift >>= 1);
201                 order = order_end - order_start + 1;
202                 sh->sh_maxorder = order_end;
203
204                 sh->sh_free = (struct sh_freelist *)
205                                                 ch_malloc(order * sizeof(struct sh_freelist));
206                 for (i = 0; i < order; i++) {
207                         LDAP_LIST_INIT(&sh->sh_free[i]);
208                 }
209
210                 LDAP_LIST_INIT(&sh->sh_sopool);
211
212                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
213                         slap_replenish_sopool(sh);
214                 }
215                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
216                 LDAP_LIST_REMOVE(so, so_link);
217                 so->so_ptr = sh->sh_base;
218
219                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[order-1], so, so_link);
220
221                 sh->sh_map = (unsigned char **)
222                                         ch_malloc(order * sizeof(unsigned char *));
223                 for (i = 0; i < order; i++) {
224                         int shiftamt = order_start + 1 + i;
225                         int nummaps = size >> shiftamt;
226                         assert(nummaps);
227                         nummaps >>= 3;
228                         if (!nummaps) nummaps = 1;
229                         sh->sh_map[i] = (unsigned char *) ch_malloc(nummaps);
230                         memset(sh->sh_map[i], 0, nummaps);
231                 }
232         }
233         return sh;
234 }
235
236 /*
237  * Separate memory context from thread context.  Future users must
238  * know the context, since ch_free/slap_sl_context() cannot find it.
239  */
240 void
241 slap_sl_mem_detach(
242         void *thrctx,
243         void *memctx
244 )
245 {
246         SET_MEMCTX(thrctx, NULL, 0);
247 }
248
249 void *
250 slap_sl_malloc(
251     ber_len_t   size,
252     void *ctx
253 )
254 {
255         struct slab_heap *sh = ctx;
256         ber_len_t *ptr, *newptr;
257
258         /* ber_set_option calls us like this */
259         if (No_sl_malloc || !ctx) {
260                 newptr = ber_memalloc_x( size, NULL );
261                 if ( newptr ) return newptr;
262                 Debug(LDAP_DEBUG_ANY, "slap_sl_malloc of %lu bytes failed\n",
263                         (unsigned long) size, 0, 0);
264                 assert( 0 );
265                 exit( EXIT_FAILURE );
266         }
267
268         /* Add room for head, ensure room for tail when freed, and
269          * round up to doubleword boundary. */
270         size = (size + sizeof(ber_len_t) + Align-1 + !size) & -Align;
271
272         if (sh->sh_stack) {
273                 if (size < (ber_len_t) ((char *) sh->sh_end - (char *) sh->sh_last)) {
274                         newptr = sh->sh_last;
275                         sh->sh_last = (char *) sh->sh_last + size;
276                         *newptr++ = size;
277                         return( (void *)newptr );
278                 }
279
280                 size -= sizeof(ber_len_t);
281
282         } else {
283                 struct slab_object *so_new, *so_left, *so_right;
284                 ber_len_t size_shift;
285                 unsigned long diff;
286                 int i, j, order = -1;
287
288                 size_shift = size - 1;
289                 do {
290                         order++;
291                 } while (size_shift >>= 1);
292
293                 size -= sizeof(ber_len_t);
294
295                 for (i = order; i <= sh->sh_maxorder &&
296                                 LDAP_LIST_EMPTY(&sh->sh_free[i-order_start]); i++);
297
298                 if (i == order) {
299                         so_new = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
300                         LDAP_LIST_REMOVE(so_new, so_link);
301                         ptr = so_new->so_ptr;
302                         diff = (unsigned long)((char*)ptr -
303                                         (char*)sh->sh_base) >> (order + 1);
304                         sh->sh_map[order-order_start][diff>>3] |= (1 << (diff & 0x7));
305                         *ptr++ = size;
306                         LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_new, so_link);
307                         return((void*)ptr);
308                 } else if (i <= sh->sh_maxorder) {
309                         for (j = i; j > order; j--) {
310                                 so_left = LDAP_LIST_FIRST(&sh->sh_free[j-order_start]);
311                                 LDAP_LIST_REMOVE(so_left, so_link);
312                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
313                                         slap_replenish_sopool(sh);
314                                 }
315                                 so_right = LDAP_LIST_FIRST(&sh->sh_sopool);
316                                 LDAP_LIST_REMOVE(so_right, so_link);
317                                 so_right->so_ptr = (void *)((char *)so_left->so_ptr + (1 << j));
318                                 if (j == order + 1) {
319                                         ptr = so_left->so_ptr;
320                                         diff = (unsigned long)((char*)ptr -
321                                                         (char*)sh->sh_base) >> (order+1);
322                                         sh->sh_map[order-order_start][diff>>3] |=
323                                                         (1 << (diff & 0x7));
324                                         *ptr++ = size;
325                                         LDAP_LIST_INSERT_HEAD(
326                                                         &sh->sh_free[j-1-order_start], so_right, so_link);
327                                         LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_left, so_link);
328                                         return((void*)ptr);
329                                 } else {
330                                         LDAP_LIST_INSERT_HEAD(
331                                                         &sh->sh_free[j-1-order_start], so_right, so_link);
332                                         LDAP_LIST_INSERT_HEAD(
333                                                         &sh->sh_free[j-1-order_start], so_left, so_link);
334                                 }
335                         }
336                 }
337                 /* FIXME: missing return; guessing we failed... */
338         }
339
340         Debug(LDAP_DEBUG_TRACE,
341                 "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
342                 (unsigned long) size, 0, 0);
343         return ch_malloc(size);
344 }
345
346 #define LIM_SQRT(t) /* some value < sqrt(max value of unsigned type t) */ \
347         ((0UL|(t)-1) >>31>>31 > 1 ? ((t)1 <<32) - 1 : \
348          (0UL|(t)-1) >>31 ? 65535U : (0UL|(t)-1) >>15 ? 255U : 15U)
349
350 void *
351 slap_sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
352 {
353         void *newptr;
354         ber_len_t total = n * size;
355
356         /* The sqrt test is a slight optimization: often avoids the division */
357         if ((n | size) <= LIM_SQRT(ber_len_t) || n == 0 || total/n == size) {
358                 newptr = slap_sl_malloc( total, ctx );
359                 memset( newptr, 0, n*size );
360         } else {
361                 Debug(LDAP_DEBUG_ANY, "slap_sl_calloc(%lu,%lu) out of range\n",
362                         (unsigned long) n, (unsigned long) size, 0);
363                 assert(0);
364                 exit(EXIT_FAILURE);
365         }
366         return newptr;
367 }
368
369 void *
370 slap_sl_realloc(void *ptr, ber_len_t size, void *ctx)
371 {
372         struct slab_heap *sh = ctx;
373         ber_len_t oldsize, *p = (ber_len_t *) ptr, *nextp;
374         void *newptr;
375
376         if (ptr == NULL)
377                 return slap_sl_malloc(size, ctx);
378
379         /* Not our memory? */
380         if (No_sl_malloc || !sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
381                 /* Like ch_realloc(), except not trying a new context */
382                 newptr = ber_memrealloc_x(ptr, size, NULL);
383                 if (newptr) {
384                         return newptr;
385                 }
386                 Debug(LDAP_DEBUG_ANY, "slap_sl_realloc of %lu bytes failed\n",
387                         (unsigned long) size, 0, 0);
388                 assert(0);
389                 exit( EXIT_FAILURE );
390         }
391
392         if (size == 0) {
393                 slap_sl_free(ptr, ctx);
394                 return NULL;
395         }
396
397         oldsize = p[-1];
398
399         if (sh->sh_stack) {
400                 /* Add room for head, round up to doubleword boundary */
401                 size = (size + sizeof(ber_len_t) + Align-1) & -Align;
402
403                 p--;
404
405                 /* Never shrink blocks */
406                 if (size <= oldsize) {
407                         return ptr;
408                 }
409         
410                 oldsize &= -2;
411                 nextp = (ber_len_t *) ((char *) p + oldsize);
412
413                 /* If reallocing the last block, try to grow it */
414                 if (nextp == sh->sh_last) {
415                         if (size < (ber_len_t) ((char *) sh->sh_end - (char *) p)) {
416                                 sh->sh_last = (char *) p + size;
417                                 p[0] = (p[0] & 1) | size;
418                                 return ptr;
419                         }
420
421                 /* Nowhere to grow, need to alloc and copy */
422                 } else {
423                         /* Slight optimization of the final realloc variant */
424                         newptr = slap_sl_malloc(size-sizeof(ber_len_t), ctx);
425                         AC_MEMCPY(newptr, ptr, oldsize-sizeof(ber_len_t));
426                         /* Not last block, can just mark old region as free */
427                         nextp[-1] = oldsize;
428                         nextp[0] |= 1;
429                         return newptr;
430                 }
431
432                 size -= sizeof(ber_len_t);
433                 oldsize -= sizeof(ber_len_t);
434
435         } else if (oldsize > size) {
436                 oldsize = size;
437         }
438
439         newptr = slap_sl_malloc(size, ctx);
440         AC_MEMCPY(newptr, ptr, oldsize);
441         slap_sl_free(ptr, ctx);
442         return newptr;
443 }
444
445 void
446 slap_sl_free(void *ptr, void *ctx)
447 {
448         struct slab_heap *sh = ctx;
449         ber_len_t size;
450         ber_len_t *p = ptr, *nextp, *tmpp;
451
452         if (!ptr)
453                 return;
454
455         if (No_sl_malloc || !sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
456                 ber_memfree_x(ptr, NULL);
457                 return;
458         }
459
460         size = *(--p);
461
462         if (sh->sh_stack) {
463                 size &= -2;
464                 nextp = (ber_len_t *) ((char *) p + size);
465                 if (sh->sh_last != nextp) {
466                         /* Mark it free: tail = size, head of next block |= 1 */
467                         nextp[-1] = size;
468                         nextp[0] |= 1;
469                 } else {
470                         /* Reclaim freed block(s) off tail */
471                         while (*p & 1) {
472                                 p = (ber_len_t *) ((char *) p - p[-1]);
473                         }
474                         sh->sh_last = p;
475                 }
476
477         } else {
478                 int size_shift, order_size;
479                 struct slab_object *so;
480                 unsigned long diff;
481                 int i, inserted = 0, order = -1;
482
483                 size_shift = size + sizeof(ber_len_t) - 1;
484                 do {
485                         order++;
486                 } while (size_shift >>= 1);
487
488                 for (i = order, tmpp = p; i <= sh->sh_maxorder; i++) {
489                         order_size = 1 << (i+1);
490                         diff = (unsigned long)((char*)tmpp - (char*)sh->sh_base) >> (i+1);
491                         sh->sh_map[i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
492                         if (diff == ((diff>>1)<<1)) {
493                                 if (!(sh->sh_map[i-order_start][(diff+1)>>3] &
494                                                 (1<<((diff+1)&0x7)))) {
495                                         so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
496                                         while (so) {
497                                                 if ((char*)so->so_ptr == (char*)tmpp) {
498                                                         LDAP_LIST_REMOVE( so, so_link );
499                                                 } else if ((char*)so->so_ptr ==
500                                                                 (char*)tmpp + order_size) {
501                                                         LDAP_LIST_REMOVE(so, so_link);
502                                                         break;
503                                                 }
504                                                 so = LDAP_LIST_NEXT(so, so_link);
505                                         }
506                                         if (so) {
507                                                 if (i < sh->sh_maxorder) {
508                                                         inserted = 1;
509                                                         so->so_ptr = tmpp;
510                                                         LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1],
511                                                                         so, so_link);
512                                                 }
513                                                 continue;
514                                         } else {
515                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
516                                                         slap_replenish_sopool(sh);
517                                                 }
518                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
519                                                 LDAP_LIST_REMOVE(so, so_link);
520                                                 so->so_ptr = tmpp;
521                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
522                                                                 so, so_link);
523                                                 break;
524
525                                                 Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
526                                                         "free object not found while bit is clear.\n",
527                                                         0, 0, 0);
528                                                 assert(so != NULL);
529
530                                         }
531                                 } else {
532                                         if (!inserted) {
533                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
534                                                         slap_replenish_sopool(sh);
535                                                 }
536                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
537                                                 LDAP_LIST_REMOVE(so, so_link);
538                                                 so->so_ptr = tmpp;
539                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
540                                                                 so, so_link);
541                                         }
542                                         break;
543                                 }
544                         } else {
545                                 if (!(sh->sh_map[i-order_start][(diff-1)>>3] &
546                                                 (1<<((diff-1)&0x7)))) {
547                                         so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
548                                         while (so) {
549                                                 if ((char*)so->so_ptr == (char*)tmpp) {
550                                                         LDAP_LIST_REMOVE(so, so_link);
551                                                 } else if ((char*)tmpp == (char *)so->so_ptr + order_size) {
552                                                         LDAP_LIST_REMOVE(so, so_link);
553                                                         tmpp = so->so_ptr;
554                                                         break;
555                                                 }
556                                                 so = LDAP_LIST_NEXT(so, so_link);
557                                         }
558                                         if (so) {
559                                                 if (i < sh->sh_maxorder) {
560                                                         inserted = 1;
561                                                         LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1],                                                                    so, so_link);
562                                                         continue;
563                                                 }
564                                         } else {
565                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
566                                                         slap_replenish_sopool(sh);
567                                                 }
568                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
569                                                 LDAP_LIST_REMOVE(so, so_link);
570                                                 so->so_ptr = tmpp;
571                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
572                                                                 so, so_link);
573                                                 break;
574
575                                                 Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
576                                                         "free object not found while bit is clear.\n",
577                                                         0, 0, 0 );
578                                                 assert(so != NULL);
579
580                                         }
581                                 } else {
582                                         if ( !inserted ) {
583                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
584                                                         slap_replenish_sopool(sh);
585                                                 }
586                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
587                                                 LDAP_LIST_REMOVE(so, so_link);
588                                                 so->so_ptr = tmpp;
589                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
590                                                                 so, so_link);
591                                         }
592                                         break;
593                                 }
594                         }
595                 }
596         }
597 }
598
599 /*
600  * Return the memory context of the current thread if the given block of
601  * memory belongs to it, otherwise return NULL.
602  */
603 void *
604 slap_sl_context( void *ptr )
605 {
606         void *memctx;
607         struct slab_heap *sh;
608
609         if ( slapMode & SLAP_TOOL_MODE ) return NULL;
610
611         sh = GET_MEMCTX(ldap_pvt_thread_pool_context(), &memctx);
612         if (sh && ptr >= sh->sh_base && ptr <= sh->sh_end) {
613                 return sh;
614         }
615         return NULL;
616 }
617
618 static struct slab_object *
619 slap_replenish_sopool(
620     struct slab_heap* sh
621 )
622 {
623     struct slab_object *so_block;
624     int i;
625
626     so_block = (struct slab_object *)ch_malloc(
627                     SLAP_SLAB_SOBLOCK * sizeof(struct slab_object));
628
629     if ( so_block == NULL ) {
630         return NULL;
631     }
632
633     so_block[0].so_blockhead = 1;
634     LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[0], so_link);
635     for (i = 1; i < SLAP_SLAB_SOBLOCK; i++) {
636         so_block[i].so_blockhead = 0;
637         LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[i], so_link );
638     }
639
640     return so_block;
641 }
642
643 #ifdef SLAPD_UNUSED
644 static void
645 print_slheap(int level, void *ctx)
646 {
647         struct slab_heap *sh = ctx;
648         struct slab_object *so;
649         int i, j, once = 0;
650
651         if (!ctx) {
652                 Debug(level, "NULL memctx\n", 0, 0, 0);
653                 return;
654         }
655
656         Debug(level, "sh->sh_maxorder=%d\n", sh->sh_maxorder, 0, 0);
657
658         for (i = order_start; i <= sh->sh_maxorder; i++) {
659                 once = 0;
660                 Debug(level, "order=%d\n", i, 0, 0);
661                 for (j = 0; j < (1<<(sh->sh_maxorder-i))/8; j++) {
662                         Debug(level, "%02x ", sh->sh_map[i-order_start][j], 0, 0);
663                         once = 1;
664                 }
665                 if (!once) {
666                         Debug(level, "%02x ", sh->sh_map[i-order_start][0], 0, 0);
667                 }
668                 Debug(level, "\n", 0, 0, 0);
669                 Debug(level, "free list:\n", 0, 0, 0);
670                 so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
671                 while (so) {
672                         Debug(level, "%p\n", so->so_ptr, 0, 0);
673                         so = LDAP_LIST_NEXT(so, so_link);
674                 }
675         }
676 }
677 #endif