Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
7 7u83 60
ExceptionP XX_dalloc_no_memory = EXCEPTION("cannot allocate memory");
2 7u83 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
 
7 7u83 81
static SizeT dalloc_data_size = ALIGN(sizeof(DallocDataT));
2 7u83 82
 
83
/*--------------------------------------------------------------------------*/
84
 
85
#ifdef __NeXT__
86
 
87
#undef TRUE
88
#undef FALSE
89
#include <mach/mach.h>
90
 
91
/*--------------------------------------------------------------------------*/
92
 
93
GenericP
7 7u83 94
X__dalloc_allocate(SizeT size, SizeT length, CStringP file, unsigned line)
2 7u83 95
{
96
    GenericP tmp;
97
 
98
    ASSERT (size != 0);
99
    if (length == 0) {
7 7u83 100
	tmp = NIL(GenericP);
2 7u83 101
    } else {
102
	SizeT        real_size = (((size) * length) + dalloc_data_size);
103
	vm_address_t address;
104
	DallocDataP  data;
105
	ByteP        base;
106
 
7 7u83 107
	if (vm_allocate(task_self (), &address, (vm_size_t) real_size,
108
			TRUE) != KERN_SUCCESS) {
109
	    THROW(XX_dalloc_no_memory);
2 7u83 110
	    UNREACHED;
111
	}
7 7u83 112
	data        = (DallocDataP)address;
113
	base        = (ByteP)address;
2 7u83 114
	tmp         = (base + dalloc_data_size);
115
	data->file  = file;
116
	data->line  = line;
117
	data->size  = real_size;
118
	data->magic = DALLOC_MAGIC;
119
    }
120
    return (tmp);
121
}
122
 
123
void
7 7u83 124
X__dalloc_deallocate(GenericP ptr, CStringP file, unsigned line)
2 7u83 125
{
126
    if (ptr) {
127
	ByteP         pointer = (ByteP) ptr;
7 7u83 128
	DallocDataP   data    = (DallocDataP)(pointer - dalloc_data_size);
2 7u83 129
	vm_address_t  address = (vm_address_t) data;
130
	vm_size_t     size    = data->size;
131
	kern_return_t result;
132
 
133
	if (data->magic == 0) {
7 7u83 134
	    E_dalloc_multi_deallocate(ptr, file, line, data->file, data->line);
2 7u83 135
	    UNREACHED;
136
	} else if (data->magic != DALLOC_MAGIC) {
137
	    E_dalloc_corrupt_block (ptr, file, line);
138
	    UNREACHED;
139
	}
140
	data->magic = 0;
141
	result = vm_protect (task_self (), address, size, FALSE, VM_PROT_NONE);
142
	ASSERT (result == KERN_SUCCESS);
143
    }
144
}
145
 
146
#else
147
 
148
GenericP
7 7u83 149
X__dalloc_allocate(SizeT size, SizeT length, CStringP file, unsigned line)
2 7u83 150
{
151
    GenericP tmp;
152
 
153
    ASSERT (size != 0);
154
    if (length == 0) {
7 7u83 155
	tmp = NIL(GenericP);
2 7u83 156
    } else {
157
	SizeT       real_size = ((size * length) + dalloc_data_size);
158
	ByteP       base;
159
	DallocDataP data;
160
 
7 7u83 161
	if ((tmp = malloc(real_size)) == NIL(GenericP)) {
162
	    THROW(XX_dalloc_no_memory);
2 7u83 163
	    UNREACHED;
164
	}
165
	(void) memset (tmp, 0, real_size);
166
	data        = tmp;
167
	base        = tmp;
168
	tmp         = (base + dalloc_data_size);
169
	data->file  = file;
170
	data->line  = line;
171
	data->magic = DALLOC_MAGIC;
172
    }
173
    return (tmp);
174
}
175
 
176
void
7 7u83 177
X__dalloc_deallocate(GenericP ptr, CStringP file, unsigned line)
2 7u83 178
{
179
    if (ptr) {
180
	ByteP       pointer = (ByteP) ptr;
7 7u83 181
	DallocDataP data    = (DallocDataP)(pointer - dalloc_data_size);
2 7u83 182
 
183
	if (data->magic == 0) {
7 7u83 184
	    E_dalloc_multi_deallocate(ptr, file, line, data->file, data->line);
2 7u83 185
	    UNREACHED;
186
	} else if (data->magic != DALLOC_MAGIC) {
187
	    E_dalloc_corrupt_block (ptr, file, line);
188
	    UNREACHED;
189
	}
190
	data->magic = 0;
191
	free ((GenericP) data);
192
    }
193
}
194
 
195
#endif /* defined (__NeXT__) */
196
 
197
#else
198
 
199
GenericP
7 7u83 200
X__dalloc_allocate(SizeT size, SizeT length)
2 7u83 201
{
202
    GenericP tmp;
203
 
204
    ASSERT (size != 0);
205
    if (length == 0) {
7 7u83 206
	tmp = NIL(GenericP);
207
    } else if ((tmp = calloc(length, size)) == NIL(GenericP)) {
208
	THROW(XX_dalloc_no_memory);
2 7u83 209
	UNREACHED;
210
    }
211
    return (tmp);
212
}
213
 
214
#endif /* defined (PO_DALLOC_DEBUG_ALIGN) */