modified: makefile
[GalaxyCodeBases.git] / BGI / soap_src / soap_builder / MemManager.h
blobaeaa4032eb4b4361602261af9a3dcf1b2b337101
1 /*
3 MemManager.h Memory Manager
5 This module provides memory management functions.
7 Copyright (C) 2004, Wong Chi Kwong.
9 This program is FREEALIGN software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #ifndef __MEM_MANAGER_H__
26 #define __MEM_MANAGER_H__
28 #include "TypeNLimit.h"
30 #define MAX_ALIGN 64 // All memory except pool memory are aligned to MAX_ALIGN; pool memory is aligned to finer boundary for small memory size
31 #define MIN_ALIGN 1
33 #define RECORD_GRAND_TOTAL
35 // Memory type:
37 // unit memory: allocation managed by malloc() individually;
38 // to be used for large and less frequently accessed items
39 // allocation can be freed individually at any time
40 // pool memory: pre-allocated memory pool for items with varying sizes
41 // allocation cannot be freed by individually
42 // to be used for small and frequently accessed items
43 // temp memory: temporary use granted from pool memory
44 // allocation is allocated and freed like the items in a stack
45 // pool memory allocation is disabled while temporary memory is in use
46 // bulk memory: pre-allocated memory pool for items with the same size
47 // to be used for massively numbered items
48 // memory address of dispatched items can be calculated by dispatch index
51 #ifdef DEBUG
52 #define Mem(mmBulk, index) MMBulkAddress(mmBulk, index)
53 #else
54 #define Mem(mmBulk, index) (void*)&(mmBulk->directory[index >> mmBulk->itemPerAllocationInPowerOf2][(index & mmBulk->indexMask) * mmBulk->itemSize])
55 #endif
57 typedef struct MMPool {
58 unsigned int poolSize; // Size of memory pool; the beginning of the pool holds the MMPool structure
59 unsigned int poolByteDispatched; // Includes any spillover and memory skipped for align
60 unsigned int poolByteSpillover; // Exclude spillover pointers
61 unsigned int currentTempByteDispatched; // Includes any spillover
62 unsigned int currentTempByteSpillover; // Exclude spillover pointers
63 unsigned int maxTotalByteDispatched; // The max of pool memory + temp memory dispatched
64 void *firstSpillOverAddress; // if pool is freed, = address of mmPool
65 } MMPool;
68 typedef struct MMBulk {
69 unsigned int itemSize;
70 unsigned int itemPerAllocationInPowerOf2;
71 unsigned int boundaryCushionSize; // boundary cushion is a piece of memory allocated so that the memory around items can be safely referenced
72 unsigned int indexMask;
73 unsigned int currentDirectoryEntry;
74 unsigned int nextUnusedItem;
75 unsigned int directorySize;
76 unsigned char **directory; // if bulk is freed, = NULL
77 } MMBulk;
79 typedef struct MMMaster {
80 unsigned int currentUnitByteAllocated;
81 unsigned int maxUnitByteAllocated;
82 unsigned int maxNumberOfPools;
83 MMPool **mmPool;
84 unsigned int maxNumberOfBulks;
85 MMBulk **mmBulk;
86 unsigned int maxTotalByteAllocated;
87 unsigned int maxTotalByteDispatched;
88 int traceUnitByteAllocation;
89 FILE *unitByteTraceFile;
90 } MMMaster;
92 void *MMMalloc(const unsigned int memSize);
93 void MMFree(void *address);
94 void MMMasterInitialize(const unsigned int maxNumberOfPools, const unsigned int maxNumberOfBulks,
95 const int traceUnitByteAllocation, FILE *unitByteTraceFile);
96 void MMMasterFreeAll();
97 unsigned int MMMasterCurrentTotalByteAllocated();
98 unsigned int MMMasterCurrentTotalByteDispatched();
99 unsigned int MMMasterMaxTotalByteAllocated();
100 unsigned int MMMasterMaxTotalByteDispatched();
101 void MMMasterSetMaxTotalByteAllocated();
102 void MMMasterSetMaxTotalByteDispatched();
103 void MMMasterPrintReport(FILE *output, const unsigned int withUnitDetails, const unsigned int withPoolDetails, const unsigned int withBulkDetails);
105 void *MMUnitAllocate(const unsigned int memSize);
106 void *MMUnitReallocate(void *address, const unsigned int newMemSize, const unsigned int oldMemSize);
107 void MMUnitFree(void *address, const unsigned int memSize);
108 unsigned int MMUnitCurrentByteAllocated();
109 unsigned int MMUnitMaxByteAllocated();
110 void MMUnitPrintReport(FILE *output);
112 MMPool *MMPoolCreate(const unsigned int poolSize);
113 unsigned int MMPoolIsActive(const MMPool *mmPool);
114 void MMPoolSetInactive(MMPool *mmPool);
115 unsigned int MMPoolCurrentTotalByteAllocated(const MMPool *mmPool);
116 unsigned int MMPoolCurrentTotalByteDispatched(const MMPool *mmPool);
117 unsigned int MMPoolMaxTotalByteDispatched(const MMPool *mmPool);
118 unsigned int MMPoolByteAvailable(const MMPool *mmPool);
119 MMPool *MMPoolFree(MMPool *mmPool);
120 void MMPoolReset(MMPool *mmPool);
121 void MMPoolDestory(MMPool *mmPool);
122 void *MMPoolDispatch(MMPool *mmPool, const unsigned int memSize);
123 unsigned int MMPoolDispatchOffset(MMPool *mmPool, const unsigned int memSize);
124 void MMPoolReturn(MMPool *mmPool, void *address, const unsigned int memSize); // Dummy function
125 void MMPoolPrintReport(MMPool *mmPool, FILE *output);
127 void *MMTempDispatch(MMPool *mmPool, const unsigned int memsize);
128 void MMTempReturn(MMPool *mmPool, void *address, const unsigned int memSize);
129 void MMTempPrintReport(MMPool *mmPool, FILE *output);
131 MMBulk *MMBulkCreate(MMPool *mmPool, const unsigned int itemSize, const unsigned int itemPerAllocationInPowerOf2,
132 unsigned int const boundaryCushionSize, unsigned int const directorySize);
133 unsigned int MMBulkIsActive(const MMBulk *mmBulk);
134 void MMBulkSetInactive(MMBulk *mmBulk);
135 unsigned int MMBulkByteAllocated(const MMBulk *mmBulk);
136 unsigned int MMBulkByteDispatched(const MMBulk *mmBulk);
137 unsigned int MMBulkUnitDispatched(const MMBulk *mmBulk);
138 void MMBulkFree(MMBulk *mmBulk);
139 void MMBulkDestory(MMBulk *mmBulk);
140 unsigned int MMBulkDispatch(MMBulk *mmBulk);
141 void *MMBulkAddress(const MMBulk *mmBulk, const unsigned int index);
142 MMPool *MMBulkFindPoolUsed(const MMBulk *mmBulk);
143 void MMBulkPrintReport(MMBulk *mmBulk, FILE *output);
145 void MMBulkSave(MMBulk *mmBulk, FILE *output);
146 MMBulk *MMBulkLoad(MMPool *mmPool, FILE *input);
149 #endif