File: jmalloc.c

package info (click to toggle)
jgraph 83-19
  • links: PTS
  • area: main
  • in suites: woody
  • size: 624 kB
  • ctags: 445
  • sloc: ansic: 4,728; makefile: 186; sh: 106; awk: 104
file content (444 lines) | stat: -rw-r--r-- 11,525 bytes parent folder | download | duplicates (4)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <stdio.h>
#include <malloc.h>

/* Each memory block has 8 extra 32-bit values associated with it.  If malloc
   returns the pointer p to you, the state really looks like:

jmal(p)------>  |------------------------------------------------------------|
                | flink (next malloc block in absolute order)                |
                |------------------------------------------------------------|
                | blink (prev malloc block in absolute order)                |
                |------------------------------------------------------------|
                | nextfree (next free malloc block - no particular order)    |
                |------------------------------------------------------------|
                | prevfree (next free malloc block - no particular order)    |
                |------------------------------------------------------------|
                | size (size of memory allocated)                            |
                |------------------------------------------------------------|
                | cs2 (pointer to the second checksum, which is right after  |
                |   the mem block)                                           |
                |------------------------------------------------------------|
                | cs (checksum right before mem block.  used to determine if |
                |   there is an error of writing around the memory block)    |
p------------>  |------------------------------------------------------------|
                | space: the memory block                                    |
                |                  ...                                       |
                |                  ...                                       |
                |------------------------------------------------------------|
	        | the second checksum                                        |
                |------------------------------------------------------------|
*/


typedef struct jmalloc {
  struct jmalloc *flink;
  struct jmalloc *blink;
  struct jmalloc *nextfree;
  struct jmalloc *prevfree;
  int size;
  int *cs2;
  int cs;
  char *space;
} *Jmalloc;

#define JMSZ (sizeof(struct jmalloc))
#define PTSZ (sizeof(char *))  /* Also assuming its > sizeof int */
#define CHUNK_SIZE (16384 - JMSZ)	/* 16K */
#define MASK 0x17826a9b
#define JNULL ((Jmalloc) 0)

static struct jmalloc j_head;
static Jmalloc memlist;
static int nfree = 0;
static int nblocks = 0;
static int init = 0;
static Jmalloc start;
static int free_called = 0;
static int malloc_called = 0;
static int used_mem = 0;
static int free_mem = 0;
static int used_blocks = 0;
static int free_blocks = 0;

#define cksum(p) (((int) &(p->cs)) - 1)
#define jloc(l) ((char *) (&l->space))
#define jmal(l) ((Jmalloc) (((char *)l) - JMSZ + PTSZ))
#define isfree(l) (l->nextfree != JNULL)

#define do_init() \
  if (!init) {\
    memlist = &j_head;\
    memlist->flink = memlist;\
    memlist->blink = memlist;\
    memlist->nextfree = memlist;\
    memlist->prevfree = memlist;\
    memlist->size = 0;\
    memlist->cs = cksum(memlist);\
    memlist->cs2 = &memlist->cs;\
    memlist->space = (char *) 0;\
    start = memlist;\
    init = 1;\
  }

dump_core()
{
  memlist->space[0] = 0;
}

char *set_used(l)
Jmalloc l;
{
  start = l->nextfree;
  l->prevfree->nextfree = l->nextfree;
  l->nextfree->prevfree = l->prevfree;
  l->prevfree = JNULL;
  l->nextfree = JNULL;
  used_mem += l->size;
  free_mem -= l->size;
  used_blocks++;
  free_blocks--;

  return jloc(l);
}

void *malloc(size)
int size;
{
  int redo;
  int done;
  Jmalloc l;
  char *tmp;
  Jmalloc newl;
  int newsize;
  
  do_init();
  malloc_called++;
  if (size <= 0) {
    fprintf(stderr, "Error: Malloc(%d) called\n", size);
    /* Dump core */
    dump_core();
  }
    
  if (size % PTSZ != 0) size += PTSZ - (size % PTSZ);

  done = 0;
  l = start;
  while(!done) {
    if (l->size >= size) {
      done = 1;
      redo = 0;
    } else {
      l = l->nextfree;
      done = (l == start);
      redo = done;
    } 
  }
      
  if (redo) {
    if (size > CHUNK_SIZE) 
      newsize = size + JMSZ; 
      else newsize = CHUNK_SIZE + JMSZ;
    newl = (Jmalloc) sbrk(newsize);
    while (newl == (Jmalloc) -1 && newsize > size + JMSZ) {
      newsize /= 2;
      if (newsize < size + JMSZ) newsize = size + JMSZ;
      newl = (Jmalloc) sbrk(newsize);
    }
      
    if (newl == (Jmalloc) -1) {
/*       fprintf(stderr, "Jmalloc: out of memory\n"); */
/*       fprintf(stderr, "Used bytes = %d, Free bytes = %d\n",  */
/*               used_mem, free_mem); */
/*       fprintf(stderr, "Trying to get %d bytes (chunk of %d)\n",  */
/*               size, newsize); */
      return NULL;
    }
    newl->flink = memlist;
    newl->blink = memlist->blink;
    newl->flink->blink = newl;
    newl->blink->flink = newl;
    newl->nextfree = memlist;
    newl->prevfree = memlist->prevfree;
    newl->nextfree->prevfree = newl;
    newl->prevfree->nextfree = newl;
    newl->size = ((char *) sbrk(0)) - jloc(newl) - PTSZ;
    free_mem += newl->size;
    newl->cs = cksum(newl);
    newl->cs2 = ((int *) (jloc(newl) + newl->size));
    *(newl->cs2) = cksum(newl);
    if(newl->size < size) {
      fprintf(stderr, "Newl->size(%d) < size(%d)\n", newl->size, size);
      exit(1);
    }
    free_blocks++;
    l = newl;
  } 

  if (l->size - size < JMSZ) {
    return set_used(l);
  } else {
    tmp = jloc(l);
    newl = (Jmalloc) (tmp + size + PTSZ);
    newl->flink = l->flink;
    newl->blink = l;
    newl->flink->blink = newl;
    newl->blink->flink = newl;
    newl->nextfree = l->nextfree;
    newl->prevfree = l;
    newl->nextfree->prevfree = newl;
    newl->prevfree->nextfree = newl;
    newl->size = l->size - size - JMSZ;
    newl->cs = cksum(newl);
    newl->cs2 = (int *) (jloc(newl) + newl->size);
    *(newl->cs2) = cksum(newl);
    free_mem += size + newl->size - l->size;
    free_blocks++;
    l->size = size;
    l->cs2 = ((int *) (jloc(l) + l->size));
    *(l->cs2) = cksum(l);
    return set_used(l);
  }
}

jmalloc_print_mem()
{
  Jmalloc l;
  int done;
  char *bufs[100];
  int sizes[100];
  int mc;
  int fc;
  int i, j;

  do_init();
  mc = malloc_called;
  fc = free_called;
  if (jmal(jloc(memlist)) != memlist) {
    fprintf(stderr, "TROUBLE: memlist=0x%x, jmal(jloc(memlist))=0x%x)\n",
            memlist, jmal(jloc(memlist)));
    exit(1);
  }
  done = 0;
  l = start;
  i = 0;
  while (!done) {
    if (cksum(l) != l->cs) {
      printf("Memory location 0x%x corrupted\n", jloc(l));
      exit(1);
    } else if (cksum(l) != *(l->cs2)) {
      printf("Memory location 0x%x corrupted\n", jloc(l));
      exit(1);
    }
    
    bufs[i] = jloc(l);
    sizes[i] = l->size;
    if (l->nextfree == 0) sizes[i] = -sizes[i];
    i++;
    l = l->flink;
    done = ((l == start) || i >= 100);
  }
  printf("Malloc called %d times\n", mc);
  printf("Free called %d times\n", fc);
  for (j = 0; j < i; j++) {
    printf("Loc = 0x%x, size = %d, free = %d\n", bufs[j], 
           (sizes[j] > 0) ? sizes[j] : -sizes[j], (sizes[j] >= 0));
  }
}

jmalloc_check_mem()
{
  Jmalloc l;
  int done;

  done = 0;

  l = start;

  while (!done) {
    if (cksum(l) != l->cs) {
      fprintf(stderr, "Memory chunk violated: 0x%x: %s 0x%x.  %s 0x%x\n", 
              jloc(l), "Checksum 1 is ", l->cs, "It should be", cksum(l));
      dump_core();
    } else if (cksum(l) != *(l->cs2)) {
      fprintf(stderr, "Memory chunk violated: 0x%x: %s 0x%x.  %s 0x%x\n", 
              jloc(l), "Checksum 2 is ", *(l->cs2), "It should be", cksum(l));
      dump_core();
    }
    l = l->flink;
    done = (l == start);
  }
}

  
void free(loc)
char *loc;
{
  Jmalloc l;
  Jmalloc pl, nl;

  do_init();
  free_called++;
  l = jmal(loc); 

  if (cksum(l) != l->cs) {
    fprintf(stderr, "Error on free: memory chunk violated: 0x%x\n", loc);
    dump_core();
  } else if (cksum(l) != *(l->cs2)) {
    fprintf(stderr, "Error on free: memory chunk violated: 0x%x\n", loc);
    dump_core();
  }

  used_mem -= l->size;
  free_mem += l->size;
  free_blocks++;
  used_blocks--;

  pl = l->blink;
  nl = l->flink;
  if (isfree(pl) && (jloc(pl)+pl->size + PTSZ == (char *) l)) {
    free_mem += JMSZ;
    pl->size += l->size + JMSZ;
    pl->flink = nl;
    pl->flink->blink = pl;
    l = pl;
    free_blocks--;
  } else {
    l->prevfree = start;
    l->nextfree = start->nextfree;
    l->nextfree->prevfree = l;
    l->prevfree->nextfree = l;
  }

  if (isfree(nl) && jloc(l)+l->size + PTSZ == (char *) nl) {
    free_mem += JMSZ;
    l->size += nl->size + JMSZ;
    l->flink = nl->flink;
    l->flink->blink = l;
    free_blocks--;
    nl->nextfree->prevfree = nl->prevfree;
    nl->prevfree->nextfree = nl->nextfree;
  }
  start = l;
}

void *realloc(loc, size)
char *loc;
int size;
{
  Jmalloc l;
  Jmalloc l2, nl;
  char *loc2;
  int i;
  Jmalloc newl;


  do_init();

  if (size <= 0) {
    fprintf(stderr, "Error: Malloc(%d) called\n", size);
    /* Dump core */
    dump_core();
  }
    
  if (size % PTSZ != 0) size += PTSZ - (size % PTSZ);

  l = jmal(loc); 

  if (cksum(l) != l->cs) {
    fprintf(stderr, "Error on realloc: memory chunk violated: 0x%x\n", loc);
    dump_core();
  } else if (cksum(l) != *(l->cs2)) {
    fprintf(stderr, "Error on realloc: memory chunk violated: 0x%x\n", loc);
    dump_core();
  }

  if (size < l->size) {
    if (l->size - size < JMSZ + 4) return loc;
    newl = (Jmalloc) (loc + size + PTSZ);
    newl->flink = l->flink;
    newl->blink = l;
    newl->flink->blink = newl;
    newl->blink->flink = newl;
    newl->nextfree = start->nextfree;
    newl->prevfree = start;
    newl->nextfree->prevfree = newl;
    newl->prevfree->nextfree = newl;
    newl->size = l->size - size - JMSZ;
    newl->cs = cksum(newl);
    newl->cs2 = (int *) (jloc(newl) + newl->size);
    *(newl->cs2) = cksum(newl);
    used_mem += size - l->size;
    free_mem += newl->size;
    free_blocks++;
    l->size = size;
    l->cs2 = ((int *) (jloc(l) + l->size));
    *(l->cs2) = cksum(l);
    start = newl;
    return loc;
  }


  nl = l->flink;

  if (isfree(nl) && (jloc(l)+l->size + PTSZ == (char *) nl) &&
      l->size + JMSZ + nl->size >= size) {
    start = nl;
    i = size - l->size - JMSZ;
    if (i < 0) i = 4;
    loc2 = malloc(i);
    l2 = jmal(loc2);
    if (l2 != nl) {
      fprintf(stderr, "Realloc internal error: l2 != nl\n");
      dump_core();
    }

    nl->flink->blink = nl->blink;
    nl->blink->flink = nl->flink;
    free_mem -= nl->size;
    used_mem += nl->size + JMSZ;
    free_blocks--;
    l->size += nl->size + JMSZ;
    l->cs2 = ((int *) (jloc(l) + l->size));
    *(l->cs2) = cksum(l);
    return loc;
  } else {
    loc2 = malloc(size);
    for (i = 0; i < l->size; i++) loc2[i] = loc[i];
    free(loc);
    return loc2;
  }
}

char *calloc(nelem, elsize)
int nelem, elsize;
{
  int *iptr;
  char *ptr;
  int sz;
  int i;

  sz = nelem*elsize;
  ptr = malloc(sz);
  iptr = (int *) ptr;
  
  for (i = 0; i < sz/sizeof(int); i++) iptr[i] = 0;
  for (i = i * sizeof(int); i < sz; i++) ptr[i] = 0;
  return ptr;
}

int mallopt(cmd, value)
int cmd, value;
{
  fprintf(stderr, "Mallopt is not defined...\n");
  exit(1);
}


jmalloc_usage()
{
  fprintf(stderr, "Jmalloc: %d %s %d block%s. %d %s %d block%s\n",
                    used_mem, "bytes used in", used_blocks, 
                    (used_blocks == 1) ? "" : "s", 
                    free_mem, "bytes free in", free_blocks,
                    (free_blocks == 1) ? "" : "s");
}