2 |
7u83 |
1 |
/*
|
6 |
7u83 |
2 |
* Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
|
|
|
3 |
* All rights reserved.
|
|
|
4 |
*
|
|
|
5 |
* Redistribution and use in source and binary forms, with or without
|
|
|
6 |
* modification, are permitted provided that the following conditions are met:
|
|
|
7 |
*
|
|
|
8 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
9 |
* this list of conditions and the following disclaimer.
|
|
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
11 |
* this list of conditions and the following disclaimer in the documentation
|
|
|
12 |
* and/or other materials provided with the distribution.
|
|
|
13 |
* 3. Neither the name of The TenDRA Project nor the names of its contributors
|
|
|
14 |
* may be used to endorse or promote products derived from this software
|
|
|
15 |
* without specific, prior written permission.
|
|
|
16 |
*
|
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
|
|
|
18 |
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
19 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
|
|
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
22 |
* EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
28 |
*
|
|
|
29 |
* $Id$
|
|
|
30 |
*/
|
|
|
31 |
/*
|
2 |
7u83 |
32 |
Crown Copyright (c) 1997
|
6 |
7u83 |
33 |
|
2 |
7u83 |
34 |
This TenDRA(r) Computer Program is subject to Copyright
|
|
|
35 |
owned by the United Kingdom Secretary of State for Defence
|
|
|
36 |
acting through the Defence Evaluation and Research Agency
|
|
|
37 |
(DERA). It is made available to Recipients with a
|
|
|
38 |
royalty-free licence for its use, reproduction, transfer
|
|
|
39 |
to other parties and amendment for any purpose not excluding
|
|
|
40 |
product development provided that any such use et cetera
|
|
|
41 |
shall be deemed to be acceptance of the following conditions:-
|
6 |
7u83 |
42 |
|
2 |
7u83 |
43 |
(1) Its Recipients shall ensure that this Notice is
|
|
|
44 |
reproduced upon any copies or amended versions of it;
|
6 |
7u83 |
45 |
|
2 |
7u83 |
46 |
(2) Any amended version of it shall be clearly marked to
|
|
|
47 |
show both the nature of and the organisation responsible
|
|
|
48 |
for the relevant amendment or amendments;
|
6 |
7u83 |
49 |
|
2 |
7u83 |
50 |
(3) Its onward transfer from a recipient to another
|
|
|
51 |
party shall be deemed to be that party's acceptance of
|
|
|
52 |
these conditions;
|
6 |
7u83 |
53 |
|
2 |
7u83 |
54 |
(4) DERA gives no warranty or assurance as to its
|
|
|
55 |
quality or suitability for any purpose and DERA accepts
|
|
|
56 |
no liability whatsoever in relation to any use to which
|
|
|
57 |
it may be put.
|
|
|
58 |
*/
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
/**** dalloc.c --- Memory allocation and deallocation.
|
|
|
62 |
*
|
|
|
63 |
** Author: Steve Folkes <smf@hermes.mod.uk>
|
|
|
64 |
*
|
|
|
65 |
**** Commentary:
|
|
|
66 |
*
|
|
|
67 |
* This file implements the dynamic memory management facility specified in
|
|
|
68 |
* the file "dalloc.h". See that file for more information.
|
|
|
69 |
*
|
|
|
70 |
**** Change Log:
|
|
|
71 |
* $Log: dalloc.c,v $
|
|
|
72 |
* Revision 1.1.1.1 1998/01/17 15:57:17 release
|
|
|
73 |
* First version to be checked into rolling release.
|
|
|
74 |
*
|
|
|
75 |
* Revision 1.2 1994/12/12 11:45:28 smf
|
|
|
76 |
* Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
|
|
|
77 |
* OSSG C Coding Standards.
|
|
|
78 |
*
|
|
|
79 |
* Revision 1.1.1.1 1994/07/25 16:06:09 smf
|
|
|
80 |
* Initial import of os-interface shared files.
|
|
|
81 |
*
|
|
|
82 |
**/
|
|
|
83 |
|
|
|
84 |
/****************************************************************************/
|
|
|
85 |
|
|
|
86 |
#include "dalloc.h"
|
|
|
87 |
|
|
|
88 |
/*--------------------------------------------------------------------------*/
|
|
|
89 |
|
6 |
7u83 |
90 |
ExceptionP XX_dalloc_no_memory = EXCEPTION("cannot allocate memory");
|
2 |
7u83 |
91 |
|
|
|
92 |
/*--------------------------------------------------------------------------*/
|
|
|
93 |
|
|
|
94 |
#ifdef PO_DALLOC_DEBUG_ALIGN
|
|
|
95 |
|
6 |
7u83 |
96 |
#define DALLOC_MAGIC 0x21436587
|
|
|
97 |
#define ALIGN(x) (((int)(((x) + (PO_DALLOC_DEBUG_ALIGN) - 1) / \
|
|
|
98 |
(PO_DALLOC_DEBUG_ALIGN))) * (PO_DALLOC_DEBUG_ALIGN))
|
2 |
7u83 |
99 |
|
|
|
100 |
/*--------------------------------------------------------------------------*/
|
|
|
101 |
|
|
|
102 |
typedef struct DallocDataT {
|
|
|
103 |
CStringP file;
|
|
|
104 |
unsigned line;
|
|
|
105 |
SizeT size;
|
|
|
106 |
int magic;
|
|
|
107 |
} DallocDataT, *DallocDataP;
|
|
|
108 |
|
|
|
109 |
/*--------------------------------------------------------------------------*/
|
|
|
110 |
|
6 |
7u83 |
111 |
static SizeT dalloc_data_size = ALIGN(sizeof(DallocDataT));
|
2 |
7u83 |
112 |
|
|
|
113 |
/*--------------------------------------------------------------------------*/
|
|
|
114 |
|
|
|
115 |
#ifdef __NeXT__
|
|
|
116 |
|
|
|
117 |
#undef TRUE
|
|
|
118 |
#undef FALSE
|
|
|
119 |
#include <mach/mach.h>
|
|
|
120 |
|
|
|
121 |
/*--------------------------------------------------------------------------*/
|
|
|
122 |
|
|
|
123 |
GenericP
|
6 |
7u83 |
124 |
X__dalloc_allocate(SizeT size, SizeT length,
|
|
|
125 |
CStringP file,
|
2 |
7u83 |
126 |
unsigned line)
|
|
|
127 |
{
|
|
|
128 |
GenericP tmp;
|
|
|
129 |
|
6 |
7u83 |
130 |
ASSERT(size != 0);
|
2 |
7u83 |
131 |
if (length == 0) {
|
6 |
7u83 |
132 |
tmp = NIL(GenericP);
|
2 |
7u83 |
133 |
} else {
|
6 |
7u83 |
134 |
SizeT real_size = (((size)* length) + dalloc_data_size);
|
2 |
7u83 |
135 |
vm_address_t address;
|
|
|
136 |
DallocDataP data;
|
|
|
137 |
ByteP base;
|
|
|
138 |
|
6 |
7u83 |
139 |
if (vm_allocate(task_self(), &address,(vm_size_t)real_size,
|
2 |
7u83 |
140 |
TRUE) != KERN_SUCCESS) {
|
6 |
7u83 |
141 |
THROW(XX_dalloc_no_memory);
|
2 |
7u83 |
142 |
UNREACHED;
|
|
|
143 |
}
|
6 |
7u83 |
144 |
data = (DallocDataP)address;
|
|
|
145 |
base = (ByteP)address;
|
2 |
7u83 |
146 |
tmp = (base + dalloc_data_size);
|
|
|
147 |
data->file = file;
|
|
|
148 |
data->line = line;
|
|
|
149 |
data->size = real_size;
|
|
|
150 |
data->magic = DALLOC_MAGIC;
|
|
|
151 |
}
|
6 |
7u83 |
152 |
return(tmp);
|
2 |
7u83 |
153 |
}
|
|
|
154 |
|
|
|
155 |
void
|
6 |
7u83 |
156 |
X__dalloc_deallocate(GenericP ptr, CStringP file,
|
2 |
7u83 |
157 |
unsigned line)
|
|
|
158 |
{
|
|
|
159 |
if (ptr) {
|
6 |
7u83 |
160 |
ByteP pointer = (ByteP)ptr;
|
|
|
161 |
DallocDataP data = (DallocDataP)(pointer - dalloc_data_size);
|
|
|
162 |
vm_address_t address = (vm_address_t)data;
|
2 |
7u83 |
163 |
vm_size_t size = data->size;
|
|
|
164 |
kern_return_t result;
|
|
|
165 |
|
|
|
166 |
if (data->magic == 0) {
|
6 |
7u83 |
167 |
E_dalloc_multi_deallocate(ptr, file, line, data->file,
|
2 |
7u83 |
168 |
data->line);
|
|
|
169 |
UNREACHED;
|
|
|
170 |
} else if (data->magic != DALLOC_MAGIC) {
|
6 |
7u83 |
171 |
E_dalloc_corrupt_block(ptr, file, line);
|
2 |
7u83 |
172 |
UNREACHED;
|
|
|
173 |
}
|
|
|
174 |
data->magic = 0;
|
6 |
7u83 |
175 |
result = vm_protect(task_self(), address, size, FALSE, VM_PROT_NONE);
|
|
|
176 |
ASSERT(result == KERN_SUCCESS);
|
2 |
7u83 |
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
#else
|
|
|
181 |
|
|
|
182 |
GenericP
|
6 |
7u83 |
183 |
X__dalloc_allocate(SizeT size, SizeT length,
|
|
|
184 |
CStringP file,
|
2 |
7u83 |
185 |
unsigned line)
|
|
|
186 |
{
|
|
|
187 |
GenericP tmp;
|
|
|
188 |
|
6 |
7u83 |
189 |
ASSERT(size != 0);
|
2 |
7u83 |
190 |
if (length == 0) {
|
6 |
7u83 |
191 |
tmp = NIL(GenericP);
|
2 |
7u83 |
192 |
} else {
|
|
|
193 |
SizeT real_size = ((size * length) + dalloc_data_size);
|
|
|
194 |
ByteP base;
|
|
|
195 |
DallocDataP data;
|
|
|
196 |
|
6 |
7u83 |
197 |
if ((tmp = malloc(real_size)) == NIL(GenericP)) {
|
|
|
198 |
THROW(XX_dalloc_no_memory);
|
2 |
7u83 |
199 |
UNREACHED;
|
|
|
200 |
}
|
6 |
7u83 |
201 |
(void)memset(tmp, 0, real_size);
|
2 |
7u83 |
202 |
data = tmp;
|
|
|
203 |
base = tmp;
|
|
|
204 |
tmp = (base + dalloc_data_size);
|
|
|
205 |
data->file = file;
|
|
|
206 |
data->line = line;
|
|
|
207 |
data->magic = DALLOC_MAGIC;
|
|
|
208 |
}
|
6 |
7u83 |
209 |
return(tmp);
|
2 |
7u83 |
210 |
}
|
|
|
211 |
|
|
|
212 |
void
|
6 |
7u83 |
213 |
X__dalloc_deallocate(GenericP ptr, CStringP file,
|
2 |
7u83 |
214 |
unsigned line)
|
|
|
215 |
{
|
|
|
216 |
if (ptr) {
|
6 |
7u83 |
217 |
ByteP pointer = (ByteP)ptr;
|
|
|
218 |
DallocDataP data = (DallocDataP)(pointer - dalloc_data_size);
|
2 |
7u83 |
219 |
|
|
|
220 |
if (data->magic == 0) {
|
6 |
7u83 |
221 |
E_dalloc_multi_deallocate(ptr, file, line, data->file,
|
2 |
7u83 |
222 |
data->line);
|
|
|
223 |
UNREACHED;
|
|
|
224 |
} else if (data->magic != DALLOC_MAGIC) {
|
6 |
7u83 |
225 |
E_dalloc_corrupt_block(ptr, file, line);
|
2 |
7u83 |
226 |
UNREACHED;
|
|
|
227 |
}
|
|
|
228 |
data->magic = 0;
|
6 |
7u83 |
229 |
free((GenericP)data);
|
2 |
7u83 |
230 |
}
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
#endif /* defined (__NeXT__) */
|
|
|
234 |
|
|
|
235 |
#else
|
|
|
236 |
|
|
|
237 |
GenericP
|
6 |
7u83 |
238 |
X__dalloc_allocate(SizeT size, SizeT length)
|
2 |
7u83 |
239 |
{
|
|
|
240 |
GenericP tmp;
|
|
|
241 |
|
6 |
7u83 |
242 |
ASSERT(size != 0);
|
2 |
7u83 |
243 |
if (length == 0) {
|
6 |
7u83 |
244 |
tmp = NIL(GenericP);
|
|
|
245 |
} else if ((tmp = calloc(length, size)) == NIL(GenericP)) {
|
|
|
246 |
THROW(XX_dalloc_no_memory);
|
2 |
7u83 |
247 |
UNREACHED;
|
|
|
248 |
}
|
6 |
7u83 |
249 |
return(tmp);
|
2 |
7u83 |
250 |
}
|
|
|
251 |
|
|
|
252 |
#endif /* defined (PO_DALLOC_DEBUG_ALIGN) */
|