-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmm.c
More file actions
150 lines (112 loc) · 4.03 KB
/
mm.c
File metadata and controls
150 lines (112 loc) · 4.03 KB
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
/*
* mm-naive.c - The fastest, least memory-efficient malloc package.
*
* In this naive approach, a block is allocated by simply incrementing
* the brk pointer. A block is pure payload. There are no headers or
* footers. Blocks are never coalesced or reused. Realloc is
* implemented directly using mm_malloc and mm_free.
*
* NOTE TO STUDENTS: Replace this header comment with your own header
* comment that gives a high level description of your solution.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include "mm.h"
#include "memlib.h"
/*********************************************************
* NOTE TO STUDENTS: Before you do anything else, please
* provide your team information in the following struct.
********************************************************/
team_t team = {
/* Team name */
"team name",
/* First member's full name */
"member 1",
/* First member's email address */
"member_1@cse.iitb.ac.in",
/* Second member's full name (leave blank if none) */
"member 2",
/* Second member's email address (leave blank if none) */
"member_2@cse.iitb.ac.in"
};
/* 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)))
/*
* mm_init - initialize the malloc package.
*/
void *init_mem_sbrk_break = NULL;
int mm_init(void)
{
//This function is called every time before each test run of the trace.
//It should reset the entire state of your malloc or the consecutive trace runs will give wrong answer.
/*
* This function should initialize and reset any data structures used to represent the starting state(empty heap)
*
* This function will be called multiple time in the driver code "mdriver.c"
*/
return 0; //Returns 0 on successfull initialization.
}
//---------------------------------------------------------------------------------------------------------------
/*
* mm_malloc - Allocate a block by incrementing the brk pointer.
* Always allocate a block whose size is a multiple of the alignment.
*/
void *mm_malloc(size_t size)
{
/*
* This function should keep track of the allocated memory blocks.
* The block allocation should minimize the number of holes (chucks of unusable memory) in the heap memory.
* The previously freed memory blocks should be reused.
* If no appropriate free block is available then the increase the heap size using 'mem_sbrk(size)'.
* Try to keep the heap size as small as possible.
*/
if(size <= 0){ // Invalid request size
return NULL;
}
size = ((size+7)/8)*8; //size alligned to 8 bytes
return mem_sbrk(size); //mem_sbrk() is wrapper function for the sbrk() system call.
//Please use mem_sbrk() instead of sbrk() otherwise the evaluation results
//may give wrong results
}
void mm_free(void *ptr)
{
/*
* Searches the previously allocated node for memory block with base address ptr.
*
* It should also perform coalesceing on both ends i.e. if the consecutive memory blocks are
* free(not allocated) then they should be combined into a single block.
*
* It should also keep track of all the free memory blocks.
* If the freed block is at the end of the heap then you can also decrease the heap size
* using 'mem_sbrk(-size)'.
*/
}
/*
* mm_realloc - Implemented simply in terms of mm_malloc and mm_free
*/
void *mm_realloc(void *ptr, size_t size)
{
size = ((size+7)/8)*8; //8-byte alignement
if(ptr == NULL){ //memory was not previously allocated
return mm_malloc(size);
}
if(size == 0){ //new size is zero
mm_free(ptr);
return NULL;
}
/*
* This function should also copy the content of the previous memory block into the new block.
* You can use 'memcpy()' for this purpose.
*
* The data structures corresponding to free memory blocks and allocated memory
* blocks should also be updated.
*/
mm_free(ptr);
return mem_sbrk(size);
}