/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 2003-2005 The OpenLDAP Foundation.
+ * Copyright 2003-2010 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#include "slap.h"
static struct slab_object * slap_replenish_sopool(struct slab_heap* sh);
+#ifdef SLAPD_UNUSED
static void print_slheap(int level, void *ctx);
+#endif
void
slap_sl_mem_destroy(
static struct slab_heap *slheap;
#endif
+/* This allocator always returns memory aligned on a 2-int boundary.
+ *
+ * The stack-based allocator stores the size as a ber_len_t at both
+ * the head and tail of the allocated block. When freeing a block, the
+ * tail length is ORed with 1 to mark it as free. Freed space can only
+ * be reclaimed from the tail forward. If the tail block is never freed,
+ * nothing else will be reclaimed until the slab is reset...
+ */
void *
slap_sl_mem_create(
ber_len_t size,
int stack,
- void *ctx
+ void *ctx,
+ int new
)
{
- struct slab_heap *sh = NULL;
+ struct slab_heap *sh;
ber_len_t size_shift;
int pad = 2*sizeof(int)-1, pad_shift;
int order = -1, order_start = -1, order_end = -1;
- int i, k;
+ int i;
struct slab_object *so;
#ifdef NO_THREADS
sh = slheap;
#else
+ void *sh_tmp = NULL;
ldap_pvt_thread_pool_getkey(
- ctx, (void *)slap_sl_mem_init, (void **)&sh, NULL );
+ ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL );
+ sh = sh_tmp;
#endif
+ if ( sh && !new )
+ return sh;
+
/* round up to doubleword boundary */
size += pad;
size &= ~pad;
slheap = sh;
#else
ldap_pvt_thread_pool_setkey(ctx, (void *)slap_sl_mem_init,
- (void *)sh, slap_sl_mem_destroy);
+ (void *)sh, slap_sl_mem_destroy, NULL, NULL);
#endif
} else if ( size > (char *)sh->sh_end - (char *)sh->sh_base ) {
- sh->sh_base = ch_realloc(sh->sh_base, size);
+ void *newptr;
+
+ newptr = ch_realloc( sh->sh_base, size );
+ if ( newptr == NULL ) return NULL;
+ sh->sh_base = newptr;
+ }
+ /* insert dummy len */
+ {
+ ber_len_t *i = sh->sh_base;
+ *i++ = 0;
+ sh->sh_last = i;
}
- sh->sh_last = sh->sh_base;
sh->sh_end = (char *) sh->sh_base + size;
sh->sh_stack = stack;
return sh;
slheap = sh;
#else
ldap_pvt_thread_pool_setkey(ctx, (void *)slap_sl_mem_init,
- (void *)sh, slap_sl_mem_destroy);
+ (void *)sh, slap_sl_mem_destroy, NULL, NULL);
#endif
} else {
for (i = 0; i <= sh->sh_maxorder - order_start; i++) {
}
if (size > (char *)sh->sh_end - (char *)sh->sh_base) {
- sh->sh_base = realloc(sh->sh_base, size);
+ void *newptr;
+
+ newptr = ch_realloc( sh->sh_base, size );
+ if ( newptr == NULL ) return NULL;
+ sh->sh_base = newptr;
}
}
sh->sh_end = (char *)sh->sh_base + size;
slheap = NULL;
#else
/* separate from context */
- ldap_pvt_thread_pool_setkey( ctx, (void *)slap_sl_mem_init, NULL, NULL );
+ ldap_pvt_thread_pool_setkey( ctx, (void *)slap_sl_mem_init,
+ NULL, 0, NULL, NULL );
#endif
}
)
{
struct slab_heap *sh = ctx;
- ber_len_t size_shift;
int pad = 2*sizeof(int)-1, pad_shift;
- int order = -1, order_start = -1;
- struct slab_object *so_new, *so_left, *so_right;
- ber_len_t *ptr, *new;
- unsigned long diff;
- int i, j, k;
+ ber_len_t *ptr, *newptr;
+
+#ifdef SLAP_NO_SL_MALLOC
+ newptr = ber_memalloc_x( size, NULL );
+ if ( newptr ) return newptr;
+ assert( 0 );
+ exit( EXIT_FAILURE );
+#endif
/* ber_set_option calls us like this */
- if (!ctx) return ber_memalloc_x(size, NULL);
+ if (!ctx) {
+ newptr = ber_memalloc_x( size, NULL );
+ if ( newptr ) return newptr;
+ assert( 0 );
+ exit( EXIT_FAILURE );
+ }
- /* round up to doubleword boundary */
- size += sizeof(ber_len_t) + pad;
+ /* round up to doubleword boundary, plus space for len at head and tail */
+ size += 2*sizeof(ber_len_t) + pad;
size &= ~pad;
if (sh->sh_stack) {
(long)size, 0, 0);
return ch_malloc(size);
}
- new = sh->sh_last;
- *new++ = size - sizeof(ber_len_t);
+ newptr = sh->sh_last;
sh->sh_last = (char *) sh->sh_last + size;
- return( (void *)new );
+ size -= sizeof(ber_len_t);
+ *newptr++ = size;
+ *(ber_len_t *)((char *)sh->sh_last - sizeof(ber_len_t)) = size;
+ return( (void *)newptr );
} else {
+ struct slab_object *so_new, *so_left, *so_right;
+ ber_len_t size_shift;
+ int order = -1, order_start = -1;
+ unsigned long diff;
+ int i, j;
+
size_shift = size - 1;
do {
order++;
void *
slap_sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
{
- void *new;
+ void *newptr;
- new = slap_sl_malloc( n*size, ctx );
- if ( new ) {
- memset( new, 0, n*size );
+ newptr = slap_sl_malloc( n*size, ctx );
+ if ( newptr ) {
+ memset( newptr, 0, n*size );
}
- return new;
+ return newptr;
}
void *
slap_sl_realloc(void *ptr, ber_len_t size, void *ctx)
{
struct slab_heap *sh = ctx;
- int size_shift;
- int pad = 2*sizeof(int)-1, pad_shift;
- int order_start = -1, order = -1;
- struct slab_object *so;
- ber_len_t *p = (ber_len_t *)ptr, *new;
- unsigned long diff;
+ int pad = 2*sizeof(int) -1;
+ ber_len_t *p = (ber_len_t *)ptr, *newptr;
if (ptr == NULL)
return slap_sl_malloc(size, ctx);
+#ifdef SLAP_NO_SL_MALLOC
+ newptr = ber_memrealloc_x( ptr, size, NULL );
+ if ( newptr ) return newptr;
+ assert( 0 );
+ exit( EXIT_FAILURE );
+#endif
+
/* Not our memory? */
if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
/* duplicate of realloc behavior, oh well */
- new = ber_memrealloc_x(ptr, size, NULL);
- if (new) {
- return new;
+ newptr = ber_memrealloc_x(ptr, size, NULL);
+ if (newptr) {
+ return newptr;
}
Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
(long) size, 0, 0);
size += pad + sizeof( ber_len_t );
size &= ~pad;
+ p--;
+
/* Never shrink blocks */
- if (size <= p[-1]) {
- new = p;
+ if (size <= p[0]) {
+ newptr = ptr;
/* If reallocing the last block, we can grow it */
- } else if ((char *)ptr + p[-1] == sh->sh_last &&
+ } else if ((char *)ptr + p[0] == sh->sh_last &&
(char *)ptr + size < (char *)sh->sh_end ) {
- new = p;
- sh->sh_last = (char *)sh->sh_last + size - p[-1];
- p[-1] = size;
-
+ newptr = ptr;
+ sh->sh_last = (char *)ptr + size;
+ p[0] = size;
+ p[size/sizeof(ber_len_t)] = size;
+
/* Nowhere to grow, need to alloc and copy */
} else {
- new = slap_sl_malloc(size, ctx);
- AC_MEMCPY(new, ptr, p[-1]);
+ newptr = slap_sl_malloc(size-sizeof(ber_len_t), ctx);
+ AC_MEMCPY(newptr, ptr, p[0]-sizeof(ber_len_t));
+ /* mark old region as free */
+ p[p[0]/sizeof(ber_len_t)] |= 1;
}
- return new;
+ return newptr;
} else {
- void *newptr;
- newptr = slap_sl_malloc(size, ctx);
+ void *newptr2;
+
+ newptr2 = slap_sl_malloc(size, ctx);
if (size < p[-1]) {
- AC_MEMCPY(newptr, ptr, size);
+ AC_MEMCPY(newptr2, ptr, size);
} else {
- AC_MEMCPY(newptr, ptr, p[-1]);
+ AC_MEMCPY(newptr2, ptr, p[-1]);
}
slap_sl_free(ptr, ctx);
- return newptr;
+ return newptr2;
}
}
slap_sl_free(void *ptr, void *ctx)
{
struct slab_heap *sh = ctx;
- int size, size_shift, order_size;
- int pad = 2*sizeof(int)-1, pad_shift;
+ ber_len_t size;
ber_len_t *p = (ber_len_t *)ptr, *tmpp;
- int order_start = -1, order = -1;
- struct slab_object *so;
- unsigned long diff;
- int i, k, inserted = 0;
+
+ if (!ptr)
+ return;
+
+#ifdef SLAP_NO_SL_MALLOC
+ ber_memfree_x( ptr, NULL );
+ return;
+#endif
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) {
- p--;
- sh->sh_last = p;
- } else if (!sh->sh_stack) {
+ } else if (sh->sh_stack) {
+ tmpp = (ber_len_t *)((char *)ptr + p[-1]);
+ /* mark it free */
+ tmpp[-1] |= 1;
+ /* reclaim free space off tail */
+ while ( tmpp == sh->sh_last ) {
+ if ( tmpp[-1] & 1 ) {
+ size = tmpp[-1] ^ 1;
+ ptr = (char *)tmpp - size;
+ p = (ber_len_t *)ptr;
+ p--;
+ sh->sh_last = p;
+ tmpp = sh->sh_last;
+ } else {
+ break;
+ }
+ }
+ } else {
+ int size_shift, order_size;
+ int pad = 2*sizeof(int)-1, pad_shift;
+ int order_start = -1, order = -1;
+ struct slab_object *so;
+ unsigned long diff;
+ int i, inserted = 0;
+
size = *(--p);
size_shift = size + sizeof(ber_len_t) - 1;
do {
Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
"free object not found while bit is clear.\n",
0, 0, 0);
- assert(so);
+ assert(so != NULL);
}
} else {
Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
"free object not found while bit is clear.\n",
0, 0, 0 );
- assert( so );
+ assert(so != NULL);
}
} else {
void *
slap_sl_context( void *ptr )
{
- struct slab_heap *sh = NULL;
- void *ctx;
+ struct slab_heap *sh;
+ void *ctx, *sh_tmp;
if ( slapMode & SLAP_TOOL_MODE ) return NULL;
#else
ctx = ldap_pvt_thread_pool_context();
- ldap_pvt_thread_pool_getkey(ctx, (void *)slap_sl_mem_init,
- (void **)&sh, NULL);
+ sh_tmp = NULL;
+ ldap_pvt_thread_pool_getkey(
+ ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL);
+ sh = sh_tmp;
#endif
if (sh && ptr >= sh->sh_base && ptr <= sh->sh_end) {
return so_block;
}
+#ifdef SLAPD_UNUSED
static void
print_slheap(int level, void *ctx)
{
Debug(level, "free list:\n", 0, 0, 0);
so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
while (so) {
- Debug(level, "%x\n",so->so_ptr, 0, 0);
+ Debug(level, "%lx\n", (unsigned long) so->so_ptr, 0, 0);
so = LDAP_LIST_NEXT(so, so_link);
}
}
}
+#endif