]> git.sur5r.net Git - openldap/blob - servers/slapd/sl_malloc.c
ITS#6437 cleanup (noop change):
[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-2011 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 enum {
25         Align = 2 * sizeof(int),
26         Align_log2 = 1 + (Align>2) + (Align>4) + (Align>8) + (Align>16),
27         order_start = Align_log2 - 1,
28         pad = Align - 1
29 };
30
31 static struct slab_object * slap_replenish_sopool(struct slab_heap* sh);
32 #ifdef SLAPD_UNUSED
33 static void print_slheap(int level, void *ctx);
34 #endif
35
36 /* Keep memory context in a thread-local var, or in a global when no threads */
37 #ifdef NO_THREADS
38 static struct slab_heap *slheap;
39 # define SET_MEMCTX(thrctx, memctx, sfree)      ((void) (slheap = (memctx)))
40 # define GET_MEMCTX(thrctx, memctxp)            (*(memctxp) = slheap))
41 #else
42 # define memctx_key ((void *) slap_sl_mem_init)
43 # define SET_MEMCTX(thrctx, memctx, kfree) \
44         ldap_pvt_thread_pool_setkey(thrctx,memctx_key, memctx,kfree, NULL,NULL)
45 # define GET_MEMCTX(thrctx, memctxp) \
46         ((void) (*(memctxp) = NULL), \
47          (void) ldap_pvt_thread_pool_getkey(thrctx,memctx_key, memctxp,NULL), \
48          *(memctxp))
49 #endif /* NO_THREADS */
50
51
52 /* Destroy the context, or if key==NULL clean it up for reuse. */
53 void
54 slap_sl_mem_destroy(
55         void *key,
56         void *data
57 )
58 {
59         struct slab_heap *sh = data;
60         struct slab_object *so;
61         int i;
62
63         if (!sh->sh_stack) {
64                 for (i = 0; i <= sh->sh_maxorder - order_start; i++) {
65                         so = LDAP_LIST_FIRST(&sh->sh_free[i]);
66                         while (so) {
67                                 struct slab_object *so_tmp = so;
68                                 so = LDAP_LIST_NEXT(so, so_link);
69                                 LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_tmp, so_link);
70                         }
71                         ch_free(sh->sh_map[i]);
72                 }
73                 ch_free(sh->sh_free);
74                 ch_free(sh->sh_map);
75
76                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
77                 while (so) {
78                         struct slab_object *so_tmp = so;
79                         so = LDAP_LIST_NEXT(so, so_link);
80                         if (!so_tmp->so_blockhead) {
81                                 LDAP_LIST_REMOVE(so_tmp, so_link);
82                         }
83                 }
84                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
85                 while (so) {
86                         struct slab_object *so_tmp = so;
87                         so = LDAP_LIST_NEXT(so, so_link);
88                         ch_free(so_tmp);
89                 }
90         }
91
92         if (key != NULL) {
93                 ber_memfree_x(sh->sh_base, NULL);
94                 ber_memfree_x(sh, NULL);
95         }
96 }
97
98 BerMemoryFunctions slap_sl_mfuncs =
99         { slap_sl_malloc, slap_sl_calloc, slap_sl_realloc, slap_sl_free };
100
101 void
102 slap_sl_mem_init()
103 {
104         assert( Align == 1 << Align_log2 );
105         /* Adding head+tail preserves alignment */
106         assert( 2*sizeof(ber_len_t) % Align == 0 );
107
108         ber_set_option( NULL, LBER_OPT_MEMORY_FNS, &slap_sl_mfuncs );
109 }
110
111 /* This allocator always returns memory aligned on a 2-int boundary.
112  *
113  * The stack-based allocator stores the size as a ber_len_t at both
114  * the head and tail of the allocated block. When freeing a block, the
115  * tail length is ORed with 1 to mark it as free. Freed space can only
116  * be reclaimed from the tail forward. If the tail block is never freed,
117  * nothing else will be reclaimed until the slab is reset...
118  */
119 void *
120 slap_sl_mem_create(
121         ber_len_t size,
122         int stack,
123         void *thrctx,
124         int new
125 )
126 {
127         void *memctx;
128         struct slab_heap *sh;
129         ber_len_t size_shift;
130         struct slab_object *so;
131
132         sh = GET_MEMCTX(thrctx, &memctx);
133         if ( sh && !new )
134                 return sh;
135
136         /* round up to doubleword boundary */
137         size = (size + Align-1) & -Align;
138
139         if (!sh) {
140                 sh = ch_malloc(sizeof(struct slab_heap));
141                 sh->sh_base = ch_malloc(size);
142                 SET_MEMCTX(thrctx, sh, slap_sl_mem_destroy);
143         } else {
144                 slap_sl_mem_destroy(NULL, sh);
145                 if ( size > (char *)sh->sh_end - (char *)sh->sh_base ) {
146                         void    *newptr;
147
148                         newptr = ch_realloc( sh->sh_base, size );
149                         if ( newptr == NULL ) return NULL;
150                         sh->sh_base = newptr;
151                 }
152         }
153         sh->sh_end = (char *) sh->sh_base + size;
154
155         sh->sh_stack = stack;
156         if (stack) {
157                 /* insert dummy len */
158                 {
159                         ber_len_t *i = sh->sh_base;
160                         *i++ = 0;
161                         sh->sh_last = i;
162                 }
163         } else {
164                 int i, order = -1, order_end = -1;
165
166                 size_shift = size - 1;
167                 do {
168                         order_end++;
169                 } while (size_shift >>= 1);
170                 order = order_end - order_start + 1;
171                 sh->sh_maxorder = order_end;
172
173                 sh->sh_free = (struct sh_freelist *)
174                                                 ch_malloc(order * sizeof(struct sh_freelist));
175                 for (i = 0; i < order; i++) {
176                         LDAP_LIST_INIT(&sh->sh_free[i]);
177                 }
178
179                 LDAP_LIST_INIT(&sh->sh_sopool);
180
181                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
182                         slap_replenish_sopool(sh);
183                 }
184                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
185                 LDAP_LIST_REMOVE(so, so_link);
186                 so->so_ptr = sh->sh_base;
187
188                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[order-1], so, so_link);
189
190                 sh->sh_map = (unsigned char **)
191                                         ch_malloc(order * sizeof(unsigned char *));
192                 for (i = 0; i < order; i++) {
193                         int shiftamt = order_start + 1 + i;
194                         int nummaps = size >> shiftamt;
195                         assert(nummaps);
196                         nummaps >>= 3;
197                         if (!nummaps) nummaps = 1;
198                         sh->sh_map[i] = (unsigned char *) ch_malloc(nummaps);
199                         memset(sh->sh_map[i], 0, nummaps);
200                 }
201         }
202         return sh;
203 }
204
205 void
206 slap_sl_mem_detach(
207         void *thrctx,
208         void *memctx
209 )
210 {
211         /* separate from context */
212         SET_MEMCTX(thrctx, NULL, 0);
213 }
214
215 void *
216 slap_sl_malloc(
217     ber_len_t   size,
218     void *ctx
219 )
220 {
221         struct slab_heap *sh = ctx;
222         ber_len_t *ptr, *newptr;
223
224 #ifdef SLAP_NO_SL_MALLOC
225         newptr = ber_memalloc_x( size, NULL );
226         if ( newptr ) return newptr;
227         assert( 0 );
228         exit( EXIT_FAILURE );
229 #endif
230
231         /* ber_set_option calls us like this */
232         if (!ctx) {
233                 newptr = ber_memalloc_x( size, NULL );
234                 if ( newptr ) return newptr;
235                 assert( 0 );
236                 exit( EXIT_FAILURE );
237         }
238
239         /* round up to doubleword boundary, plus space for len at head and tail */
240         size = (size + 2*sizeof(ber_len_t) + Align-1) & -Align;
241
242         if (sh->sh_stack) {
243                 if ((char *)sh->sh_last + size >= (char *)sh->sh_end) {
244                         Debug(LDAP_DEBUG_TRACE,
245                                 "slap_sl_malloc of %lu bytes failed, using ch_malloc\n",
246                                 (long)size, 0, 0);
247                         return ch_malloc(size);
248                 }
249                 newptr = sh->sh_last;
250                 sh->sh_last = (char *) sh->sh_last + size;
251                 size -= sizeof(ber_len_t);
252                 *newptr++ = size;
253                 *(ber_len_t *)((char *)sh->sh_last - sizeof(ber_len_t)) = size;
254                 return( (void *)newptr );
255         } else {
256                 struct slab_object *so_new, *so_left, *so_right;
257                 ber_len_t size_shift;
258                 unsigned long diff;
259                 int i, j, order = -1;
260
261                 size_shift = size - 1;
262                 do {
263                         order++;
264                 } while (size_shift >>= 1);
265
266                 for (i = order; i <= sh->sh_maxorder &&
267                                 LDAP_LIST_EMPTY(&sh->sh_free[i-order_start]); i++);
268
269                 if (i == order) {
270                         so_new = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
271                         LDAP_LIST_REMOVE(so_new, so_link);
272                         ptr = so_new->so_ptr;
273                         diff = (unsigned long)((char*)ptr -
274                                         (char*)sh->sh_base) >> (order + 1);
275                         sh->sh_map[order-order_start][diff>>3] |= (1 << (diff & 0x7));
276                         *ptr++ = size - sizeof(ber_len_t);
277                         LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_new, so_link);
278                         return((void*)ptr);
279                 } else if (i <= sh->sh_maxorder) {
280                         for (j = i; j > order; j--) {
281                                 so_left = LDAP_LIST_FIRST(&sh->sh_free[j-order_start]);
282                                 LDAP_LIST_REMOVE(so_left, so_link);
283                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
284                                         slap_replenish_sopool(sh);
285                                 }
286                                 so_right = LDAP_LIST_FIRST(&sh->sh_sopool);
287                                 LDAP_LIST_REMOVE(so_right, so_link);
288                                 so_right->so_ptr = (void *)((char *)so_left->so_ptr + (1 << j));
289                                 if (j == order + 1) {
290                                         ptr = so_left->so_ptr;
291                                         diff = (unsigned long)((char*)ptr -
292                                                         (char*)sh->sh_base) >> (order+1);
293                                         sh->sh_map[order-order_start][diff>>3] |=
294                                                         (1 << (diff & 0x7));
295                                         *ptr++ = size - sizeof(ber_len_t);
296                                         LDAP_LIST_INSERT_HEAD(
297                                                         &sh->sh_free[j-1-order_start], so_right, so_link);
298                                         LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, so_left, so_link);
299                                         return((void*)ptr);
300                                 } else {
301                                         LDAP_LIST_INSERT_HEAD(
302                                                         &sh->sh_free[j-1-order_start], so_right, so_link);
303                                         LDAP_LIST_INSERT_HEAD(
304                                                         &sh->sh_free[j-1-order_start], so_left, so_link);
305                                 }
306                         }
307                 } else {
308                         Debug( LDAP_DEBUG_TRACE,
309                                 "sl_malloc %lu: ch_malloc\n",
310                                 (long)size, 0, 0);
311                         return (void*)ch_malloc(size);
312                 }
313         }
314
315         /* FIXME: missing return; guessing... */
316         return NULL;
317 }
318
319 void *
320 slap_sl_calloc( ber_len_t n, ber_len_t size, void *ctx )
321 {
322         void *newptr;
323
324         newptr = slap_sl_malloc( n*size, ctx );
325         if ( newptr ) {
326                 memset( newptr, 0, n*size );
327         }
328         return newptr;
329 }
330
331 void *
332 slap_sl_realloc(void *ptr, ber_len_t size, void *ctx)
333 {
334         struct slab_heap *sh = ctx;
335         ber_len_t oldsize, *p = (ber_len_t *) ptr;
336         void *newptr;
337
338         if (ptr == NULL)
339                 return slap_sl_malloc(size, ctx);
340
341 #ifdef SLAP_NO_SL_MALLOC
342         newptr = ber_memrealloc_x( ptr, size, NULL );
343         if ( newptr ) return newptr;
344         assert( 0 );
345         exit( EXIT_FAILURE );
346 #endif
347
348         /* Not our memory? */
349         if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
350                 /* duplicate of realloc behavior, oh well */
351                 newptr = ber_memrealloc_x(ptr, size, NULL);
352                 if (newptr) {
353                         return newptr;
354                 }
355                 Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
356                                 (long) size, 0, 0);
357                 assert(0);
358                 exit( EXIT_FAILURE );
359         }
360
361         if (size == 0) {
362                 slap_sl_free(ptr, ctx);
363                 return NULL;
364         }
365
366         oldsize = p[-1];
367
368         if (sh->sh_stack) {
369                 /* Round up to doubleword boundary, add room for head */
370                 size = ((size + Align-1) & -Align) + sizeof( ber_len_t );
371
372                 p--;
373
374                 /* Never shrink blocks */
375                 if (size <= oldsize) {
376                         return ptr;
377         
378                 /* If reallocing the last block, try to grow it */
379                 } else if ((char *) ptr + oldsize == sh->sh_last) {
380                         if (size < (char *) sh->sh_end - (char *) ptr) {
381                                 sh->sh_last = (char *) ptr + size;
382                                 p[0] = size;
383                                 p[size/sizeof(ber_len_t)] = size;
384                                 return ptr;
385                         }
386
387                 /* Nowhere to grow, need to alloc and copy */
388                 } else {
389                         /* Slight optimization of the final realloc variant */
390                         size -= sizeof(ber_len_t);
391                         oldsize -= sizeof(ber_len_t);
392                         newptr = slap_sl_malloc(size, ctx);
393                         AC_MEMCPY(newptr, ptr, oldsize);
394                         /* Not last block, can just mark old region as free */
395                         p[p[0]/sizeof(ber_len_t)] |= 1;
396                         return newptr;
397                 }
398
399                 size -= sizeof(ber_len_t);
400                 oldsize -= sizeof(ber_len_t);
401
402         } else if (oldsize > size) {
403                 oldsize = size;
404         }
405
406         newptr = slap_sl_malloc(size, ctx);
407         AC_MEMCPY(newptr, ptr, oldsize);
408         slap_sl_free(ptr, ctx);
409         return newptr;
410 }
411
412 void
413 slap_sl_free(void *ptr, void *ctx)
414 {
415         struct slab_heap *sh = ctx;
416         ber_len_t size;
417         ber_len_t *p = (ber_len_t *)ptr, *tmpp;
418
419         if (!ptr)
420                 return;
421
422 #ifdef SLAP_NO_SL_MALLOC
423         ber_memfree_x( ptr, NULL );
424         return;
425 #endif
426
427         if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
428                 ber_memfree_x(ptr, NULL);
429
430         } else if (sh->sh_stack) {
431                 size = p[-1];
432                 p = (ber_len_t *) ((char *) ptr + size);
433                 /* mark it free */
434                 p[-1] = size |= 1;
435                 /* reclaim free space off tail */
436                 if (sh->sh_last == p) {
437                         do {
438                                 p = (ber_len_t *) ((char *) p - size + 1) - 1;
439                                 size = p[-1];
440                         } while (size & 1);
441                         sh->sh_last = p;
442                 }
443
444         } else {
445                 int size_shift, order_size;
446                 struct slab_object *so;
447                 unsigned long diff;
448                 int i, inserted = 0, order = -1;
449
450                 size = *(--p);
451                 size_shift = size + sizeof(ber_len_t) - 1;
452                 do {
453                         order++;
454                 } while (size_shift >>= 1);
455
456                 for (i = order, tmpp = p; i <= sh->sh_maxorder; i++) {
457                         order_size = 1 << (i+1);
458                         diff = (unsigned long)((char*)tmpp - (char*)sh->sh_base) >> (i+1);
459                         sh->sh_map[i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
460                         if (diff == ((diff>>1)<<1)) {
461                                 if (!(sh->sh_map[i-order_start][(diff+1)>>3] &
462                                                 (1<<((diff+1)&0x7)))) {
463                                         so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
464                                         while (so) {
465                                                 if ((char*)so->so_ptr == (char*)tmpp) {
466                                                         LDAP_LIST_REMOVE( so, so_link );
467                                                 } else if ((char*)so->so_ptr ==
468                                                                 (char*)tmpp + order_size) {
469                                                         LDAP_LIST_REMOVE(so, so_link);
470                                                         break;
471                                                 }
472                                                 so = LDAP_LIST_NEXT(so, so_link);
473                                         }
474                                         if (so) {
475                                                 if (i < sh->sh_maxorder) {
476                                                         inserted = 1;
477                                                         so->so_ptr = tmpp;
478                                                         LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1],
479                                                                         so, so_link);
480                                                 }
481                                                 continue;
482                                         } else {
483                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
484                                                         slap_replenish_sopool(sh);
485                                                 }
486                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
487                                                 LDAP_LIST_REMOVE(so, so_link);
488                                                 so->so_ptr = tmpp;
489                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
490                                                                 so, so_link);
491                                                 break;
492
493                                                 Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
494                                                         "free object not found while bit is clear.\n",
495                                                         0, 0, 0);
496                                                 assert(so != NULL);
497
498                                         }
499                                 } else {
500                                         if (!inserted) {
501                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
502                                                         slap_replenish_sopool(sh);
503                                                 }
504                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
505                                                 LDAP_LIST_REMOVE(so, so_link);
506                                                 so->so_ptr = tmpp;
507                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
508                                                                 so, so_link);
509                                         }
510                                         break;
511                                 }
512                         } else {
513                                 if (!(sh->sh_map[i-order_start][(diff-1)>>3] &
514                                                 (1<<((diff-1)&0x7)))) {
515                                         so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
516                                         while (so) {
517                                                 if ((char*)so->so_ptr == (char*)tmpp) {
518                                                         LDAP_LIST_REMOVE(so, so_link);
519                                                 } else if ((char*)tmpp == (char *)so->so_ptr + order_size) {
520                                                         LDAP_LIST_REMOVE(so, so_link);
521                                                         tmpp = so->so_ptr;
522                                                         break;
523                                                 }
524                                                 so = LDAP_LIST_NEXT(so, so_link);
525                                         }
526                                         if (so) {
527                                                 if (i < sh->sh_maxorder) {
528                                                         inserted = 1;
529                                                         LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start+1],                                                                    so, so_link);
530                                                         continue;
531                                                 }
532                                         } else {
533                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
534                                                         slap_replenish_sopool(sh);
535                                                 }
536                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
537                                                 LDAP_LIST_REMOVE(so, so_link);
538                                                 so->so_ptr = tmpp;
539                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
540                                                                 so, so_link);
541                                                 break;
542
543                                                 Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
544                                                         "free object not found while bit is clear.\n",
545                                                         0, 0, 0 );
546                                                 assert(so != NULL);
547
548                                         }
549                                 } else {
550                                         if ( !inserted ) {
551                                                 if (LDAP_LIST_EMPTY(&sh->sh_sopool)) {
552                                                         slap_replenish_sopool(sh);
553                                                 }
554                                                 so = LDAP_LIST_FIRST(&sh->sh_sopool);
555                                                 LDAP_LIST_REMOVE(so, so_link);
556                                                 so->so_ptr = tmpp;
557                                                 LDAP_LIST_INSERT_HEAD(&sh->sh_free[i-order_start],
558                                                                 so, so_link);
559                                         }
560                                         break;
561                                 }
562                         }
563                 }
564         }
565 }
566
567 void *
568 slap_sl_context( void *ptr )
569 {
570         void *memctx;
571         struct slab_heap *sh;
572
573         if ( slapMode & SLAP_TOOL_MODE ) return NULL;
574
575         sh = GET_MEMCTX(ldap_pvt_thread_pool_context(), &memctx);
576         if (sh && ptr >= sh->sh_base && ptr <= sh->sh_end) {
577                 return sh;
578         }
579         return NULL;
580 }
581
582 static struct slab_object *
583 slap_replenish_sopool(
584     struct slab_heap* sh
585 )
586 {
587     struct slab_object *so_block;
588     int i;
589
590     so_block = (struct slab_object *)ch_malloc(
591                     SLAP_SLAB_SOBLOCK * sizeof(struct slab_object));
592
593     if ( so_block == NULL ) {
594         return NULL;
595     }
596
597     so_block[0].so_blockhead = 1;
598     LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[0], so_link);
599     for (i = 1; i < SLAP_SLAB_SOBLOCK; i++) {
600         so_block[i].so_blockhead = 0;
601         LDAP_LIST_INSERT_HEAD(&sh->sh_sopool, &so_block[i], so_link );
602     }
603
604     return so_block;
605 }
606
607 #ifdef SLAPD_UNUSED
608 static void
609 print_slheap(int level, void *ctx)
610 {
611         struct slab_heap *sh = ctx;
612         struct slab_object *so;
613         int i, j, once = 0;
614
615         if (!ctx) {
616                 Debug(level, "NULL memctx\n", 0, 0, 0);
617                 return;
618         }
619
620         Debug(level, "sh->sh_maxorder=%d\n", sh->sh_maxorder, 0, 0);
621
622         for (i = order_start; i <= sh->sh_maxorder; i++) {
623                 once = 0;
624                 Debug(level, "order=%d\n", i, 0, 0);
625                 for (j = 0; j < (1<<(sh->sh_maxorder-i))/8; j++) {
626                         Debug(level, "%02x ", sh->sh_map[i-order_start][j], 0, 0);
627                         once = 1;
628                 }
629                 if (!once) {
630                         Debug(level, "%02x ", sh->sh_map[i-order_start][0], 0, 0);
631                 }
632                 Debug(level, "\n", 0, 0, 0);
633                 Debug(level, "free list:\n", 0, 0, 0);
634                 so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
635                 while (so) {
636                         Debug(level, "%lx\n", (unsigned long) so->so_ptr, 0, 0);
637                         so = LDAP_LIST_NEXT(so, so_link);
638                 }
639         }
640 }
641 #endif