1 /* sl_malloc.c - malloc routines using a per-thread slab */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 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>.
20 #include <ac/string.h>
36 struct slab_heap *sh = data;
38 ber_memfree_x(sh->h_base, NULL);
39 ber_memfree_x(sh, NULL);
42 BER_MEMALLOC_FN sl_malloc;
43 BER_MEMCALLOC_FN sl_calloc;
44 BER_MEMREALLOC_FN sl_realloc;
45 BER_MEMFREE_FN sl_free;
48 BerMemoryFunctions sl_mfuncs =
49 { sl_malloc, sl_calloc, sl_realloc, sl_free };
54 ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &sl_mfuncs );
58 static struct slab_heap *slheap;
67 struct slab_heap *sh = NULL;
68 int pad = 2*sizeof(int)-1;
73 ldap_pvt_thread_pool_getkey( ctx, (void *)sl_mem_init, (void **)&sh, NULL );
76 /* round up to doubleword boundary */
81 sh = ch_malloc( sizeof(struct slab_heap) );
82 sh->h_base = ch_malloc( size );
86 ldap_pvt_thread_pool_setkey( ctx, (void *)sl_mem_init, (void *)sh, sl_mem_destroy );
88 } else if ( size > (char *) sh->h_end - (char *) sh->h_base ) {
89 sh->h_base = ch_realloc( sh->h_base, size );
91 sh->h_last = sh->h_base;
92 sh->h_end = (char *) sh->h_base + size;
105 /* separate from context */
106 ldap_pvt_thread_pool_setkey( ctx, (void *)sl_mem_init, NULL, NULL );
116 struct slab_heap *sh = ctx;
117 int pad = 2*sizeof(int)-1;
120 /* ber_set_option calls us like this */
121 if (!ctx) return ber_memalloc_x( size, NULL );
123 /* round up to doubleword boundary */
124 size += pad + sizeof( ber_len_t );
127 if ((char *) sh->h_last + size >= (char *) sh->h_end ) {
129 LDAP_LOG( OPERATION, INFO,
130 "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
132 Debug( LDAP_DEBUG_TRACE,
133 "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
135 return ch_malloc( size );
138 *new++ = size - sizeof(ber_len_t);
139 sh->h_last = (char *) sh->h_last + size;
141 return( (void *)new );
145 sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
149 new = sl_malloc( n*size, ctx );
151 memset( new, 0, n*size );
157 sl_realloc( void *ptr, ber_len_t size, void *ctx )
159 struct slab_heap *sh = ctx;
160 int pad = 2*sizeof(int)-1;
161 ber_len_t *p = (ber_len_t *)ptr;
164 if ( ptr == NULL ) return sl_malloc( size, ctx );
166 /* Not our memory? */
167 if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
168 /* duplicate of ch_realloc behavior, oh well */
169 new = ber_memrealloc_x( ptr, size, NULL );
174 LDAP_LOG( OPERATION, ERR,
175 "ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 );
177 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
181 exit( EXIT_FAILURE );
189 /* round up to doubleword boundary */
190 size += pad + sizeof( ber_len_t );
193 /* Never shrink blocks */
197 /* If reallocing the last block, we can grow it */
198 } else if ( (char *)ptr + p[-1] == sh->h_last &&
199 (char *)ptr + size < (char *)sh->h_end ) {
201 sh->h_last = (char *) sh->h_last + size - p[-1];
204 /* Nowhere to grow, need to alloc and copy */
206 new = sl_malloc( size, ctx );
207 AC_MEMCPY( new, ptr, p[-1] );
213 sl_free( void *ptr, void *ctx )
215 struct slab_heap *sh = ctx;
216 ber_len_t *p = (ber_len_t *)ptr;
218 if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
219 ber_memfree_x( ptr, NULL );
220 } else if ( (char *)ptr + p[-1] == sh->h_last ) {
227 sl_context( void *ptr )
229 struct slab_heap *sh = NULL;
235 ctx = ldap_pvt_thread_pool_context();
237 ldap_pvt_thread_pool_getkey( ctx, (void *)sl_mem_init, (void **)&sh, NULL );
240 if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) {