Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

Arena.H

Go to the documentation of this file.
00001 /* _______              __
00002   / ___/ /  ___  __ _  / /  ___
00003  / /__/ _ \/ _ \/  ' \/ _ \/ _ \
00004  \___/_//_/\___/_/_/_/_.__/\___/ 
00005 */
00006 //
00007 // This software is copyright (C) by the Lawrence Berkeley
00008 // National Laboratory.  Permission is granted to reproduce
00009 // this software for non-commercial purposes provided that
00010 // this notice is left intact.
00011 // 
00012 // It is acknowledged that the U.S. Government has rights to
00013 // this software under Contract DE-AC03-765F00098 between
00014 // the U.S.  Department of Energy and the University of
00015 // California.
00016 //
00017 // This software is provided as a professional and academic
00018 // contribution for joint exchange. Thus it is experimental,
00019 // is provided ``as is'', with no warranties of any kind
00020 // whatsoever, no support, no promise of updates, or printed
00021 // documentation. By using this software, you acknowledge
00022 // that the Lawrence Berkeley National Laboratory and
00023 // Regents of the University of California shall have no
00024 // liability with respect to the infringement of other
00025 // copyrights by any part of this software.
00026 //
00027 
00028 #ifndef CH_ARENA
00029 #define CH_ARENA
00030 
00031 //
00032 // $Id: Arena.H,v 1.9 2003/07/24 22:41:15 bvs Exp $
00033 //
00034 
00035 #include <cstddef>
00036 #include <list>
00037 
00038 class Arena;
00039 typedef std::list<Arena*> ArenaList;
00040 
00042 
00049 class Arena
00050 {
00051 public:
00053 
00055   Arena();
00056 
00058 
00060   virtual ~Arena();
00061 
00065     virtual void* alloc (size_t sz) = 0;
00066 
00070     virtual void free (void* pt) = 0;
00071 
00077     static size_t align (size_t sz);
00078     typedef void (*FP) ();
00079 
00080 #ifdef ENABLE_MEMORY_TRACKING  
00081 
00083 
00084   long int bytes;
00085   long int peak;
00086   static ArenaList* arenaList_;
00087   char name_[120];
00090 #endif
00091 protected:
00092     //
00093     // Types used by align().
00094     //
00095 
00096 #ifndef DOXYGEN
00097 
00098     union Word
00099     {
00100         void*  p;
00101         double d;
00102         long   l;
00103         FP     f;
00104     };
00105 #endif
00106 };
00107 
00108 //
00109 // Inlines.
00110 //
00111 
00112 inline
00113 size_t
00114 Arena::align (size_t s)
00115 {
00116     size_t x = s + sizeof(Word) - 1;
00117     x -= x%sizeof(Word);
00118     return x;
00119 }
00120 
00121 
00123 
00130 class BArena
00131     :
00132     public Arena
00133 {
00134 public:
00135 
00137 
00141   BArena(const char* name = "unnamed");
00145     virtual void* alloc (size_t sz);
00146     //
00148     virtual void free (void* pt);
00149 };
00150 
00151 #include <cstddef>
00152 
00153 #include <set>
00154 #include <vector>
00155 using std::set;
00156 
00157 
00158 
00160 
00167 class CArena
00168     :
00169     public Arena
00170 {
00171 public:
00172 
00177     CArena (size_t hunk_size = 0);
00178     //
00180     //
00181     virtual ~CArena ();
00182     //
00184     //
00185     virtual void* alloc (size_t nbytes);
00186 
00190     virtual void free (void* ap);
00191 
00196     void* calloc (size_t nmemb, size_t size);
00197 
00202     void* realloc (void* ptr, size_t size);
00203     //
00205     enum { DefaultHunkSize = 1024*1024 };
00206 
00207 protected:
00208     //
00209     // The nodes in our free list and block list.
00210     //
00211 #ifndef DOXYGEN
00212     class Node
00213     {
00214     public:
00215         //
00216         // The default constructor.
00217         //
00218         Node ()
00219             :
00220             m_block(0), m_size(0) {}
00221         //
00222         // Another constructor.
00223         //
00224         Node (void* block, size_t size)
00225             :
00226             m_block(block), m_size(size) {}
00227         //
00228         // The copy constructor.
00229         //
00230         Node (const Node& rhs)
00231             :
00232             m_block(rhs.m_block), m_size(rhs.m_size) {}
00233         //
00234         // The copy assignment constructor.
00235         //
00236         Node& operator= (const Node& rhs)
00237         {
00238             m_block = rhs.m_block;
00239             m_size  = rhs.m_size;
00240             return *this;
00241         }
00242         //
00243         // The "less-than" operator.
00244         //
00245         bool operator< (const Node& rhs) const
00246         {
00247             return m_block < rhs.m_block;
00248         }
00249         //
00250         // The equality operator. 
00251         //
00252         bool operator== (const Node& rhs) const
00253         {
00254             return m_block == rhs.m_block;
00255         }
00256         //
00257         // The block address.
00258         //
00259         void* block () const { return m_block; }
00260         //
00261         // Set block address.
00262         //
00263         void block (void* blk) { m_block = blk; }
00264         //
00265         // The size of the memory block.
00266         //
00267         size_t size () const { return m_size; }
00268         //
00269         // Set size.
00270         //
00271         void size (size_t sz) { m_size = sz; }
00272 
00273     private:
00274         //
00275         // The block of memory we reference.
00276         //
00277         void* m_block;
00278         //
00279         // The size of the block we represent.
00280         //
00281         size_t m_size;
00282     };
00283 
00284     //
00285     // The type of our freelist and blocklist.
00286     // We use a set sorted from lo to hi memory addresses.
00287     //
00288     typedef set < Node > NL;
00289     //
00290     // The list of blocks allocated via ::operator new().
00291     //
00292     std::vector<void*> m_alloc;
00293     //
00294     // The free list of allocated but not currently used blocks.
00295     // Maintained in lo to hi memory sorted order.
00296     //
00297     NL m_freelist;
00298     //
00299     // The list of busy blocks.
00300     // A block is either on the freelist or on the blocklist, but not on both.
00301     //
00302     NL m_busylist;
00303     //
00304     // The minimal size of hunks to request via ::operator new().
00305     //
00306     size_t m_hunk;
00307 
00308 #endif
00309 
00310 private:
00311     //
00312     // Disallowed.
00313     //
00314     CArena (const CArena& rhs);
00315     CArena& operator= (const CArena& rhs);
00316 };
00317 
00318 //
00319 // The Arena used by BaseFab code.
00320 //
00321 extern Arena* The_FAB_Arena;
00322 
00323 
00324 #endif /*CH_ARENA*/

Generated on Wed Jun 2 13:53:31 2004 for Chombo&INSwithParticles by doxygen 1.3.2