ConOpSys V2970  P004.07
ANVILEX control operating system
Memory_Manager.h
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Description : Dynamic memory manager
3 // Filename : MemoryManager.h
4 //------------------------------------------------------------------------------
5 
6 #pragma once
7 
8 //------------------------------------------------------------------------------
9 
10 #include <StdLib.h>
11 
12 #include "Defines.h"
13 
14 //--- C++ definition -----------------------------------------------------------
15 
16 #ifdef __cplusplus
17 extern "C"
18 {
19 #endif
20 
21 //--- Structur TAllocatedSegment -----------------------------------------------
22 
23 class TAllocatedSegment;
25 {
26 
27  public:
28  U32 Status; // Status of segment
29  U32 Size; // Size of segment
30  TAllocatedSegment *PreviousSegment; // Pointer to previous segment
31  TAllocatedSegment *NextSegment; // Pointer to next segment
32  #ifdef MEMORY_MANAGER_ADD_OWNER_INFORMATION
33  CHAR File[ 32 ]; // File name
34  U32 Line; // File line
35  #endif
36 
37 };
38 
39 //--- Structur TMemoryStatistics -----------------------------------------------
40 
41 typedef struct
42 {
43 
44  VOID *BaseAddress; // Base Address of the virtual memory
45  VOID *BottomSegment; // Address of bottom segment
46  VOID *TopSegment; // Address of top segment
47  U32 TotalMemorySize; // Total size of virtual memory
48  U32 AllocatedMemorySize; // Size of allocated memory
49  U32 WastedMemorySize; // Size of wated memory
50  U32 FreeMemorySize; // Size of free memory
51 
52  U32 MaximalUsedMemory; // Maximal used memory
53 
54  U32 AllocatedSegments; // Allocated segments
55  U32 FreeSegments; // Free segments
56  U32 TotalSegments; // Total allocated segments
57 
58  U32 MaximalAllocatedSegmentSize; // Maximal size of allocated segment
59  U32 MaximalFreeSegmentSize; // Maximal size of free segment
60 
61  U32 AllocateClearedMemoryRequests; // Count of allocate and clear memory requests
62  U32 AllocateMemoryRequests; // Count of allocate memory requests
63  U32 DeallocateMemoryRequests; // Count of deallocate memory requests
64  U32 ReallocateMemoryRequests; // Count of reallocate memory requesta
65 
66  U32 AllocationErrorCount; // Count of allocation errors. (Out of memory or allocated size is null. )
67  U32 ReallocationErrorCount; // Count of reallocation errors.
68  U32 DeallocationErrorCount; // Count of deallocation errors. (Pointer is NULL.)
69 
70  VOID *FailedSegment; // Last failed segment
71  U32 FailedSegmentSatus; // Status of the failed segment
72 
73  #ifdef MEMORY_MANAGER_ADD_OWNER_INFORMATION
74  CHAR File[ 32 ];
75  U32 Line;
76  #endif
77 
79 
80 //--- Class TMemoryManager -----------------------------------------------------
81 
83 {
84 
85  public:
86 
87  TMemoryManager( VOID *ParentBaseAddress, U32 MemorySize ); // Constructor
88 
89  #ifdef MEMORY_MANAGER_ADD_OWNER_INFORMATION
90  VOID *allocMemory( U32 Size, BOOL Clear, const CHAR* File, U32 Line ); // Allocate memory segment
91  VOID *rllocMemory( VOID *Pointer, U32 Size, const CHAR* File, U32 Line ); // Reallocate memory segment
92  VOID dllocMemory( VOID *Pointer ); // Deallocate memory segment
93  #else
94  VOID *allocMemory( U32 Size, BOOL Clear ); // Allocate memory segment
95  VOID *rllocMemory( VOID *Pointer, U32 Size ); // Reallocate memory segment
96  VOID dllocMemory( VOID *Pointer ); // Deallocate memory segment
97  #endif
98 
99  BOOL CheckIntegrity(); // Check integrety of virtual memory
100 
101  VOID GetStatistics( TMemoryStatistics *Statistics ); // Get statistics
102 
103  private:
104 
105  BOOL Is_Segment_Valid( TAllocatedSegment *object_Segment );
106  BOOL Is_Segment_Free( TAllocatedSegment *object_Segment );
107  BOOL Is_Segment_Allocated( TAllocatedSegment *object_Segment );
108 
109  VOID MergeSegments( TAllocatedSegment *Segment ); // Merge segements
110 
111  BOOL SplitSegment( TAllocatedSegment *Segment, U32 SegmentSizeToAllocation ); // Split segement
112 
114 
115  // Start address of virtual memory
117 
118  // Pointer to bottom memory segment
120 
121  // Pointer to top memory segment
123 
124  // First free segment
126 
127  // Total size of virtual memory
129 
130  // Size of allocated memory
132 
133  // Size of wated memory
135 
136  // Size of free memory
138 
139  // Maximal used memory
141 
142  // Allocated segments
144 
145  // Free segments
147 
148  // Total allocated segments
150 
151  // Maximal size of allocated segment
153 
154  // Maximal size of free segment
156 
157  // Count of allocate and clear memory requests
159 
160  // Count of allocate memory requests
162 
163  // Count of deallocate memory requests
165 
166  // Count of reallocate memory requesta
168 
169  // Count of allocation errors. Out of memory or allocated size is null.
171 
172  // Count of reallocation errors.
174 
175  // Count of deallocation errors. Pointer is NULL.
177 
178  // Last failed segment
180 
181  // Status of the failed segment
183 
184 };
185 
186 //------------------------------------------------------------------------------
187 // Extern object declaration
188 //------------------------------------------------------------------------------
189 
191 
192 //--- End of C++ definition ----------------------------------------------------
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 //------------------------------------------------------------------------------
199 
200 #ifdef MEMORY_MANAGER_ADD_OWNER_INFORMATION
201 
202 //------------------------------------------------------------------------------
203 //
204 //------------------------------------------------------------------------------
205 
206 #define new new ( __FILE__, __LINE__ )
207 #define delete( ptr ) MemoryManager.dllocMemory( ptr )
208 #define malloc( sz ) MemoryManager.allocMemory( (U32)sz, false, __FILE__, __LINE__ )
209 #define free( ptr ) MemoryManager.dllocMemory( ptr )
210 #define realloc( ptr, sz ) MemoryManager.rllocMemory( ptr, (U32)sz, __FILE__, __LINE__ )
211 #define calloc( cnt, sz ) MemoryManager.allocMemory( (U32)cnt * (U32)sz, true,__FILE__, __LINE__ )
212 
213 //--- Overridden global "new" --------------------------------------------------
214 
215 inline VOID *operator new( size_t count, const CHAR* File, U32 Line )
216 {
217 
218  // Return pointer to allocated memory
219  return( MemoryManager.allocMemory( (U32)count, false, File, Line ) );
220 
221 }
222 
223 //--- Overridden global "new[]" ------------------------------------------------
224 
225 inline VOID *operator new[]( size_t count, const CHAR* File, U32 Line )
226 {
227 
228  // Return pointer to allocated memory
229  return( MemoryManager.allocMemory( (U32)count, false, File, Line ) );
230 
231 }
232 
233 //------------------------------------------------------------------------------
234 // Overridden global "delete"
235 //------------------------------------------------------------------------------
236 
237 inline VOID operator delete[]( VOID* void_Pointer_To_Memory )
238 {
239 
240  // Deallocate memory
241  MemoryManager.dllocMemory( void_Pointer_To_Memory );
242 
243 }
244 
245 //------------------------------------------------------------------------------
246 // Overridden global "delete[]"
247 //------------------------------------------------------------------------------
248 
249 inline VOID operator delete( VOID* void_Pointer_To_Memory )
250 {
251 
252  // Deallocate memory
253  MemoryManager.dllocMemory( void_Pointer_To_Memory );
254 
255 }
256 
257 #else
258 
259 //------------------------------------------------------------------------------
260 //
261 //------------------------------------------------------------------------------
262 
263 #define new new
264 #define delete delete
265 #define malloc( sz ) MemoryManager.allocMemory( (U32)sz, false )
266 #define free( ptr ) MemoryManager.dllocMemory( ptr )
267 #define realloc( ptr, sz ) MemoryManager.rllocMemory( ptr, (U32)sz )
268 #define calloc( cnt, sz ) MemoryManager.allocMemory( (U32)cnt * (U32)sz, true )
269 
270 //------------------------------------------------------------------------------
271 // Override global "new"
272 //------------------------------------------------------------------------------
273 
274 inline VOID *operator new( size_t count )
275 {
276 
277  // Return pointer to allocated memory
278  return( MemoryManager.allocMemory( (U32)count, false ) );
279 
280 }
281 
282 //------------------------------------------------------------------------------
283 // Override global "new[]"
284 //------------------------------------------------------------------------------
285 
286 inline VOID *operator new[]( size_t count )
287 {
288 
289  // Return pointer to allocated memory
290  return( MemoryManager.allocMemory( (U32)count, false ) );
291 
292 }
293 
294 //------------------------------------------------------------------------------
295 // Override global "delete"
296 //------------------------------------------------------------------------------
297 
298 inline VOID operator delete( VOID* void_Pointer_To_Memory )
299 {
300 
301  // Deallocate memory
302  MemoryManager.dllocMemory( void_Pointer_To_Memory );
303 
304 }
305 
306 //------------------------------------------------------------------------------
307 // Override global "delete[]"
308 //------------------------------------------------------------------------------
309 
310 inline VOID operator delete[]( VOID* void_Pointer_To_Memory )
311 {
312 
313  // Deallocate memory
314  MemoryManager.dllocMemory( void_Pointer_To_Memory );
315 
316 }
317 
318 #endif
319 
320 //------------------------------------------------------------------------------
321 // End of file
322 //------------------------------------------------------------------------------
#define false
Definition: rtwtypes.h:24
ConOpSys data type definitions header file.
int BOOL
Boolean datatype definition.
Definition: Defines.h:124
void VOID
Datatypesess datatype definition.
Definition: Defines.h:105
unsigned long U32
Binary 32-Bit unsigned integer datatype defenition.
Definition: Defines.h:203
TMemoryManager MemoryManager
Definition: Memory_Manager.h:25
U32 Status
Definition: Memory_Manager.h:28
U32 Size
Definition: Memory_Manager.h:29
TAllocatedSegment * NextSegment
Definition: Memory_Manager.h:31
TAllocatedSegment * PreviousSegment
Definition: Memory_Manager.h:30
Definition: Memory_Manager.h:83
U32 MaximalUsedMemory
Definition: Memory_Manager.h:140
BOOL CheckIntegrity()
Definition: Memory_Manager.cpp:938
U32 MaximalFreeSegmentSize
Definition: Memory_Manager.h:155
U32 AllocationErrorCount
Definition: Memory_Manager.h:170
U32 WastedMemorySize
Definition: Memory_Manager.h:134
TAllocatedSegment * FirstFreeSegment
Definition: Memory_Manager.h:125
U32 TotalSegments
Definition: Memory_Manager.h:149
U32 ReallocationErrorCount
Definition: Memory_Manager.h:173
U32 TotalMemorySize
Definition: Memory_Manager.h:128
U32 AllocatedSegments
Definition: Memory_Manager.h:143
VOID * allocMemory(U32 Size, BOOL Clear)
Definition: Memory_Manager.cpp:284
TAllocatedSegment * BottomSegment
Definition: Memory_Manager.h:119
VOID MergeSegments(TAllocatedSegment *Segment)
Definition: Memory_Manager.cpp:783
VOID GetStatistics(TMemoryStatistics *Statistics)
Definition: Memory_Manager.cpp:1161
TAllocatedSegment * TopSegment
Definition: Memory_Manager.h:122
U32 AllocatedMemorySize
Definition: Memory_Manager.h:131
TMemoryManager(VOID *ParentBaseAddress, U32 MemorySize)
Definition: Memory_Manager.cpp:45
U32 DeallocationErrorCount
Definition: Memory_Manager.h:176
VOID * rllocMemory(VOID *Pointer, U32 Size)
Definition: Memory_Manager.cpp:540
U32 FailedSegmentSatus
Definition: Memory_Manager.h:182
VOID dllocMemory(VOID *Pointer)
Definition: Memory_Manager.cpp:682
VOID * BaseAddress
Definition: Memory_Manager.h:116
BOOL Is_Segment_Valid(TAllocatedSegment *object_Segment)
Definition: Memory_Manager.cpp:100
U32 AllocateClearedMemoryRequests
Definition: Memory_Manager.h:158
TAllocatedSegment * FailedSegment
Definition: Memory_Manager.h:179
BOOL Is_Segment_Free(TAllocatedSegment *object_Segment)
Definition: Memory_Manager.cpp:161
U32 AllocateMemoryRequests
Definition: Memory_Manager.h:161
U32 ReallocateMemoryRequests
Definition: Memory_Manager.h:167
BOOL SplitSegment(TAllocatedSegment *Segment, U32 SegmentSizeToAllocation)
Definition: Memory_Manager.cpp:833
U32 FreeSegments
Definition: Memory_Manager.h:146
VOID SetFirstFreeSegment(TAllocatedSegment *Segment)
Definition: Memory_Manager.cpp:907
BOOL Is_Segment_Allocated(TAllocatedSegment *object_Segment)
Definition: Memory_Manager.cpp:221
U32 DeallocateMemoryRequests
Definition: Memory_Manager.h:164
U32 MaximalAllocatedSegmentSize
Definition: Memory_Manager.h:152
U32 FreeMemorySize
Definition: Memory_Manager.h:137
Definition: Memory_Manager.h:42
U32 WastedMemorySize
Definition: Memory_Manager.h:49
VOID * BottomSegment
Definition: Memory_Manager.h:45
U32 FreeMemorySize
Definition: Memory_Manager.h:50
U32 MaximalUsedMemory
Definition: Memory_Manager.h:52
U32 TotalSegments
Definition: Memory_Manager.h:56
VOID * BaseAddress
Definition: Memory_Manager.h:44
U32 DeallocateMemoryRequests
Definition: Memory_Manager.h:63
U32 AllocatedMemorySize
Definition: Memory_Manager.h:48
U32 TotalMemorySize
Definition: Memory_Manager.h:47
U32 FailedSegmentSatus
Definition: Memory_Manager.h:71
U32 ReallocationErrorCount
Definition: Memory_Manager.h:67
U32 AllocateMemoryRequests
Definition: Memory_Manager.h:62
U32 ReallocateMemoryRequests
Definition: Memory_Manager.h:64
U32 MaximalAllocatedSegmentSize
Definition: Memory_Manager.h:58
U32 FreeSegments
Definition: Memory_Manager.h:55
VOID * TopSegment
Definition: Memory_Manager.h:46
U32 AllocationErrorCount
Definition: Memory_Manager.h:66
U32 AllocateClearedMemoryRequests
Definition: Memory_Manager.h:61
U32 AllocatedSegments
Definition: Memory_Manager.h:54
U32 DeallocationErrorCount
Definition: Memory_Manager.h:68
VOID * FailedSegment
Definition: Memory_Manager.h:70
U32 MaximalFreeSegmentSize
Definition: Memory_Manager.h:59