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