Imported Upstream version 2.9.0
[debian/cc1111] / support / Util / newalloc.h
1 /*
2 ===============================================================================
3 NEWALLOC - SDCC Memory allocation functions
4
5 These functions are wrappers for the standard malloc, realloc and free
6 functions.
7
8
9      This program is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by the
11    Free Software Foundation; either version 2, or (at your option) any
12    later version.
13
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.
18
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23    In other words, you are welcome to use, share and improve this program.
24    You are forbidden to forbid anyone else to use, share and improve
25    what you give them.   Help stamp out software-hoarding!
26
27 ===============================================================================
28 */
29
30 #if !defined(_NewAlloc_H)
31
32 #define _NewAlloc_H
33
34 #include <memory.h>
35
36 typedef struct _allocTrace
37 {
38   int num;
39   int max;
40   void **palloced;
41 } allocTrace;
42
43 /*
44 -------------------------------------------------------------------------------
45 Clear_realloc - Reallocate a memory block and clear any memory added with
46 out of memory error detection
47
48 -------------------------------------------------------------------------------
49 */
50
51 void *Clear_realloc(void *OldPtr,size_t OldSize,size_t NewSize) ;
52
53 /*
54 -------------------------------------------------------------------------------
55 Safe_realloc - Reallocate a memory block with out of memory error detection
56
57 -------------------------------------------------------------------------------
58 */
59
60 void *Safe_realloc(void *OldPtr,size_t NewSize) ;
61
62 /*
63 -------------------------------------------------------------------------------
64 Safe_calloc - Allocate a block of memory from the application heap, clearing
65 all data to zero and checking for out or memory errors.
66
67 -------------------------------------------------------------------------------
68 */
69
70 void *Safe_calloc(size_t Elements,size_t Size) ;
71
72 /*
73 -------------------------------------------------------------------------------
74 Safe_malloc - Allocate a block of memory from the application heap
75 and checking for out or memory errors.
76
77 -------------------------------------------------------------------------------
78 */
79
80 void *Safe_malloc(size_t Size) ;
81
82 /** Replacement for Safe_malloc that also zeros memory.  To make it interchangable.
83  */
84 void *Safe_alloc(size_t Size) ;
85
86 /** Function to make the replacements complete.
87  */
88 void Safe_free(void *p);
89
90 /** Creates a copy of a string in a safe way.
91  */
92 char *Safe_strdup(const char *sz);
93
94 /** Logs the allocated memory 'p' in the given trace for batch freeing
95     later using freeTrace.
96 */
97 void *traceAlloc(allocTrace *ptrace, void *p);
98
99 /** Frees all the memory logged in the trace and resets the trace.
100  */
101 void freeTrace(allocTrace *ptrace);
102
103 #endif