-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinxalloc_plain.c
285 lines (241 loc) · 7.43 KB
/
inxalloc_plain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/******************************************************************************
Copyright (c) 2020, Ioannis Nompelis
All rights reserved.
Redistribution and use in source and binary forms, with or without any
modification, are permitted provided that the following conditions are met:
1. Redistribution of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistribution in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
"This product includes software developed by Ioannis Nompelis."
4. Neither the name of Ioannis Nompelis and his partners/affiliates nor the
names of other contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
5. Redistribution or use of source code and binary forms for profit must
have written permission of the copyright holder.
THIS SOFTWARE IS PROVIDED BY IOANNIS NOMPELIS ''AS IS'' AND ANY
EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL IOANNIS NOMPELIS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inxalloc.h"
//
// Function to initialize the structure storing memory data
//
__inline void inxalloc_Struct_Init( struct inXmem_s* p )
{
p->id = -1;
p->size = 0;
p->num_alloc = 0;
p->num_free = 0;
}
void* inxalloc( struct inXmem_s* p, size_t size )
{
#ifdef _DEBUG_
char FUNC[] = "inxalloc";
#endif
void* tmp;
size_t size_hdr = sizeof( struct inXmemhdr_s );
struct inXmemhdr_s* hdr;
if( p == NULL ) {
#ifdef _DEBUG_
fprintf( stdout, " [%s] Incoming structure is null \n",FUNC);
#endif
return NULL;
#ifdef _DEBUG_
} else {
fprintf( stdout, " [%s] Incoming structure ID is %d \n",FUNC,p->id);
#endif
}
tmp = (void*) malloc( size + size_hdr );
if( tmp != NULL ) {
#ifdef _DEBUG_
fprintf( stdout, " [%s] Allocated chunk (%ld bytes) at %p\n",
FUNC, (long int) size, tmp );
#endif
// increment the memory structure's size and number of allocations
p->size += size;
p->num_alloc += 1;
// store header of block
hdr = (struct inXmemhdr_s*) tmp;
hdr->segment = p; // parent memory structure
hdr->size = size; // size of block
hdr->ialloc = p->num_alloc; // allocation step
// do a shift to offset from the start of the block's header
tmp += size_hdr;
#ifdef _DEBUG_
fprintf( stdout, " [%s] Returning pointer at %p \n",FUNC,tmp);
#endif
return tmp;
} else {
#ifdef _DEBUG_
fprintf( stdout, " [%s] Could not allocate chunk (%ld bytes)\n",
FUNC, (long int) size );
#endif
}
return NULL;
}
//
// Function to free a memory block from a segment
//
int inxfree( struct inXmem_s* p, void* mem )
{
#ifdef _DEBUG_
char FUNC[] = "inxfree";
#endif
size_t size_hdr = sizeof( struct inXmemhdr_s );
struct inXmemhdr_s* hdr;
void* tmp;
size_t size;
if( p == NULL ) {
#ifdef _DEBUG_
fprintf( stdout, " [%s] Incoming structure is null \n",FUNC);
#endif
return 1;
#ifdef _DEBUG_
} else {
fprintf( stdout, " [%s] Incoming structure ID is %d \n",FUNC,p->id);
#endif
}
if( mem == NULL ) {
#ifdef _DEBUG_
fprintf( stdout, " [%s] Incoming memory block ipoiner s null \n",FUNC);
#endif
return 2;
#ifdef _DEBUG_
} else {
fprintf( stdout, " [%s] Incoming memory block pointer %p \n",FUNC,mem);
#endif
}
tmp = mem - size_hdr;
hdr = (struct inXmemhdr_s*) tmp;
if( hdr->segment != p ) {
fprintf( stdout, "Error: block not associated with this tructure!\n");
fprintf( stdout, " Not performing any freeing.\n");
return 3;
}
size = hdr->size;
free( tmp );
p->size -= size;
p->num_free += 1;
return 0;
}
//
// Function to free a memory block by fiding its segment
// (This is the "carefree" version of the proper free()-ing function.)
//
int inxfreefree( void* mem )
{
#ifdef _DEBUG_
char FUNC[] = "inxfreefree";
#endif
size_t size_hdr = sizeof( struct inXmemhdr_s );
struct inXmemhdr_s* hdr;
struct inXmem_s* seg;
void* tmp;
size_t size;
if( mem == NULL ) {
#ifdef _DEBUG_
fprintf( stdout, " [%s] Incoming memory block ipoiner s null \n",FUNC);
#endif
return 2;
#ifdef _DEBUG_
} else {
fprintf( stdout, " [%s] Incoming memory block pointer %p \n",FUNC,mem);
#endif
}
tmp = mem - size_hdr;
hdr = (struct inXmemhdr_s*) tmp;
seg = (struct inXmem_s*) hdr->segment;
#ifdef _DEBUG_
fprintf( stdout, " [%s] Queried segment at %p (id=%d) \n",FUNC,
seg, seg->id );
#endif
size = hdr->size;
free( tmp );
seg->size -= size;
seg->num_free += 1;
return 0;
}
//
// Function to report memory usage
//
void inxalloc_Struct_Report( struct inXmem_s* p )
{
#ifdef _DEBUG_
char FUNC[] = "inxalloc_Struct_Report";
#endif
if( p == NULL ) {
fprintf( stdout, "Incoming inXalloc structure pointer is null \n");
return;
#ifdef _DEBUG_
} else {
fprintf( stdout, " [%s] Incoming inXalloc structure pointer %p \n",
FUNC, p );
#endif
}
fprintf( stdout, "Memory struct ID: %d \n",p->id);
fprintf( stdout, "Memory struct size: %ld \n",(long int) p->size);
fprintf( stdout, "Memory struct number of allocations: %ld \n",p->num_alloc);
fprintf( stdout, "Memory struct number of frees: %ld \n",p->num_free);
}
//
// Function to query the memory segment to which a block belongs
//
struct inXmem_s* inxalloc_Block_Parent( void* b )
{
#ifdef _DEBUG_
char FUNC[] = "inxalloc_Block_Info";
#endif
struct inXmemhdr_s* hdr;
if( b == NULL ) {
fprintf( stdout, "Incoming inXalloc block pointer is null \n");
return NULL;
} else {
void* tmp = b;
hdr = (struct inXmemhdr_s*) (tmp - sizeof(struct inXmemhdr_s));
#ifdef _DEBUG_
fprintf( stdout, " [%s] Incoming inXalloc block pointer %p \n",
FUNC, b );
fprintf( stdout, " [%s] Created from segment at: %p (ID=%d)\n",
FUNC, hdr->segment, hdr->segment->id );
fprintf( stdout, " [%s] Was allocation number: %ld \n",
FUNC, hdr->ialloc );
#endif
}
return hdr->segment;
}
//
// a global variable to have an opaque memory structure
//
static struct inXmem_s global_inmem =
{
.id = -99999,
.size = 0,
.num_alloc = 0,
.num_free = 0
};
void* inyalloc( size_t size )
{
return inxalloc( & global_inmem, size );
}
int inyfree( void* mem )
{
return inxfree( & global_inmem, mem );
}
void inyalloc_Report()
{
inxalloc_Struct_Report( & global_inmem );
}