1
- #include <unistd.h>
2
- #include <errno.h>
3
- #include <stdio.h>
4
- #include <stdlib.h>
5
- #include <sys/mman.h>
6
-
7
-
8
-
9
- //Initialize freelist
10
- struct header {
11
- size_t size ;
12
- struct header * next ;
13
- };
14
- static struct header * freelist = NULL ;
15
- void init (){
16
- if (freelist == NULL ){
17
- freelist = mmap (NULL ,4096 ,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 );
18
- freelist -> size = 4096 - sizeof (struct header );
19
- freelist -> next = NULL ;
20
- }
21
- }
22
- //Function to show state of freelist
23
- void info (){
24
- struct header * current = freelist ;
25
- int blocknumber = 1 ;
26
-
27
- fprintf (stderr ,"------------------------------------\n" );
28
- fprintf (stderr ,"Free List Contents:\n" );
29
- while (current ) {
30
- fprintf (stderr ,"Block %d: Address=%p, Size=%zu\n" , blocknumber , current , current -> size );
31
- current = current -> next ;
32
- blocknumber ++ ;
33
- }
34
- fprintf (stderr ,"------------------------------------\n" );
35
- }
36
-
37
-
38
-
39
- //Function to check int overflow on multiplication
40
- int checkoverflow (int m , int n ){
41
- if (m == 0 )return 0 ;
42
- int p = m * n ;
43
- if (p /n == m )return 0 ;
44
- else return 1 ;
45
- }
46
-
47
-
48
-
49
- // Own implemetnation of malloc
50
- void * my_malloc (size_t size ) {
51
-
52
- if (size == 0 )return NULL ;
53
- init ();
54
-
55
- struct header * prev = NULL ;
56
- struct header * current = freelist ;
57
-
58
- // First Fit
59
- while (current ) {
60
- if (current -> size >= size + sizeof (struct header )) {
61
- if (current -> size > size + sizeof (struct header )) {
62
-
63
- struct header * new_block = (struct header * )((char * )current + size + sizeof (struct header ));
64
- new_block -> size = current -> size - size - sizeof (struct header );
65
- new_block -> next = current -> next ;
66
- current -> size = size ;
67
- current -> next = (struct header * )123456 ; //magic
68
- if (prev )
69
- prev -> next = new_block ;
70
- else
71
- freelist = new_block ;
72
- return (void * )(current + 1 );
73
- }
74
- else {
75
- if (prev )
76
- prev -> next = current -> next ;
77
- else
78
- freelist = current -> next ;
79
- current -> next = (struct header * )123456 ; //magic
80
- current -> size = size ;
81
- return (void * )(current + 1 );
82
- }
83
- }
84
- prev = current ;
85
- current = current -> next ;
86
- }
87
-
88
- // If no suitable block is found, request more memory.
89
- struct header * new_block = sbrk (size + sizeof (struct header ));
90
- if (new_block == (void * )-1 ) {
91
- errno = ENOMEM ;
92
- return NULL ; // Out of memory
93
- }
94
- new_block -> size = size ;
95
- new_block -> next = (struct header * )123456 ; //magic
96
- return (void * )(new_block + 1 );
97
- }
98
-
99
- // Own implementation of calloc
100
- void * my_calloc (size_t nelem , size_t size ) {
101
-
102
- if (checkoverflow (nelem ,size )== 1 ){
103
- errno = EOVERFLOW ;
104
- return NULL ;
105
- }
106
- size_t finalsize = nelem * size ;
107
- void * ptr = my_malloc (finalsize );
108
- if (ptr == NULL )return NULL ;
109
- memset (ptr , 0 , finalsize );
110
- return ptr ;
111
-
112
- }
113
-
114
- // Own implementation of free
115
- void my_free (void * ptr ) {
116
-
117
- if (ptr == NULL )return ;
118
- struct header * block = (struct header * )ptr - 1 ;
119
- if (block -> next != (struct header * )123456 ) return ;
120
- block -> next = freelist ;
121
- freelist = block ;
122
-
1
+ #include <unistd.h>
2
+ #include <errno.h>
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <sys/mman.h>
6
+
7
+
8
+
9
+ //Initialize freelist
10
+ struct header {
11
+ size_t size ;
12
+ struct header * next ;
13
+ };
14
+ static struct header * freelist = NULL ;
15
+ void init (){
16
+ if (freelist == NULL ){
17
+ freelist = mmap (NULL ,4096 ,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 );
18
+ freelist -> size = 4096 - sizeof (struct header );
19
+ freelist -> next = NULL ;
20
+ }
21
+ }
22
+ //Function to show state of freelist
23
+ void info (){
24
+ struct header * current = freelist ;
25
+ int blocknumber = 1 ;
26
+
27
+ fprintf (stderr ,"------------------------------------\n" );
28
+ fprintf (stderr ,"Free List Contents:\n" );
29
+ while (current ) {
30
+ fprintf (stderr ,"Block %d: Address=%p, Size=%zu\n" , blocknumber , current , current -> size );
31
+ current = current -> next ;
32
+ blocknumber ++ ;
33
+ }
34
+ fprintf (stderr ,"------------------------------------\n" );
35
+ }
36
+
37
+
38
+
39
+ //Function to check int overflow on multiplication
40
+ int checkoverflow (int m , int n ){
41
+ if (m == 0 )return 0 ;
42
+ int p = m * n ;
43
+ if (p /n == m )return 0 ;
44
+ else return 1 ;
45
+ }
46
+
47
+
48
+
49
+ // Own implemetnation of malloc
50
+ void * my_malloc (size_t size ) {
51
+
52
+ if (size == 0 )return NULL ;
53
+ init ();
54
+
55
+ struct header * prev = NULL ;
56
+ struct header * current = freelist ;
57
+
58
+ // First Fit
59
+ while (current ) {
60
+ if (current -> size >= size + sizeof (struct header )) {
61
+ if (current -> size > size + sizeof (struct header )) {
62
+
63
+ struct header * new_block = (struct header * )((char * )current + size + sizeof (struct header ));
64
+ new_block -> size = current -> size - size - sizeof (struct header );
65
+ new_block -> next = current -> next ;
66
+ current -> size = size ;
67
+ current -> next = (struct header * )123456 ; //magic
68
+ if (prev )
69
+ prev -> next = new_block ;
70
+ else
71
+ freelist = new_block ;
72
+ return (void * )(current + 1 );
73
+ }
74
+ else {
75
+ if (prev )
76
+ prev -> next = current -> next ;
77
+ else
78
+ freelist = current -> next ;
79
+ current -> next = (struct header * )123456 ; //magic
80
+ current -> size = size ;
81
+ return (void * )(current + 1 );
82
+ }
83
+ }
84
+ prev = current ;
85
+ current = current -> next ;
86
+ }
87
+
88
+ // If no suitable block is found, request more memory.
89
+ struct header * new_block = sbrk (size + sizeof (struct header ));
90
+ if (new_block == (void * )-1 ) {
91
+ errno = ENOMEM ;
92
+ return NULL ; // Out of memory
93
+ }
94
+ new_block -> size = size ;
95
+ new_block -> next = (struct header * )123456 ; //magic
96
+ return (void * )(new_block + 1 );
97
+ }
98
+
99
+ // Own implementation of calloc
100
+ void * my_calloc (size_t nelem , size_t size ) {
101
+
102
+ if (checkoverflow (nelem ,size )== 1 ){
103
+ errno = EOVERFLOW ;
104
+ return NULL ;
105
+ }
106
+ size_t finalsize = nelem * size ;
107
+ void * ptr = my_malloc (finalsize );
108
+ if (ptr == NULL )return NULL ;
109
+ memset (ptr , 0 , finalsize );
110
+ return ptr ;
111
+
112
+ }
113
+
114
+ // Own implementation of free
115
+ void my_free (void * ptr ) {
116
+
117
+ if (ptr == NULL )return ;
118
+ struct header * block = (struct header * )ptr - 1 ;
119
+ if (block -> next != (struct header * )123456 ) return ;
120
+ block -> next = freelist ;
121
+ freelist = block ;
122
+
123
123
}
0 commit comments