00001 
00002 
00003 #ifndef _CORE_MEMORY_HEAP_HPP
00004 #define _CORE_MEMORY_HEAP_HPP
00005 
00006 
00007 #include "src/ipc/lock.hpp"
00008 #include "src/common/blist.hpp"
00009 #include "src/memory/zone.hpp"
00010 
00011 
00012 const uint32 FAC_OWN    = 0x001f;       
00013 const uint32 FAC_TYPE   = 0x07e0;       
00014 const uint32 FAC_MIDDLE = 0x7800;       
00015 
00016 const uint32 FAC_NULL   = 0x0000;       
00017 const uint32 FAC_UNDEF  = 0x0001;       
00018 const uint32 FAC_OTHER  = 0x0002;       
00019 const uint32 FAC_MEMORY = 0x0003;       
00020 const uint32 FAC_INTERR = 0x0004;       
00021 const uint32 FAC_THREAD = 0x0005;       
00022 const uint32 FAC_MSG    = 0x0006;       
00023 const uint32 FAC_MMAP   = 0x0007;       
00024 const uint32 FAC_SYMS   = 0x0008;       
00025 
00026 const uint32 FAC_VMEM           = 0x0020;       
00027 const uint32 FAC_USEDBLOCKS     = 0x0020;       
00028 const uint32 FAC_PGD            = 0x0040;       
00029 const uint32 FAC_IDT            = 0x0020;       
00030 const uint32 FAC_EXCLIST        = 0x0040;       
00031 const uint32 FAC_THRLIST        = 0x0020;       
00032 const uint32 FAC_TSP            = 0x0040;       
00033 const uint32 FAC_KSP            = 0x0060;       
00034 const uint32 FAC_ISP            = 0x0080;       
00035 const uint32 FAC_THRSLEEP       = 0x00a0;       
00036 const uint32 FAC_THRRAN         = 0x00c0;       
00037 const uint32 FAC_MESSAGE        = 0x0020;       
00038 const uint32 FAC_MSGLIST        = 0x0040;       
00039 const uint32 FAC_MEMREG         = 0x0020;       
00040 const uint32 FAC_SYMTAB         = 0x0020;       
00041 const uint32 FAC_STRTAB         = 0x0040;       
00042 const uint32 FAC_QSORTBUF       = 0x0020;       
00043 const uint32 FAC_MANGLE         = 0x0040;       
00044 const uint32 FAC_MANGLE2        = 0x0040;       
00045 
00046 const uint32 FAC_BLIST          = 0x0800;       
00047 const uint32 FAC_BLIST_BK       = 0x1000;       
00048 const uint32 FAC_BLIST_PTR      = 0x1800;       
00049 
00050 namespace Memory {
00051 namespace Heap {
00052 
00053 const uint32 heap_magic         = 0xcafebabe;
00054 const uint32 HEAP_GRAN          = 0x1;  
00055 const uint32 HEAP_STACK_GRAN    = 0x10000;
00056 const uint32 HEAP_TBLE          = 0x20; 
00057 const uint32 HEAP_TBLE_GRAN     = 0x100;
00058 const uint32 HEAP_MIN_BLOCK     = 0x10; 
00059 const uint32 HEAPBOX_NAME       = 0x10; 
00060 
00061 const uint32 MALLOC_NOLOCKS     = 1;
00062 const uint32 MALLOC_NORESCUE    = 2;
00063 const uint32 MALLOC_MORECOR_NEW_NODE = 4;
00064 
00065 
00066 
00067 struct block_used
00068  {
00069         void *ptr;
00070         size_t size;
00071         union
00072          {
00073                 uint32 facility;        
00074                 const char *name;
00075          };
00076         int (*destruct)(class heapbox*,void*);
00077  };
00078 
00079 struct block_free
00080  {
00081         block_free *prev;
00082         block_free *next;
00083         size_t size;
00084         uint32 checksum;
00085  };
00086 
00087 extern class blist_t HeapList;  
00088 
00089 void init();    
00090 int allocate_heapbox(class heapbox *hbx, const char *name);     
00091 addr_t morecore(size_t cnt);    
00092 void freecore(addr_t virt, size_t cnt); 
00093 
00094 int checkMagic(block_free *item);       
00095 int checkMagic(block_used *item);       
00096 int countMagic(block_free *item);       
00097 int countMagic(block_used *item);       
00098 
00099 const uint32 PROLOG_CHECKSUM  = 0xbabaf00l;
00100 
00101 struct block_prolog {
00102    uint32 ov;     
00103    uint32 check;  
00104 };
00105 
00106 
00107 
00108 class heapbox
00109  {
00110 protected:
00111 public:
00112         class blist_t VirtAddrs;        
00113         block_used *used;       
00114         uint32 begin_search;    
00115         size_t max_blocks;      
00116         block_free *first;      
00117         bool volatile NowResizingUsed;  
00118         IPC::Lock::lock_t lock_heap;            
00119         struct {
00120                 uint32 volatile allocs;         
00121                 uint32 volatile max;            
00122                 uint32 volatile used;           
00123          } stats;       
00124         char name[HEAPBOX_NAME];
00125    uint32 heapbox_no;
00126    struct blist_s *morecor_new_node;   
00127 
00128         void unAnchorBlock(block_free *item);
00129         int take_block (block_free *item, uint32 facility, int (*destruct)(class heapbox*,void*));
00130         addr_t morecor(uint32 cnt, bool alloc_pgs);     
00131         int knew_used(void *ptr, uint32 size, uint32 facility, int (destructor)(class heapbox*,void*)); 
00132         block_free *klastfree();
00133         uint32 blsize(char *ptr);
00134         block_used *kget_info(void *ptr);
00135         void kdefragment(void *ptr);
00136         int resize_used_blocks_table(); 
00137    void *mallocFuncPr(size_t size, size_t alignment, uint32 facility, int (*destruct)(class heapbox*, void*), uint32 MallocState);
00138         void *mallocFunc(size_t size, size_t alignment, uint32 facility, int (*destruct)(class heapbox*,void*), uint32 MallocState);
00139         int freeFuncPr(void *ptr, uint32 FreeState);
00140         int freeFunc(void *ptr, uint32 FreeState);
00141 public:
00142 
00143 #if 1
00144         int freeFunc2(void *ptr, uint32 FreeState);
00145         int freeFuncPr2(void *ptr, uint32 FreeState);
00146 #endif
00147         heapbox();
00148         void init(block_used *usd, size_t max_blcks, block_free *fir, const char *nam);
00149         void destruct();        
00150         void *malloc(size_t size, size_t alignment, uint32 facility, int (*destruct)(class heapbox*,void*));    
00151         void *malloc(size_t size, size_t alignment, const char *name, int (*destruct)(class heapbox*,void*));   
00152         int free(void *ptr);    
00153 
00155         uint32 allocated_count();       
00157  };
00158 
00159 
00160 extern class heapbox heap0;     
00161 extern class ::Memory::zone virtual_pages;
00162 
00163   };    
00164  };     
00165 
00166 void debug_heap_stats(Memory::Heap::heapbox *hp);       
00167 
00168 #endif