]> git.sur5r.net Git - openldap/blob - servers/slapd/sl_malloc.c
Eliminate getkey search
[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         ch_free(sh->h_base);
30         ch_free(sh);
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 static BerMemoryFunctions sl_bmf =
39         { sl_malloc, sl_calloc, sl_realloc, sl_free };
40
41 void
42 sl_mem_init()
43 {
44         ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &sl_bmf );
45 }
46
47 void *
48 sl_mem_create(
49         ber_len_t size,
50         void *ctx
51 )
52 {
53         struct slab_heap *sh = NULL;
54         int pad = 2*sizeof(int)-1;
55
56         ldap_pvt_thread_pool_getkey( ctx, sl_mem_init, (void **)&sh, NULL );
57
58         /* round up to doubleword boundary */
59         size += pad;
60         size &= ~pad;
61
62         if (!sh) {
63                 sh = ch_malloc( sizeof(struct slab_heap) );
64                 sh->h_base = ch_malloc( size );
65                 ldap_pvt_thread_pool_setkey( ctx, sl_mem_init, (void *)sh, sl_mem_destroy );
66         } else if ( size > sh->h_end - sh->h_base ) {
67                 sh->h_base = ch_realloc( sh->h_base, size );
68         }
69         sh->h_last = sh->h_base;
70         sh->h_end = sh->h_base + size;
71         return sh;
72 }
73
74 void *
75 sl_malloc(
76     ber_len_t   size,
77     void *ctx
78 )
79 {
80         struct slab_heap *sh = ctx;
81         int pad = 2*sizeof(int)-1;
82         ber_len_t *new;
83
84         /* ber_set_option calls us like this */
85         if (!ctx) return ber_memalloc_x( size, NULL );
86
87         /* round up to doubleword boundary */
88         size += pad + sizeof( ber_len_t );
89         size &= ~pad;
90
91         if (sh->h_last + size >= sh->h_end ) {
92 #ifdef NEW_LOGGING
93                 LDAP_LOG( OPERATION, ERR, 
94                            "sl_malloc: allocation of %lu bytes failed\n", (long)size, 0,0 );
95 #else
96                 Debug( LDAP_DEBUG_ANY, "sl_malloc of %lu bytes failed\n",
97                         (long) size, 0, 0 );
98 #endif
99                 assert( 0 );
100                 exit( EXIT_FAILURE );
101         }
102         new = sh->h_last;
103         *new++ = size - sizeof(ber_len_t);
104         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         if ( ptr < sh->h_base || ptr >= sh->h_end ) {
132 #ifdef NEW_LOGGING
133                 LDAP_LOG( OPERATION, ERR, 
134                            "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 );
135 #else
136                 Debug( LDAP_DEBUG_ANY,
137                            "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 );
138 #endif
139                 assert( 0 );
140                 exit( EXIT_FAILURE );
141         }
142
143         if ( size == 0 ) return NULL;
144
145         /* round up to doubleword boundary */
146         size += pad + sizeof( ber_len_t );
147         size &= ~pad;
148
149         /* We always alloc a new block */
150         if (size <= p[-1]) {
151                 p[-1] = size;
152                 new = p;
153         } else {
154                 new = sl_malloc( size, ctx );
155                 AC_MEMCPY( new, ptr, p[-1] );
156         }
157         return new;
158 }
159
160 void
161 sl_free( void *ptr, void *ctx )
162 {
163         struct slab_heap *sh = ctx;
164
165         if ( ptr < sh->h_base || ptr >= sh->h_end ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG( OPERATION, ERR, 
168                            "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 );
169 #else
170                 Debug( LDAP_DEBUG_ANY,
171                            "sl_free: not mine: 0x%lx\n", (long)ptr, 0,0 );
172 #endif
173                 assert( 0 );
174                 exit( EXIT_FAILURE );
175         }
176 }