usermode/library/malloc-original/src/wrapper.cpp

00001 
00002 //
00003 // The Hoard Multiprocessor Memory Allocator
00004 // www.hoard.org
00005 //
00006 // Author: Emery Berger, http://www.cs.utexas.edu/users/emery
00007 //
00008 // Copyright (c) 1998-2001, The University of Texas at Austin.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Library General Public License as
00012 // published by the Free Software Foundation, http://www.fsf.org.
00013 //
00014 // This library is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Library General Public License for more details.
00018 //
00020 
00021 /*
00022   wrapper.cpp
00023   ------------------------------------------------------------------------
00024   Implementations of malloc(), free(), etc. in terms of hoard.
00025   This lets us link in hoard in place of the stock malloc.
00026   ------------------------------------------------------------------------
00027   @(#) $Id: wrapper.cpp,v 1.40 2001/12/10 20:11:29 emery Exp $
00028   ------------------------------------------------------------------------
00029   Emery Berger                    | <http://www.cs.utexas.edu/users/emery>
00030   Department of Computer Sciences |             <http://www.cs.utexas.edu>
00031   University of Texas at Austin   |                <http://www.utexas.edu>
00032   ========================================================================
00033 */
00034 
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #if !defined(WIN32)
00038 #include <strings.h>
00039 #endif
00040 #if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x420
00041 #include <new>
00042 #endif
00043 #include "config.h"
00044 
00045 #include "threadheap.h"
00046 #include "processheap.h"
00047 #include "arch-specific.h"
00048 
00049 //
00050 // Access exactly one instance of the process heap
00051 // (which contains the thread heaps).
00052 // We create this object dynamically to avoid bloating the object code.
00053 //
00054 
00055 inline static processHeap * getAllocator (void) {
00056   static char * buf = (char *) hoardGetMemory (sizeof(processHeap));
00057   static processHeap * theAllocator = new (buf) processHeap;
00058   return theAllocator;
00059 }
00060 
00061 #define HOARD_MALLOC(x) pmalloc(x)
00062 #define HOARD_FREE(x) pfree(x)
00063 #define HOARD_REALLOC(x,y) prealloc(x,y)
00064 #define HOARD_CALLOC(x,y) pcalloc(x,y)
00065 #define HOARD_MEMALIGN(x,y) pmemalign(x,y)
00066 #define HOARD_VALLOC(x) pvalloc(x)
00067 #define HOARD_GET_USABLE_SIZE(x) pmalloc_usable_size(x)
00068 
00069 extern "C" void * HOARD_MALLOC(size_t);
00070 extern "C" void HOARD_FREE(void *);
00071 extern "C" void * HOARD_REALLOC(void *, size_t);
00072 extern "C" void * HOARD_CALLOC(size_t, size_t);
00073 extern "C" void * HOARD_MEMALIGN(size_t, size_t);
00074 extern "C" void * HOARD_VALLOC(size_t);
00075 extern "C" size_t HOARD_GET_USABLE_SIZE(void *);
00076 
00077 
00078 extern "C" void * HOARD_MALLOC (size_t sz)
00079 {
00080   static processHeap * pHeap = getAllocator();
00081   if (sz == 0) {
00082           sz = 1;
00083   }
00084   void * addr = pHeap->getHeap(pHeap->getHeapIndex()).malloc (sz);
00085   return addr;
00086 }
00087 
00088 extern "C" void * HOARD_CALLOC (size_t nelem, size_t elsize)
00089 {
00090   static processHeap * pHeap = getAllocator();
00091   size_t sz = nelem * elsize;
00092   if (sz == 0) {
00093           sz = 1;
00094   }
00095   void * ptr = pHeap->getHeap(pHeap->getHeapIndex()).malloc (sz);
00096   // Zero out the malloc'd block.
00097   memset (ptr, 0, sz);
00098   return ptr;
00099 }
00100 
00101 extern "C" void HOARD_FREE (void * ptr)
00102 {
00103   static processHeap * pHeap = getAllocator();
00104   pHeap->free (ptr);
00105 }
00106 
00107 
00108 extern "C" void * HOARD_MEMALIGN (size_t alignment, size_t size)
00109 {
00110   static processHeap * pHeap = getAllocator();
00111   void * addr = pHeap->getHeap(pHeap->getHeapIndex()).memalign (alignment, size);
00112   return addr;
00113 }
00114 
00115 
00116 extern "C" void * HOARD_VALLOC (size_t size)
00117 {
00118   return HOARD_MEMALIGN (hoardGetPageSize(), size);
00119 }
00120 
00121 
00122 extern "C" void * HOARD_REALLOC (void * ptr, size_t sz)
00123 {
00124   if (ptr == NULL) {
00125     return HOARD_MALLOC (sz);
00126   }
00127   if (sz == 0) {
00128     HOARD_FREE (ptr);
00129     return NULL;
00130   }
00131 
00132   // If the existing object can hold the new size,
00133   // just return it.
00134 
00135   size_t objSize = threadHeap::objectSize (ptr);
00136 
00137   if (objSize >= sz) {
00138     return ptr;
00139   }
00140 
00141   // Allocate a new block of size sz.
00142 
00143   void * buf = HOARD_MALLOC (sz);
00144 
00145   // Copy the contents of the original object
00146   // up to the size of the new block.
00147 
00148   size_t minSize = (objSize < sz) ? objSize : sz;
00149   memcpy (buf, ptr, minSize);
00150 
00151   // Free the old block.
00152 
00153   HOARD_FREE (ptr);
00154 
00155   // Return a pointer to the new one.
00156 
00157   return buf;
00158 }
00159 
00160 extern "C" size_t HOARD_GET_USABLE_SIZE (void * ptr)
00161 {
00162   return threadHeap::objectSize (ptr);
00163 }
00164 
00165 
00166 #if 0
00167 extern "C" void malloc_stats (void)
00168 {
00169   TheWrapper.TheAllocator()->stats();
00170 }
00171 #endif

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7