memry.h 6.7 KB
Newer Older
T
tmbdev 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
/**********************************************************************
 * File:        memry.h  (Formerly memory.h)
 * Description: Header file for basic memory allocation/deallocation.
 * Author:      Ray Smith
 * Created:     Tue May  8 16:03:48 BST 1990
 *
 * (C) Copyright 1990, Hewlett-Packard Ltd.
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 ** http://www.apache.org/licenses/LICENSE-2.0
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 *
 **********************************************************************/

#ifndef           MEMRY_H
#define           MEMRY_H

#include          <stddef.h>
#include          "host.h"

#define JUSTCHECKS      0        /*just check consistency */
#define MEMCHECKS     1          /*report totals */
#define FULLMEMCHECKS   2        /*report on all blocks */

#ifdef __MSW32__
#define NEWDELETE										/*replace new & delete*/\
	void					*operator new(				/*fast allocator*/\
	size_t					size,						/*size of object*/\
	const char*				file=NULL,					/*filename*/\
	INT32					line=0)						/*line number*/\
	{\
		return alloc_struct(size);						/*simple to do*/\
	}\
\
	void					operator delete(			/*fast destructor*/\
	void					*deadstruct,				/*thing to free*/\
	size_t					size)						/*sizeof struct*/\
	{\
		free_struct(deadstruct,size);					/*free it*/\
	}\

#define NEWDELETE2(name)								/*replace new & delete*/\
	void					*operator new(				/*fast allocator*/\
	size_t					size,						/*size of object*/\
	const char*				file=NULL,					/*filename*/\
	INT32					line=0)						/*line number*/\
	{\
		return alloc_struct(size,#name);				/*simple to do*/\
	}\
\
	void					operator delete(			/*fast destructor*/\
	void					*deadstruct,				/*thing to free*/\
	size_t					size)						/*sizeof struct*/\
	{\
		free_struct(deadstruct,size,#name);				/*free it*/\
	}\


#undef NEWDELETE
#define NEWDELETE
#undef NEWDELETE2
#define NEWDELETE2(name)

#else
#define NEWDELETE										/*replace new & delete*/\
	void					*operator new(				/*fast allocator*/\
	size_t					size)						/*size of object*/\
	{\
		return alloc_struct(size);						/*simple to do*/\
	}\
\
	void					operator delete(			/*fast destructor*/\
	void					*deadstruct,				/*thing to free*/\
	size_t					size)						/*sizeof struct*/\
	{\
		free_struct(deadstruct,size);					/*free it*/\
	}\

#define NEWDELETE2(name)								/*replace new & delete*/\
	void					*operator new(				/*fast allocator*/\
	size_t					size)						/*size of object*/\
	{\
		return alloc_struct(size,#name);				/*simple to do*/\
	}\
\
	void					operator delete(			/*fast destructor*/\
	void					*deadstruct,				/*thing to free*/\
	size_t					size)						/*sizeof struct*/\
	{\
		free_struct(deadstruct,size,#name);				/*free it*/\
	}\

#endif
/**********************************************************************
 * ALLOC_2D_ARRAY
 *
 * Create a dynamic 2D array.
 **********************************************************************/

#define ALLOC_2D_ARRAY(x,y,mem,ptrs,type)				/*make 2d array*/\
{ \
	INT32					TMP_i; \
	mem=(type*)alloc_mem((x)*(y)*sizeof(type));			/*get memory*/\
	ptrs=(type**)alloc_mem((x)*sizeof(type*));			/*get ptrs*/\
	for (TMP_i=0;TMP_i<(x);TMP_i++)\
		ptrs[TMP_i]=mem+(y)*TMP_i;						/*set ptrs*/\
} \

/**********************************************************************
 * FREE_2D_ARRAY
 *
 * Destroy a 2D array created by ALLOC_2D_ARRAY
 **********************************************************************/

#define FREE_2D_ARRAY(mem,ptrs)							/*free a 2D array*/\
{ \
	free_mem(mem);										/*free the memory*/\
	free_mem(ptrs);										/*and the ptrs*/\
} \

/**********************************************************************
 * ALLOC_BIG_2D_ARRAY
 *
 * Create a dynamic 2D array. Use a memory allocator that allows
 * allocation of bigger chunks.
 **********************************************************************/

#define ALLOC_BIG_2D_ARRAY(x,y,mem,ptrs,type)			/*make 2d array*/\
{ \
	INT32					TMP_i; \
	mem=(type*)alloc_big_mem((x)*(y)*sizeof(type));		/*get memory*/\
	ptrs=(type**)alloc_big_mem((x)*sizeof(type*));		/*get ptrs*/\
	for (TMP_i=0;TMP_i<(x);TMP_i++)\
		ptrs[TMP_i]=mem+(y)*TMP_i;						/*set ptrs*/\
} \

/**********************************************************************
 * FREE_BIG_2D_ARRAY
 *
 * Destroy a 2D array created by ALLOC_BIG_2D_ARRAY
 **********************************************************************/

#define FREE_BIG_2D_ARRAY(mem,ptrs)						/*free a 2D array*/\
{ \
	free_big_mem(mem);									/*free the memory*/\
	free_big_mem(ptrs);									/*and the ptrs*/\
} \

extern DLLSYM void check_mem(                     //check consistency
                             const char *string,  //context message
                             INT8 level           //level of check
                            );
                                 //allocate string
extern DLLSYM char *alloc_string(INT32 count  //no of chars required
                                );
extern DLLSYM void free_string(              //free a string
                               char *string  //string to free
                              );
                                 //allocate memory
extern DLLSYM void *alloc_struct (
INT32 count,                     //no of chars required
const char *name = NULL          //class name
);
extern DLLSYM void free_struct ( //free a structure
void *deadstruct,                //structure to free
INT32 count,                     //no of bytes
const char *name = NULL          //class name
);
extern DLLSYM void *alloc_mem_p(             //allocate permanent space
                                INT32 count  //block size to allocate
                               );
extern DLLSYM void *alloc_mem(             //get some memory
                              INT32 count  //no of bytes to get
                             );
                                 //get some memory
extern DLLSYM void *alloc_big_mem(INT32 count  //no of bytes to get
                                 );
                                 //get some memory
extern DLLSYM void *alloc_big_zeros(INT32 count  //no of bytes to get
                                   );
extern DLLSYM void free_mem(                //free mem from alloc_mem
                            void *oldchunk  //chunk to free
                           );
extern DLLSYM void free_big_mem(                //free mem from alloc_mem
                                void *oldchunk  //chunk to free
                               );
#endif