Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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