go to single .html
[fw/sdcc] / support / Util / NewAlloc.c
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 #include <stdio.h>
31 #include <malloc.h>
32 #include <stdlib.h>
33 #include <memory.h>
34
35 #include "newalloc.h"
36 /*
37 -------------------------------------------------------------------------------
38 Clear_realloc - Reallocate a memory block and clear any memory added with
39 out of memory error detection
40
41 -------------------------------------------------------------------------------
42 */
43
44 void *Clear_realloc(void *OldPtr,size_t OldSize,size_t NewSize)
45
46 {
47 void *NewPtr ;
48
49 NewPtr = realloc(OldPtr,NewSize) ;
50
51 if (!NewPtr)
52   {
53   printf("ERROR - No more memory\n") ;
54 /*  werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
55   exit (1);
56   }
57
58 if (NewPtr)
59   if (NewSize > OldSize)
60     memset((char *) NewPtr + OldSize,0x00,NewSize - OldSize) ;
61
62 return NewPtr ;
63 }
64 /*
65 -------------------------------------------------------------------------------
66 Safe_realloc - Reallocate a memory block with out of memory error detection
67
68 -------------------------------------------------------------------------------
69 */
70
71 void *Safe_realloc(void *OldPtr,size_t NewSize)
72
73 {
74 void *NewPtr ;
75
76 NewPtr = realloc(OldPtr,NewSize) ;
77
78 if (!NewPtr)
79   {
80   printf("ERROR - No more memory\n") ;
81 /*  werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
82   exit (1);
83   }
84
85 return NewPtr ;
86 }
87 /*
88 -------------------------------------------------------------------------------
89 Safe_calloc - Allocate a block of memory from the application heap, clearing
90 all data to zero and checking for out of memory errors.
91
92 -------------------------------------------------------------------------------
93 */
94
95 void *Safe_calloc(size_t Elements,size_t Size)
96
97 {
98 void *NewPtr ;
99
100 NewPtr = calloc(Elements,Size) ;
101
102 if (!NewPtr)
103   {
104   printf("ERROR - No more memory\n") ;
105 /*  werror(E_OUT_OF_MEM,__FILE__,Size);*/
106   exit (1);
107   }
108
109 return NewPtr ;
110 }
111 /*
112 -------------------------------------------------------------------------------
113 Safe_malloc - Allocate a block of memory from the application heap
114 and checking for out of memory errors.
115
116 -------------------------------------------------------------------------------
117 */
118
119 void *Safe_malloc(size_t Size)
120
121 {
122 void *NewPtr ;
123
124 NewPtr = malloc(Size) ;
125
126 if (!NewPtr)
127   {
128   printf("ERROR - No more memory\n") ;
129 /*  werror(E_OUT_OF_MEM,__FILE__,Size);*/
130   exit (1);
131   }
132
133 return NewPtr ;
134 }