Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

vnl_alloc.h

Go to the documentation of this file.
00001 #ifndef vnl_alloc_h_
00002 #define vnl_alloc_h_
00003 
00004 // This is vxl/vnl/vnl_alloc.h
00005 
00006 //:
00007 // \file
00008 // \author unknown
00009 //
00010 // Default node allocator.
00011 // With a reasonable compiler, this should be roughly as fast as the
00012 // original STL class-specific allocators, but with less fragmentation.
00013 // Default_alloc_template parameters are experimental and MAY
00014 // DISAPPEAR in the future.  Clients should just use vcl_alloc for now.
00015 //
00016 // Important implementation properties:
00017 // 1. If the client request an object of size > __MAX_BYTES, the resulting
00018 //    object will be obtained directly from malloc.
00019 // 2. In all other cases, we allocate an object of size exactly
00020 //    ROUND_UP(requested_size).  Thus the client has enough size
00021 //    information that we can return the object to the proper free li*st
00022 //    without permanently losing part of the object.
00023 //
00024 //
00025 // The first template parameter specifies whether more than one thread
00026 // may use this allocator.  It is safe to allocate an object from
00027 // one instance of a default_alloc and deallocate it with another
00028 // one.  This effectively transfers its ownership to the second one.
00029 // This may have undesirable effects on reference locality.
00030 // The second parameter is unreferenced and serves only to allow the
00031 // creation of multiple default_alloc instances.
00032 // Node that containers built on different allocator instances have
00033 // different types, limiting the utility of this approach.
00034 
00035 #include <vcl_cstddef.h> // size_t lives here, not in <cstdlib>
00036 
00037 const int VNL_ALLOC_ALIGN = 8;
00038 const vcl_size_t VNL_ALLOC_MAX_BYTES = 256;
00039 const vcl_size_t VNL_ALLOC_NFREELISTS = VNL_ALLOC_MAX_BYTES/VNL_ALLOC_ALIGN;
00040 
00041 class vnl_alloc {
00042   
00043 private:
00044   static vcl_size_t ROUND_UP(vcl_size_t bytes) {
00045     return (((bytes) + VNL_ALLOC_ALIGN-1) & ~(VNL_ALLOC_ALIGN - 1));
00046   }
00047 private:
00048   union obj;
00049   friend union obj;
00050   union obj {
00051     union obj * free_list_link;
00052     char client_data[1];    /* The client sees this.        */
00053   };
00054 private:
00055 # if defined ( __SUNPRO_CC ) || defined ( _AIX )
00056   static obj * free_list[]; 
00057   // Specifying a size results in duplicate def for 4.1
00058 # else
00059   static obj * free_list[VNL_ALLOC_NFREELISTS]; 
00060 # endif
00061   static  vcl_size_t FREELIST_INDEX(vcl_size_t bytes) {
00062     return (((bytes) + VNL_ALLOC_ALIGN-1)/VNL_ALLOC_ALIGN - 1);
00063   }
00064     
00065   // Returns an object of size n, and optionally adds to size n free li*st.
00066   static void *refill(vcl_size_t n);
00067   // Allocates a chunk for nobjs of size size.  nobjs may be reduced
00068   // if it is inconvenient to allocate the requested number.
00069   static char *chunk_alloc(vcl_size_t size, int &nobjs);
00070   
00071   // Chunk allocation state.
00072   static char *start_free;
00073   static char *end_free;
00074   static vcl_size_t heap_size;
00075 
00076   class lock {
00077   public:
00078     lock() { }
00079     ~lock() { }
00080   };
00081   friend class lock;
00082   
00083 public:
00084   // this one is needed for proper vcl_simple_alloc wrapping
00085   typedef char value_type;
00086     
00087   /* n must be > 0      */
00088   static void * allocate(vcl_size_t n) {
00089     obj * * my_free_list;
00090     obj *  result;
00091     
00092     if (n > VNL_ALLOC_MAX_BYTES) {
00093       return (void*)new char[n];
00094     }
00095     my_free_list = free_list + FREELIST_INDEX(n);
00096     // Acquire the lock here with a constructor call.
00097     // This ensures that it is released in exit or during stack
00098     // unwinding.
00099     result = *my_free_list;
00100     if (result == 0) {
00101       void *r = refill(ROUND_UP(n));
00102       return r;
00103     }
00104     *my_free_list = result -> free_list_link;
00105     return (result);
00106   };
00107     
00108   /* p may not be 0 */
00109   static void deallocate(void *p, vcl_size_t n)
00110   {
00111     obj *q = (obj *)p;
00112     obj *  * my_free_list;
00113     
00114     if (n > VNL_ALLOC_MAX_BYTES) {
00115       delete [] (char*)p;
00116       return;
00117     }
00118     my_free_list = free_list + FREELIST_INDEX(n);
00119     q -> free_list_link = *my_free_list;
00120     *my_free_list = q;
00121   }
00122     
00123   static void * reallocate(void *p, vcl_size_t old_sz, vcl_size_t new_sz);
00124 };
00125     
00126 # endif // vnl_alloc_h_

Generated at Wed Mar 12 01:13:14 2003 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000