Home | History | Annotate | Line # | Download | only in slapd
zn_malloc.c revision 1.2
      1  1.2  christos /*	$NetBSD: zn_malloc.c,v 1.2 2020/08/11 13:15:39 christos Exp $	*/
      2  1.2  christos 
      3  1.1     lukem /* zn_malloc.c - zone-based malloc routines */
      4  1.2  christos /* $OpenLDAP$*/
      5  1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  1.1     lukem  *
      7  1.2  christos  * Copyright 2003-2020 The OpenLDAP Foundation.
      8  1.1     lukem  * All rights reserved.
      9  1.1     lukem  *
     10  1.1     lukem  * Redistribution and use in source and binary forms, with or without
     11  1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     12  1.1     lukem  * Public License.
     13  1.1     lukem  *
     14  1.1     lukem  * A copy of this license is available in the file LICENSE in the
     15  1.1     lukem  * top-level directory of the distribution or, alternatively, at
     16  1.1     lukem  * <http://www.OpenLDAP.org/license.html>.
     17  1.1     lukem  */
     18  1.1     lukem /* Portions Copyright 2004 IBM Corporation
     19  1.1     lukem  * All rights reserved.
     20  1.1     lukem  * Redistribution and use in source and binary forms, with or without
     21  1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     22  1.1     lukem  * Public License.
     23  1.1     lukem  */
     24  1.1     lukem /* ACKNOWLEDGEMENTS
     25  1.1     lukem  * This work originally developed by Jong-Hyuk Choi for inclusion in
     26  1.1     lukem  * OpenLDAP Software.
     27  1.1     lukem  */
     28  1.1     lukem 
     29  1.2  christos #include <sys/cdefs.h>
     30  1.2  christos __RCSID("$NetBSD: zn_malloc.c,v 1.2 2020/08/11 13:15:39 christos Exp $");
     31  1.2  christos 
     32  1.1     lukem #include "portable.h"
     33  1.1     lukem 
     34  1.1     lukem #include <stdio.h>
     35  1.1     lukem #include <ac/string.h>
     36  1.1     lukem #include <sys/types.h>
     37  1.1     lukem #include <fcntl.h>
     38  1.1     lukem 
     39  1.1     lukem #include "slap.h"
     40  1.1     lukem 
     41  1.1     lukem #ifdef SLAP_ZONE_ALLOC
     42  1.1     lukem 
     43  1.1     lukem #include <sys/mman.h>
     44  1.1     lukem 
     45  1.1     lukem static int slap_zone_cmp(const void *v1, const void *v2);
     46  1.1     lukem void * slap_replenish_zopool(void *ctx);
     47  1.1     lukem 
     48  1.1     lukem static void
     49  1.1     lukem slap_zo_release(void *data)
     50  1.1     lukem {
     51  1.1     lukem 	struct zone_object *zo = (struct zone_object *)data;
     52  1.1     lukem 	ch_free( zo );
     53  1.1     lukem }
     54  1.1     lukem 
     55  1.1     lukem void
     56  1.1     lukem slap_zn_mem_destroy(
     57  1.1     lukem 	void *ctx
     58  1.1     lukem )
     59  1.1     lukem {
     60  1.1     lukem 	struct zone_heap *zh = ctx;
     61  1.1     lukem 	int pad = 2*sizeof(int)-1, pad_shift;
     62  1.1     lukem 	int order_start = -1, i, j;
     63  1.1     lukem 	struct zone_object *zo;
     64  1.1     lukem 
     65  1.1     lukem 	pad_shift = pad - 1;
     66  1.1     lukem 	do {
     67  1.1     lukem 		order_start++;
     68  1.1     lukem 	} while (pad_shift >>= 1);
     69  1.1     lukem 
     70  1.1     lukem 	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
     71  1.1     lukem 	for (i = 0; i < zh->zh_zoneorder - order_start + 1; i++) {
     72  1.1     lukem 		zo = LDAP_LIST_FIRST(&zh->zh_free[i]);
     73  1.1     lukem 		while (zo) {
     74  1.1     lukem 			struct zone_object *zo_tmp = zo;
     75  1.1     lukem 			zo = LDAP_LIST_NEXT(zo, zo_link);
     76  1.1     lukem 			LDAP_LIST_REMOVE(zo_tmp, zo_link);
     77  1.1     lukem 			LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_tmp, zo_link);
     78  1.1     lukem 		}
     79  1.1     lukem 	}
     80  1.1     lukem 	ch_free(zh->zh_free);
     81  1.1     lukem 
     82  1.1     lukem 	for (i = 0; i < zh->zh_numzones; i++) {
     83  1.1     lukem 		for (j = 0; j < zh->zh_zoneorder - order_start + 1; j++) {
     84  1.1     lukem 			ch_free(zh->zh_maps[i][j]);
     85  1.1     lukem 		}
     86  1.1     lukem 		ch_free(zh->zh_maps[i]);
     87  1.1     lukem 		munmap(zh->zh_zones[i], zh->zh_zonesize);
     88  1.1     lukem 		ldap_pvt_thread_rdwr_destroy(&zh->zh_znlock[i]);
     89  1.1     lukem 	}
     90  1.1     lukem 	ch_free(zh->zh_maps);
     91  1.1     lukem 	ch_free(zh->zh_zones);
     92  1.1     lukem 	ch_free(zh->zh_seqno);
     93  1.1     lukem 	ch_free(zh->zh_znlock);
     94  1.1     lukem 
     95  1.1     lukem 	avl_free(zh->zh_zonetree, slap_zo_release);
     96  1.1     lukem 
     97  1.1     lukem 	zo = LDAP_LIST_FIRST(&zh->zh_zopool);
     98  1.1     lukem 	while (zo) {
     99  1.1     lukem 		struct zone_object *zo_tmp = zo;
    100  1.1     lukem 		zo = LDAP_LIST_NEXT(zo, zo_link);
    101  1.1     lukem 		if (!zo_tmp->zo_blockhead) {
    102  1.1     lukem 			LDAP_LIST_REMOVE(zo_tmp, zo_link);
    103  1.1     lukem 		}
    104  1.1     lukem 	}
    105  1.1     lukem 	zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    106  1.1     lukem 	while (zo) {
    107  1.1     lukem 		struct zone_object *zo_tmp = zo;
    108  1.1     lukem 		zo = LDAP_LIST_NEXT(zo, zo_link);
    109  1.1     lukem 		ch_free(zo_tmp);
    110  1.1     lukem 	}
    111  1.1     lukem 	ldap_pvt_thread_mutex_unlock(&zh->zh_mutex);
    112  1.1     lukem 	ldap_pvt_thread_rdwr_destroy(&zh->zh_lock);
    113  1.1     lukem 	ldap_pvt_thread_mutex_destroy(&zh->zh_mutex);
    114  1.1     lukem 	ch_free(zh);
    115  1.1     lukem }
    116  1.1     lukem 
    117  1.1     lukem void *
    118  1.1     lukem slap_zn_mem_create(
    119  1.1     lukem 	ber_len_t initsize,
    120  1.1     lukem 	ber_len_t maxsize,
    121  1.1     lukem 	ber_len_t deltasize,
    122  1.1     lukem 	ber_len_t zonesize
    123  1.1     lukem )
    124  1.1     lukem {
    125  1.1     lukem 	struct zone_heap *zh = NULL;
    126  1.1     lukem 	ber_len_t zpad;
    127  1.1     lukem 	int pad = 2*sizeof(int)-1, pad_shift;
    128  1.1     lukem 	int size_shift;
    129  1.1     lukem 	int order = -1, order_start = -1, order_end = -1;
    130  1.1     lukem 	int i, j;
    131  1.1     lukem 	struct zone_object *zo;
    132  1.1     lukem 
    133  1.1     lukem 	Debug(LDAP_DEBUG_NONE,
    134  1.1     lukem 		"--> slap_zn_mem_create: initsize=%d, maxsize=%d\n",
    135  1.1     lukem 		initsize, maxsize, 0);
    136  1.1     lukem 	Debug(LDAP_DEBUG_NONE,
    137  1.1     lukem 		"++> slap_zn_mem_create: deltasize=%d, zonesize=%d\n",
    138  1.1     lukem 		deltasize, zonesize, 0);
    139  1.1     lukem 
    140  1.1     lukem 	zh = (struct zone_heap *)ch_calloc(1, sizeof(struct zone_heap));
    141  1.1     lukem 
    142  1.1     lukem 	zh->zh_fd = open("/dev/zero", O_RDWR);
    143  1.1     lukem 
    144  1.1     lukem 	if ( zonesize ) {
    145  1.1     lukem 		zh->zh_zonesize = zonesize;
    146  1.1     lukem 	} else {
    147  1.1     lukem 		zh->zh_zonesize = SLAP_ZONE_SIZE;
    148  1.1     lukem 	}
    149  1.1     lukem 
    150  1.1     lukem 	zpad = zh->zh_zonesize - 1;
    151  1.1     lukem 	zh->zh_numzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
    152  1.1     lukem 
    153  1.1     lukem 	if ( maxsize && maxsize >= initsize ) {
    154  1.1     lukem 		zh->zh_maxzones = ((maxsize + zpad) & ~zpad) / zh->zh_zonesize;
    155  1.1     lukem 	} else {
    156  1.1     lukem 		zh->zh_maxzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
    157  1.1     lukem 	}
    158  1.1     lukem 
    159  1.1     lukem 	if ( deltasize ) {
    160  1.1     lukem 		zh->zh_deltazones = ((deltasize + zpad) & ~zpad) / zh->zh_zonesize;
    161  1.1     lukem 	} else {
    162  1.1     lukem 		zh->zh_deltazones = ((SLAP_ZONE_DELTA+zpad) & ~zpad) / zh->zh_zonesize;
    163  1.1     lukem 	}
    164  1.1     lukem 
    165  1.1     lukem 	size_shift = zh->zh_zonesize - 1;
    166  1.1     lukem 	do {
    167  1.1     lukem 		order_end++;
    168  1.1     lukem 	} while (size_shift >>= 1);
    169  1.1     lukem 
    170  1.1     lukem 	pad_shift = pad - 1;
    171  1.1     lukem 	do {
    172  1.1     lukem 		order_start++;
    173  1.1     lukem 	} while (pad_shift >>= 1);
    174  1.1     lukem 
    175  1.1     lukem 	order = order_end - order_start + 1;
    176  1.1     lukem 
    177  1.1     lukem 	zh->zh_zones = (void **)ch_malloc(zh->zh_maxzones * sizeof(void*));
    178  1.1     lukem 	zh->zh_znlock = (ldap_pvt_thread_rdwr_t *)ch_malloc(
    179  1.1     lukem 						zh->zh_maxzones * sizeof(ldap_pvt_thread_rdwr_t *));
    180  1.1     lukem 	zh->zh_maps = (unsigned char ***)ch_malloc(
    181  1.1     lukem 					zh->zh_maxzones * sizeof(unsigned char**));
    182  1.1     lukem 
    183  1.1     lukem 	zh->zh_zoneorder = order_end;
    184  1.1     lukem 	zh->zh_free = (struct zh_freelist *)
    185  1.1     lukem 					ch_malloc(order * sizeof(struct zh_freelist));
    186  1.1     lukem 	zh->zh_seqno = (unsigned long *)ch_calloc(zh->zh_maxzones,
    187  1.1     lukem 											sizeof(unsigned long));
    188  1.1     lukem 	for (i = 0; i < order; i++) {
    189  1.1     lukem 		LDAP_LIST_INIT(&zh->zh_free[i]);
    190  1.1     lukem 	}
    191  1.1     lukem 	LDAP_LIST_INIT(&zh->zh_zopool);
    192  1.1     lukem 
    193  1.1     lukem 	for (i = 0; i < zh->zh_numzones; i++) {
    194  1.1     lukem 		zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
    195  1.1     lukem 							MAP_PRIVATE, zh->zh_fd, 0);
    196  1.1     lukem 		zh->zh_maps[i] = (unsigned char **)
    197  1.1     lukem 					ch_malloc(order * sizeof(unsigned char *));
    198  1.1     lukem 		for (j = 0; j < order; j++) {
    199  1.1     lukem 			int shiftamt = order_start + 1 + j;
    200  1.1     lukem 			int nummaps = zh->zh_zonesize >> shiftamt;
    201  1.1     lukem 			assert(nummaps);
    202  1.1     lukem 			nummaps >>= 3;
    203  1.1     lukem 			if (!nummaps) nummaps = 1;
    204  1.1     lukem 			zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
    205  1.1     lukem 			memset(zh->zh_maps[i][j], 0, nummaps);
    206  1.1     lukem 		}
    207  1.1     lukem 
    208  1.1     lukem 		if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    209  1.1     lukem 			slap_replenish_zopool(zh);
    210  1.1     lukem 		}
    211  1.1     lukem 		zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    212  1.1     lukem 		LDAP_LIST_REMOVE(zo, zo_link);
    213  1.1     lukem 		zo->zo_ptr = zh->zh_zones[i];
    214  1.1     lukem 		zo->zo_idx = i;
    215  1.1     lukem 		LDAP_LIST_INSERT_HEAD(&zh->zh_free[order-1], zo, zo_link);
    216  1.1     lukem 
    217  1.1     lukem 		if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    218  1.1     lukem 			slap_replenish_zopool(zh);
    219  1.1     lukem 		}
    220  1.1     lukem 		zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    221  1.1     lukem 		LDAP_LIST_REMOVE(zo, zo_link);
    222  1.1     lukem 		zo->zo_ptr = zh->zh_zones[i];
    223  1.1     lukem 		zo->zo_siz = zh->zh_zonesize;
    224  1.1     lukem 		zo->zo_idx = i;
    225  1.1     lukem 		avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
    226  1.1     lukem 		ldap_pvt_thread_rdwr_init(&zh->zh_znlock[i]);
    227  1.1     lukem 	}
    228  1.1     lukem 
    229  1.1     lukem 	LDAP_STAILQ_INIT(&zh->zh_latency_history_queue);
    230  1.1     lukem 	ldap_pvt_thread_mutex_init(&zh->zh_mutex);
    231  1.1     lukem 	ldap_pvt_thread_rdwr_init(&zh->zh_lock);
    232  1.1     lukem 
    233  1.1     lukem 	return zh;
    234  1.1     lukem }
    235  1.1     lukem 
    236  1.1     lukem void *
    237  1.1     lukem slap_zn_malloc(
    238  1.1     lukem     ber_len_t	size,
    239  1.1     lukem 	void *ctx
    240  1.1     lukem )
    241  1.1     lukem {
    242  1.1     lukem 	struct zone_heap *zh = ctx;
    243  1.1     lukem 	ber_len_t size_shift;
    244  1.1     lukem 	int pad = 2*sizeof(int)-1, pad_shift;
    245  1.1     lukem 	int order = -1, order_start = -1;
    246  1.1     lukem 	struct zone_object *zo, *zo_new, *zo_left, *zo_right;
    247  1.1     lukem 	ber_len_t *ptr, *new;
    248  1.1     lukem 	int idx;
    249  1.1     lukem 	unsigned long diff;
    250  1.1     lukem 	int i, j, k;
    251  1.1     lukem 
    252  1.1     lukem 	Debug(LDAP_DEBUG_NONE,
    253  1.1     lukem 		"--> slap_zn_malloc: size=%d\n", size, 0, 0);
    254  1.1     lukem 
    255  1.1     lukem 	if (!zh) return ber_memalloc_x(size, NULL);
    256  1.1     lukem 
    257  1.1     lukem 	/* round up to doubleword boundary */
    258  1.1     lukem 	size += 2*sizeof(ber_len_t) + pad;
    259  1.1     lukem 	size &= ~pad;
    260  1.1     lukem 
    261  1.1     lukem 	size_shift = size - 1;
    262  1.1     lukem 	do {
    263  1.1     lukem 		order++;
    264  1.1     lukem 	} while (size_shift >>= 1);
    265  1.1     lukem 
    266  1.1     lukem 	pad_shift = pad - 1;
    267  1.1     lukem 	do {
    268  1.1     lukem 		order_start++;
    269  1.1     lukem 	} while (pad_shift >>= 1);
    270  1.1     lukem 
    271  1.1     lukem retry:
    272  1.1     lukem 
    273  1.1     lukem 	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    274  1.1     lukem 	for (i = order; i <= zh->zh_zoneorder &&
    275  1.1     lukem 			LDAP_LIST_EMPTY(&zh->zh_free[i-order_start]); i++);
    276  1.1     lukem 
    277  1.1     lukem 	if (i == order) {
    278  1.1     lukem 		zo_new = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
    279  1.1     lukem 		LDAP_LIST_REMOVE(zo_new, zo_link);
    280  1.1     lukem 		ptr = zo_new->zo_ptr;
    281  1.1     lukem 		idx = zo_new->zo_idx;
    282  1.1     lukem 		diff = (unsigned long)((char*)ptr -
    283  1.1     lukem 				(char*)zh->zh_zones[idx]) >> (order + 1);
    284  1.1     lukem 		zh->zh_maps[idx][order-order_start][diff>>3] |= (1 << (diff & 0x7));
    285  1.1     lukem 		*ptr++ = zh->zh_seqno[idx];
    286  1.1     lukem 		*ptr++ = size - 2*sizeof(ber_len_t);
    287  1.1     lukem 		zo_new->zo_ptr = NULL;
    288  1.1     lukem 		zo_new->zo_idx = -1;
    289  1.1     lukem 		LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_new, zo_link);
    290  1.1     lukem 		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    291  1.1     lukem 		Debug(LDAP_DEBUG_NONE, "slap_zn_malloc: returning 0x%x, 0x%x\n",
    292  1.1     lukem 				ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
    293  1.1     lukem 		return((void*)ptr);
    294  1.1     lukem 	} else if (i <= zh->zh_zoneorder) {
    295  1.1     lukem 		for (j = i; j > order; j--) {
    296  1.1     lukem 			zo_left = LDAP_LIST_FIRST(&zh->zh_free[j-order_start]);
    297  1.1     lukem 			LDAP_LIST_REMOVE(zo_left, zo_link);
    298  1.1     lukem 			if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    299  1.1     lukem 				slap_replenish_zopool(zh);
    300  1.1     lukem 			}
    301  1.1     lukem 			zo_right = LDAP_LIST_FIRST(&zh->zh_zopool);
    302  1.1     lukem 			LDAP_LIST_REMOVE(zo_right, zo_link);
    303  1.1     lukem 			zo_right->zo_ptr = zo_left->zo_ptr + (1 << j);
    304  1.1     lukem 			zo_right->zo_idx = zo_left->zo_idx;
    305  1.1     lukem 			Debug(LDAP_DEBUG_NONE,
    306  1.1     lukem 				"slap_zn_malloc: split (left=0x%x, right=0x%x)\n",
    307  1.1     lukem 				zo_left->zo_ptr, zo_right->zo_ptr, 0);
    308  1.1     lukem 			if (j == order + 1) {
    309  1.1     lukem 				ptr = zo_left->zo_ptr;
    310  1.1     lukem 				diff = (unsigned long)((char*)ptr -
    311  1.1     lukem 						(char*)zh->zh_zones[zo_left->zo_idx]) >> (order+1);
    312  1.1     lukem 				zh->zh_maps[zo_left->zo_idx][order-order_start][diff>>3] |=
    313  1.1     lukem 						(1 << (diff & 0x7));
    314  1.1     lukem 				*ptr++ = zh->zh_seqno[zo_left->zo_idx];
    315  1.1     lukem 				*ptr++ = size - 2*sizeof(ber_len_t);
    316  1.1     lukem 				LDAP_LIST_INSERT_HEAD(
    317  1.1     lukem 						&zh->zh_free[j-1-order_start], zo_right, zo_link);
    318  1.1     lukem 				LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_left, zo_link);
    319  1.1     lukem 				ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    320  1.1     lukem 				Debug(LDAP_DEBUG_NONE,
    321  1.1     lukem 					"slap_zn_malloc: returning 0x%x, 0x%x\n",
    322  1.1     lukem 					ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
    323  1.1     lukem 				return((void*)ptr);
    324  1.1     lukem 			} else {
    325  1.1     lukem 				LDAP_LIST_INSERT_HEAD(
    326  1.1     lukem 						&zh->zh_free[j-1-order_start], zo_right, zo_link);
    327  1.1     lukem 				LDAP_LIST_INSERT_HEAD(
    328  1.1     lukem 						&zh->zh_free[j-1-order_start], zo_left, zo_link);
    329  1.1     lukem 			}
    330  1.1     lukem 		}
    331  1.1     lukem 		assert(0);
    332  1.1     lukem 	} else {
    333  1.1     lukem 
    334  1.1     lukem 		if ( zh->zh_maxzones < zh->zh_numzones + zh->zh_deltazones ) {
    335  1.1     lukem 			ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    336  1.1     lukem 			Debug( LDAP_DEBUG_TRACE,
    337  1.2  christos 				"zn_malloc %lu: ch_malloc\n",
    338  1.1     lukem 				(long)size, 0, 0);
    339  1.1     lukem 			Debug(LDAP_DEBUG_NONE,
    340  1.1     lukem 				"slap_zn_malloc: returning 0x%x, 0x%x\n",
    341  1.1     lukem 				ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
    342  1.1     lukem 			return (void*)ch_malloc(size);
    343  1.1     lukem 		}
    344  1.1     lukem 
    345  1.1     lukem 		for (i = zh->zh_numzones; i < zh->zh_numzones+zh->zh_deltazones; i++) {
    346  1.1     lukem 			zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
    347  1.1     lukem 								MAP_PRIVATE, zh->zh_fd, 0);
    348  1.1     lukem 			zh->zh_maps[i] = (unsigned char **)
    349  1.1     lukem 						ch_malloc((zh->zh_zoneorder - order_start + 1) *
    350  1.1     lukem 						sizeof(unsigned char *));
    351  1.1     lukem 			for (j = 0; j < zh->zh_zoneorder-order_start+1; j++) {
    352  1.1     lukem 				int shiftamt = order_start + 1 + j;
    353  1.1     lukem 				int nummaps = zh->zh_zonesize >> shiftamt;
    354  1.1     lukem 				assert(nummaps);
    355  1.1     lukem 				nummaps >>= 3;
    356  1.1     lukem 				if (!nummaps) nummaps = 1;
    357  1.1     lukem 				zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
    358  1.1     lukem 				memset(zh->zh_maps[i][j], 0, nummaps);
    359  1.1     lukem 			}
    360  1.1     lukem 
    361  1.1     lukem 			if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    362  1.1     lukem 				slap_replenish_zopool(zh);
    363  1.1     lukem 			}
    364  1.1     lukem 			zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    365  1.1     lukem 			LDAP_LIST_REMOVE(zo, zo_link);
    366  1.1     lukem 			zo->zo_ptr = zh->zh_zones[i];
    367  1.1     lukem 			zo->zo_idx = i;
    368  1.1     lukem 			LDAP_LIST_INSERT_HEAD(&zh->
    369  1.1     lukem 						zh_free[zh->zh_zoneorder-order_start],zo,zo_link);
    370  1.1     lukem 
    371  1.1     lukem 			if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    372  1.1     lukem 				slap_replenish_zopool(zh);
    373  1.1     lukem 			}
    374  1.1     lukem 			zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    375  1.1     lukem 			LDAP_LIST_REMOVE(zo, zo_link);
    376  1.1     lukem 			zo->zo_ptr = zh->zh_zones[i];
    377  1.1     lukem 			zo->zo_siz = zh->zh_zonesize;
    378  1.1     lukem 			zo->zo_idx = i;
    379  1.1     lukem 			avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
    380  1.1     lukem 			ldap_pvt_thread_rdwr_init(&zh->zh_znlock[i]);
    381  1.1     lukem 		}
    382  1.1     lukem 		zh->zh_numzones += zh->zh_deltazones;
    383  1.1     lukem 		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    384  1.1     lukem 		goto retry;
    385  1.1     lukem 	}
    386  1.1     lukem }
    387  1.1     lukem 
    388  1.1     lukem void *
    389  1.1     lukem slap_zn_calloc( ber_len_t n, ber_len_t size, void *ctx )
    390  1.1     lukem {
    391  1.1     lukem 	void *new;
    392  1.1     lukem 
    393  1.1     lukem 	new = slap_zn_malloc( n*size, ctx );
    394  1.1     lukem 	if ( new ) {
    395  1.1     lukem 		memset( new, 0, n*size );
    396  1.1     lukem 	}
    397  1.1     lukem 	return new;
    398  1.1     lukem }
    399  1.1     lukem 
    400  1.1     lukem void *
    401  1.1     lukem slap_zn_realloc(void *ptr, ber_len_t size, void *ctx)
    402  1.1     lukem {
    403  1.1     lukem 	struct zone_heap *zh = ctx;
    404  1.1     lukem 	int pad = 2*sizeof(int)-1, pad_shift;
    405  1.1     lukem 	int order_start = -1, order = -1;
    406  1.1     lukem 	struct zone_object zoi, *zoo;
    407  1.1     lukem 	ber_len_t *p = (ber_len_t *)ptr, *new;
    408  1.1     lukem 	unsigned long diff;
    409  1.1     lukem 	int i;
    410  1.1     lukem 	void *newptr = NULL;
    411  1.1     lukem 	struct zone_heap *zone = NULL;
    412  1.1     lukem 
    413  1.1     lukem 	Debug(LDAP_DEBUG_NONE,
    414  1.1     lukem 		"--> slap_zn_realloc: ptr=0x%x, size=%d\n", ptr, size, 0);
    415  1.1     lukem 
    416  1.1     lukem 	if (ptr == NULL)
    417  1.1     lukem 		return slap_zn_malloc(size, zh);
    418  1.1     lukem 
    419  1.1     lukem 	zoi.zo_ptr = p;
    420  1.1     lukem 	zoi.zo_idx = -1;
    421  1.1     lukem 
    422  1.1     lukem 	if (zh) {
    423  1.1     lukem 		ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    424  1.1     lukem 		zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    425  1.1     lukem 		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    426  1.1     lukem 	}
    427  1.1     lukem 
    428  1.1     lukem 	/* Not our memory? */
    429  1.1     lukem 	if (!zoo) {
    430  1.1     lukem 		/* duplicate of realloc behavior, oh well */
    431  1.1     lukem 		new = ber_memrealloc_x(ptr, size, NULL);
    432  1.1     lukem 		if (new) {
    433  1.1     lukem 			return new;
    434  1.1     lukem 		}
    435  1.1     lukem 		Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
    436  1.1     lukem 				(long) size, 0, 0);
    437  1.1     lukem 		assert(0);
    438  1.1     lukem 		exit( EXIT_FAILURE );
    439  1.1     lukem 	}
    440  1.1     lukem 
    441  1.1     lukem 	assert(zoo->zo_idx != -1);
    442  1.1     lukem 
    443  1.1     lukem 	zone = zh->zh_zones[zoo->zo_idx];
    444  1.1     lukem 
    445  1.1     lukem 	if (size == 0) {
    446  1.1     lukem 		slap_zn_free(ptr, zh);
    447  1.1     lukem 		return NULL;
    448  1.1     lukem 	}
    449  1.1     lukem 
    450  1.1     lukem 	newptr = slap_zn_malloc(size, zh);
    451  1.1     lukem 	if (size < p[-1]) {
    452  1.1     lukem 		AC_MEMCPY(newptr, ptr, size);
    453  1.1     lukem 	} else {
    454  1.1     lukem 		AC_MEMCPY(newptr, ptr, p[-1]);
    455  1.1     lukem 	}
    456  1.1     lukem 	slap_zn_free(ptr, zh);
    457  1.1     lukem 	return newptr;
    458  1.1     lukem }
    459  1.1     lukem 
    460  1.1     lukem void
    461  1.1     lukem slap_zn_free(void *ptr, void *ctx)
    462  1.1     lukem {
    463  1.1     lukem 	struct zone_heap *zh = ctx;
    464  1.1     lukem 	int size, size_shift, order_size;
    465  1.1     lukem 	int pad = 2*sizeof(int)-1, pad_shift;
    466  1.1     lukem 	ber_len_t *p = (ber_len_t *)ptr, *tmpp;
    467  1.1     lukem 	int order_start = -1, order = -1;
    468  1.1     lukem 	struct zone_object zoi, *zoo, *zo;
    469  1.1     lukem 	unsigned long diff;
    470  1.1     lukem 	int i, k, inserted = 0, idx;
    471  1.1     lukem 	struct zone_heap *zone = NULL;
    472  1.1     lukem 
    473  1.1     lukem 	zoi.zo_ptr = p;
    474  1.1     lukem 	zoi.zo_idx = -1;
    475  1.1     lukem 
    476  1.1     lukem 	Debug(LDAP_DEBUG_NONE, "--> slap_zn_free: ptr=0x%x\n", ptr, 0, 0);
    477  1.1     lukem 
    478  1.1     lukem 	if (zh) {
    479  1.1     lukem 		ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    480  1.1     lukem 		zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    481  1.1     lukem 		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    482  1.1     lukem 	}
    483  1.1     lukem 
    484  1.1     lukem 	if (!zoo) {
    485  1.1     lukem 		ber_memfree_x(ptr, NULL);
    486  1.1     lukem 	} else {
    487  1.1     lukem 		idx = zoo->zo_idx;
    488  1.1     lukem 		assert(idx != -1);
    489  1.1     lukem 		zone = zh->zh_zones[idx];
    490  1.1     lukem 
    491  1.1     lukem 		size = *(--p);
    492  1.1     lukem 		size_shift = size + 2*sizeof(ber_len_t) - 1;
    493  1.1     lukem 		do {
    494  1.1     lukem 			order++;
    495  1.1     lukem 		} while (size_shift >>= 1);
    496  1.1     lukem 
    497  1.1     lukem 		pad_shift = pad - 1;
    498  1.1     lukem 		do {
    499  1.1     lukem 			order_start++;
    500  1.1     lukem 		} while (pad_shift >>= 1);
    501  1.1     lukem 
    502  1.1     lukem 		ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    503  1.1     lukem 		for (i = order, tmpp = p; i <= zh->zh_zoneorder; i++) {
    504  1.1     lukem 			order_size = 1 << (i+1);
    505  1.1     lukem 			diff = (unsigned long)((char*)tmpp - (char*)zone) >> (i+1);
    506  1.1     lukem 			zh->zh_maps[idx][i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
    507  1.1     lukem 			if (diff == ((diff>>1)<<1)) {
    508  1.1     lukem 				if (!(zh->zh_maps[idx][i-order_start][(diff+1)>>3] &
    509  1.1     lukem 						(1<<((diff+1)&0x7)))) {
    510  1.1     lukem 					zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
    511  1.1     lukem 					while (zo) {
    512  1.1     lukem 						if ((char*)zo->zo_ptr == (char*)tmpp) {
    513  1.1     lukem 							LDAP_LIST_REMOVE( zo, zo_link );
    514  1.1     lukem 						} else if ((char*)zo->zo_ptr ==
    515  1.1     lukem 								(char*)tmpp + order_size) {
    516  1.1     lukem 							LDAP_LIST_REMOVE(zo, zo_link);
    517  1.1     lukem 							break;
    518  1.1     lukem 						}
    519  1.1     lukem 						zo = LDAP_LIST_NEXT(zo, zo_link);
    520  1.1     lukem 					}
    521  1.1     lukem 					if (zo) {
    522  1.1     lukem 						if (i < zh->zh_zoneorder) {
    523  1.1     lukem 							inserted = 1;
    524  1.1     lukem 							zo->zo_ptr = tmpp;
    525  1.1     lukem 							Debug(LDAP_DEBUG_NONE,
    526  1.1     lukem 								"slap_zn_free: merging 0x%x\n",
    527  1.1     lukem 								zo->zo_ptr, 0, 0);
    528  1.1     lukem 							LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
    529  1.1     lukem 									zo, zo_link);
    530  1.1     lukem 						}
    531  1.1     lukem 						continue;
    532  1.1     lukem 					} else {
    533  1.1     lukem 						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    534  1.1     lukem 							slap_replenish_zopool(zh);
    535  1.1     lukem 						}
    536  1.1     lukem 						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    537  1.1     lukem 						LDAP_LIST_REMOVE(zo, zo_link);
    538  1.1     lukem 						zo->zo_ptr = tmpp;
    539  1.1     lukem 						zo->zo_idx = idx;
    540  1.1     lukem 						Debug(LDAP_DEBUG_NONE,
    541  1.1     lukem 							"slap_zn_free: merging 0x%x\n",
    542  1.1     lukem 							zo->zo_ptr, 0, 0);
    543  1.1     lukem 						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
    544  1.1     lukem 								zo, zo_link);
    545  1.1     lukem 						break;
    546  1.1     lukem 
    547  1.1     lukem 						Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
    548  1.1     lukem 							"free object not found while bit is clear.\n",
    549  1.1     lukem 							0, 0, 0);
    550  1.1     lukem 						assert(zo != NULL);
    551  1.1     lukem 
    552  1.1     lukem 					}
    553  1.1     lukem 				} else {
    554  1.1     lukem 					if (!inserted) {
    555  1.1     lukem 						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    556  1.1     lukem 							slap_replenish_zopool(zh);
    557  1.1     lukem 						}
    558  1.1     lukem 						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    559  1.1     lukem 						LDAP_LIST_REMOVE(zo, zo_link);
    560  1.1     lukem 						zo->zo_ptr = tmpp;
    561  1.1     lukem 						zo->zo_idx = idx;
    562  1.1     lukem 						Debug(LDAP_DEBUG_NONE,
    563  1.1     lukem 							"slap_zn_free: merging 0x%x\n",
    564  1.1     lukem 							zo->zo_ptr, 0, 0);
    565  1.1     lukem 						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
    566  1.1     lukem 								zo, zo_link);
    567  1.1     lukem 					}
    568  1.1     lukem 					break;
    569  1.1     lukem 				}
    570  1.1     lukem 			} else {
    571  1.1     lukem 				if (!(zh->zh_maps[idx][i-order_start][(diff-1)>>3] &
    572  1.1     lukem 						(1<<((diff-1)&0x7)))) {
    573  1.1     lukem 					zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
    574  1.1     lukem 					while (zo) {
    575  1.1     lukem 						if ((char*)zo->zo_ptr == (char*)tmpp) {
    576  1.1     lukem 							LDAP_LIST_REMOVE(zo, zo_link);
    577  1.1     lukem 						} else if ((char*)tmpp == zo->zo_ptr + order_size) {
    578  1.1     lukem 							LDAP_LIST_REMOVE(zo, zo_link);
    579  1.1     lukem 							tmpp = zo->zo_ptr;
    580  1.1     lukem 							break;
    581  1.1     lukem 						}
    582  1.1     lukem 						zo = LDAP_LIST_NEXT(zo, zo_link);
    583  1.1     lukem 					}
    584  1.1     lukem 					if (zo) {
    585  1.1     lukem 						if (i < zh->zh_zoneorder) {
    586  1.1     lukem 							inserted = 1;
    587  1.1     lukem 							Debug(LDAP_DEBUG_NONE,
    588  1.1     lukem 								"slap_zn_free: merging 0x%x\n",
    589  1.1     lukem 								zo->zo_ptr, 0, 0);
    590  1.1     lukem 							LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
    591  1.1     lukem 									zo, zo_link);
    592  1.1     lukem 							continue;
    593  1.1     lukem 						}
    594  1.1     lukem 					} else {
    595  1.1     lukem 						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    596  1.1     lukem 							slap_replenish_zopool(zh);
    597  1.1     lukem 						}
    598  1.1     lukem 						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    599  1.1     lukem 						LDAP_LIST_REMOVE(zo, zo_link);
    600  1.1     lukem 						zo->zo_ptr = tmpp;
    601  1.1     lukem 						zo->zo_idx = idx;
    602  1.1     lukem 						Debug(LDAP_DEBUG_NONE,
    603  1.1     lukem 							"slap_zn_free: merging 0x%x\n",
    604  1.1     lukem 							zo->zo_ptr, 0, 0);
    605  1.1     lukem 						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
    606  1.1     lukem 								zo, zo_link);
    607  1.1     lukem 						break;
    608  1.1     lukem 
    609  1.1     lukem 						Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
    610  1.1     lukem 							"free object not found while bit is clear.\n",
    611  1.1     lukem 							0, 0, 0 );
    612  1.1     lukem 						assert(zo != NULL);
    613  1.1     lukem 
    614  1.1     lukem 					}
    615  1.1     lukem 				} else {
    616  1.1     lukem 					if ( !inserted ) {
    617  1.1     lukem 						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    618  1.1     lukem 							slap_replenish_zopool(zh);
    619  1.1     lukem 						}
    620  1.1     lukem 						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    621  1.1     lukem 						LDAP_LIST_REMOVE(zo, zo_link);
    622  1.1     lukem 						zo->zo_ptr = tmpp;
    623  1.1     lukem 						zo->zo_idx = idx;
    624  1.1     lukem 						Debug(LDAP_DEBUG_NONE,
    625  1.1     lukem 							"slap_zn_free: merging 0x%x\n",
    626  1.1     lukem 							zo->zo_ptr, 0, 0);
    627  1.1     lukem 						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
    628  1.1     lukem 								zo, zo_link);
    629  1.1     lukem 					}
    630  1.1     lukem 					break;
    631  1.1     lukem 				}
    632  1.1     lukem 			}
    633  1.1     lukem 		}
    634  1.1     lukem 		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    635  1.1     lukem 	}
    636  1.1     lukem }
    637  1.1     lukem 
    638  1.1     lukem static int
    639  1.1     lukem slap_zone_cmp(const void *v1, const void *v2)
    640  1.1     lukem {
    641  1.1     lukem 	const struct zone_object *zo1 = v1;
    642  1.1     lukem 	const struct zone_object *zo2 = v2;
    643  1.1     lukem 	char *ptr1;
    644  1.1     lukem 	char *ptr2;
    645  1.1     lukem 	ber_len_t zpad;
    646  1.1     lukem 
    647  1.1     lukem 	zpad = zo2->zo_siz - 1;
    648  1.1     lukem 	ptr1 = (char*)(((unsigned long)zo1->zo_ptr + zpad) & ~zpad);
    649  1.1     lukem 	ptr2 = (char*)zo2->zo_ptr + ((char*)ptr1 - (char*)zo1->zo_ptr);
    650  1.1     lukem 	ptr2 = (char*)(((unsigned long)ptr2 + zpad) & ~zpad);
    651  1.1     lukem 	return (int)((char*)ptr1 - (char*)ptr2);
    652  1.1     lukem }
    653  1.1     lukem 
    654  1.1     lukem void *
    655  1.1     lukem slap_replenish_zopool(
    656  1.1     lukem 	void *ctx
    657  1.1     lukem )
    658  1.1     lukem {
    659  1.1     lukem 	struct zone_heap* zh = ctx;
    660  1.1     lukem 	struct zone_object *zo_block;
    661  1.1     lukem 	int i;
    662  1.1     lukem 
    663  1.1     lukem 	zo_block = (struct zone_object *)ch_malloc(
    664  1.1     lukem 					SLAP_ZONE_ZOBLOCK * sizeof(struct zone_object));
    665  1.1     lukem 
    666  1.1     lukem 	if ( zo_block == NULL ) {
    667  1.1     lukem 		return NULL;
    668  1.1     lukem 	}
    669  1.1     lukem 
    670  1.1     lukem 	zo_block[0].zo_blockhead = 1;
    671  1.1     lukem 	LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[0], zo_link);
    672  1.1     lukem 	for (i = 1; i < SLAP_ZONE_ZOBLOCK; i++) {
    673  1.1     lukem 		zo_block[i].zo_blockhead = 0;
    674  1.1     lukem 		LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[i], zo_link );
    675  1.1     lukem 	}
    676  1.1     lukem 
    677  1.1     lukem 	return zo_block;
    678  1.1     lukem }
    679  1.1     lukem 
    680  1.1     lukem int
    681  1.1     lukem slap_zn_invalidate(
    682  1.1     lukem 	void *ctx,
    683  1.1     lukem 	void *ptr
    684  1.1     lukem )
    685  1.1     lukem {
    686  1.1     lukem 	struct zone_heap* zh = ctx;
    687  1.1     lukem 	struct zone_object zoi, *zoo;
    688  1.1     lukem 	struct zone_heap *zone = NULL;
    689  1.1     lukem 	int seqno = *((ber_len_t*)ptr - 2);
    690  1.1     lukem 	int idx = -1, rc = 0;
    691  1.1     lukem 	int pad = 2*sizeof(int)-1, pad_shift;
    692  1.1     lukem 	int order_start = -1, i;
    693  1.1     lukem 	struct zone_object *zo;
    694  1.1     lukem 
    695  1.1     lukem 	pad_shift = pad - 1;
    696  1.1     lukem 	do {
    697  1.1     lukem 		order_start++;
    698  1.1     lukem 	} while (pad_shift >>= 1);
    699  1.1     lukem 
    700  1.1     lukem 	zoi.zo_ptr = ptr;
    701  1.1     lukem 	zoi.zo_idx = -1;
    702  1.1     lukem 
    703  1.1     lukem 	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    704  1.1     lukem 	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    705  1.1     lukem 
    706  1.1     lukem 	if (zoo) {
    707  1.1     lukem 		idx = zoo->zo_idx;
    708  1.1     lukem 		assert(idx != -1);
    709  1.1     lukem 		madvise(zh->zh_zones[idx], zh->zh_zonesize, MADV_DONTNEED);
    710  1.1     lukem 		for (i = 0; i < zh->zh_zoneorder - order_start + 1; i++) {
    711  1.1     lukem 			int shiftamt = order_start + 1 + i;
    712  1.1     lukem 			int nummaps = zh->zh_zonesize >> shiftamt;
    713  1.1     lukem 			assert(nummaps);
    714  1.1     lukem 			nummaps >>= 3;
    715  1.1     lukem 			if (!nummaps) nummaps = 1;
    716  1.1     lukem 			memset(zh->zh_maps[idx][i], 0, nummaps);
    717  1.1     lukem 			zo = LDAP_LIST_FIRST(&zh->zh_free[i]);
    718  1.1     lukem 			while (zo) {
    719  1.1     lukem 				struct zone_object *zo_tmp = zo;
    720  1.1     lukem 				zo = LDAP_LIST_NEXT(zo, zo_link);
    721  1.1     lukem 				if (zo_tmp && zo_tmp->zo_idx == idx) {
    722  1.1     lukem 					LDAP_LIST_REMOVE(zo_tmp, zo_link);
    723  1.1     lukem 					LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_tmp, zo_link);
    724  1.1     lukem 				}
    725  1.1     lukem 			}
    726  1.1     lukem 		}
    727  1.1     lukem 		if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
    728  1.1     lukem 			slap_replenish_zopool(zh);
    729  1.1     lukem 		}
    730  1.1     lukem 		zo = LDAP_LIST_FIRST(&zh->zh_zopool);
    731  1.1     lukem 		LDAP_LIST_REMOVE(zo, zo_link);
    732  1.1     lukem 		zo->zo_ptr = zh->zh_zones[idx];
    733  1.1     lukem 		zo->zo_idx = idx;
    734  1.1     lukem 		LDAP_LIST_INSERT_HEAD(&zh->zh_free[zh->zh_zoneorder-order_start],
    735  1.1     lukem 								zo, zo_link);
    736  1.1     lukem 		zh->zh_seqno[idx]++;
    737  1.1     lukem 	} else {
    738  1.1     lukem 		Debug(LDAP_DEBUG_NONE, "zone not found for (ctx=0x%x, ptr=0x%x) !\n",
    739  1.1     lukem 				ctx, ptr, 0);
    740  1.1     lukem 	}
    741  1.1     lukem 
    742  1.1     lukem 	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    743  1.1     lukem 	Debug(LDAP_DEBUG_NONE, "zone %d invalidate\n", idx, 0, 0);
    744  1.1     lukem 	return rc;
    745  1.1     lukem }
    746  1.1     lukem 
    747  1.1     lukem int
    748  1.1     lukem slap_zn_validate(
    749  1.1     lukem 	void *ctx,
    750  1.1     lukem 	void *ptr,
    751  1.1     lukem 	int seqno
    752  1.1     lukem )
    753  1.1     lukem {
    754  1.1     lukem 	struct zone_heap* zh = ctx;
    755  1.1     lukem 	struct zone_object zoi, *zoo;
    756  1.1     lukem 	struct zone_heap *zone = NULL;
    757  1.1     lukem 	int idx, rc = 0;
    758  1.1     lukem 
    759  1.1     lukem 	zoi.zo_ptr = ptr;
    760  1.1     lukem 	zoi.zo_idx = -1;
    761  1.1     lukem 
    762  1.1     lukem 	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    763  1.1     lukem 
    764  1.1     lukem 	if (zoo) {
    765  1.1     lukem 		idx = zoo->zo_idx;
    766  1.1     lukem 		assert(idx != -1);
    767  1.1     lukem 		assert(seqno <= zh->zh_seqno[idx]);
    768  1.1     lukem 		rc = (seqno == zh->zh_seqno[idx]);
    769  1.1     lukem 	}
    770  1.1     lukem 
    771  1.1     lukem 	return rc;
    772  1.1     lukem }
    773  1.1     lukem 
    774  1.1     lukem int slap_zh_rlock(
    775  1.1     lukem 	void *ctx
    776  1.1     lukem )
    777  1.1     lukem {
    778  1.1     lukem 	struct zone_heap* zh = ctx;
    779  1.1     lukem 	ldap_pvt_thread_rdwr_rlock(&zh->zh_lock);
    780  1.1     lukem }
    781  1.1     lukem 
    782  1.1     lukem int slap_zh_runlock(
    783  1.1     lukem 	void *ctx
    784  1.1     lukem )
    785  1.1     lukem {
    786  1.1     lukem 	struct zone_heap* zh = ctx;
    787  1.1     lukem 	ldap_pvt_thread_rdwr_runlock(&zh->zh_lock);
    788  1.1     lukem }
    789  1.1     lukem 
    790  1.1     lukem int slap_zh_wlock(
    791  1.1     lukem 	void *ctx
    792  1.1     lukem )
    793  1.1     lukem {
    794  1.1     lukem 	struct zone_heap* zh = ctx;
    795  1.1     lukem 	ldap_pvt_thread_rdwr_wlock(&zh->zh_lock);
    796  1.1     lukem }
    797  1.1     lukem 
    798  1.1     lukem int slap_zh_wunlock(
    799  1.1     lukem 	void *ctx
    800  1.1     lukem )
    801  1.1     lukem {
    802  1.1     lukem 	struct zone_heap* zh = ctx;
    803  1.1     lukem 	ldap_pvt_thread_rdwr_wunlock(&zh->zh_lock);
    804  1.1     lukem }
    805  1.1     lukem 
    806  1.1     lukem int slap_zn_rlock(
    807  1.1     lukem 	void *ctx,
    808  1.1     lukem 	void *ptr
    809  1.1     lukem )
    810  1.1     lukem {
    811  1.1     lukem 	struct zone_heap* zh = ctx;
    812  1.1     lukem 	struct zone_object zoi, *zoo;
    813  1.1     lukem 	struct zone_heap *zone = NULL;
    814  1.1     lukem 	int idx;
    815  1.1     lukem 
    816  1.1     lukem 	zoi.zo_ptr = ptr;
    817  1.1     lukem 	zoi.zo_idx = -1;
    818  1.1     lukem 
    819  1.1     lukem 	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    820  1.1     lukem 	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    821  1.1     lukem 	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    822  1.1     lukem 
    823  1.1     lukem 	if (zoo) {
    824  1.1     lukem 		idx = zoo->zo_idx;
    825  1.1     lukem 		assert(idx != -1);
    826  1.1     lukem 		ldap_pvt_thread_rdwr_rlock(&zh->zh_znlock[idx]);
    827  1.1     lukem 	}
    828  1.1     lukem }
    829  1.1     lukem 
    830  1.1     lukem int slap_zn_runlock(
    831  1.1     lukem 	void *ctx,
    832  1.1     lukem 	void *ptr
    833  1.1     lukem )
    834  1.1     lukem {
    835  1.1     lukem 	struct zone_heap* zh = ctx;
    836  1.1     lukem 	struct zone_object zoi, *zoo;
    837  1.1     lukem 	struct zone_heap *zone = NULL;
    838  1.1     lukem 	int idx;
    839  1.1     lukem 
    840  1.1     lukem 	zoi.zo_ptr = ptr;
    841  1.1     lukem 	zoi.zo_idx = -1;
    842  1.1     lukem 
    843  1.1     lukem 	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    844  1.1     lukem 	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    845  1.1     lukem 	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    846  1.1     lukem 
    847  1.1     lukem 	if (zoo) {
    848  1.1     lukem 		idx = zoo->zo_idx;
    849  1.1     lukem 		assert(idx != -1);
    850  1.1     lukem 		ldap_pvt_thread_rdwr_runlock(&zh->zh_znlock[idx]);
    851  1.1     lukem 	}
    852  1.1     lukem }
    853  1.1     lukem 
    854  1.1     lukem int slap_zn_wlock(
    855  1.1     lukem 	void *ctx,
    856  1.1     lukem 	void *ptr
    857  1.1     lukem )
    858  1.1     lukem {
    859  1.1     lukem 	struct zone_heap* zh = ctx;
    860  1.1     lukem 	struct zone_object zoi, *zoo;
    861  1.1     lukem 	struct zone_heap *zone = NULL;
    862  1.1     lukem 	int idx;
    863  1.1     lukem 
    864  1.1     lukem 	zoi.zo_ptr = ptr;
    865  1.1     lukem 	zoi.zo_idx = -1;
    866  1.1     lukem 
    867  1.1     lukem 	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    868  1.1     lukem 	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    869  1.1     lukem 	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    870  1.1     lukem 
    871  1.1     lukem 	if (zoo) {
    872  1.1     lukem 		idx = zoo->zo_idx;
    873  1.1     lukem 		assert(idx != -1);
    874  1.1     lukem 		ldap_pvt_thread_rdwr_wlock(&zh->zh_znlock[idx]);
    875  1.1     lukem 	}
    876  1.1     lukem }
    877  1.1     lukem 
    878  1.1     lukem int slap_zn_wunlock(
    879  1.1     lukem 	void *ctx,
    880  1.1     lukem 	void *ptr
    881  1.1     lukem )
    882  1.1     lukem {
    883  1.1     lukem 	struct zone_heap* zh = ctx;
    884  1.1     lukem 	struct zone_object zoi, *zoo;
    885  1.1     lukem 	struct zone_heap *zone = NULL;
    886  1.1     lukem 	int idx;
    887  1.1     lukem 
    888  1.1     lukem 	zoi.zo_ptr = ptr;
    889  1.1     lukem 	zoi.zo_idx = -1;
    890  1.1     lukem 
    891  1.1     lukem 	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
    892  1.1     lukem 	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
    893  1.1     lukem 	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
    894  1.1     lukem 
    895  1.1     lukem 	if (zoo) {
    896  1.1     lukem 		idx = zoo->zo_idx;
    897  1.1     lukem 		assert(idx != -1);
    898  1.1     lukem 		ldap_pvt_thread_rdwr_wunlock(&zh->zh_znlock[idx]);
    899  1.1     lukem 	}
    900  1.1     lukem }
    901  1.1     lukem 
    902  1.1     lukem #define T_SEC_IN_USEC 1000000
    903  1.1     lukem 
    904  1.1     lukem static int
    905  1.1     lukem slap_timediff(struct timeval *tv_begin, struct timeval *tv_end)
    906  1.1     lukem {
    907  1.1     lukem 	uint64_t t_begin, t_end, t_diff;
    908  1.1     lukem 
    909  1.1     lukem 	t_begin = T_SEC_IN_USEC * tv_begin->tv_sec + tv_begin->tv_usec;
    910  1.1     lukem 	t_end  = T_SEC_IN_USEC * tv_end->tv_sec  + tv_end->tv_usec;
    911  1.1     lukem 	t_diff  = t_end - t_begin;
    912  1.1     lukem 
    913  1.1     lukem 	if ( t_diff < 0 )
    914  1.1     lukem 		t_diff = 0;
    915  1.1     lukem 
    916  1.1     lukem 	return (int)t_diff;
    917  1.1     lukem }
    918  1.1     lukem 
    919  1.1     lukem void
    920  1.1     lukem slap_set_timing(struct timeval *tv_set)
    921  1.1     lukem {
    922  1.1     lukem 	gettimeofday(tv_set, (struct timezone *)NULL);
    923  1.1     lukem }
    924  1.1     lukem 
    925  1.1     lukem int
    926  1.1     lukem slap_measure_timing(struct timeval *tv_set, struct timeval *tv_measure)
    927  1.1     lukem {
    928  1.1     lukem 	gettimeofday(tv_measure, (struct timezone *)NULL);
    929  1.1     lukem 	return(slap_timediff(tv_set, tv_measure));
    930  1.1     lukem }
    931  1.1     lukem 
    932  1.1     lukem #define EMA_WEIGHT 0.999000
    933  1.1     lukem #define SLAP_ZN_LATENCY_HISTORY_QLEN 500
    934  1.1     lukem int
    935  1.1     lukem slap_zn_latency_history(void* ctx, int ea_latency)
    936  1.1     lukem {
    937  1.1     lukem /* TODO: monitor /proc/stat (swap) as well */
    938  1.1     lukem 	struct zone_heap* zh = ctx;
    939  1.1     lukem 	double t_diff = 0.0;
    940  1.1     lukem 
    941  1.1     lukem 	zh->zh_ema_latency = (double)ea_latency * (1.0 - EMA_WEIGHT)
    942  1.1     lukem 					+ zh->zh_ema_latency * EMA_WEIGHT;
    943  1.1     lukem 	if (!zh->zh_swapping && zh->zh_ema_samples++ % 100 == 99) {
    944  1.1     lukem 		struct zone_latency_history *zlh_entry;
    945  1.1     lukem 		zlh_entry = ch_calloc(1, sizeof(struct zone_latency_history));
    946  1.1     lukem 		zlh_entry->zlh_latency = zh->zh_ema_latency;
    947  1.1     lukem 		LDAP_STAILQ_INSERT_TAIL(
    948  1.1     lukem 				&zh->zh_latency_history_queue, zlh_entry, zlh_next);
    949  1.1     lukem 		zh->zh_latency_history_qlen++;
    950  1.1     lukem 		while (zh->zh_latency_history_qlen > SLAP_ZN_LATENCY_HISTORY_QLEN) {
    951  1.1     lukem 			struct zone_latency_history *zlh;
    952  1.1     lukem 			zlh = LDAP_STAILQ_FIRST(&zh->zh_latency_history_queue);
    953  1.1     lukem 			LDAP_STAILQ_REMOVE_HEAD(
    954  1.1     lukem 					&zh->zh_latency_history_queue, zlh_next);
    955  1.1     lukem 			zh->zh_latency_history_qlen--;
    956  1.1     lukem 			ch_free(zlh);
    957  1.1     lukem 		}
    958  1.1     lukem 		if (zh->zh_latency_history_qlen == SLAP_ZN_LATENCY_HISTORY_QLEN) {
    959  1.1     lukem 			struct zone_latency_history *zlh_first, *zlh_last;
    960  1.1     lukem 			zlh_first = LDAP_STAILQ_FIRST(&zh->zh_latency_history_queue);
    961  1.1     lukem 			zlh_last = LDAP_STAILQ_LAST(&zh->zh_latency_history_queue,
    962  1.1     lukem 						zone_latency_history, zlh_next);
    963  1.1     lukem 			t_diff = zlh_last->zlh_latency - zlh_first->zlh_latency;
    964  1.1     lukem 		}
    965  1.1     lukem 		if (t_diff >= 2000) {
    966  1.1     lukem 			zh->zh_latency_jump++;
    967  1.1     lukem 		} else {
    968  1.1     lukem 			zh->zh_latency_jump = 0;
    969  1.1     lukem 		}
    970  1.1     lukem 		if (zh->zh_latency_jump > 3) {
    971  1.1     lukem 			zh->zh_latency_jump = 0;
    972  1.1     lukem 			zh->zh_swapping = 1;
    973  1.1     lukem 		}
    974  1.1     lukem 	}
    975  1.1     lukem 	return zh->zh_swapping;
    976  1.1     lukem }
    977  1.1     lukem #endif /* SLAP_ZONE_ALLOC */
    978