"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/heaplayers/ansiwrapper.h" (27 May 2005, 1952 Bytes) of package /linux/misc/old/heaplayers_3_5_1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ansiwrapper.h" see the Fossies "Dox" file reference documentation.

    1 /* -*- C++ -*- */
    2 
    3 #ifndef _ANSIWRAPPER_H_
    4 #define _ANSIWRAPPER_H_
    5 
    6 #include <stdlib.h>
    7 
    8 /*
    9  * @class ANSIWrapper
   10  * @brief Provide ANSI C behavior for malloc & free.
   11  *
   12  * Implements all prescribed ANSI behavior, including zero-sized
   13  * requests & aligned request sizes to a double word (or long word).
   14  */
   15 
   16 namespace HL {
   17 
   18 template <class SuperHeap>
   19 class ANSIWrapper : public SuperHeap {
   20 private:
   21   int inside;
   22 public:
   23   
   24   ANSIWrapper (void)
   25     : inside (0)
   26     {}
   27 
   28   inline void * malloc (size_t sz) {
   29     if (sz < 2 * sizeof(size_t)) {
   30       // Make sure it's at least big enough to hold two pointers (and
   31       // conveniently enough, that's at least the size of a double, as
   32       // required by ANSI).
   33       sz = 2 * sizeof(size_t);
   34     }
   35     sz = align(sz);
   36     void * ptr = SuperHeap::malloc (sz);
   37     return ptr;
   38   }
   39  
   40   inline void free (void * ptr) {
   41     if (ptr != 0) {
   42       SuperHeap::free (ptr);
   43     }
   44   }
   45 
   46   inline void * calloc (const size_t s1, const size_t s2) {
   47     void * ptr = malloc (s1 * s2);
   48     if (ptr) {
   49       memset (ptr, 0, s1 * s2);
   50     }
   51     return ptr;
   52   }
   53   
   54   inline void * realloc (void * ptr, const size_t sz) {
   55     if (ptr == 0) {
   56       return malloc (sz);
   57     }
   58     if (sz == 0) {
   59       free (ptr);
   60       return 0;
   61     }
   62 
   63     size_t objSize = getSize (ptr);
   64     if (objSize == sz) {
   65       return ptr;
   66     }
   67 
   68     // Allocate a new block of size sz.
   69     void * buf = malloc (sz);
   70 
   71     // Copy the contents of the original object
   72     // up to the size of the new block.
   73 
   74     size_t minSize = (objSize < sz) ? objSize : sz;
   75     if (buf) {
   76       memcpy (buf, ptr, minSize);
   77     }
   78 
   79     // Free the old block.
   80     free (ptr);
   81     return buf;
   82   }
   83   
   84   inline size_t getSize (void * ptr) {
   85     if (ptr) {
   86       return SuperHeap::getSize (ptr);
   87     } else {
   88       return 0;
   89     }
   90   }
   91 
   92 private:
   93   inline static size_t align (size_t sz) {
   94     return (sz + (sizeof(double) - 1)) & ~(sizeof(double) - 1);
   95   }
   96 };
   97 
   98 }
   99 
  100 #endif