fixed_size_allocator.hpp

00001 #ifndef SRC_INC_FIXED_SIZE_ALLOCATOR_HPP
00002 #define SRC_INC_FIXED_SIZE_ALLOCATOR_HPP
00003 
00004 #include <common.hpp>
00005 #include <list>
00006 #include <vector>
00007 #include <intrusive_list.hpp>
00008 
00012 namespace GCpp
00013 {
00014 
00015 extern Byte* OS_malloc (size_t p_size);
00016 extern void OS_free (Byte* p_block);
00017 
00020 class GCPP_PUBLIC_SYMBOL FixedSizeAllocator;
00021 
00026 class GCPP_PUBLIC_SYMBOL FixedSizeAllocatorManager
00027 {
00028     private:
00031         static IntrusiveList<FixedSizeAllocator> f_allocators;
00032         
00035         static void register_allocator (FixedSizeAllocator* p_item);
00036         
00039         static void forget_allocator (FixedSizeAllocator* p_item);
00040         
00041     public:
00049         static bool contains (void* p_block);
00050     
00051     friend class FixedSizeAllocator;
00052 };
00053 
00054 class GCPP_PUBLIC_SYMBOL FixedSizeAllocator
00055 {
00056     public:
00059         FixedSizeAllocator* next;
00060         
00063         FixedSizeAllocator* prior;
00064     
00065     private:
00068         size_t f_block_size;
00069         
00072         void* f_last_free;
00073         
00076         void* f_last_malloc;
00077         
00080         std::vector<void*> f_buckets;
00081         
00084         std::list<void*> f_free_list;
00085         
00088         std::list<void*> f_kill_list;
00089         
00092         void* find_allocation_bucket ();
00093         
00096         bool valid_pointer (void* p_ptr);
00097     
00100         void validate_buckets ();
00101     
00104         bool contains (void* p_block);
00105     
00106     protected:
00112         void* internal_malloc ();
00113     
00121         void* internal_add_block ();
00122         
00126         size_t block_size () const
00127         {
00128             return f_block_size;
00129         }
00130     
00131     public:
00139         explicit FixedSizeAllocator (size_t p_block_size, bool p_make_known=true);
00140         
00145         ~FixedSizeAllocator ();
00146         
00154         void* malloc ();
00155         
00161         void free (void* p_block);
00162         
00167         void compact ();
00168         
00169     friend class FixedSizeAllocatorManager;
00170 };
00171 
00172 } // namespace GCpp
00173 
00174 #endif /* SRC_INC_FIXED_SIZE_ALLOCATOR_HPP */

Generated on Thu Dec 27 14:01:59 2007 for GC++ by  doxygen 1.5.4