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