1 //-----------------------------------------------------------------------------
2 // Memory management
3 //-----------------------------------------------------------------------------
4 
5 #ifndef __MEM_H__
6 #define __MEM_H__
7 
8 #include "definitions.h"
9 
10 #define DESCRIPTION_MAXLENGTH 64
11 
12 #ifdef _DEBUG
13   #if 0
14     #define DEBUG_MEM     // Activate the memory debug manager (very slow !)
15   #endif
16 #endif
17 
18 /**
19  * The memory management provides internal methods for memory allocation
20  * functions. The cake allocation is by default disabled, but it could be
21  * used for debugging.
22  */
23 #ifdef DEBUG_MEM
24   #include <stddef.h>
25 
26   typedef char strDescription[DESCRIPTION_MAXLENGTH+1];
27   typedef struct
28   {
29     void *mem;          /**< Pointer to memory zone */
30     size_t len;         /**< Memory zone length */
31     strDescription descr;   /**< Description string (for debug help) */
32   } memZone;
33 
34   /**
35    * Adds a new reference into register.
36    * @param m The start of memory zone to register.
37    * @param l The length of memory zone.
38    * @param d A description for the memory zone (to help for debug)
39    */
40   void add_reference(void *m, int l, strDescription d);
41 
42   /**
43    * Updates an existing reference.
44    * @param m The start of memory zone to update.
45    * @param new_m The new start memory zone.
46    * @param new_l The new length of memory zone.
47    * @param new_d The new description of memory zone.
48    */
49   void update_reference(void *m, void *new_m, int new_l, strDescription new_d);
50 
51   /**
52    * Removes an existing reference.
53    * @param m The start of memory zone to remove from register.
54    */
55   void remove_reference(void *m);
56 
57   /**
58    * Generates a break in program execution.
59    * This should allow to interrupt the program to perform debugging.
60    */
61   void force_break(void);
62 
63   /**
64    * Free the allocated memory automatically.
65    * Attention: Use this function if you are sure that what you are doing !
66    */
67   void auto_freemem(void);
68 
69   /**
70    * Destroys the register.
71    */
72   void delete_memregister(void);
73 
74   /**
75    * Writes a report on current memory allocation in logfile.
76    */
77   void log_mem_report(void);
78 
79   /**
80   * Gets the number of references in memory register
81   * @return the number of allocations
82   */
83   int get_references(void);
84 
85   //-------------------------------------------------------------------------
86 
87   /**
88    * Allocate memory space
89    * @param size the memory size to allocate
90    * @return a pointer at the beginning of memory zone or a NULL pointer
91    *         if the requested memory couldn't be allocated
92    */
93   void *cake_malloc(size_t size, strDescription description = "");
94 
95   /**
96    * Resize an allocated memory zone
97    * @param mem the start of memory zone
98    * @param size the new size of memory zone
99    */
100   void *cake_realloc(void *mem, size_t size, strDescription description = "");
101 
102   /**
103    * Free a memory zone
104    * @param mem the start of memory zone
105    */
106   void cake_free(void *mem);
107 
108 #else
109   #include <stdlib.h>
110 
111   #define cake_malloc(l, d) malloc(l)         /**< standard malloc function */
112   #define cake_realloc(m, l, d) realloc(m, l)     /**< standard realloc function */
113   #define cake_free(m) free(m)            /**< standard free function */
114 #endif
115 
116 #endif  /* __MEM_H__ */
117