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