-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.c
51 lines (35 loc) · 951 Bytes
/
malloc.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
#include <stdlib.h>
#include "myAllocator.h"
#include "string.h"
#define align4(x) ((x+3) & ~3)
#define align8(x) ((x+7) & ~7)
/* first, the standard malloc functions */
void *malloc(size_t NBYTES) {
return firstFitAllocRegion(NBYTES);
}
void *realloc(void *APTR, size_t NBYTES) {
return resizeRegion(APTR, NBYTES);
}
void free(void *APTR) { freeRegion(APTR); }
void *memalign(size_t ALIGN, size_t NBYTES) { /* ignore ALIGN -- hack -- */
void *p = malloc(NBYTES+ALIGN);
return p;
}
size_t malloc_usable_size(void *APTR) { return computeUsableSpace(regionToPrefix(APTR)); }
/* some systems require that malloc replacements provide these... */
void *calloc(size_t N, size_t S) {
size_t req;
void *p;
if (S <= 4)
req = N * align4(S);
else
req = N * align8(S);
p = malloc(req);
memset(p, 0, req);
return p;
}
char *strdup(const char *s) {
void *p = malloc(strlen(s) + 1);
strcpy(p, s);
return p;
}