Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_salloc.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_salloc.c,v 12.4 2005/10/15 00:51:56 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #endif
00018 
00019 #include "db_int.h"
00020 
00021 /*
00022  * Implement shared memory region allocation, using simple first-fit algorithm.
00023  * The model is that we take a "chunk" of shared memory store and begin carving
00024  * it up into areas, similarly to how malloc works.  We do coalescing on free.
00025  *
00026  * The "len" field in the __data struct contains the length of the free region
00027  * (less the size_t bytes that holds the length).  We use the address provided
00028  * by the caller to find this length, which allows us to free a chunk without
00029  * requiring that the caller pass in the length of the chunk they're freeing.
00030  */
00031 SH_LIST_HEAD(__head);
00032 struct __data {
00033         size_t len;
00034         SH_LIST_ENTRY links;
00035 };
00036 
00037 #define ILLEGAL_SIZE    1                       /* An illegal size. */
00038 
00039 /*
00040  * __db_shalloc_init --
00041  *      Initialize the area as one large chunk.
00042  *
00043  * PUBLIC: void __db_shalloc_init __P((REGINFO *, size_t));
00044  */
00045 void
00046 __db_shalloc_init(infop, size)
00047         REGINFO *infop;
00048         size_t size;
00049 {
00050         struct __data *elp;
00051         struct __head *hp;
00052 
00053         /* No initialization needed for heap memory regions. */
00054         if (F_ISSET(infop->dbenv, DB_ENV_PRIVATE))
00055                 return;
00056 
00057         hp = infop->addr;
00058         SH_LIST_INIT(hp);
00059 
00060         elp = (struct __data *)(hp + 1);
00061         elp->len = (size - sizeof(struct __head)) - sizeof(elp->len);
00062         SH_LIST_INSERT_HEAD(hp, elp, links, __data);
00063 }
00064 
00065 /*
00066  * __db_shalloc_size --
00067  *      Return the space needed for an allocation, including alignment.
00068  *
00069  * PUBLIC: size_t __db_shalloc_size __P((size_t, size_t));
00070  */
00071 size_t
00072 __db_shalloc_size(len, align)
00073         size_t len, align;
00074 {
00075         /* Never allocate less than the size of a struct __data. */
00076         if (len < sizeof(struct __data))
00077                 len = sizeof(struct __data);
00078 
00079 #ifdef DIAGNOSTIC
00080         /* Add room for a guard byte. */
00081         ++len;
00082 #endif
00083 
00084         /* Never align to less than a uintmax_t boundary. */
00085         if (align <= sizeof(uintmax_t))
00086                 align = sizeof(uintmax_t);
00087 
00088         return ((size_t)DB_ALIGN(len, align) + sizeof(struct __data));
00089 }
00090 
00091 /*
00092  * __db_shalloc --
00093  *      Allocate space from the shared region.
00094  *
00095  * PUBLIC: int __db_shalloc __P((REGINFO *, size_t, size_t, void *));
00096  */
00097 int
00098 __db_shalloc(infop, len, align, retp)
00099         REGINFO *infop;
00100         size_t len, align;
00101         void *retp;
00102 {
00103         DB_ENV *dbenv;
00104         struct __data *elp;
00105         size_t *sp;
00106         int ret;
00107         void *p, *rp;
00108 
00109         dbenv = infop->dbenv;
00110 
00111         /* Never align to less than a uintmax_t boundary. */
00112         if (align <= sizeof(uintmax_t))
00113                 align = sizeof(uintmax_t);
00114 
00115         /* In a private region, we call malloc for additional space. */
00116         if (F_ISSET(dbenv, DB_ENV_PRIVATE)) {
00117                 /* Check to see if we're over our limit. */
00118                 if (infop->allocated >= infop->max_alloc)
00119                         return (ENOMEM);
00120 
00121                 /* Add enough room for a size. */
00122                 len += sizeof(size_t);
00123 
00124                 /* Add enough room to guarantee alignment is possible. */
00125                 len += align - 1;
00126 
00127                 /* Allocate the space. */
00128                 if ((ret = __os_malloc(dbenv, len, &p)) != 0)
00129                         return (ret);
00130                 infop->allocated += len;
00131 
00132                 /* Store the size. */
00133                 sp = p;
00134                 *sp++ = len;
00135 
00136                 /* Find the aligned location. */
00137                 *(void **)retp = rp = ALIGNP_INC(sp, align);
00138 
00139                 /* Fill in any gaps with illegal sizes. */
00140                 for (; (void *)sp < rp; ++sp)
00141                         *sp = ILLEGAL_SIZE;
00142 
00143                 return (0);
00144         }
00145 
00146         /* Never allocate less than the size of a struct __data. */
00147         if (len < sizeof(struct __data))
00148                 len = sizeof(struct __data);
00149 
00150 #ifdef DIAGNOSTIC
00151         /* Add room for a guard byte. */
00152         ++len;
00153 #endif
00154 
00155         p = infop->addr;
00156 
00157         /* Walk the list, looking for a slot. */
00158         for (elp = SH_LIST_FIRST((struct __head *)p, __data);
00159             elp != NULL;
00160             elp = SH_LIST_NEXT(elp, links, __data)) {
00161                 /*
00162                  * Skip chunks that are too small to work.  This avoids address
00163                  * wrap-around in the subsequent calculations (if len were too
00164                  * large).
00165                  */
00166                 if (elp->len < len)
00167                         continue;
00168 
00169                 /*
00170                  * Calculate the value of the returned pointer if we were to
00171                  * use this chunk.
00172                  *      + Find the end of the chunk.
00173                  *      + Subtract the memory the user wants.
00174                  *      + Find the closest previous correctly-aligned address.
00175                  */
00176                 rp = (u_int8_t *)elp + sizeof(size_t) + elp->len;
00177                 rp = (u_int8_t *)rp - len;
00178                 rp = ALIGNP_DEC(rp, align);
00179 
00180                 /*
00181                  * Rp may now point before elp->links, in which case the chunk
00182                  * was too small, and we have to try again.
00183                  */
00184                 if ((u_int8_t *)rp < (u_int8_t *)&elp->links)
00185                         continue;
00186 
00187                 *(void **)retp = rp;
00188 #ifdef DIAGNOSTIC
00189                 /*
00190                  * At this point, whether or not we still need to split up a
00191                  * chunk, retp is the address of the region we are returning,
00192                  * and (u_int8_t *)elp + sizeof(size_t) + elp->len gives us
00193                  * the address of the first byte after the end of the chunk.
00194                  * Make the byte immediately before that the guard byte.
00195                  */
00196                 *((u_int8_t *)elp + sizeof(size_t) + elp->len - 1) = GUARD_BYTE;
00197 #endif
00198 
00199 #define SHALLOC_FRAGMENT        32
00200                 /*
00201                  * If there are at least SHALLOC_FRAGMENT additional bytes of
00202                  * memory, divide the chunk into two chunks.
00203                  */
00204                 if ((u_int8_t *)rp >=
00205                     (u_int8_t *)&elp->links + SHALLOC_FRAGMENT) {
00206                         sp = rp;
00207                         *--sp = elp->len -
00208                             ((u_int8_t *)rp - (u_int8_t *)&elp->links);
00209                         elp->len -= *sp + sizeof(size_t);
00210                         return (0);
00211                 }
00212 
00213                 /*
00214                  * Otherwise, we return the entire chunk, wasting some amount
00215                  * of space to keep the list compact.  However, because the
00216                  * address we're returning to the user may not be the address
00217                  * of the start of the region for alignment reasons, set the
00218                  * size_t length fields back to the "real" length field to a
00219                  * flag value, so that we can find the real length during free.
00220                  */
00221                 SH_LIST_REMOVE(elp, links, __data);
00222                 for (sp = rp; (u_int8_t *)--sp >= (u_int8_t *)&elp->links;)
00223                         *sp = ILLEGAL_SIZE;
00224                 return (0);
00225         }
00226 
00227         return (ENOMEM);
00228 }
00229 
00230 /*
00231  * __db_shalloc_free --
00232  *      Free space into the shared region.
00233  *
00234  * PUBLIC: void __db_shalloc_free __P((REGINFO *, void *));
00235  */
00236 void
00237 __db_shalloc_free(infop, ptr)
00238         REGINFO *infop;
00239         void *ptr;
00240 {
00241         DB_ENV *dbenv;
00242         struct __data *elp, *lastp, *newp;
00243         struct __head *hp;
00244         size_t free_size, *sp;
00245         int merged;
00246 
00247         dbenv = infop->dbenv;
00248 
00249         /*
00250          * Step back over flagged length fields to find the beginning of
00251          * the object and its real size.
00252          */
00253         for (sp = (size_t *)ptr; sp[-1] == ILLEGAL_SIZE; --sp)
00254                 ;
00255         ptr = sp;
00256 
00257         newp = (struct __data *)((u_int8_t *)ptr - sizeof(size_t));
00258         free_size = newp->len;
00259 
00260         /* In a private region, we call free. */
00261         if (F_ISSET(dbenv, DB_ENV_PRIVATE)) {
00262                 DB_ASSERT(infop->allocated >= free_size);
00263                 infop->allocated -= free_size;
00264 
00265                 __os_free(dbenv, newp);
00266                 return;
00267         }
00268 
00269 #ifdef DIAGNOSTIC
00270         /*
00271          * The "real size" includes the guard byte;  it's just the last
00272          * byte in the chunk, and the caller never knew it existed.
00273          *
00274          * Check it to make sure it hasn't been stomped.
00275          */
00276         if (*((u_int8_t *)ptr + free_size - 1) != GUARD_BYTE) {
00277                 /*
00278                  * Eventually, once we push a DB_ENV handle down to these
00279                  * routines, we should use the standard output channels.
00280                  */
00281                 fprintf(stderr,
00282                     "Guard byte incorrect during shared memory free.\n");
00283                 abort();
00284                 /* NOTREACHED */
00285         }
00286 
00287         /* Trash the returned memory (including guard byte). */
00288         memset(ptr, CLEAR_BYTE, free_size);
00289 #endif
00290         /*
00291          * Walk the list, looking for where this entry goes.
00292          *
00293          * We keep the free list sorted by address so that coalescing is
00294          * trivial.
00295          *
00296          * XXX
00297          * Probably worth profiling this to see how expensive it is.
00298          */
00299         hp = (struct __head *)(infop->addr);
00300         for (elp = SH_LIST_FIRST(hp, __data), lastp = NULL;
00301             elp != NULL && (void *)elp < (void *)ptr;
00302             lastp = elp, elp = SH_LIST_NEXT(elp, links, __data))
00303                 ;
00304 
00305         /*
00306          * Elp is either NULL (we reached the end of the list), or the slot
00307          * after the one that's being returned.  Lastp is either NULL (we're
00308          * returning the first element of the list) or the element before the
00309          * one being returned.
00310          *
00311          * Check for coalescing with the next element.
00312          */
00313         merged = 0;
00314         if ((u_int8_t *)ptr + free_size == (u_int8_t *)elp) {
00315                 newp->len += elp->len + sizeof(size_t);
00316                 SH_LIST_REMOVE(elp, links, __data);
00317                 if (lastp != NULL)
00318                         SH_LIST_INSERT_AFTER(lastp, newp, links, __data);
00319                 else
00320                         SH_LIST_INSERT_HEAD(hp, newp, links, __data);
00321                 merged = 1;
00322         }
00323 
00324         /* Check for coalescing with the previous element. */
00325         if (lastp != NULL && (u_int8_t *)lastp +
00326             lastp->len + sizeof(size_t) == (u_int8_t *)newp) {
00327                 lastp->len += newp->len + sizeof(size_t);
00328 
00329                 /*
00330                  * If we have already put the new element into the list take
00331                  * it back off again because it's just been merged with the
00332                  * previous element.
00333                  */
00334                 if (merged)
00335                         SH_LIST_REMOVE(newp, links, __data);
00336                 merged = 1;
00337         }
00338 
00339         if (!merged) {
00340                 if (lastp == NULL)
00341                         SH_LIST_INSERT_HEAD(hp, newp, links, __data);
00342                 else
00343                         SH_LIST_INSERT_AFTER(lastp, newp, links, __data);
00344         }
00345 }
00346 
00347 /*
00348  * __db_shalloc_sizeof --
00349  *      Return the size of a shalloc'd piece of memory.
00350  *
00351  * !!!
00352  * Note that this is from an internal standpoint -- it includes not only
00353  * the size of the memory being used, but also the extra alignment bytes
00354  * in front and, #ifdef DIAGNOSTIC, the guard byte at the end.
00355  *
00356  * PUBLIC: size_t __db_shalloc_sizeof __P((void *));
00357  */
00358 size_t
00359 __db_shalloc_sizeof(ptr)
00360         void *ptr;
00361 {
00362         struct __data *elp;
00363         size_t *sp;
00364 
00365         /*
00366          * Step back over flagged length fields to find the beginning of
00367          * the object and its real size.
00368          */
00369         for (sp = (size_t *)ptr; sp[-1] == ILLEGAL_SIZE; --sp)
00370                 ;
00371 
00372         elp = (struct __data *)((u_int8_t *)sp - sizeof(size_t));
00373         return (elp->len);
00374 }

Generated on Sun Dec 25 12:14:24 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2