]> git.sur5r.net Git - openldap/blob - servers/slapd/zn_malloc.c
zone-based memory allocator for adaptive caching
[openldap] / servers / slapd / zn_malloc.c
1 /* zn_malloc.c - zone-based malloc routines */
2 /* $OpenLDAP$*/
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 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 /* Copyright 2004 IBM Corporation
17  * All rights reserved.
18  * Redisribution and use in source and binary forms, with or without
19  * modification, are permitted only as  authorizd by the OpenLADP
20  * Public License.
21  */
22 /* ACKNOWLEDGEMENTS
23  * This work originally developed by Jong-Hyuk Choi
24  * 2004/11/29   jongchoi@OpenLDAP.org
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30 #include <ac/string.h>
31 #include <sys/types.h>
32 #include <sys/mman.h>
33 #include <fcntl.h>
34
35 #include "slap.h"
36
37 #ifdef SLAP_ZONE_ALLOC
38
39 static int slap_zone_cmp(const void *v1, const void *v2);
40 void * slap_replenish_zopool(void *ctx);
41
42 static void
43 slap_zo_release(void *data)
44 {
45         struct zone_object *zo = (struct zone_object *)data;
46         ch_free( zo );
47 }
48
49 void
50 slap_zn_mem_destroy(
51         void *ctx
52 )
53 {
54         struct zone_heap *zh = ctx;
55         int pad = 2*sizeof(int)-1, pad_shift;
56         int order_start = -1, i, j;
57         struct zone_object *zo;
58
59         pad_shift = pad - 1;
60         do {
61                 order_start++;
62         } while (pad_shift >>= 1);
63
64         ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
65         for (i = 0; i <= zh->zh_zoneorder - order_start; i++) {
66                 zo = LDAP_LIST_FIRST(&zh->zh_free[i]);
67                 while (zo) {
68                         struct zone_object *zo_tmp = zo;
69                         zo = LDAP_LIST_NEXT(zo, zo_link);
70                         LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_tmp, zo_link);
71                 }
72         }
73         ch_free(zh->zh_free);
74
75         for (i = 0; i < zh->zh_numzones; i++) {
76                 for (j = 0; j < zh->zh_zoneorder - order_start; j++) {
77                         ch_free(zh->zh_maps[i][j]);
78                 }
79                 ch_free(zh->zh_maps[i]);
80                 munmap(zh->zh_zones[i], zh->zh_zonesize);
81         }
82         ch_free(zh->zh_maps);
83         ch_free(zh->zh_zones);
84         ch_free(zh->zh_seqno);
85
86         avl_free(zh->zh_zonetree, slap_zo_release);
87
88         zo = LDAP_LIST_FIRST(&zh->zh_zopool);
89         while (zo) {
90                 struct zone_object *zo_tmp = zo;
91                 zo = LDAP_LIST_NEXT(zo, zo_link);
92                 if (!zo_tmp->zo_blockhead) {
93                         LDAP_LIST_REMOVE(zo_tmp, zo_link);
94                 }
95         }
96         zo = LDAP_LIST_FIRST(&zh->zh_zopool);
97         while (zo) {
98                 struct zone_object *zo_tmp = zo;
99                 zo = LDAP_LIST_NEXT(zo, zo_link);
100                 ch_free(zo_tmp);
101         }
102         ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
103         ldap_pvt_thread_mutex_destroy( &zh->zh_mutex );
104         ch_free(zh);
105 }
106
107 void *
108 slap_zn_mem_create(
109         ber_len_t initsize,
110         ber_len_t maxsize,
111         ber_len_t deltasize,
112         ber_len_t zonesize
113 )
114 {
115         struct zone_heap *zh = NULL;
116         ber_len_t zpad;
117         int pad = 2*sizeof(int)-1, pad_shift;
118         int size_shift;
119         int order = -1, order_start = -1, order_end = -1;
120         int i, j;
121         struct zone_object *zo;
122
123         zh = (struct zone_heap *)ch_calloc(1, sizeof(struct zone_heap));
124
125         zh->zh_fd = open("/dev/zero", O_RDWR);
126
127         if ( zonesize ) {
128                 zh->zh_zonesize = zonesize;
129         } else {
130                 zh->zh_zonesize = SLAP_ZONE_SIZE;
131         }
132
133         zpad = zh->zh_zonesize - 1;
134         zh->zh_numzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
135
136         if ( maxsize && maxsize >= initsize ) {
137                 zh->zh_maxzones = ((maxsize + zpad) & ~zpad) / zh->zh_zonesize;
138         } else {
139                 zh->zh_maxzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
140         }
141
142         if ( deltasize ) {
143                 zh->zh_deltazones = ((deltasize + zpad) & ~zpad) / zh->zh_zonesize;
144         } else {
145                 zh->zh_deltazones = ((SLAP_ZONE_DELTA+zpad) & ~zpad) / zh->zh_zonesize;
146         }
147
148         Debug(LDAP_DEBUG_ANY, "==> slap_zn_mem_create: %d\n",
149                                         zh->zh_numzones, 0, 0 );
150
151         size_shift = zh->zh_zonesize - 1;
152         do {
153                 order_end++;
154         } while (size_shift >>= 1);
155
156         pad_shift = pad - 1;
157         do {
158                 order_start++;
159         } while (pad_shift >>= 1);
160
161         order = order_end - order_start + 1;
162
163         zh->zh_zones = (void **)ch_malloc(zh->zh_maxzones * sizeof(void*));
164         zh->zh_maps = (unsigned char ***)ch_malloc(
165                                         zh->zh_maxzones * sizeof(unsigned char**));
166
167         zh->zh_zoneorder = order_end;
168         zh->zh_free = (struct zh_freelist *)
169                                         ch_malloc(order * sizeof(struct zh_freelist));
170         zh->zh_seqno = (unsigned long *)ch_calloc(zh->zh_maxzones,
171                                                                                         sizeof(unsigned long));
172         for (i = 0; i < order; i++) {
173                 LDAP_LIST_INIT(&zh->zh_free[i]);
174         }
175         LDAP_LIST_INIT(&zh->zh_zopool);
176
177         for (i = 0; i < zh->zh_numzones; i++) {
178                 zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
179                                                         MAP_PRIVATE, zh->zh_fd, 0);
180                 zh->zh_maps[i] = (unsigned char **)
181                                         ch_malloc(order * sizeof(unsigned char *));
182                 for (j = 0; j < order; j++) {
183                         int shiftamt = order_start + 1 + j;
184                         int nummaps = zh->zh_zonesize >> shiftamt;
185                         assert(nummaps);
186                         nummaps /= 8;
187                         if (!nummaps) nummaps = 1;
188                         zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
189                         memset(zh->zh_maps[i][j], 0, nummaps);
190                 }
191
192                 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
193                         slap_replenish_zopool(zh);
194                 }
195                 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
196                 LDAP_LIST_REMOVE(zo, zo_link);
197                 zo->zo_ptr = zh->zh_zones[i];
198                 zo->zo_idx = i;
199                 LDAP_LIST_INSERT_HEAD(&zh->zh_free[order-1], zo, zo_link);
200
201                 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
202                         slap_replenish_zopool(zh);
203                 }
204                 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
205                 LDAP_LIST_REMOVE(zo, zo_link);
206                 zo->zo_ptr = (void*)((unsigned long)zh->zh_zones[i]>>zh->zh_zoneorder);
207                 zo->zo_idx = i;
208                 avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
209         }
210
211         ldap_pvt_thread_mutex_init(&zh->zh_mutex);
212
213         return zh;
214 }
215
216 void *
217 slap_zn_malloc(
218     ber_len_t   size,
219         void *ctx
220 )
221 {
222         struct zone_heap *zh = ctx;
223         ber_len_t size_shift;
224         int pad = 2*sizeof(int)-1, pad_shift;
225         int order = -1, order_start = -1;
226         struct zone_object *zo, *zo_new, *zo_left, *zo_right;
227         ber_len_t *ptr, *new;
228         int idx;
229         unsigned long diff;
230         int i, j, k;
231
232         if (!zh) return ber_memalloc_x(size, NULL);
233
234         /* round up to doubleword boundary */
235         size += 2*sizeof(ber_len_t) + pad;
236         size &= ~pad;
237
238         size_shift = size - 1;
239         do {
240                 order++;
241         } while (size_shift >>= 1);
242
243         pad_shift = pad - 1;
244         do {
245                 order_start++;
246         } while (pad_shift >>= 1);
247
248 retry:
249
250         ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
251         for (i = order; i <= zh->zh_zoneorder &&
252                         LDAP_LIST_EMPTY(&zh->zh_free[i-order_start]); i++);
253
254         if (i == order) {
255                 zo_new = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
256                 LDAP_LIST_REMOVE(zo_new, zo_link);
257                 ptr = zo_new->zo_ptr;
258                 idx = zo_new->zo_idx;
259                 diff = (unsigned long)((char*)ptr -
260                                 (char*)zh->zh_zones[idx]) >> (order + 1);
261                 zh->zh_maps[idx][order-order_start][diff>>3] |= (1 << (diff & 0x7));
262                 *ptr++ = zh->zh_seqno[idx];
263                 *ptr++ = size - 2*sizeof(ber_len_t);
264                 zo_new->zo_ptr = NULL;
265                 zo_new->zo_idx = -1;
266                 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_new, zo_link);
267                 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
268                 return((void*)ptr);
269         } else if (i <= zh->zh_zoneorder) {
270                 for (j = i; j > order; j--) {
271                         zo_left = LDAP_LIST_FIRST(&zh->zh_free[j-order_start]);
272                         LDAP_LIST_REMOVE(zo_left, zo_link);
273                         if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
274                                 slap_replenish_zopool(zh);
275                         }
276                         zo_right = LDAP_LIST_FIRST(&zh->zh_zopool);
277                         LDAP_LIST_REMOVE(zo_right, zo_link);
278                         zo_right->zo_ptr = zo_left->zo_ptr + (1 << j);
279                         zo_right->zo_idx = zo_left->zo_idx;
280                         if (j == order + 1) {
281                                 ptr = zo_left->zo_ptr;
282                                 diff = (unsigned long)((char*)ptr -
283                                                 (char*)zh->zh_zones[zo_left->zo_idx]) >> (order+1);
284                                 zh->zh_maps[zo_left->zo_idx][order-order_start][diff>>3] |=
285                                                 (1 << (diff & 0x7));
286                                 *ptr++ = zh->zh_seqno[zo_left->zo_idx];
287                                 *ptr++ = size - 2*sizeof(ber_len_t);
288                                 LDAP_LIST_INSERT_HEAD(
289                                                 &zh->zh_free[j-1-order_start], zo_right, zo_link);
290                                 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_left, zo_link);
291                                 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
292                                 return((void*)ptr);
293                         } else {
294                                 LDAP_LIST_INSERT_HEAD(
295                                                 &zh->zh_free[j-1-order_start], zo_right, zo_link);
296                                 LDAP_LIST_INSERT_HEAD(
297                                                 &zh->zh_free[j-1-order_start], zo_left, zo_link);
298                         }
299                 }
300                 assert(0);
301         } else {
302
303                 if ( zh->zh_maxzones < zh->zh_numzones + zh->zh_deltazones ) {
304                         ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
305                         Debug( LDAP_DEBUG_TRACE,
306                                 "slap_zn_malloc of %lu bytes failed, using ch_malloc\n",
307                                 (long)size, 0, 0);
308                         return (void*)ch_malloc(size);
309                 }
310
311                 for (i = zh->zh_numzones; i < zh->zh_numzones+zh->zh_deltazones; i++) {
312                         zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
313                                                                 MAP_PRIVATE, zh->zh_fd, 0);
314                         zh->zh_maps[i] = (unsigned char **)
315                                                 ch_malloc((zh->zh_zoneorder - order_start +1) *
316                                                 sizeof(unsigned char *));
317                         for (j = 0; j < order; j++) {
318                                 int shiftamt = order_start + 1 + j;
319                                 int nummaps = zh->zh_zonesize >> shiftamt;
320                                 assert(nummaps);
321                                 nummaps /= 8;
322                                 if (!nummaps) nummaps = 1;
323                                 zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
324                                 memset(zh->zh_maps[i][j], 0, nummaps);
325                         }
326         
327                         if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
328                                 slap_replenish_zopool(zh);
329                         }
330                         zo = LDAP_LIST_FIRST(&zh->zh_zopool);
331                         LDAP_LIST_REMOVE(zo, zo_link);
332                         zo->zo_ptr = zh->zh_zones[i];
333                         zo->zo_idx = i;
334                         LDAP_LIST_INSERT_HEAD(&zh->
335                                                 zh_free[zh->zh_zoneorder-order_start],zo,zo_link);
336         
337                         if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
338                                 slap_replenish_zopool(zh);
339                         }
340                         zo = LDAP_LIST_FIRST(&zh->zh_zopool);
341                         LDAP_LIST_REMOVE(zo, zo_link);
342                         zo->zo_ptr = (void*)((unsigned long)zh->zh_zones[i]>>
343                                                 zh->zh_zoneorder);
344                         zo->zo_idx = i;
345                         avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
346                 }
347                 zh->zh_numzones += zh->zh_deltazones;
348                 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
349                 goto retry;
350         }
351 }
352
353 void *
354 slap_zn_calloc( ber_len_t n, ber_len_t size, void *ctx )
355 {
356         void *new;
357
358         new = slap_zn_malloc( n*size, ctx );
359         if ( new ) {
360                 memset( new, 0, n*size );
361         }
362         return new;
363 }
364
365 void *
366 slap_zn_realloc(void *ptr, ber_len_t size, void *ctx)
367 {
368         struct zone_heap *zh = ctx;
369         int pad = 2*sizeof(int)-1, pad_shift;
370         int order_start = -1, order = -1;
371         struct zone_object zoi, *zoo;
372         ber_len_t *p = (ber_len_t *)ptr, *new;
373         unsigned long diff;
374         int i;
375         void *newptr = NULL;
376         struct zone_heap *zone = NULL;
377
378         if (ptr == NULL)
379                 return slap_zn_malloc(size, zh);
380
381         zoi.zo_ptr = (void*)((unsigned long)p >> zh->zh_zoneorder);
382         zoi.zo_idx = -1;
383
384         if (zh) {
385                 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
386                 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
387                 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
388         }
389
390         /* Not our memory? */
391         if (!zoo) {
392                 /* duplicate of realloc behavior, oh well */
393                 new = ber_memrealloc_x(ptr, size, NULL);
394                 if (new) {
395                         return new;
396                 }
397                 Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
398                                 (long) size, 0, 0);
399                 assert(0);
400                 exit( EXIT_FAILURE );
401         }
402
403         assert(zoo->zo_idx != -1);      
404
405         zone = zh->zh_zones[zoo->zo_idx];
406
407         if (size == 0) {
408                 slap_zn_free(ptr, zh);
409                 return NULL;
410         }
411
412         newptr = slap_zn_malloc(size, zh);
413         if (size < p[-1]) {
414                 AC_MEMCPY(newptr, ptr, size);
415         } else {
416                 AC_MEMCPY(newptr, ptr, p[-1]);
417         }
418         slap_zn_free(ptr, zh);
419         return newptr;
420 }
421
422 void
423 slap_zn_free(void *ptr, void *ctx)
424 {
425         struct zone_heap *zh = 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 zone_object zoi, *zoo, *zo;
431         unsigned long diff;
432         int i, k, inserted = 0, idx;
433         struct zone_heap *zone = NULL;
434
435         fprintf(stderr,"slap_zn_free... 0x%x\n", ptr);
436         zoi.zo_ptr = (void*)((unsigned long)p >> zh->zh_zoneorder);
437         zoi.zo_idx = -1;
438
439         if (zh) {
440                 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
441                 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
442                 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
443         }
444
445         if (!zoo) {
446                 ber_memfree_x(ptr, NULL);
447         } else {
448                 idx = zoo->zo_idx;
449                 assert(idx != -1);
450                 zone = zh->zh_zones[idx];
451
452                 size = *(--p);
453                 size_shift = size + 2*sizeof(ber_len_t) - 1;
454                 do {
455                         order++;
456                 } while (size_shift >>= 1);
457
458                 pad_shift = pad - 1;
459                 do {
460                         order_start++;
461                 } while (pad_shift >>= 1);
462
463                 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
464                 for (i = order, tmpp = p; i <= zh->zh_zoneorder; i++) {
465                         order_size = 1 << (i+1);
466                         diff = (unsigned long)((char*)tmpp - (char*)zone) >> (i+1);
467                         zh->zh_maps[idx][i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
468                         if (diff == ((diff>>1)<<1)) {
469                                 if (!(zh->zh_maps[idx][i-order_start][(diff+1)>>3] &
470                                                 (1<<((diff+1)&0x7)))) {
471                                         zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
472                                         while (zo) {
473                                                 if ((char*)zo->zo_ptr == (char*)tmpp) {
474                                                         LDAP_LIST_REMOVE( zo, zo_link );
475                                                 } else if ((char*)zo->zo_ptr ==
476                                                                 (char*)tmpp + order_size) {
477                                                         LDAP_LIST_REMOVE(zo, zo_link);
478                                                         break;
479                                                 }
480                                                 zo = LDAP_LIST_NEXT(zo, zo_link);
481                                         }
482                                         if (zo) {
483                                                 if (i < zh->zh_zoneorder) {
484                                                         inserted = 1;
485                                                         zo->zo_ptr = tmpp;
486                                                         LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
487                                                                         zo, zo_link);
488                                                 }
489                                                 continue;
490                                         } else {
491                                                 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
492                                                         slap_replenish_zopool(zh);
493                                                 }
494                                                 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
495                                                 LDAP_LIST_REMOVE(zo, zo_link);
496                                                 zo->zo_ptr = tmpp;
497                                                 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
498                                                                 zo, zo_link);
499                                                 break;
500
501                                                 Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
502                                                         "free object not found while bit is clear.\n",
503                                                         0, 0, 0);
504                                                 assert(zo);
505
506                                         }
507                                 } else {
508                                         if (!inserted) {
509                                                 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
510                                                         slap_replenish_zopool(zh);
511                                                 }
512                                                 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
513                                                 LDAP_LIST_REMOVE(zo, zo_link);
514                                                 zo->zo_ptr = tmpp;
515                                                 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
516                                                                 zo, zo_link);
517                                         }
518                                         break;
519                                 }
520                         } else {
521                                 if (!(zh->zh_maps[idx][i-order_start][(diff-1)>>3] &
522                                                 (1<<((diff-1)&0x7)))) {
523                                         zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
524                                         while (zo) {
525                                                 if ((char*)zo->zo_ptr == (char*)tmpp) {
526                                                         LDAP_LIST_REMOVE(zo, zo_link);
527                                                 } else if ((char*)tmpp == zo->zo_ptr + order_size) {
528                                                         LDAP_LIST_REMOVE(zo, zo_link);
529                                                         tmpp = zo->zo_ptr;
530                                                         break;
531                                                 }
532                                                 zo = LDAP_LIST_NEXT(zo, zo_link);
533                                         }
534                                         if (zo) {
535                                                 if (i < zh->zh_zoneorder) {
536                                                         inserted = 1;
537                                                         LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
538                                                                         zo, zo_link);
539                                                         continue;
540                                                 }
541                                         } else {
542                                                 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
543                                                         slap_replenish_zopool(zh);
544                                                 }
545                                                 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
546                                                 LDAP_LIST_REMOVE(zo, zo_link);
547                                                 zo->zo_ptr = tmpp;
548                                                 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
549                                                                 zo, zo_link);
550                                                 break;
551
552                                                 Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
553                                                         "free object not found while bit is clear.\n",
554                                                         0, 0, 0 );
555                                                 assert( zo );
556
557                                         }
558                                 } else {
559                                         if ( !inserted ) {
560                                                 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
561                                                         slap_replenish_zopool(zh);
562                                                 }
563                                                 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
564                                                 LDAP_LIST_REMOVE(zo, zo_link);
565                                                 zo->zo_ptr = tmpp;
566                                                 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
567                                                                 zo, zo_link);
568                                         }
569                                         break;
570                                 }
571                         }
572                 }
573                 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
574         }
575 }
576
577 static int
578 slap_zone_cmp(const void *v1, const void *v2)
579 {
580         const struct zone_object *zo1 = v1;
581         const struct zone_object *zo2 = v2;
582
583         return zo1->zo_ptr - zo2->zo_ptr;
584 }
585
586 void *
587 slap_replenish_zopool(
588         void *ctx
589 )
590 {
591         struct zone_heap* zh = ctx;
592         struct zone_object *zo_block;
593         int i;
594
595         zo_block = (struct zone_object *)ch_malloc(
596                                         SLAP_ZONE_ZOBLOCK * sizeof(struct zone_object));
597
598         if ( zo_block == NULL ) {
599                 return NULL;
600         }
601
602         zo_block[0].zo_blockhead = 1;
603         LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[0], zo_link);
604         for (i = 1; i < SLAP_ZONE_ZOBLOCK; i++) {
605                 zo_block[i].zo_blockhead = 0;
606                 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[i], zo_link );
607         }
608
609         return zo_block;
610 }
611 #endif /* SLAP_ZONE_ALLOC */