1 /* sl_malloc.c - malloc routines using a per-thread slab */
4 * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
27 struct slab_heap *sh = data;
29 ber_memfree_x(sh->h_base, NULL);
30 ber_memfree_x(sh, NULL);
33 BER_MEMALLOC_FN sl_malloc;
34 BER_MEMCALLOC_FN sl_calloc;
35 BER_MEMREALLOC_FN sl_realloc;
36 BER_MEMFREE_FN sl_free;
39 BerMemoryFunctions sl_mfuncs =
40 { sl_malloc, sl_calloc, sl_realloc, sl_free };
45 ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &sl_mfuncs );
54 struct slab_heap *sh = NULL;
55 int pad = 2*sizeof(int)-1;
57 ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL );
59 /* round up to doubleword boundary */
64 sh = ch_malloc( sizeof(struct slab_heap) );
65 sh->h_base = ch_malloc( size );
66 ldap_pvt_thread_pool_setkey( ctx, sl_mem_init, (void *)sh, sl_mem_destroy );
67 } else if ( size > (char *) sh->h_end - (char *) sh->h_base ) {
68 sh->h_base = ch_realloc( sh->h_base, size );
70 sh->h_last = sh->h_base;
71 sh->h_end = (char *) sh->h_base + size;
81 /* separate from context */
82 ldap_pvt_thread_pool_setkey( ctx, sl_mem_init, NULL, NULL );
91 struct slab_heap *sh = ctx;
92 int pad = 2*sizeof(int)-1;
95 /* ber_set_option calls us like this */
96 if (!ctx) return ber_memalloc_x( size, NULL );
98 /* round up to doubleword boundary */
99 size += pad + sizeof( ber_len_t );
102 if ((char *) sh->h_last + size >= (char *) sh->h_end ) {
104 LDAP_LOG( OPERATION, INFO,
105 "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
107 Debug( LDAP_DEBUG_TRACE,
108 "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
110 return ch_malloc( size );
113 *new++ = size - sizeof(ber_len_t);
114 sh->h_last = (char *) sh->h_last + size;
116 return( (void *)new );
120 sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
124 new = sl_malloc( n*size, ctx );
126 memset( new, 0, n*size );
132 sl_realloc( void *ptr, ber_len_t size, void *ctx )
134 struct slab_heap *sh = ctx;
135 int pad = 2*sizeof(int)-1;
136 ber_len_t *p = (ber_len_t *)ptr;
139 if ( ptr == NULL ) return sl_malloc( size, ctx );
141 /* Not our memory? */
142 if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
143 /* duplicate of ch_realloc behavior, oh well */
144 new = ber_memrealloc_x( ptr, size, NULL );
149 LDAP_LOG( OPERATION, ERR,
150 "ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 );
152 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
156 exit( EXIT_FAILURE );
164 /* round up to doubleword boundary */
165 size += pad + sizeof( ber_len_t );
168 /* Never shrink blocks */
172 /* If reallocing the last block, we can grow it */
173 } else if ( (char *)ptr + p[-1] == sh->h_last ) {
175 sh->h_last = (char *) sh->h_last + size - p[-1];
178 /* Nowhere to grow, need to alloc and copy */
180 new = sl_malloc( size, ctx );
181 AC_MEMCPY( new, ptr, p[-1] );
187 sl_free( void *ptr, void *ctx )
189 struct slab_heap *sh = ctx;
190 ber_len_t *p = (ber_len_t *)ptr;
192 if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
193 ber_memfree_x( ptr, NULL );
194 } else if ( (char *)ptr + p[-1] == sh->h_last ) {
201 sl_release( void *ptr, void *ctx )
203 struct slab_heap *sh = ctx;
205 if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) {
213 struct slab_heap *sh = ctx;
216 if (sh) ret = sh->h_last;
222 sl_context( void *ptr )
224 struct slab_heap *sh = NULL;
227 ctx = ldap_pvt_thread_pool_context();
229 ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL );
231 if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) {