-
Notifications
You must be signed in to change notification settings - Fork 1
/
mymalloc.h
82 lines (63 loc) · 1.65 KB
/
mymalloc.h
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
#ifndef MYMALLOC_H
#define MYMALLOC_H
#define malloc(x) mymalloc(x, __FILE__, __LINE__)
#define free(x) myfree(x, __FILE__, __LINE__)
struct _meta { // Since this struct has only one member,
// an alternative would be to do something like
// typedef short meta;
unsigned short info;
//
// layout of info:
//
// Byte 2 | Byte 1
// __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
// ^ ^
// | \_______
// in_use Size begins here
};
/*
* To read in_use:
* (info >> 15) & 1
*
* To set in_use to true:
* info |= 1 << 15
*
* To set in_use to false:
* info &= ~(1 << 15)
*
* To read size:
* We need to AND info with 0111 1111 1111 1111
* info & 0x7FFF
*
* To write to size:
* WRONG:
* First mask the new size to be sure we don't
* overwrite the in_use bit
* info = newsize & 0x7FFF
*
* RIGHT:
* We only want to change the lower 15 bits. We want
* to leave the upper bit alone.
*
* Proceed as follows: First zero out the lower 15 bits.
* To do this, AND info with 1000 0000 0000 0000
* info = info & 0x8000
*
* Now take that result, and OR it with the (masked) new size.
* Note we don't need to shift it, since it's the LOWER 15 bits.
*
* info = (info & 0x8000) | (new_size & 0x7FFF)
*/
enum _boolean {FALSE, TRUE};
typedef struct _meta meta;
typedef enum _boolean boolean;
int read_in_use(meta *m);
void set_in_use(meta *m);
void clear_in_use(meta *m);
unsigned short read_size(meta *m);
void write_size(meta *m, unsigned short new_size);
void bootstrap();
void *mymalloc(size_t size, char *filename, int line);
void myfree(void *p, char *filename, int line);
void info();
#endif