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-2004 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 BerMemoryFunctions slap_sl_mfuncs =
43 { slap_sl_malloc, slap_sl_calloc, slap_sl_realloc, slap_sl_free };
48 ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &slap_sl_mfuncs );
52 static struct slab_heap *slheap;
61 struct slab_heap *sh = NULL;
62 int pad = 2*sizeof(int)-1;
67 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sl_mem_init, (void **)&sh, NULL );
70 /* round up to doubleword boundary */
75 sh = ch_malloc( sizeof(struct slab_heap) );
76 sh->h_base = ch_malloc( size );
80 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_sl_mem_init,
81 (void *)sh, slap_sl_mem_destroy );
83 } else if ( size > (char *) sh->h_end - (char *) sh->h_base ) {
84 sh->h_base = ch_realloc( sh->h_base, size );
86 sh->h_last = sh->h_base;
87 sh->h_end = (char *) sh->h_base + size;
100 /* separate from context */
101 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_sl_mem_init, NULL, NULL );
111 struct slab_heap *sh = ctx;
112 int pad = 2*sizeof(int)-1;
115 /* ber_set_option calls us like this */
116 if (!ctx) return ber_memalloc_x( size, NULL );
118 /* round up to doubleword boundary */
119 size += pad + sizeof( ber_len_t );
122 if ((char *) sh->h_last + size >= (char *) sh->h_end ) {
123 Debug( LDAP_DEBUG_TRACE,
124 "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
126 return ch_malloc( size );
129 *new++ = size - sizeof(ber_len_t);
130 sh->h_last = (char *) sh->h_last + size;
132 return( (void *)new );
136 slap_sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
140 new = slap_sl_malloc( n*size, ctx );
142 memset( new, 0, n*size );
148 slap_sl_realloc( void *ptr, ber_len_t size, void *ctx )
150 struct slab_heap *sh = ctx;
151 int pad = 2*sizeof(int)-1;
152 ber_len_t *p = (ber_len_t *)ptr;
155 if ( ptr == NULL ) return slap_sl_malloc( size, ctx );
157 /* Not our memory? */
158 if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
159 /* duplicate of ch_realloc behavior, oh well */
160 new = ber_memrealloc_x( ptr, size, NULL );
164 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
167 exit( EXIT_FAILURE );
171 slap_sl_free( ptr, ctx );
175 /* round up to doubleword boundary */
176 size += pad + sizeof( ber_len_t );
179 /* Never shrink blocks */
183 /* If reallocing the last block, we can grow it */
184 } else if ( (char *)ptr + p[-1] == sh->h_last ) {
186 sh->h_last = (char *) sh->h_last + size - p[-1];
189 /* Nowhere to grow, need to alloc and copy */
191 new = slap_sl_malloc( size, ctx );
192 AC_MEMCPY( new, ptr, p[-1] );
198 slap_sl_free( void *ptr, void *ctx )
200 struct slab_heap *sh = ctx;
201 ber_len_t *p = (ber_len_t *)ptr;
203 if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
204 ber_memfree_x( ptr, NULL );
205 } else if ( (char *)ptr + p[-1] == sh->h_last ) {
212 slap_sl_context( void *ptr )
214 struct slab_heap *sh = NULL;
220 ctx = ldap_pvt_thread_pool_context();
222 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sl_mem_init, (void **)&sh, NULL );
225 if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) {