]> git.sur5r.net Git - openldap/blob - servers/slapd/sl_malloc.c
Sync with 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 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 void *
58 sl_mem_create(
59         ber_len_t size,
60         void *ctx
61 )
62 {
63         struct slab_heap *sh = NULL;
64         int pad = 2*sizeof(int)-1;
65
66         ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL );
67
68         /* round up to doubleword boundary */
69         size += pad;
70         size &= ~pad;
71
72         if (!sh) {
73                 sh = ch_malloc( sizeof(struct slab_heap) );
74                 sh->h_base = ch_malloc( size );
75                 ldap_pvt_thread_pool_setkey( ctx, sl_mem_init, (void *)sh, sl_mem_destroy );
76         } else if ( size > (char *) sh->h_end - (char *) sh->h_base ) {
77                 sh->h_base = ch_realloc( sh->h_base, size );
78         }
79         sh->h_last = sh->h_base;
80         sh->h_end = (char *) sh->h_base + size;
81         return sh;
82 }
83
84 void
85 sl_mem_detach(
86         void *ctx,
87         void *memctx
88 )
89 {
90         /* separate from context */
91         ldap_pvt_thread_pool_setkey( ctx, sl_mem_init, NULL, NULL );
92 }
93
94 void *
95 sl_malloc(
96     ber_len_t   size,
97     void *ctx
98 )
99 {
100         struct slab_heap *sh = ctx;
101         int pad = 2*sizeof(int)-1;
102         ber_len_t *new;
103
104         /* ber_set_option calls us like this */
105         if (!ctx) return ber_memalloc_x( size, NULL );
106
107         /* round up to doubleword boundary */
108         size += pad + sizeof( ber_len_t );
109         size &= ~pad;
110
111         if ((char *) sh->h_last + size >= (char *) sh->h_end ) {
112 #ifdef NEW_LOGGING
113                 LDAP_LOG( OPERATION, INFO, 
114                            "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
115 #else
116                 Debug( LDAP_DEBUG_TRACE,
117                            "sl_malloc of %lu bytes failed, using ch_malloc\n", (long)size, 0,0 );
118 #endif
119                 return ch_malloc( size );
120         }
121         new = sh->h_last;
122         *new++ = size - sizeof(ber_len_t);
123         sh->h_last = (char *) sh->h_last + size;
124         
125         return( (void *)new );
126 }
127
128 void *
129 sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
130 {
131         void *new;
132
133         new = sl_malloc( n*size, ctx );
134         if ( new ) {
135                 memset( new, 0, n*size );
136         }
137         return new;
138 }
139
140 void *
141 sl_realloc( void *ptr, ber_len_t size, void *ctx )
142 {
143         struct slab_heap *sh = ctx;
144         int pad = 2*sizeof(int)-1;
145         ber_len_t *p = (ber_len_t *)ptr;
146         ber_len_t *new;
147
148         if ( ptr == NULL ) return sl_malloc( size, ctx );
149
150         /* Not our memory? */
151         if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
152                 /* duplicate of ch_realloc behavior, oh well */
153                 new = ber_memrealloc_x( ptr, size, NULL );
154                 if (new ) {
155                         return new;
156                 }
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( OPERATION, ERR, 
159                            "ch_realloc: reallocation of %lu bytes failed\n", (long)size, 0,0 );
160 #else
161                 Debug( LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
162                         (long) size, 0, 0 );
163 #endif
164                 assert( 0 );
165                 exit( EXIT_FAILURE );
166         }
167
168         if ( size == 0 ) {
169                 sl_free( ptr, ctx );
170                 return NULL;
171         }
172
173         /* round up to doubleword boundary */
174         size += pad + sizeof( ber_len_t );
175         size &= ~pad;
176
177         /* Never shrink blocks */
178         if (size <= p[-1]) {
179                 new = p;
180         
181         /* If reallocing the last block, we can grow it */
182         } else if ( (char *)ptr + p[-1] == sh->h_last ) {
183                 new = p;
184                 sh->h_last = (char *) sh->h_last + size - p[-1];
185                 p[-1] = size;
186         
187         /* Nowhere to grow, need to alloc and copy */
188         } else {
189                 new = sl_malloc( size, ctx );
190                 AC_MEMCPY( new, ptr, p[-1] );
191         }
192         return new;
193 }
194
195 void
196 sl_free( void *ptr, void *ctx )
197 {
198         struct slab_heap *sh = ctx;
199         ber_len_t *p = (ber_len_t *)ptr;
200
201         if ( !sh || ptr < sh->h_base || ptr >= sh->h_end ) {
202                 ber_memfree_x( ptr, NULL );
203         } else if ( (char *)ptr + p[-1] == sh->h_last ) {
204                 p--;
205                 sh->h_last = p;
206         }
207 }
208
209 void
210 sl_release( void *ptr, void *ctx )
211 {
212         struct slab_heap *sh = ctx;
213
214         if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) {
215                 sh->h_last = ptr;
216         }
217 }
218
219 void *
220 sl_mark( void *ctx )
221 {
222         struct slab_heap *sh = ctx;
223         void *ret = NULL;
224
225         if (sh) ret = sh->h_last;
226
227         return ret;
228 }
229
230 void *
231 sl_context( void *ptr )
232 {
233         struct slab_heap *sh = NULL;
234         void *ctx;
235
236         ctx = ldap_pvt_thread_pool_context();
237
238         ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL );
239
240         if ( sh && ptr >= sh->h_base && ptr <= sh->h_end ) {
241                 return sh;
242         }
243         return NULL;
244 }