-
Notifications
You must be signed in to change notification settings - Fork 29
/
teramem.c
271 lines (247 loc) · 7.45 KB
/
teramem.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
/* SYSIFCOPT(*IFSIO) TERASPACE(*YES *TSIFC) STGMDL(*INHERIT) */
/* ------------------------------------------------------------- */
/* Date . . . . : 14.09.2014 */
/* Design . . . : Niels Liisberg */
/* Function . . : Base utilies - Memory manager */
/* */
/* debug by: */
/* */
/* QIBM_MALLOC_TYPE=DEBUG */
/* */
/* See more at: */
/* https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rtref/debug_memory_manager.htm */
/* */
/* By Date PTF Description */
/* NL 14.09.2014 New program */
/* ------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mallocinfo.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "ostypes.h"
#include "teramem.h"
static INT64 used = 0;
static INT64 allocated = 0;
static INT64 deallocated = 0;
static INT64 balance = 0;
static INT64 limit = (5.0 * 1000.0 * 1000.0);
static BOOL debug = true;
// -------------------------------------------------------------
PVOID memAlloc (UINT64 len)
{
UINT64 totlen = len + sizeof(MEMHDR);
UINT64 used;
_C_mallinfo_t info;
int rc;
PMEMHDR mem = _C_TS_malloc64(totlen);
#ifdef MEMDEBUG
if (debug) {
rc = _C_TS_malloc_debug(_C_DUMP_TOTALS,
_C_NO_CHECKS,
&info, sizeof(info));
}
#endif
// Debugging !!!
if (mem == NULL ) {
#ifdef MEMDEBUG
printf("Out of memory, NULL returned from malloc : Usage : %lld \n", balance);
#endif
return NULL;
}
mem->signature = MEMSIG;
mem->size = len;
allocated += totlen;
balance += totlen;
// Debugging !!!
#ifdef MEMDEBUG
if (balance > limit ) {
sleep(10); // Slow down to let us debug
}
#endif
return (PUCHAR) mem + sizeof(MEMHDR);
}
// -------------------------------------------------------------
PVOID memCalloc (UINT64 len)
{
PVOID mem= memAlloc (len);
memset( mem , '\0', len);
return mem;
}
// -------------------------------------------------------------
void memFree (PVOID * pp)
{
PUCHAR p;
PMEMHDR mem;
UINT64 totlen;
if (pp == NULL) {
#ifdef MEMDEBUG
printf("Free null pointer pointer\n");
#endif
return;
}
p = (PUCHAR) *pp;
if (p == NULL) {
// printf("Free null pointer\n");
return;
}
mem = (PMEMHDR) (p - sizeof(MEMHDR)) ;
if (mem->signature != MEMSIG ) {
#ifdef MEMDEBUG
printf("Free non valid memory\n");
#endif
return;
}
mem->signature = 0; // Enusre that we release the signature
totlen = mem->size + sizeof(MEMHDR);
deallocated += totlen;
balance -= totlen;
// memFree (mem);
_C_TS_free(mem);
*pp = NULL;
}
// -------------------------------------------------------------
PUCHAR memStrDup(PUCHAR s)
{
PUCHAR p;
UINT64 len;
if (s == NULL) return NULL;
len = strlen(s) + 1; // Len including the zero term.
p = memAlloc (len);
memcpy (p , s , len); // Copy the string including the zerotermination
return p;
}
// -------------------------------------------------------------
PUCHAR memStrTrimDup(PUCHAR s)
{
PUCHAR p;
PUCHAR t;
UINT64 len = 0;
if (s == NULL) return NULL;
for (t=s; *t ; t++) {
if (*t > ' ') len = (t - s) + 1;
}
p = memAlloc (len+1);
memcpy (p , s , len); // Copy the string including the zerotermination
*(p+len) = 0;
return p;
}
// -------------------------------------------------------------
PVOID memRealloc (PVOID * p, UINT64 len)
{
PUCHAR oldMem = *p;
if (oldMem) {
PMEMHDR mem = (PMEMHDR) (oldMem - sizeof(MEMHDR)) ;
UINT64 newSize = len+ sizeof(MEMHDR);
// _C_TS_realloc64 does not exists !! reacclo only work up to 2G
//PMEMHDR newMem = _C_TS_realloc64(mem , newSize); // Preserve space for the signature
PMEMHDR newMem = _C_TS_realloc(mem , newSize); // Preserve space for the signature
balance += newSize - newMem->size;
newMem->size = newSize;
*p = (PUCHAR)newMem + sizeof(MEMHDR); // Return the pointer after the header
} else {
*p = memAlloc(len);
}
return *p;
}
// -------------------------------------------------------------
UINT64 memSize (PVOID p)
{
PMEMHDR mem;
if (p == NULL ) return 0;
mem = (PMEMHDR) ((PUCHAR) p - sizeof(MEMHDR)) ;
if (mem->signature != MEMSIG ) {
#ifdef MEMDEBUG
printf("Non valid memory\n");
#endif
return 0;
}
return mem->size;
}
// -------------------------------------------------------------
PVOID memShare (PUCHAR path, UINT64 len)
{
LONG fd;
LONG result;
PUCHAR map;
fd = open(path , O_RDWR);
// Does not exists - creat it
if (fd == -1) {
fd = open(path , O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
if (fd == -1) {
perror("Error opening file for writing");
return NULL;
}
// Stretch the file size to the size of the (mmapped) array of BYTES
result = lseek(fd, len-1, SEEK_SET);
if (result == -1) {
close(fd);
perror("Error calling lseek() to 'stretch' the file");
return NULL;
}
/* Something needs to be written at the end of the file to
* have the file actually have the new size.
* Just writing an empty string at the current file position will do.
*
* Note:
* - The current position in the file is at the end of the stretched
* file due to the call to lseek().
* - An empty string is actually a single '\0' character, so a zero-byte
* will be written at the last byte of the file.
*/
result = write(fd, "", 1);
if (result != 1) {
close(fd);
perror("Error writing last byte of the file");
return NULL;
}
}
// Now the file is ready to be mmapped.
map = mmap(0, len , PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
return NULL;
}
return map;
}
// -------------------------------------------------------------
void memStat (void)
{
printf("\n");
printf("Allocated: %-16.16lld " , allocated);
printf("Deallocated: %-16.16lld " , deallocated);
printf("Balance: %-16.16lld\n" , balance);
}
// -------------------------------------------------------------
BOOL memLeak (void)
{
return (balance != 0) ;
}
// -------------------------------------------------------------
UINT64 memUse (void)
{
return balance;
}
// -------------------------------------------------------------
// Test case:
// -------------------------------------------------------------
/*********************
void main()
{
PUCHAR p [1000];
int i ;
INT64 b1, b2, b3;
b1 = memUse();
for(i=0;i<20 ;i++) {
p[i] = memAlloc(MEMMAX);
}
b2 = memUse();
for(i=0;i<20 ;i++) {
memFree (&p[i]);
}
b3 = memUse();
}
*/