]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/sl_malloc.c
ITS#5037 from HEAD, index warning
[openldap] / servers / slapd / sl_malloc.c
index b7a40644301287c112907c146707c57fa2bb1c20..cf3ffcefe4548942968eb2978bcc390e4266ace2 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2003-2004 The OpenLDAP Foundation.
+ * Copyright 2003-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -22,7 +22,9 @@
 #include "slap.h"
 
 static struct slab_object * slap_replenish_sopool(struct slab_heap* sh);
+#ifdef SLAPD_UNUSED
 static void print_slheap(int level, void *ctx);
+#endif
 
 void
 slap_sl_mem_destroy(
@@ -95,18 +97,20 @@ slap_sl_mem_create(
        void *ctx
 )
 {
-       struct slab_heap *sh = NULL;
+       struct slab_heap *sh;
        ber_len_t size_shift;
        int pad = 2*sizeof(int)-1, pad_shift;
        int order = -1, order_start = -1, order_end = -1;
-       int i, k;
+       int i;
        struct slab_object *so;
 
 #ifdef NO_THREADS
        sh = slheap;
 #else
+       void *sh_tmp = NULL;
        ldap_pvt_thread_pool_getkey(
-               ctx, (void *)slap_sl_mem_init, (void **)&sh, NULL );
+               ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL );
+       sh = sh_tmp;
 #endif
 
        /* round up to doubleword boundary */
@@ -207,9 +211,13 @@ slap_sl_mem_create(
                sh->sh_map = (unsigned char **)
                                        ch_malloc(order * sizeof(unsigned char *));
                for (i = 0; i < order; i++) {
-                       sh->sh_map[i] = (unsigned char *)
-                                                       ch_malloc(size >> (1 << (order_start + i + 3)));
-                       memset(sh->sh_map[i], 0, size >> (1 << (order_start + i + 3)));
+                       int shiftamt = order_start + 1 + i;
+                       int nummaps = size >> shiftamt;
+                       assert(nummaps);
+                       nummaps >>= 3;
+                       if (!nummaps) nummaps = 1;
+                       sh->sh_map[i] = (unsigned char *) ch_malloc(nummaps);
+                       memset(sh->sh_map[i], 0, nummaps);
                }
                sh->sh_stack = stack;
                return sh;
@@ -243,13 +251,11 @@ slap_sl_malloc(
        struct slab_object *so_new, *so_left, *so_right;
        ber_len_t *ptr, *new;
        unsigned long diff;
-       int i, j, k;
+       int i, j;
 
        /* ber_set_option calls us like this */
        if (!ctx) return ber_memalloc_x(size, NULL);
 
-       Debug(LDAP_DEBUG_TRACE, "==> slap_sl_malloc (%d)\n", size, 0, 0);
-
        /* round up to doubleword boundary */
        size += sizeof(ber_len_t) + pad;
        size &= ~pad;
@@ -298,7 +304,7 @@ slap_sl_malloc(
                                }
                                so_right = LDAP_LIST_FIRST(&sh->sh_sopool);
                                LDAP_LIST_REMOVE(so_right, so_link);
-                               so_right->so_ptr = so_left->so_ptr + (1 << j);
+                               so_right->so_ptr = (void *)((char *)so_left->so_ptr + (1 << j));
                                if (j == order + 1) {
                                        ptr = so_left->so_ptr;
                                        diff = (unsigned long)((char*)ptr -
@@ -324,6 +330,9 @@ slap_sl_malloc(
                        return (void*)ch_malloc(size);
                }
        }
+
+       /* FIXME: missing return; guessing... */
+       return NULL;
 }
 
 void *
@@ -342,12 +351,8 @@ void *
 slap_sl_realloc(void *ptr, ber_len_t size, void *ctx)
 {
        struct slab_heap *sh = ctx;
-       int size_shift;
-       int pad = 2*sizeof(int)-1, pad_shift;
-       int order_start = -1, order = -1;
-       struct slab_object *so;
+       int pad = 2*sizeof(int) -1;
        ber_len_t *p = (ber_len_t *)ptr, *new;
-       unsigned long diff;
 
        if (ptr == NULL)
                return slap_sl_malloc(size, ctx);
@@ -371,6 +376,10 @@ slap_sl_realloc(void *ptr, ber_len_t size, void *ctx)
        }
 
        if (sh->sh_stack) {
+               /* round up to doubleword boundary */
+               size += pad + sizeof( ber_len_t );
+               size &= ~pad;
+
                /* Never shrink blocks */
                if (size <= p[-1]) {
                        new = p;
@@ -411,9 +420,7 @@ slap_sl_free(void *ptr, void *ctx)
        int order_start = -1, order = -1;
        struct slab_object *so;
        unsigned long diff;
-       int i, k, inserted = 0;
-
-       Debug( LDAP_DEBUG_TRACE, "==> slap_sl_free \n", 0, 0, 0);
+       int i, inserted = 0;
 
        if (!sh || ptr < sh->sh_base || ptr >= sh->sh_end) {
                ber_memfree_x(ptr, NULL);
@@ -472,7 +479,7 @@ slap_sl_free(void *ptr, void *ctx)
                                                Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
                                                        "free object not found while bit is clear.\n",
                                                        0, 0, 0);
-                                               assert(so);
+                                               assert(so != NULL);
 
                                        }
                                } else {
@@ -495,7 +502,7 @@ slap_sl_free(void *ptr, void *ctx)
                                        while (so) {
                                                if ((char*)so->so_ptr == (char*)tmpp) {
                                                        LDAP_LIST_REMOVE(so, so_link);
-                                               } else if ((char*)tmpp == so->so_ptr + order_size) {
+                                               } else if ((char*)tmpp == (char *)so->so_ptr + order_size) {
                                                        LDAP_LIST_REMOVE(so, so_link);
                                                        tmpp = so->so_ptr;
                                                        break;
@@ -522,7 +529,7 @@ slap_sl_free(void *ptr, void *ctx)
                                                Debug(LDAP_DEBUG_TRACE, "slap_sl_free: "
                                                        "free object not found while bit is clear.\n",
                                                        0, 0, 0 );
-                                               assert( so );
+                                               assert(so != NULL);
 
                                        }
                                } else {
@@ -546,16 +553,20 @@ slap_sl_free(void *ptr, void *ctx)
 void *
 slap_sl_context( void *ptr )
 {
-       struct slab_heap *sh = NULL;
-       void *ctx;
+       struct slab_heap *sh;
+       void *ctx, *sh_tmp;
+
+       if ( slapMode & SLAP_TOOL_MODE ) return NULL;
 
 #ifdef NO_THREADS
        sh = slheap;
 #else
        ctx = ldap_pvt_thread_pool_context();
 
-       ldap_pvt_thread_pool_getkey(ctx, (void *)slap_sl_mem_init,
-                       (void **)&sh, NULL);
+       sh_tmp = NULL;
+       ldap_pvt_thread_pool_getkey(
+               ctx, (void *)slap_sl_mem_init, &sh_tmp, NULL);
+       sh = sh_tmp;
 #endif
 
        if (sh && ptr >= sh->sh_base && ptr <= sh->sh_end) {
@@ -589,6 +600,7 @@ slap_replenish_sopool(
     return so_block;
 }
 
+#ifdef SLAPD_UNUSED
 static void
 print_slheap(int level, void *ctx)
 {
@@ -624,8 +636,9 @@ print_slheap(int level, void *ctx)
                Debug(level, "free list:\n", 0, 0, 0);
                so = LDAP_LIST_FIRST(&sh->sh_free[i-order_start]);
                while (so) {
-                       Debug(level, "%x\n",so->so_ptr, 0, 0);
+                       Debug(level, "%lx\n", (unsigned long) so->so_ptr, 0, 0);
                        so = LDAP_LIST_NEXT(so, so_link);
                }
        }
 }
+#endif