1 /* zn_malloc.c - zone-based malloc routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
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
23 * This work originally developed by Jong-Hyuk Choi
24 * 2004/11/29 jongchoi@OpenLDAP.org
30 #include <ac/string.h>
31 #include <sys/types.h>
37 #ifdef SLAP_ZONE_ALLOC
39 static int slap_zone_cmp(const void *v1, const void *v2);
40 void * slap_replenish_zopool(void *ctx);
43 slap_zo_release(void *data)
45 struct zone_object *zo = (struct zone_object *)data;
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;
62 } while (pad_shift >>= 1);
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]);
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);
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]);
79 ch_free(zh->zh_maps[i]);
80 munmap(zh->zh_zones[i], zh->zh_zonesize);
83 ch_free(zh->zh_zones);
84 ch_free(zh->zh_seqno);
86 avl_free(zh->zh_zonetree, slap_zo_release);
88 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
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);
96 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
98 struct zone_object *zo_tmp = zo;
99 zo = LDAP_LIST_NEXT(zo, zo_link);
102 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
103 ldap_pvt_thread_mutex_destroy( &zh->zh_mutex );
115 struct zone_heap *zh = NULL;
117 int pad = 2*sizeof(int)-1, pad_shift;
119 int order = -1, order_start = -1, order_end = -1;
121 struct zone_object *zo;
123 zh = (struct zone_heap *)ch_calloc(1, sizeof(struct zone_heap));
125 zh->zh_fd = open("/dev/zero", O_RDWR);
128 zh->zh_zonesize = zonesize;
130 zh->zh_zonesize = SLAP_ZONE_SIZE;
133 zpad = zh->zh_zonesize - 1;
134 zh->zh_numzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
136 if ( maxsize && maxsize >= initsize ) {
137 zh->zh_maxzones = ((maxsize + zpad) & ~zpad) / zh->zh_zonesize;
139 zh->zh_maxzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
143 zh->zh_deltazones = ((deltasize + zpad) & ~zpad) / zh->zh_zonesize;
145 zh->zh_deltazones = ((SLAP_ZONE_DELTA+zpad) & ~zpad) / zh->zh_zonesize;
148 Debug(LDAP_DEBUG_ANY, "==> slap_zn_mem_create: %d\n",
149 zh->zh_numzones, 0, 0 );
151 size_shift = zh->zh_zonesize - 1;
154 } while (size_shift >>= 1);
159 } while (pad_shift >>= 1);
161 order = order_end - order_start + 1;
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**));
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]);
175 LDAP_LIST_INIT(&zh->zh_zopool);
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;
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);
192 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
193 slap_replenish_zopool(zh);
195 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
196 LDAP_LIST_REMOVE(zo, zo_link);
197 zo->zo_ptr = zh->zh_zones[i];
199 LDAP_LIST_INSERT_HEAD(&zh->zh_free[order-1], zo, zo_link);
201 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
202 slap_replenish_zopool(zh);
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);
208 avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
211 ldap_pvt_thread_mutex_init(&zh->zh_mutex);
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;
232 if (!zh) return ber_memalloc_x(size, NULL);
234 /* round up to doubleword boundary */
235 size += 2*sizeof(ber_len_t) + pad;
238 size_shift = size - 1;
241 } while (size_shift >>= 1);
246 } while (pad_shift >>= 1);
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++);
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;
266 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_new, zo_link);
267 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
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);
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] |=
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 );
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);
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",
308 return (void*)ch_malloc(size);
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;
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);
327 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
328 slap_replenish_zopool(zh);
330 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
331 LDAP_LIST_REMOVE(zo, zo_link);
332 zo->zo_ptr = zh->zh_zones[i];
334 LDAP_LIST_INSERT_HEAD(&zh->
335 zh_free[zh->zh_zoneorder-order_start],zo,zo_link);
337 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
338 slap_replenish_zopool(zh);
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]>>
345 avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
347 zh->zh_numzones += zh->zh_deltazones;
348 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
354 slap_zn_calloc( ber_len_t n, ber_len_t size, void *ctx )
358 new = slap_zn_malloc( n*size, ctx );
360 memset( new, 0, n*size );
366 slap_zn_realloc(void *ptr, ber_len_t size, void *ctx)
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;
376 struct zone_heap *zone = NULL;
379 return slap_zn_malloc(size, zh);
381 zoi.zo_ptr = (void*)((unsigned long)p >> zh->zh_zoneorder);
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 );
390 /* Not our memory? */
392 /* duplicate of realloc behavior, oh well */
393 new = ber_memrealloc_x(ptr, size, NULL);
397 Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
400 exit( EXIT_FAILURE );
403 assert(zoo->zo_idx != -1);
405 zone = zh->zh_zones[zoo->zo_idx];
408 slap_zn_free(ptr, zh);
412 newptr = slap_zn_malloc(size, zh);
414 AC_MEMCPY(newptr, ptr, size);
416 AC_MEMCPY(newptr, ptr, p[-1]);
418 slap_zn_free(ptr, zh);
423 slap_zn_free(void *ptr, void *ctx)
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;
432 int i, k, inserted = 0, idx;
433 struct zone_heap *zone = NULL;
435 fprintf(stderr,"slap_zn_free... 0x%x\n", ptr);
436 zoi.zo_ptr = (void*)((unsigned long)p >> zh->zh_zoneorder);
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 );
446 ber_memfree_x(ptr, NULL);
450 zone = zh->zh_zones[idx];
453 size_shift = size + 2*sizeof(ber_len_t) - 1;
456 } while (size_shift >>= 1);
461 } while (pad_shift >>= 1);
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]);
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);
480 zo = LDAP_LIST_NEXT(zo, zo_link);
483 if (i < zh->zh_zoneorder) {
486 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
491 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
492 slap_replenish_zopool(zh);
494 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
495 LDAP_LIST_REMOVE(zo, zo_link);
497 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
501 Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
502 "free object not found while bit is clear.\n",
509 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
510 slap_replenish_zopool(zh);
512 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
513 LDAP_LIST_REMOVE(zo, zo_link);
515 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
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]);
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);
532 zo = LDAP_LIST_NEXT(zo, zo_link);
535 if (i < zh->zh_zoneorder) {
537 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
542 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
543 slap_replenish_zopool(zh);
545 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
546 LDAP_LIST_REMOVE(zo, zo_link);
548 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
552 Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
553 "free object not found while bit is clear.\n",
560 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
561 slap_replenish_zopool(zh);
563 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
564 LDAP_LIST_REMOVE(zo, zo_link);
566 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
573 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
578 slap_zone_cmp(const void *v1, const void *v2)
580 const struct zone_object *zo1 = v1;
581 const struct zone_object *zo2 = v2;
583 return zo1->zo_ptr - zo2->zo_ptr;
587 slap_replenish_zopool(
591 struct zone_heap* zh = ctx;
592 struct zone_object *zo_block;
595 zo_block = (struct zone_object *)ch_malloc(
596 SLAP_ZONE_ZOBLOCK * sizeof(struct zone_object));
598 if ( zo_block == NULL ) {
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 );
611 #endif /* SLAP_ZONE_ALLOC */