2003-04-10 00:52:03 +08:00
|
|
|
/* sl_malloc.c - malloc routines using a per-thread slab */
|
|
|
|
/* $OpenLDAP$ */
|
2003-11-27 09:17:14 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2009-01-22 08:40:04 +08:00
|
|
|
* Copyright 2003-2009 The OpenLDAP Foundation.
|
2003-11-27 09:17:14 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
2003-04-10 00:52:03 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
|
2004-12-01 00:32:37 +08:00
|
|
|
static struct slab_object * slap_replenish_sopool(struct slab_heap* sh);
|
2005-11-15 05:47:17 +08:00
|
|
|
#ifdef SLAPD_UNUSED
|
2004-11-23 23:37:06 +08:00
|
|
|
static void print_slheap(int level, void *ctx);
|
2005-11-15 05:47:17 +08:00
|
|
|
#endif
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2003-05-21 04:21:39 +08:00
|
|
|
void
|
2004-04-20 11:44:57 +08:00
|
|
|
slap_sl_mem_destroy(
|
2003-04-10 00:52:03 +08:00
|
|
|
void *key,
|
|
|
|
void *data
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct slab_heap *sh = data;
|
2004-11-23 23:37:06 +08:00
|
|
|
int pad = 2*sizeof(int)-1, pad_shift;
|
|
|
|
int order_start = -1, i;
|
|
|
|
struct slab_object *so;
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (sh->sh_stack) {
|
|
|
|
ber_memfree_x(sh->sh_base, NULL);
|
|
|
|
ber_memfree_x(sh, NULL);
|
|
|
|
} else {
|
|
|
|
pad_shift = pad - 1;
|
|
|
|
do {
|
|
|
|
order_start++;
|
|
|
|
} while (pad_shift >>= 1);
|
|
|
|
|
|
|
|
for (i = 0; i <= sh->sh_maxorder - order_start; i++) {
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_free[i]);
|
|
|
|
while (so) {
|
|
|
|
struct slab_object *so_tmp = so;
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_tmp, so_link);
|
|
|
|
}
|
|
|
|
ch_free(sh->sh_map[i]);
|
|
|
|
}
|
|
|
|
ch_free(sh->sh_free);
|
|
|
|
ch_free(sh->sh_map);
|
|
|
|
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
while (so) {
|
|
|
|
struct slab_object *so_tmp = so;
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
if (!so_tmp->so_blockhead) {
|
|
|
|
LDAP_LIST_REMOVE(so_tmp, so_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
while (so) {
|
|
|
|
struct slab_object *so_tmp = so;
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
ch_free(so_tmp);
|
|
|
|
}
|
|
|
|
ber_memfree_x(sh->sh_base, NULL);
|
|
|
|
ber_memfree_x(sh, NULL);
|
|
|
|
}
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
|
2004-04-20 11:44:57 +08:00
|
|
|
BerMemoryFunctions slap_sl_mfuncs =
|
|
|
|
{ slap_sl_malloc, slap_sl_calloc, slap_sl_realloc, slap_sl_free };
|
2003-04-10 00:52:03 +08:00
|
|
|
|
|
|
|
void
|
2004-04-20 11:44:57 +08:00
|
|
|
slap_sl_mem_init()
|
2003-04-10 00:52:03 +08:00
|
|
|
{
|
2004-04-20 11:44:57 +08:00
|
|
|
ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &slap_sl_mfuncs );
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
|
2004-05-27 16:54:34 +08:00
|
|
|
#ifdef NO_THREADS
|
|
|
|
static struct slab_heap *slheap;
|
|
|
|
#endif
|
|
|
|
|
2003-04-10 01:34:58 +08:00
|
|
|
void *
|
2004-04-20 11:44:57 +08:00
|
|
|
slap_sl_mem_create(
|
2003-04-10 00:52:03 +08:00
|
|
|
ber_len_t size,
|
2004-11-23 23:37:06 +08:00
|
|
|
int stack,
|
2007-02-14 16:51:47 +08:00
|
|
|
void *ctx,
|
|
|
|
int new
|
2003-04-10 00:52:03 +08:00
|
|
|
)
|
|
|
|
{
|
2005-07-15 05:57:25 +08:00
|
|
|
struct slab_heap *sh;
|
2004-11-23 23:37:06 +08:00
|
|
|
ber_len_t size_shift;
|
|
|
|
int pad = 2*sizeof(int)-1, pad_shift;
|
|
|
|
int order = -1, order_start = -1, order_end = -1;
|
2005-07-11 20:11:15 +08:00
|
|
|
int i;
|
2004-12-01 00:32:37 +08:00
|
|
|
struct slab_object *so;
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2004-05-27 16:54:34 +08:00
|
|
|
#ifdef NO_THREADS
|
|
|
|
sh = slheap;
|
|
|
|
#else
|
2005-07-15 05:57:25 +08:00
|
|
|
void *sh_tmp = NULL;
|
2004-11-23 23:37:06 +08:00
|
|
|
ldap_pvt_thread_pool_getkey(
|
2005-07-15 05:57:25 +08:00
|
|
|
ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL );
|
|
|
|
sh = sh_tmp;
|
2004-05-27 16:54:34 +08:00
|
|
|
#endif
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2007-02-14 16:51:47 +08:00
|
|
|
if ( !new )
|
|
|
|
return sh;
|
|
|
|
|
2003-04-10 00:52:03 +08:00
|
|
|
/* round up to doubleword boundary */
|
|
|
|
size += pad;
|
|
|
|
size &= ~pad;
|
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (stack) {
|
|
|
|
if (!sh) {
|
|
|
|
sh = ch_malloc(sizeof(struct slab_heap));
|
|
|
|
sh->sh_base = ch_malloc(size);
|
|
|
|
#ifdef NO_THREADS
|
|
|
|
slheap = sh;
|
|
|
|
#else
|
|
|
|
ldap_pvt_thread_pool_setkey(ctx, (void *)slap_sl_mem_init,
|
2008-01-08 05:35:03 +08:00
|
|
|
(void *)sh, slap_sl_mem_destroy, NULL, NULL);
|
2004-11-23 23:37:06 +08:00
|
|
|
#endif
|
|
|
|
} else if ( size > (char *)sh->sh_end - (char *)sh->sh_base ) {
|
2006-04-07 03:07:48 +08:00
|
|
|
void *newptr;
|
|
|
|
|
|
|
|
newptr = ch_realloc( sh->sh_base, size );
|
|
|
|
if ( newptr == NULL ) return NULL;
|
|
|
|
sh->sh_base = newptr;
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
|
|
|
sh->sh_last = sh->sh_base;
|
|
|
|
sh->sh_end = (char *) sh->sh_base + size;
|
|
|
|
sh->sh_stack = stack;
|
|
|
|
return sh;
|
|
|
|
} else {
|
|
|
|
size_shift = size - 1;
|
|
|
|
do {
|
|
|
|
order_end++;
|
|
|
|
} while (size_shift >>= 1);
|
|
|
|
|
|
|
|
pad_shift = pad - 1;
|
|
|
|
do {
|
|
|
|
order_start++;
|
|
|
|
} while (pad_shift >>= 1);
|
|
|
|
|
|
|
|
order = order_end - order_start + 1;
|
|
|
|
|
|
|
|
if (!sh) {
|
|
|
|
sh = (struct slab_heap *) ch_malloc(sizeof(struct slab_heap));
|
|
|
|
sh->sh_base = ch_malloc(size);
|
2004-05-27 16:54:34 +08:00
|
|
|
#ifdef NO_THREADS
|
2004-11-23 23:37:06 +08:00
|
|
|
slheap = sh;
|
2004-05-27 16:54:34 +08:00
|
|
|
#else
|
2004-11-23 23:37:06 +08:00
|
|
|
ldap_pvt_thread_pool_setkey(ctx, (void *)slap_sl_mem_init,
|
2008-01-08 05:35:03 +08:00
|
|
|
(void *)sh, slap_sl_mem_destroy, NULL, NULL);
|
2004-05-27 16:54:34 +08:00
|
|
|
#endif
|
2004-11-23 23:37:06 +08:00
|
|
|
} else {
|
|
|
|
for (i = 0; i <= sh->sh_maxorder - order_start; i++) {
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_free[i]);
|
|
|
|
while (so) {
|
|
|
|
struct slab_object *so_tmp = so;
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_tmp, so_link);
|
|
|
|
}
|
|
|
|
ch_free(sh->sh_map[i]);
|
|
|
|
}
|
|
|
|
ch_free(sh->sh_free);
|
|
|
|
ch_free(sh->sh_map);
|
|
|
|
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
while (so) {
|
|
|
|
struct slab_object *so_tmp = so;
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
if (!so_tmp->so_blockhead) {
|
|
|
|
LDAP_LIST_REMOVE(so_tmp, so_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
while (so) {
|
|
|
|
struct slab_object *so_tmp = so;
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
ch_free(so_tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > (char *)sh->sh_end - (char *)sh->sh_base) {
|
2006-04-07 03:07:48 +08:00
|
|
|
void *newptr;
|
|
|
|
|
2009-03-09 08:48:44 +08:00
|
|
|
newptr = ch_realloc( sh->sh_base, size );
|
2006-04-07 03:07:48 +08:00
|
|
|
if ( newptr == NULL ) return NULL;
|
|
|
|
sh->sh_base = newptr;
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sh->sh_end = (char *)sh->sh_base + size;
|
|
|
|
sh->sh_maxorder = order_end;
|
|
|
|
|
|
|
|
sh->sh_free = (struct sh_freelist *)
|
|
|
|
ch_malloc(order * sizeof(struct sh_freelist));
|
|
|
|
for (i = 0; i < order; i++) {
|
|
|
|
LDAP_LIST_INIT(&sh->sh_free[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LDAP_LIST_INIT(&sh->sh_sopool);
|
|
|
|
|
|
|
|
if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
|
2004-12-01 00:32:37 +08:00
|
|
|
slap_replenish_sopool(sh);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
2004-12-01 00:32:37 +08:00
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
|
|
|
so->so_ptr = sh->sh_base;
|
2004-11-23 23:37:06 +08:00
|
|
|
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_free[order-1], so, so_link);
|
|
|
|
|
|
|
|
sh->sh_map = (unsigned char **)
|
2004-12-01 00:32:37 +08:00
|
|
|
ch_malloc(order * sizeof(unsigned char *));
|
2004-11-23 23:37:06 +08:00
|
|
|
for (i = 0; i < order; i++) {
|
2004-12-08 16:12:15 +08:00
|
|
|
int shiftamt = order_start + 1 + i;
|
|
|
|
int nummaps = size >> shiftamt;
|
|
|
|
assert(nummaps);
|
|
|
|
nummaps >>= 3;
|
|
|
|
if (!nummaps) nummaps = 1;
|
|
|
|
sh->sh_map[i] = (unsigned char *) ch_malloc(nummaps);
|
|
|
|
memset(sh->sh_map[i], 0, nummaps);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
|
|
|
sh->sh_stack = stack;
|
|
|
|
return sh;
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-21 04:21:39 +08:00
|
|
|
void
|
2004-04-20 11:44:57 +08:00
|
|
|
slap_sl_mem_detach(
|
2003-05-21 04:21:39 +08:00
|
|
|
void *ctx,
|
|
|
|
void *memctx
|
|
|
|
)
|
|
|
|
{
|
2004-05-27 16:54:34 +08:00
|
|
|
#ifdef NO_THREADS
|
|
|
|
slheap = NULL;
|
|
|
|
#else
|
2003-05-23 17:10:05 +08:00
|
|
|
/* separate from context */
|
2008-01-11 14:07:43 +08:00
|
|
|
ldap_pvt_thread_pool_setkey( ctx, (void *)slap_sl_mem_init,
|
|
|
|
NULL, 0, NULL, NULL );
|
2004-05-27 16:54:34 +08:00
|
|
|
#endif
|
2003-05-21 04:21:39 +08:00
|
|
|
}
|
|
|
|
|
2003-04-10 00:52:03 +08:00
|
|
|
void *
|
2004-04-20 11:44:57 +08:00
|
|
|
slap_sl_malloc(
|
2003-04-10 00:52:03 +08:00
|
|
|
ber_len_t size,
|
|
|
|
void *ctx
|
|
|
|
)
|
|
|
|
{
|
2003-04-10 01:34:58 +08:00
|
|
|
struct slab_heap *sh = ctx;
|
2004-12-01 00:32:37 +08:00
|
|
|
ber_len_t size_shift;
|
2004-11-23 23:37:06 +08:00
|
|
|
int pad = 2*sizeof(int)-1, pad_shift;
|
|
|
|
int order = -1, order_start = -1;
|
2004-12-01 00:32:37 +08:00
|
|
|
struct slab_object *so_new, *so_left, *so_right;
|
2006-04-07 03:07:48 +08:00
|
|
|
ber_len_t *ptr, *newptr;
|
2004-11-23 23:37:06 +08:00
|
|
|
unsigned long diff;
|
2005-07-11 20:11:15 +08:00
|
|
|
int i, j;
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2006-04-07 03:13:41 +08:00
|
|
|
#ifdef SLAP_NO_SL_MALLOC
|
2009-04-11 11:40:12 +08:00
|
|
|
newptr = ber_memalloc_x( size, NULL );
|
|
|
|
if ( newptr ) return newptr;
|
|
|
|
assert( 0 );
|
|
|
|
exit( EXIT_FAILURE );
|
2006-04-07 03:13:41 +08:00
|
|
|
#endif
|
|
|
|
|
2003-04-10 00:52:03 +08:00
|
|
|
/* ber_set_option calls us like this */
|
2009-04-11 11:40:12 +08:00
|
|
|
if (!ctx) {
|
|
|
|
newptr = ber_memalloc_x( size, NULL );
|
|
|
|
if ( newptr ) return newptr;
|
|
|
|
assert( 0 );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2004-11-23 23:37:06 +08:00
|
|
|
|
2003-04-10 00:52:03 +08:00
|
|
|
/* round up to doubleword boundary */
|
2004-12-01 00:32:37 +08:00
|
|
|
size += sizeof(ber_len_t) + pad;
|
2003-04-10 00:52:03 +08:00
|
|
|
size &= ~pad;
|
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (sh->sh_stack) {
|
|
|
|
if ((char *)sh->sh_last + size >= (char *)sh->sh_end) {
|
|
|
|
Debug(LDAP_DEBUG_TRACE,
|
|
|
|
"slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
|
|
|
|
(long)size, 0, 0);
|
|
|
|
return ch_malloc(size);
|
|
|
|
}
|
2006-04-07 03:07:48 +08:00
|
|
|
newptr = sh->sh_last;
|
|
|
|
*newptr++ = size - sizeof(ber_len_t);
|
2004-11-23 23:37:06 +08:00
|
|
|
sh->sh_last = (char *) sh->sh_last + size;
|
2006-04-07 03:07:48 +08:00
|
|
|
return( (void *)newptr );
|
2004-11-23 23:37:06 +08:00
|
|
|
} else {
|
|
|
|
size_shift = size - 1;
|
|
|
|
do {
|
|
|
|
order++;
|
|
|
|
} while (size_shift >>= 1);
|
|
|
|
|
|
|
|
pad_shift = pad - 1;
|
|
|
|
do {
|
|
|
|
order_start++;
|
|
|
|
} while (pad_shift >>= 1);
|
|
|
|
|
|
|
|
for (i = order; i <= sh->sh_maxorder &&
|
|
|
|
LDAP_LIST_EMPTY(&sh->sh_free[i-order_start]); i++);
|
|
|
|
|
|
|
|
if (i == order) {
|
|
|
|
so_new = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
|
|
|
|
LDAP_LIST_REMOVE(so_new, so_link);
|
|
|
|
ptr = so_new->so_ptr;
|
|
|
|
diff = (unsigned long)((char*)ptr -
|
|
|
|
(char*)sh->sh_base) >> (order + 1);
|
|
|
|
sh->sh_map[order-order_start][diff>>3] |= (1 << (diff & 0x7));
|
|
|
|
*ptr++ = size - sizeof(ber_len_t);
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_new, so_link);
|
|
|
|
return((void*)ptr);
|
|
|
|
} else if (i <= sh->sh_maxorder) {
|
|
|
|
for (j = i; j > order; j--) {
|
|
|
|
so_left = LDAP_LIST_FIRST(&sh->sh_free[j-order_start]);
|
|
|
|
LDAP_LIST_REMOVE(so_left, so_link);
|
|
|
|
if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
|
2004-12-01 00:32:37 +08:00
|
|
|
slap_replenish_sopool(sh);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
2004-12-01 00:32:37 +08:00
|
|
|
so_right = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
LDAP_LIST_REMOVE(so_right, so_link);
|
2005-01-14 19:50:24 +08:00
|
|
|
so_right->so_ptr = (void *)((char *)so_left->so_ptr + (1 << j));
|
2004-11-23 23:37:06 +08:00
|
|
|
if (j == order + 1) {
|
|
|
|
ptr = so_left->so_ptr;
|
|
|
|
diff = (unsigned long)((char*)ptr -
|
|
|
|
(char*)sh->sh_base) >> (order+1);
|
|
|
|
sh->sh_map[order-order_start][diff>>3] |=
|
|
|
|
(1 << (diff & 0x7));
|
|
|
|
*ptr++ = size - sizeof(ber_len_t);
|
|
|
|
LDAP_LIST_INSERT_HEAD(
|
|
|
|
&sh->sh_free[j-1-order_start], so_right, so_link);
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_left, so_link);
|
|
|
|
return((void*)ptr);
|
|
|
|
} else {
|
|
|
|
LDAP_LIST_INSERT_HEAD(
|
|
|
|
&sh->sh_free[j-1-order_start], so_right, so_link);
|
|
|
|
LDAP_LIST_INSERT_HEAD(
|
|
|
|
&sh->sh_free[j-1-order_start], so_left, so_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
|
|
|
|
(long)size, 0, 0);
|
|
|
|
return (void*)ch_malloc(size);
|
|
|
|
}
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
2005-01-14 19:50:24 +08:00
|
|
|
|
|
|
|
/* FIXME: missing return; guessing... */
|
|
|
|
return NULL;
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2004-04-20 11:44:57 +08:00
|
|
|
slap_sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
|
2003-04-10 00:52:03 +08:00
|
|
|
{
|
2006-04-07 03:07:48 +08:00
|
|
|
void *newptr;
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2006-04-07 03:07:48 +08:00
|
|
|
newptr = slap_sl_malloc( n*size, ctx );
|
|
|
|
if ( newptr ) {
|
|
|
|
memset( newptr, 0, n*size );
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
2006-04-07 03:07:48 +08:00
|
|
|
return newptr;
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2004-11-23 23:37:06 +08:00
|
|
|
slap_sl_realloc(void *ptr, ber_len_t size, void *ctx)
|
2003-04-10 00:52:03 +08:00
|
|
|
{
|
2003-04-10 01:34:58 +08:00
|
|
|
struct slab_heap *sh = ctx;
|
2005-07-11 20:11:15 +08:00
|
|
|
int pad = 2*sizeof(int) -1;
|
2006-04-07 03:07:48 +08:00
|
|
|
ber_len_t *p = (ber_len_t *)ptr, *newptr;
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (ptr == NULL)
|
|
|
|
return slap_sl_malloc(size, ctx);
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2006-04-07 03:13:41 +08:00
|
|
|
#ifdef SLAP_NO_SL_MALLOC
|
2009-04-11 11:40:12 +08:00
|
|
|
newptr = ber_memrealloc_x( ptr, size, NULL );
|
|
|
|
if ( newptr ) return newptr;
|
|
|
|
assert( 0 );
|
|
|
|
exit( EXIT_FAILURE );
|
2006-04-07 03:13:41 +08:00
|
|
|
#endif
|
|
|
|
|
2003-04-10 14:21:53 +08:00
|
|
|
/* Not our memory? */
|
2004-11-23 23:37:06 +08:00
|
|
|
if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
|
|
|
|
/* duplicate of realloc behavior, oh well */
|
2006-04-07 03:07:48 +08:00
|
|
|
newptr = ber_memrealloc_x(ptr, size, NULL);
|
|
|
|
if (newptr) {
|
|
|
|
return newptr;
|
2003-04-12 13:12:40 +08:00
|
|
|
}
|
2004-11-23 23:37:06 +08:00
|
|
|
Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
|
|
|
|
(long) size, 0, 0);
|
|
|
|
assert(0);
|
2003-04-12 13:12:40 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (size == 0) {
|
|
|
|
slap_sl_free(ptr, ctx);
|
2003-04-12 13:12:40 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-04-10 00:52:03 +08:00
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (sh->sh_stack) {
|
2005-04-30 09:15:10 +08:00
|
|
|
/* round up to doubleword boundary */
|
|
|
|
size += pad + sizeof( ber_len_t );
|
|
|
|
size &= ~pad;
|
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
/* Never shrink blocks */
|
|
|
|
if (size <= p[-1]) {
|
2006-04-07 03:07:48 +08:00
|
|
|
newptr = p;
|
2003-04-12 13:12:40 +08:00
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
/* If reallocing the last block, we can grow it */
|
2004-12-07 16:48:28 +08:00
|
|
|
} else if ((char *)ptr + p[-1] == sh->sh_last &&
|
|
|
|
(char *)ptr + size < (char *)sh->sh_end ) {
|
2006-04-07 03:07:48 +08:00
|
|
|
newptr = p;
|
2004-11-23 23:37:06 +08:00
|
|
|
sh->sh_last = (char *)sh->sh_last + size - p[-1];
|
|
|
|
p[-1] = size;
|
2003-04-12 13:12:40 +08:00
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
/* Nowhere to grow, need to alloc and copy */
|
|
|
|
} else {
|
2006-04-07 03:07:48 +08:00
|
|
|
newptr = slap_sl_malloc(size, ctx);
|
|
|
|
AC_MEMCPY(newptr, ptr, p[-1]);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
2006-04-07 03:07:48 +08:00
|
|
|
return newptr;
|
2003-04-10 00:52:03 +08:00
|
|
|
} else {
|
2006-04-07 03:07:48 +08:00
|
|
|
void *newptr2;
|
|
|
|
|
|
|
|
newptr2 = slap_sl_malloc(size, ctx);
|
2004-11-23 23:37:06 +08:00
|
|
|
if (size < p[-1]) {
|
2006-04-07 03:07:48 +08:00
|
|
|
AC_MEMCPY(newptr2, ptr, size);
|
2004-11-23 23:37:06 +08:00
|
|
|
} else {
|
2006-04-07 03:07:48 +08:00
|
|
|
AC_MEMCPY(newptr2, ptr, p[-1]);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
|
|
|
slap_sl_free(ptr, ctx);
|
2006-04-07 03:07:48 +08:00
|
|
|
return newptr2;
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-11-23 23:37:06 +08:00
|
|
|
slap_sl_free(void *ptr, void *ctx)
|
2003-04-10 00:52:03 +08:00
|
|
|
{
|
2003-04-10 01:34:58 +08:00
|
|
|
struct slab_heap *sh = ctx;
|
2004-11-23 23:37:06 +08:00
|
|
|
int size, size_shift, order_size;
|
|
|
|
int pad = 2*sizeof(int)-1, pad_shift;
|
|
|
|
ber_len_t *p = (ber_len_t *)ptr, *tmpp;
|
|
|
|
int order_start = -1, order = -1;
|
2004-12-01 00:32:37 +08:00
|
|
|
struct slab_object *so;
|
2004-11-23 23:37:06 +08:00
|
|
|
unsigned long diff;
|
2005-07-11 20:11:15 +08:00
|
|
|
int i, inserted = 0;
|
2004-11-23 23:37:06 +08:00
|
|
|
|
2006-03-31 18:45:23 +08:00
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
2006-04-07 03:13:41 +08:00
|
|
|
#ifdef SLAP_NO_SL_MALLOC
|
|
|
|
ber_memfree_x( ptr, NULL );
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
|
|
|
|
ber_memfree_x(ptr, NULL);
|
|
|
|
} else if (sh->sh_stack && (char *)ptr + p[-1] == sh->sh_last) {
|
2003-04-12 13:12:40 +08:00
|
|
|
p--;
|
2004-11-23 23:37:06 +08:00
|
|
|
sh->sh_last = p;
|
|
|
|
} else if (!sh->sh_stack) {
|
|
|
|
size = *(--p);
|
|
|
|
size_shift = size + sizeof(ber_len_t) - 1;
|
|
|
|
do {
|
|
|
|
order++;
|
|
|
|
} while (size_shift >>= 1);
|
|
|
|
|
|
|
|
pad_shift = pad - 1;
|
|
|
|
do {
|
|
|
|
order_start++;
|
|
|
|
} while (pad_shift >>= 1);
|
|
|
|
|
|
|
|
for (i = order, tmpp = p; i <= sh->sh_maxorder; i++) {
|
|
|
|
order_size = 1 << (i+1);
|
|
|
|
diff = (unsigned long)((char*)tmpp - (char*)sh->sh_base) >> (i+1);
|
|
|
|
sh->sh_map[i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
|
|
|
|
if (diff == ((diff>>1)<<1)) {
|
|
|
|
if (!(sh->sh_map[i-order_start][(diff+1)>>3] &
|
|
|
|
(1<<((diff+1)&0x7)))) {
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
|
|
|
|
while (so) {
|
|
|
|
if ((char*)so->so_ptr == (char*)tmpp) {
|
|
|
|
LDAP_LIST_REMOVE( so, so_link );
|
|
|
|
} else if ((char*)so->so_ptr ==
|
|
|
|
(char*)tmpp + order_size) {
|
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
}
|
|
|
|
if (so) {
|
|
|
|
if (i < sh->sh_maxorder) {
|
|
|
|
inserted = 1;
|
|
|
|
so->so_ptr = tmpp;
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1],
|
|
|
|
so, so_link);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
|
2004-12-01 00:32:37 +08:00
|
|
|
slap_replenish_sopool(sh);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
2004-12-01 00:32:37 +08:00
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
|
|
|
so->so_ptr = tmpp;
|
2004-11-23 23:37:06 +08:00
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
|
|
|
|
so, so_link);
|
|
|
|
break;
|
|
|
|
|
2004-12-01 00:32:37 +08:00
|
|
|
Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
|
2004-11-23 23:37:06 +08:00
|
|
|
"free object not found while bit is clear.\n",
|
|
|
|
0, 0, 0);
|
2005-07-18 14:22:33 +08:00
|
|
|
assert(so != NULL);
|
2004-11-23 23:37:06 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!inserted) {
|
|
|
|
if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
|
2004-12-01 00:32:37 +08:00
|
|
|
slap_replenish_sopool(sh);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
2004-12-01 00:32:37 +08:00
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
|
|
|
so->so_ptr = tmpp;
|
2004-11-23 23:37:06 +08:00
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
|
|
|
|
so, so_link);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(sh->sh_map[i-order_start][(diff-1)>>3] &
|
|
|
|
(1<<((diff-1)&0x7)))) {
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
|
|
|
|
while (so) {
|
|
|
|
if ((char*)so->so_ptr == (char*)tmpp) {
|
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
2005-01-14 19:50:24 +08:00
|
|
|
} else if ((char*)tmpp == (char *)so->so_ptr + order_size) {
|
2004-11-23 23:37:06 +08:00
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
|
|
|
tmpp = so->so_ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
}
|
|
|
|
if (so) {
|
|
|
|
if (i < sh->sh_maxorder) {
|
|
|
|
inserted = 1;
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1], so, so_link);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
|
2004-12-01 00:32:37 +08:00
|
|
|
slap_replenish_sopool(sh);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
2004-12-01 00:32:37 +08:00
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
|
|
|
so->so_ptr = tmpp;
|
2004-11-23 23:37:06 +08:00
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
|
|
|
|
so, so_link);
|
|
|
|
break;
|
|
|
|
|
2004-12-01 00:32:37 +08:00
|
|
|
Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
|
2004-11-23 23:37:06 +08:00
|
|
|
"free object not found while bit is clear.\n",
|
|
|
|
0, 0, 0 );
|
2005-07-18 14:22:33 +08:00
|
|
|
assert(so != NULL);
|
2004-11-23 23:37:06 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( !inserted ) {
|
|
|
|
if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
|
2004-12-01 00:32:37 +08:00
|
|
|
slap_replenish_sopool(sh);
|
2004-11-23 23:37:06 +08:00
|
|
|
}
|
2004-12-01 00:32:37 +08:00
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_sopool);
|
|
|
|
LDAP_LIST_REMOVE(so, so_link);
|
|
|
|
so->so_ptr = tmpp;
|
2004-11-23 23:37:06 +08:00
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
|
|
|
|
so, so_link);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-04-10 00:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-10 14:21:53 +08:00
|
|
|
|
2003-04-12 13:12:40 +08:00
|
|
|
void *
|
2004-04-20 11:44:57 +08:00
|
|
|
slap_sl_context( void *ptr )
|
2003-04-12 13:12:40 +08:00
|
|
|
{
|
2005-07-15 05:57:25 +08:00
|
|
|
struct slab_heap *sh;
|
|
|
|
void *ctx, *sh_tmp;
|
2003-04-12 13:12:40 +08:00
|
|
|
|
2005-01-20 05:42:04 +08:00
|
|
|
if ( slapMode & SLAP_TOOL_MODE ) return NULL;
|
|
|
|
|
2004-05-27 16:54:34 +08:00
|
|
|
#ifdef NO_THREADS
|
|
|
|
sh = slheap;
|
|
|
|
#else
|
2003-04-12 13:12:40 +08:00
|
|
|
ctx = ldap_pvt_thread_pool_context();
|
|
|
|
|
2005-07-15 05:57:25 +08:00
|
|
|
sh_tmp = NULL;
|
|
|
|
ldap_pvt_thread_pool_getkey(
|
|
|
|
ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL);
|
|
|
|
sh = sh_tmp;
|
2004-05-27 16:54:34 +08:00
|
|
|
#endif
|
2003-04-12 13:12:40 +08:00
|
|
|
|
2004-11-23 23:37:06 +08:00
|
|
|
if (sh && ptr >= sh->sh_base && ptr <= sh->sh_end) {
|
2003-04-12 13:12:40 +08:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-11-23 23:37:06 +08:00
|
|
|
|
2004-12-01 00:32:37 +08:00
|
|
|
static struct slab_object *
|
|
|
|
slap_replenish_sopool(
|
|
|
|
struct slab_heap* sh
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct slab_object *so_block;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
so_block = (struct slab_object *)ch_malloc(
|
|
|
|
SLAP_SLAB_SOBLOCK * sizeof(struct slab_object));
|
|
|
|
|
|
|
|
if ( so_block == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
so_block[0].so_blockhead = 1;
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[0], so_link);
|
|
|
|
for (i = 1; i < SLAP_SLAB_SOBLOCK; i++) {
|
|
|
|
so_block[i].so_blockhead = 0;
|
|
|
|
LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[i], so_link );
|
|
|
|
}
|
|
|
|
|
|
|
|
return so_block;
|
|
|
|
}
|
|
|
|
|
2005-11-15 05:47:17 +08:00
|
|
|
#ifdef SLAPD_UNUSED
|
2004-11-23 23:37:06 +08:00
|
|
|
static void
|
|
|
|
print_slheap(int level, void *ctx)
|
|
|
|
{
|
|
|
|
struct slab_heap *sh = ctx;
|
|
|
|
int order_start = -1;
|
|
|
|
int pad = 2*sizeof(int)-1, pad_shift;
|
|
|
|
struct slab_object *so;
|
|
|
|
int i, j, once = 0;
|
|
|
|
|
|
|
|
if (!ctx) {
|
|
|
|
Debug(level, "NULL memctx\n", 0, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pad_shift = pad - 1;
|
|
|
|
do {
|
|
|
|
order_start++;
|
|
|
|
} while (pad_shift >>= 1);
|
|
|
|
|
|
|
|
Debug(level, "sh->sh_maxorder=%d\n", sh->sh_maxorder, 0, 0);
|
|
|
|
|
|
|
|
for (i = order_start; i <= sh->sh_maxorder; i++) {
|
|
|
|
once = 0;
|
|
|
|
Debug(level, "order=%d\n", i, 0, 0);
|
|
|
|
for (j = 0; j < (1<<(sh->sh_maxorder-i))/8; j++) {
|
|
|
|
Debug(level, "%02x ", sh->sh_map[i-order_start][j], 0, 0);
|
|
|
|
once = 1;
|
|
|
|
}
|
|
|
|
if (!once) {
|
|
|
|
Debug(level, "%02x ", sh->sh_map[i-order_start][0], 0, 0);
|
|
|
|
}
|
|
|
|
Debug(level, "\n", 0, 0, 0);
|
|
|
|
Debug(level, "free list:\n", 0, 0, 0);
|
|
|
|
so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
|
|
|
|
while (so) {
|
2005-07-04 13:29:12 +08:00
|
|
|
Debug(level, "%lx\n", (unsigned long) so->so_ptr, 0, 0);
|
2004-11-23 23:37:06 +08:00
|
|
|
so = LDAP_LIST_NEXT(so, so_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-15 05:47:17 +08:00
|
|
|
#endif
|