]> git.sur5r.net Git - openldap/blob - servers/slapd/sl_malloc.c
From HEAD
[openldap] / servers / slapd / sl_malloc.c
1 /* sl_malloc.c - malloc routines using a per-thread slab */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "slap.h"
23
24 struct slab_heap {
25         void *h_base;
26         void *h_last;
27         void *h_end;
28 };
29
30 void
31 sl_mem_destroy(
32         void *key,
33         void *data
34 )
35 {
36         struct slab_heap *sh = data;
37
38         ber_memfree_x(sh->h_base, NULL);
39         ber_memfree_x(sh, NULL);
40 }
41
42 BER_MEMALLOC_FN sl_malloc;
43 BER_MEMCALLOC_FN sl_calloc;
44 BER_MEMREALLOC_FN sl_realloc;
45 BER_MEMFREE_FN sl_free;
46
47
48 BerMemoryFunctions sl_mfuncs =
49         { sl_malloc, sl_calloc, sl_realloc, sl_free };
50
51 void
52 sl_mem_init()
53 {
54         ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &sl_mfuncs );
55 }
56
57 #ifdef NO_THREADS
58 static struct slab_heap *slheap;
59 #endif
60
61 void *
62 sl_mem_create(
63         ber_len_t size,
64         void *ctx
65 )
66 {
67         struct slab_heap *sh = NULL;
68         int pad = 2*sizeof(int)-1;
69
70 #ifdef NO_THREADS
71         sh = slheap;
72 #else
73         ldap_pvt_thread_pool_getkey( ctx, (void *)sl_mem_init, (void **)&sh, NULL );
74 #endif
75
76         /* round up to doubleword boundary */
77         size += pad;
78         size &= ~pad;
79
80         if (!sh) {
81                 sh = ch_malloc( sizeof(struct slab_heap) );
82                 sh->h_base = ch_malloc( size );
83 #ifdef NO_THREADS
84                 slheap = sh;
85 #else
86                 ldap_pvt_thread_pool_setkey( ctx, (void *)sl_mem_init, (void *)sh, sl_mem_destroy );
87 #endif
88         } else if ( size > (char *) sh->h_end - (char *) sh->h_base ) {
89                 sh->h_base = ch_realloc( sh->h_base, size );
90         }
91         sh->h_last = sh->h_base;
92         sh->h_end = (char *) sh->h_base + size;
93         return sh;
94 }
95
96 void
97 sl_mem_detach(
98         void *ctx,
99         void *memctx
100 )
101 {
102 #ifdef NO_THREADS
103         slheap = NULL;
104 #else
105         /* separate from context */
106         ldap_pvt_thread_pool_setkey( ctx, (void *)sl_mem_init, NULL, NULL );
107 #endif
108 }
109
110 void *
111 sl_malloc(
112     ber_len_t   size,
113     void *ctx
114 )
115 {
116         struct slab_heap *sh = ctx;
117         int pad = 2*sizeof(int)-1;
118         ber_len_t *new;
119
120         /* ber_set_option calls us like this */
121         if (!ctx) return ber_memalloc_x( size, NULL );
122
123         /* round up to doubleword boundary */
124         size += pad + sizeof( ber_len_t );
125         size &= ~pad;
126
127         if ((char *) sh->h_last + size >= (char *) sh->h_end ) {
128 #ifdef NEW_LOGGING
129                 LDAP_LOG( OPERATION, INFO, 
130                            "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
131 #else
132                 Debug( LDAP_DEBUG_TRACE,
133                            "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
134 #endif
135                 return ch_malloc( size );
136         }
137         new = sh->h_last;
138         *new++ = size - sizeof(ber_len_t);
139         sh->h_last = (char *) sh->h_last + size;
140         
141         return( (void *)new );
142 }
143
144 void *
145 sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
146 {
147         void *new;
148
149         new = sl_malloc( n*size, ctx );
150         if ( new ) {
151                 memset( new, 0, n*size );
152         }
153         return new;
154 }
155
156 void *
157 sl_realloc( void *ptr, ber_len_t size, void *ctx )
158 {
159         struct slab_heap *sh = ctx;
160         int pad = 2*sizeof(int)-1;
161         ber_len_t *p = (ber_len_t *)ptr;
162         ber_len_t *new;
163
164         if ( ptr == NULL ) return sl_malloc( size, ctx );
165
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 );
170                 if (new ) {
171                         return new;
172                 }
173 #ifdef NEW_LOGGING
174                 LDAP_LOG( OPERATION, ERR, 
175                            "ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 );
176 #else
177                 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
178                         (long) size, 0, 0 );
179 #endif
180                 assert( 0 );
181                 exit( EXIT_FAILURE );
182         }
183
184         if ( size == 0 ) {
185                 sl_free( ptr, ctx );
186                 return NULL;
187         }
188
189         /* round up to doubleword boundary */
190         size += pad + sizeof( ber_len_t );
191         size &= ~pad;
192
193         /* Never shrink blocks */
194         if (size <= p[-1]) {
195                 new = p;
196         
197         /* If reallocing the last block, we can grow it */
198         } else if ( (char *)ptr + p[-1] == sh->h_last ) {
199                 new = p;
200                 sh->h_last = (char *) sh->h_last + size - p[-1];
201                 p[-1] = size;
202         
203         /* Nowhere to grow, need to alloc and copy */
204         } else {
205                 new = sl_malloc( size, ctx );
206                 AC_MEMCPY( new, ptr, p[-1] );
207         }
208         return new;
209 }
210
211 void
212 sl_free( void *ptr, void *ctx )
213 {
214         struct slab_heap *sh = ctx;
215         ber_len_t *p = (ber_len_t *)ptr;
216
217         if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
218                 ber_memfree_x( ptr, NULL );
219         } else if ( (char *)ptr + p[-1] == sh->h_last ) {
220                 p--;
221                 sh->h_last = p;
222         }
223 }
224
225 void *
226 sl_context( void *ptr )
227 {
228         struct slab_heap *sh = NULL;
229         void *ctx;
230
231 #ifdef NO_THREADS
232         sh = slheap;
233 #else
234         ctx = ldap_pvt_thread_pool_context();
235
236         ldap_pvt_thread_pool_getkey( ctx, (void *)sl_mem_init, (void **)&sh, NULL );
237 #endif
238
239         if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) {
240                 return sh;
241         }
242         return NULL;
243 }