• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**********************************************************************
2  * File:        memry.h  (Formerly memory.h)
3  * Description: Header file for basic memory allocation/deallocation.
4  * Author:      Ray Smith
5  * Created:     Tue May  8 16:03:48 BST 1990
6  *
7  * (C) Copyright 1990, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef           MEMRY_H
21 #define           MEMRY_H
22 
23 #include          <stddef.h>
24 #include          "host.h"
25 
26 #define JUSTCHECKS      0        /*just check consistency */
27 #define MEMCHECKS     1          /*report totals */
28 #define FULLMEMCHECKS   2        /*report on all blocks */
29 
30 #ifdef __MSW32__
31 #define NEWDELETE										/*replace new & delete*/\
32 	void					*operator new(				/*fast allocator*/\
33 	size_t					size,						/*size of object*/\
34 	const char*				file=NULL,					/*filename*/\
35 	inT32					line=0)						/*line number*/\
36 	{\
37 		return alloc_struct(size);						/*simple to do*/\
38 	}\
39 \
40 	void					operator delete(			/*fast destructor*/\
41 	void					*deadstruct,				/*thing to free*/\
42 	size_t					size)						/*sizeof struct*/\
43 	{\
44 		free_struct(deadstruct,size);					/*free it*/\
45 	}\
46 
47 #define NEWDELETE2(name)								/*replace new & delete*/\
48 	void					*operator new(				/*fast allocator*/\
49 	size_t					size,						/*size of object*/\
50 	const char*				file=NULL,					/*filename*/\
51 	inT32					line=0)						/*line number*/\
52 	{\
53 		return alloc_struct(size,#name);				/*simple to do*/\
54 	}\
55 \
56 	void					operator delete(			/*fast destructor*/\
57 	void					*deadstruct,				/*thing to free*/\
58 	size_t					size)						/*sizeof struct*/\
59 	{\
60 		free_struct(deadstruct,size,#name);				/*free it*/\
61 	}\
62 
63 
64 #undef NEWDELETE
65 #define NEWDELETE
66 #undef NEWDELETE2
67 #define NEWDELETE2(name)
68 
69 #else
70 #define NEWDELETE										/*replace new & delete*/\
71 	void					*operator new(				/*fast allocator*/\
72 	size_t					size)						/*size of object*/\
73 	{\
74 		return alloc_struct(size);						/*simple to do*/\
75 	}\
76 \
77 	void					operator delete(			/*fast destructor*/\
78 	void					*deadstruct,				/*thing to free*/\
79 	size_t					size)						/*sizeof struct*/\
80 	{\
81 		free_struct(deadstruct,size);					/*free it*/\
82 	}\
83 
84 #define NEWDELETE2(name)								/*replace new & delete*/\
85 	void					*operator new(				/*fast allocator*/\
86 	size_t					size)						/*size of object*/\
87 	{\
88 		return alloc_struct(size,#name);				/*simple to do*/\
89 	}\
90 \
91 	void					operator delete(			/*fast destructor*/\
92 	void					*deadstruct,				/*thing to free*/\
93 	size_t					size)						/*sizeof struct*/\
94 	{\
95 		free_struct(deadstruct,size,#name);				/*free it*/\
96 	}\
97 
98 #endif
99 /**********************************************************************
100  * ALLOC_2D_ARRAY
101  *
102  * Create a dynamic 2D array.
103  **********************************************************************/
104 
105 #define ALLOC_2D_ARRAY(x,y,mem,ptrs,type)				/*make 2d array*/\
106 { \
107 	inT32					TMP_i; \
108 	mem=(type*)alloc_mem((x)*(y)*sizeof(type));			/*get memory*/\
109 	ptrs=(type**)alloc_mem((x)*sizeof(type*));			/*get ptrs*/\
110 	for (TMP_i=0;TMP_i<(x);TMP_i++)\
111 		ptrs[TMP_i]=mem+(y)*TMP_i;						/*set ptrs*/\
112 } \
113 
114 /**********************************************************************
115  * FREE_2D_ARRAY
116  *
117  * Destroy a 2D array created by ALLOC_2D_ARRAY
118  **********************************************************************/
119 
120 #define FREE_2D_ARRAY(mem,ptrs)							/*free a 2D array*/\
121 { \
122 	free_mem(mem);										/*free the memory*/\
123 	free_mem(ptrs);										/*and the ptrs*/\
124 } \
125 
126 /**********************************************************************
127  * ALLOC_BIG_2D_ARRAY
128  *
129  * Create a dynamic 2D array. Use a memory allocator that allows
130  * allocation of bigger chunks.
131  **********************************************************************/
132 
133 #define ALLOC_BIG_2D_ARRAY(x,y,mem,ptrs,type)			/*make 2d array*/\
134 { \
135 	inT32					TMP_i; \
136 	mem=(type*)alloc_big_mem((x)*(y)*sizeof(type));		/*get memory*/\
137 	ptrs=(type**)alloc_big_mem((x)*sizeof(type*));		/*get ptrs*/\
138 	for (TMP_i=0;TMP_i<(x);TMP_i++)\
139 		ptrs[TMP_i]=mem+(y)*TMP_i;						/*set ptrs*/\
140 } \
141 
142 /**********************************************************************
143  * FREE_BIG_2D_ARRAY
144  *
145  * Destroy a 2D array created by ALLOC_BIG_2D_ARRAY
146  **********************************************************************/
147 
148 #define FREE_BIG_2D_ARRAY(mem,ptrs)						/*free a 2D array*/\
149 { \
150 	free_big_mem(mem);									/*free the memory*/\
151 	free_big_mem(ptrs);									/*and the ptrs*/\
152 } \
153 
154 extern DLLSYM void check_mem(                     //check consistency
155                              const char *string,  //context message
156                              inT8 level           //level of check
157                             );
158                                  //allocate string
159 extern DLLSYM char *alloc_string(inT32 count  //no of chars required
160                                 );
161 extern DLLSYM void free_string(              //free a string
162                                char *string  //string to free
163                               );
164                                  //allocate memory
165 extern DLLSYM void *alloc_struct (
166 inT32 count,                     //no of chars required
167 const char *name = NULL          //class name
168 );
169 extern DLLSYM void free_struct ( //free a structure
170 void *deadstruct,                //structure to free
171 inT32 count,                     //no of bytes
172 const char *name = NULL          //class name
173 );
174 extern DLLSYM void *alloc_mem_p(             //allocate permanent space
175                                 inT32 count  //block size to allocate
176                                );
177 extern DLLSYM void *alloc_mem(             //get some memory
178                               inT32 count  //no of bytes to get
179                              );
180                                  //get some memory
181 extern DLLSYM void *alloc_big_mem(inT32 count  //no of bytes to get
182                                  );
183                                  //get some memory
184 extern DLLSYM void *alloc_big_zeros(inT32 count  //no of bytes to get
185                                    );
186 extern DLLSYM void free_mem(                //free mem from alloc_mem
187                             void *oldchunk  //chunk to free
188                            );
189 extern DLLSYM void free_big_mem(                //free mem from alloc_mem
190                                 void *oldchunk  //chunk to free
191                                );
192 #endif
193