lib/localmem.c

Go to the documentation of this file.
00001 /* LocalMem.c - local memory routines. 
00002  * 
00003  * These routines are meant for the sort of scenario where
00004  * a lot of little to medium size pieces of memory are
00005  * allocated, and then disposed of all at once.
00006  *
00007  * This file is copyright 2002 Jim Kent, but license is hereby
00008  * granted for all use - public, private or commercial. */
00009 
00010 
00011 #include "common.h"
00012 #include "localmem.h"
00013 
00014 static char const rcsid[] = "$Id: localmem.c,v 1.10 2005/04/11 07:20:03 markd Exp $";
00015 
00016 struct lm
00017     {
00018     struct lmBlock *blocks;
00019     size_t blockSize;
00020     size_t allignMask;
00021     size_t allignAdd;
00022     };
00023 
00024 struct lmBlock
00025     {
00026     struct lmBlock *next;
00027     char *free;
00028     char *end;
00029     char *extra;
00030     };
00031 
00032 static struct lmBlock *newBlock(struct lm *lm, size_t reqSize)
00033 /* Allocate a new block of at least reqSize */
00034 {
00035 size_t size = (reqSize > lm->blockSize ? reqSize : lm->blockSize);
00036 size_t fullSize = size + sizeof(struct lmBlock);
00037 struct lmBlock *mb = needLargeZeroedMem(fullSize);
00038 if (mb == NULL)
00039     errAbort("Couldn't allocate %lld bytes", (long long)fullSize);
00040 mb->free = (char *)(mb+1);
00041 mb->end = ((char *)mb) + fullSize;
00042 mb->next = lm->blocks;
00043 lm->blocks = mb;
00044 return mb;
00045 }
00046 
00047 struct lm *lmInit(int blockSize)
00048 /* Create a local memory pool. */
00049 {
00050 struct lm *lm;
00051 int aliSize = sizeof(long);
00052 if (aliSize < sizeof(double))
00053     aliSize = sizeof(double);
00054 if (aliSize < sizeof(void *))
00055     aliSize = sizeof(void *);
00056 lm = needMem(sizeof(*lm));
00057 lm->blocks = NULL;
00058 if (blockSize <= 0)
00059     blockSize = (1<<14);    /* 16k default. */
00060 lm->blockSize = blockSize;
00061 lm->allignAdd = (aliSize-1);
00062 lm->allignMask = ~lm->allignAdd;
00063 newBlock(lm, blockSize);
00064 return lm;
00065 }
00066 
00067 void lmCleanup(struct lm **pLm)
00068 /* Clean up a local memory pool. */
00069 {
00070     struct lm *lm = *pLm;
00071     if (lm == NULL)
00072         return;
00073     slFreeList(&lm->blocks);
00074     freeMem(lm);
00075     *pLm = NULL;
00076 }
00077 
00078 void *lmAlloc(struct lm *lm, size_t size)
00079 /* Allocate memory from local pool. */
00080 {
00081 struct lmBlock *mb = lm->blocks;
00082 void *ret;
00083 size_t memLeft = mb->end - mb->free;
00084 if (memLeft < size)
00085     mb = newBlock(lm, size);
00086 ret = mb->free;
00087 mb->free += ((size+lm->allignAdd)&lm->allignMask);
00088 if (mb->free > mb->end)
00089     mb->free = mb->end;
00090 return ret;
00091 }
00092 
00093 void *lmCloneMem(struct lm *lm, void *pt, size_t size)
00094 /* Return a local mem copy of memory block. */
00095 {
00096 void *d = lmAlloc(lm, size);
00097 memcpy(d, pt, size);
00098 return d;
00099 }
00100 
00101 char *lmCloneString(struct lm *lm, char *string)
00102 /* Return local mem copy of string. */
00103 {
00104 if (string == NULL)
00105     return NULL;
00106 else
00107     {
00108     int size = strlen(string)+1;
00109     char *s = lmAlloc(lm, size);
00110     memcpy(s, string, size);
00111     return s;
00112     }
00113 }
00114 
00115 struct slName *lmSlName(struct lm *lm, char *name)
00116 /* Return slName in memory. */
00117 {
00118 struct slName *n;
00119 int size = sizeof(*n) + strlen(name) + 1;
00120 n = lmAlloc(lm, size);
00121 strcpy(n->name, name);
00122 return n;
00123 }
00124 

Generated on Tue Dec 25 18:39:31 2007 for blat by  doxygen 1.5.2