Subversion Repositories tendra.SVN

Rev

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

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