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