2 |
7u83 |
1 |
/*
|
|
|
2 |
Crown Copyright (c) 1997
|
|
|
3 |
|
|
|
4 |
This TenDRA(r) Computer Program is subject to Copyright
|
|
|
5 |
owned by the United Kingdom Secretary of State for Defence
|
|
|
6 |
acting through the Defence Evaluation and Research Agency
|
|
|
7 |
(DERA). It is made available to Recipients with a
|
|
|
8 |
royalty-free licence for its use, reproduction, transfer
|
|
|
9 |
to other parties and amendment for any purpose not excluding
|
|
|
10 |
product development provided that any such use et cetera
|
|
|
11 |
shall be deemed to be acceptance of the following conditions:-
|
|
|
12 |
|
|
|
13 |
(1) Its Recipients shall ensure that this Notice is
|
|
|
14 |
reproduced upon any copies or amended versions of it;
|
|
|
15 |
|
|
|
16 |
(2) Any amended version of it shall be clearly marked to
|
|
|
17 |
show both the nature of and the organisation responsible
|
|
|
18 |
for the relevant amendment or amendments;
|
|
|
19 |
|
|
|
20 |
(3) Its onward transfer from a recipient to another
|
|
|
21 |
party shall be deemed to be that party's acceptance of
|
|
|
22 |
these conditions;
|
|
|
23 |
|
|
|
24 |
(4) DERA gives no warranty or assurance as to its
|
|
|
25 |
quality or suitability for any purpose and DERA accepts
|
|
|
26 |
no liability whatsoever in relation to any use to which
|
|
|
27 |
it may be put.
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**** dalloc.c --- Memory allocation and deallocation.
|
|
|
32 |
*
|
|
|
33 |
** Author: Steve Folkes <smf@hermes.mod.uk>
|
|
|
34 |
*
|
|
|
35 |
**** Commentary:
|
|
|
36 |
*
|
|
|
37 |
* This file implements the dynamic memory management facility specified in
|
|
|
38 |
* the file "dalloc.h". See that file for more information.
|
|
|
39 |
*
|
|
|
40 |
**** Change Log:
|
|
|
41 |
* $Log: dalloc.c,v $
|
|
|
42 |
* Revision 1.1.1.1 1998/01/17 15:57:45 release
|
|
|
43 |
* First version to be checked into rolling release.
|
|
|
44 |
*
|
|
|
45 |
* Revision 1.2 1994/12/12 11:45:28 smf
|
|
|
46 |
* Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
|
|
|
47 |
* OSSG C Coding Standards.
|
|
|
48 |
*
|
|
|
49 |
* Revision 1.1.1.1 1994/07/25 16:06:09 smf
|
|
|
50 |
* Initial import of os-interface shared files.
|
|
|
51 |
*
|
|
|
52 |
**/
|
|
|
53 |
|
|
|
54 |
/****************************************************************************/
|
|
|
55 |
|
|
|
56 |
#include "dalloc.h"
|
|
|
57 |
|
|
|
58 |
/*--------------------------------------------------------------------------*/
|
|
|
59 |
|
|
|
60 |
ExceptionP XX_dalloc_no_memory = EXCEPTION ("cannot allocate memory");
|
|
|
61 |
|
|
|
62 |
/*--------------------------------------------------------------------------*/
|
|
|
63 |
|
|
|
64 |
#ifdef PO_DALLOC_DEBUG_ALIGN
|
|
|
65 |
|
|
|
66 |
#define DALLOC_MAGIC 0x21436587
|
|
|
67 |
#define ALIGN(x) (((int) (((x) + (PO_DALLOC_DEBUG_ALIGN) - 1) / \
|
|
|
68 |
(PO_DALLOC_DEBUG_ALIGN))) * (PO_DALLOC_DEBUG_ALIGN))
|
|
|
69 |
|
|
|
70 |
/*--------------------------------------------------------------------------*/
|
|
|
71 |
|
|
|
72 |
typedef struct DallocDataT {
|
|
|
73 |
CStringP file;
|
|
|
74 |
unsigned line;
|
|
|
75 |
SizeT size;
|
|
|
76 |
int magic;
|
|
|
77 |
} DallocDataT, *DallocDataP;
|
|
|
78 |
|
|
|
79 |
/*--------------------------------------------------------------------------*/
|
|
|
80 |
|
|
|
81 |
static SizeT dalloc_data_size = ALIGN (sizeof (DallocDataT));
|
|
|
82 |
|
|
|
83 |
/*--------------------------------------------------------------------------*/
|
|
|
84 |
|
|
|
85 |
#ifdef __NeXT__
|
|
|
86 |
|
|
|
87 |
#undef TRUE
|
|
|
88 |
#undef FALSE
|
|
|
89 |
#include <mach/mach.h>
|
|
|
90 |
|
|
|
91 |
/*--------------------------------------------------------------------------*/
|
|
|
92 |
|
|
|
93 |
GenericP
|
|
|
94 |
X__dalloc_allocate PROTO_N ((size, length, file, line))
|
|
|
95 |
PROTO_T (SizeT size X
|
|
|
96 |
SizeT length X
|
|
|
97 |
CStringP file X
|
|
|
98 |
unsigned line)
|
|
|
99 |
{
|
|
|
100 |
GenericP tmp;
|
|
|
101 |
|
|
|
102 |
ASSERT (size != 0);
|
|
|
103 |
if (length == 0) {
|
|
|
104 |
tmp = NIL (GenericP);
|
|
|
105 |
} else {
|
|
|
106 |
SizeT real_size = (((size) * length) + dalloc_data_size);
|
|
|
107 |
vm_address_t address;
|
|
|
108 |
DallocDataP data;
|
|
|
109 |
ByteP base;
|
|
|
110 |
|
|
|
111 |
if (vm_allocate (task_self (), &address, (vm_size_t) real_size,
|
|
|
112 |
TRUE) != KERN_SUCCESS) {
|
|
|
113 |
THROW (XX_dalloc_no_memory);
|
|
|
114 |
UNREACHED;
|
|
|
115 |
}
|
|
|
116 |
data = (DallocDataP) address;
|
|
|
117 |
base = (ByteP) address;
|
|
|
118 |
tmp = (base + dalloc_data_size);
|
|
|
119 |
data->file = file;
|
|
|
120 |
data->line = line;
|
|
|
121 |
data->size = real_size;
|
|
|
122 |
data->magic = DALLOC_MAGIC;
|
|
|
123 |
}
|
|
|
124 |
return (tmp);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
void
|
|
|
128 |
X__dalloc_deallocate PROTO_N ((ptr, file, line))
|
|
|
129 |
PROTO_T (GenericP ptr X
|
|
|
130 |
CStringP file X
|
|
|
131 |
unsigned line)
|
|
|
132 |
{
|
|
|
133 |
if (ptr) {
|
|
|
134 |
ByteP pointer = (ByteP) ptr;
|
|
|
135 |
DallocDataP data = (DallocDataP) (pointer - dalloc_data_size);
|
|
|
136 |
vm_address_t address = (vm_address_t) data;
|
|
|
137 |
vm_size_t size = data->size;
|
|
|
138 |
kern_return_t result;
|
|
|
139 |
|
|
|
140 |
if (data->magic == 0) {
|
|
|
141 |
E_dalloc_multi_deallocate (ptr, file, line, data->file,
|
|
|
142 |
data->line);
|
|
|
143 |
UNREACHED;
|
|
|
144 |
} else if (data->magic != DALLOC_MAGIC) {
|
|
|
145 |
E_dalloc_corrupt_block (ptr, file, line);
|
|
|
146 |
UNREACHED;
|
|
|
147 |
}
|
|
|
148 |
data->magic = 0;
|
|
|
149 |
result = vm_protect (task_self (), address, size, FALSE, VM_PROT_NONE);
|
|
|
150 |
ASSERT (result == KERN_SUCCESS);
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
#else
|
|
|
155 |
|
|
|
156 |
GenericP
|
|
|
157 |
X__dalloc_allocate PROTO_N ((size, length, file, line))
|
|
|
158 |
PROTO_T (SizeT size X
|
|
|
159 |
SizeT length X
|
|
|
160 |
CStringP file X
|
|
|
161 |
unsigned line)
|
|
|
162 |
{
|
|
|
163 |
GenericP tmp;
|
|
|
164 |
|
|
|
165 |
ASSERT (size != 0);
|
|
|
166 |
if (length == 0) {
|
|
|
167 |
tmp = NIL (GenericP);
|
|
|
168 |
} else {
|
|
|
169 |
SizeT real_size = ((size * length) + dalloc_data_size);
|
|
|
170 |
ByteP base;
|
|
|
171 |
DallocDataP data;
|
|
|
172 |
|
|
|
173 |
if ((tmp = malloc (real_size)) == NIL (GenericP)) {
|
|
|
174 |
THROW (XX_dalloc_no_memory);
|
|
|
175 |
UNREACHED;
|
|
|
176 |
}
|
|
|
177 |
(void) memset (tmp, 0, real_size);
|
|
|
178 |
data = tmp;
|
|
|
179 |
base = tmp;
|
|
|
180 |
tmp = (base + dalloc_data_size);
|
|
|
181 |
data->file = file;
|
|
|
182 |
data->line = line;
|
|
|
183 |
data->magic = DALLOC_MAGIC;
|
|
|
184 |
}
|
|
|
185 |
return (tmp);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
void
|
|
|
189 |
X__dalloc_deallocate PROTO_N ((ptr, file, line))
|
|
|
190 |
PROTO_T (GenericP ptr X
|
|
|
191 |
CStringP file X
|
|
|
192 |
unsigned line)
|
|
|
193 |
{
|
|
|
194 |
if (ptr) {
|
|
|
195 |
ByteP pointer = (ByteP) ptr;
|
|
|
196 |
DallocDataP data = (DallocDataP) (pointer - dalloc_data_size);
|
|
|
197 |
|
|
|
198 |
if (data->magic == 0) {
|
|
|
199 |
E_dalloc_multi_deallocate (ptr, file, line, data->file,
|
|
|
200 |
data->line);
|
|
|
201 |
UNREACHED;
|
|
|
202 |
} else if (data->magic != DALLOC_MAGIC) {
|
|
|
203 |
E_dalloc_corrupt_block (ptr, file, line);
|
|
|
204 |
UNREACHED;
|
|
|
205 |
}
|
|
|
206 |
data->magic = 0;
|
|
|
207 |
free ((GenericP) data);
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
#endif /* defined (__NeXT__) */
|
|
|
212 |
|
|
|
213 |
#else
|
|
|
214 |
|
|
|
215 |
GenericP
|
|
|
216 |
X__dalloc_allocate PROTO_N ((size, length))
|
|
|
217 |
PROTO_T (SizeT size X
|
|
|
218 |
SizeT length)
|
|
|
219 |
{
|
|
|
220 |
GenericP tmp;
|
|
|
221 |
|
|
|
222 |
ASSERT (size != 0);
|
|
|
223 |
if (length == 0) {
|
|
|
224 |
tmp = NIL (GenericP);
|
|
|
225 |
} else if ((tmp = calloc (length, size)) == NIL (GenericP)) {
|
|
|
226 |
THROW (XX_dalloc_no_memory);
|
|
|
227 |
UNREACHED;
|
|
|
228 |
}
|
|
|
229 |
return (tmp);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
#endif /* defined (PO_DALLOC_DEBUG_ALIGN) */
|