forked from sadiredd-sv/Malloc--Dynamic-Memory-Allocator
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mm-naive.c
148 lines (122 loc) · 3.11 KB
/
mm-naive.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
/*
* mm-naive.c - The fastest, least memory-efficient malloc package.
*
* In this naive approach, a block is allocated by simply incrementing
* the brk pointer. Blocks are never coalesced or reused. The size of
* a block is found at the first aligned word before the block (we need
* it for realloc).
*
* This code is correct and blazingly fast, but very bad usage-wise since
* it never frees anything.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mm.h"
#include "memlib.h"
/* If you want debugging output, use the following macro. When you hand
* in, remove the #define DEBUG line. */
#define DEBUG
#ifdef DEBUG
# define dbg_printf(...) printf(__VA_ARGS__)
#else
# define dbg_printf(...)
#endif
/* do not change the following! */
#ifdef DRIVER
/* create aliases for driver tests */
#define malloc mm_malloc
#define free mm_free
#define realloc mm_realloc
#define calloc mm_calloc
#endif /* def DRIVER */
/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
#define SIZE_PTR(p) ((size_t*)(((char*)(p)) - SIZE_T_SIZE))
/*
* mm_init - Called when a new trace starts.
*/
int mm_init(void)
{
return 0;
}
/*
* malloc - Allocate a block by incrementing the brk pointer.
* Always allocate a block whose size is a multiple of the alignment.
*/
void *malloc(size_t size)
{
int newsize = ALIGN(size + SIZE_T_SIZE);
unsigned char *p = mem_sbrk(newsize);
//dbg_printf("malloc %u => %p\n", size, p);
if ((long)p < 0)
return NULL;
else {
p += SIZE_T_SIZE;
*SIZE_PTR(p) = size;
return p;
}
}
/*
* free - We don't know how to free a block. So we ignore this call.
* Computers have big memories; surely it won't be a problem.
*/
void free(void *ptr){
/*Get gcc to be quiet */
ptr = ptr;
}
/*
* realloc - Change the size of the block by mallocing a new block,
* copying its data, and freeing the old block. I'm too lazy
* to do better.
*/
void *realloc(void *oldptr, size_t size)
{
size_t oldsize;
void *newptr;
/* If size == 0 then this is just free, and we return NULL. */
if(size == 0) {
free(oldptr);
return 0;
}
/* If oldptr is NULL, then this is just malloc. */
if(oldptr == NULL) {
return malloc(size);
}
newptr = malloc(size);
/* If realloc() fails the original block is left untouched */
if(!newptr) {
return 0;
}
/* Copy the old data. */
oldsize = *SIZE_PTR(oldptr);
if(size < oldsize) oldsize = size;
memcpy(newptr, oldptr, oldsize);
/* Free the old block. */
free(oldptr);
return newptr;
}
/*
* calloc - Allocate the block and set it to zero.
*/
void *calloc (size_t nmemb, size_t size)
{
size_t bytes = nmemb * size;
void *newptr;
newptr = malloc(bytes);
memset(newptr, 0, bytes);
return newptr;
}
/*
* mm_checkheap - There are no bugs in my code, so I don't need to check,
* so nah!
*/
void mm_checkheap(int verbose){
/*Get gcc to be quiet. */
verbose = verbose;
}