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    / /__/ _ \/ _ \/  V \/ _ \/ _ \
00004    \___/_//_/\___/_/_/_/_.__/\___/
00005 */
00006 // CHOMBO Copyright (c) 2000-2004, The Regents of the University of
00007 // California, through Lawrence Berkeley National Laboratory (subject to
00008 // receipt of any required approvals from U.S. Dept. of Energy).  All
00009 // rights reserved.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are met:
00013 //
00014 // (1) Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 // (2) Redistributions in binary form must reproduce the above copyright
00017 // notice, this list of conditions and the following disclaimer in the
00018 // documentation and/or other materials provided with the distribution.
00019 // (3) Neither the name of Lawrence Berkeley National Laboratory, U.S.
00020 // Dept. of Energy nor the names of its contributors may be used to endorse
00021 // or promote products derived from this software without specific prior
00022 // written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035 //
00036 // You are under no obligation whatsoever to provide any bug fixes,
00037 // patches, or upgrades to the features, functionality or performance of
00038 // the source code ("Enhancements") to anyone; however, if you choose to
00039 // make your Enhancements available either publicly, or directly to
00040 // Lawrence Berkeley National Laboratory, without imposing a separate
00041 // written license agreement for such Enhancements, then you hereby grant
00042 // the following license: a non-exclusive, royalty-free perpetual license
00043 // to install, use, modify, prepare derivative works, incorporate into
00044 // other computer software, distribute, and sublicense such Enhancements or
00045 // derivative works thereof, in binary and source code form.
00046 //
00047 // TRADEMARKS. Product and company names mentioned herein may be the
00048 // trademarks of their respective owners.  Any rights not expressly granted
00049 // herein are reserved.
00050 //
00051 
00052 #ifndef _ARENA_H_
00053 #define _ARENA_H_
00054 
00055 #include <cstddef>
00056 #ifdef ENABLE_MEMORY_TRACKING
00057 #include <list>
00058 
00059 class Arena;
00060 typedef std::list<Arena*> ArenaList;
00061 #endif
00062 
00063 
00070 class Arena
00071 {
00072 public:
00074 
00077   Arena();
00078 
00080 
00083   virtual ~Arena();
00084 
00089   virtual void* alloc(size_t a_sz) = 0;
00090 
00094   virtual void free(void* a_pt) = 0;
00095 
00102   static size_t align(size_t a_sz);
00103 
00104   typedef void (*FP)();
00105 
00106 #ifdef ENABLE_MEMORY_TRACKING
00107 
00110   long int bytes;
00111   long int peak;
00112   static ArenaList* arenaList_;
00113   char name_[120];
00115 #endif
00116 
00117 
00118 protected:
00119     //
00120     // Types used by align().
00121     //
00122 #ifndef DOXYGEN
00123 
00124     union Word
00125     {
00126         void*  p;
00127         double d;
00128         long   l;
00129         FP     f;
00130     };
00131 #endif
00132 };
00133 
00134 //
00135 // Inlines.
00136 //
00137 
00138 inline size_t Arena::align (size_t a_s)
00139 {
00140     size_t x = a_s + sizeof(Word) - 1;
00141     x -= x % sizeof(Word);
00142     return x;
00143 }
00144 
00146 
00153 class BArena: public Arena
00154 {
00155 public:
00157 
00161   BArena(const char* a_name = "unnamed");
00162 
00166   virtual void* alloc (size_t a_sz);
00167 
00169   virtual void free (void* a_pt);
00170 };
00171 
00172 #include <cstddef>
00173 #include <set>
00174 #include <vector>
00175 using std::set;
00176 
00178 
00185 class CArena: public Arena
00186 {
00187 public:
00192     CArena(size_t a_hunk_size = 0);
00193 
00195     virtual ~CArena();
00196 
00198     virtual void* alloc(size_t a_nbytes);
00199 
00203     virtual void free(void* a_vp);
00204 
00209     void* calloc(size_t a_nmemb,
00210                  size_t a_size);
00211 
00216     void* realloc (void*  a_ptr,
00217                    size_t a_size);
00218 
00220     enum { DefaultHunkSize = 1024*1024 };
00221 
00222 
00223 protected:
00224     //
00225     // The nodes in our free list and block list.
00226     //
00227 #ifndef DOXYGEN
00228     class Node
00229     {
00230     public:
00231       //
00232       // The default constructor.
00233       //
00234       Node()
00235         :
00236         m_block(0),
00237         m_size(0)
00238       {}
00239       //
00240       // Another constructor.
00241       //
00242       Node(void* a_block, size_t a_size)
00243         :
00244         m_block(a_block),
00245         m_size(a_size)
00246       {}
00247       //
00248       // The copy constructor.
00249       //
00250       Node(const Node& a_rhs)
00251         :
00252         m_block(a_rhs.m_block),
00253         m_size(a_rhs.m_size)
00254       {}
00255       //
00256       // The copy assignment constructor.
00257       //
00258       Node& operator = (const Node& a_rhs)
00259       {
00260           m_block = a_rhs.m_block;
00261           m_size  = a_rhs.m_size;
00262           return *this;
00263       }
00264       //
00265       // The "less-than" operator.
00266       //
00267       bool operator < (const Node& a_rhs) const
00268       {
00269           return m_block < a_rhs.m_block;
00270       }
00271       //
00272       // The equality operator.
00273       //
00274       bool operator == (const Node& a_rhs) const
00275       {
00276           return m_block == a_rhs.m_block;
00277       }
00278       //
00279       // The block address.
00280       //
00281       void* block() const
00282       {
00283         return m_block;
00284       }
00285       //
00286       // Set block address.
00287       //
00288       void block (void* a_blk)
00289       {
00290         m_block = a_blk;
00291       }
00292       //
00293       // The size of the memory block.
00294       //
00295       size_t size() const
00296       {
00297         return m_size;
00298       }
00299       //
00300       // Set size.
00301       //
00302       void size(size_t a_sz)
00303       {
00304         m_size = a_sz;
00305       }
00306 
00307 
00308     private:
00309         //
00310         // The block of memory we reference.
00311         //
00312         void* m_block;
00313         //
00314         // The size of the block we represent.
00315         //
00316         size_t m_size;
00317     };
00318 
00319     //
00320     // The type of our freelist and blocklist.
00321     // We use a set sorted from lo to hi memory addresses.
00322     //
00323     typedef set < Node > NL;
00324 
00325     //
00326     // The list of blocks allocated via ::operator new().
00327     //
00328     std::vector<void*> m_alloc;
00329 
00330     //
00331     // The free list of allocated but not currently used blocks.
00332     // Maintained in lo to hi memory sorted order.
00333     //
00334     NL m_freelist;
00335 
00336     //
00337     // The list of busy blocks.
00338     // A block is either on the freelist or on the blocklist, but not on both.
00339     //
00340     NL m_busylist;
00341 
00342     //
00343     // The minimal size of hunks to request via ::operator new().
00344     //
00345     size_t m_hunk;
00346 #endif
00347 
00348 
00349 private:
00350     //
00351     // Disallowed.
00352     //
00353     CArena (const CArena& a_rhs);
00354     CArena& operator= (const CArena& a_rhs);
00355 };
00356 
00357 //
00358 // The Arena used by BaseFab code.
00359 //
00360 extern Arena* The_FAB_Arena;
00361 
00362 #endif /*CH_ARENA*/

Generated on Wed Jan 19 17:51:22 2005 for Chombo&INSwithParticles by doxygen1.2.16