├── LICENSE ├── README.md └── malloc.c /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Dasun Thathsara 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # user-define-malloc-using-char-array 2 | User define malloc using char array 3 | 4 |
5 |

6 | 7 | License: MIT 8 | 10 | -------------------------------------------------------------------------------- /malloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define memSize 1000 3 | 4 | char memory[memSize], *sPoint, * ePoint; 5 | int currentPosition = 0; 6 | 7 | struct memoryBlock{ 8 | int avail; 9 | int lastIndex; 10 | int firstIndex; 11 | struct memoryBlock * next; 12 | struct memoryBlock * lastPoint; 13 | struct memoryBlock * firstPoint; 14 | } * start, * ptr3, * ptr, * ptr4; 15 | 16 | void * MyMalloc(int size){ 17 | if(currentPosition + size + sizeof(struct memoryBlock) > memSize){ 18 | return NULL; 19 | } 20 | 21 | struct memoryBlock * newNode; 22 | if(start == NULL){ 23 | start = (void*)memory; 24 | newNode = start; 25 | sPoint = (char*) start; 26 | newNode->firstPoint = start; 27 | newNode->lastPoint = sPoint + size + sizeof(struct memoryBlock); 28 | start = newNode; 29 | newNode->avail = 0; 30 | newNode->firstIndex = 0; 31 | newNode->lastIndex = size + sizeof(struct memoryBlock); 32 | newNode->next = NULL; 33 | } 34 | else{ 35 | ptr = start; 36 | int find = 0; 37 | // ptr4 = ptr->next; 38 | while(ptr->next != NULL){ 39 | if(ptr->avail != 1){ 40 | // If the free block size and required memory size is equal 41 | if(ptr->lastIndex - ptr->firstIndex == size + sizeof(struct memoryBlock)){ 42 | newNode = ptr->firstPoint; 43 | newNode->avail = 0; 44 | 45 | find = 1; 46 | } 47 | 48 | // If the current block size is greater than required space 49 | else if(ptr->lastIndex - ptr->firstIndex > size + sizeof(struct memoryBlock)){ 50 | struct memoryBlock * newNode1; 51 | ptr3 = ptr; 52 | int f = ptr->lastIndex; 53 | char * la = (char*)ptr->lastPoint; 54 | char * na = (char*)ptr->next; 55 | sPoint = (char*) ptr->firstPoint; 56 | 57 | newNode = ptr; 58 | newNode->lastPoint = sPoint + size + sizeof(struct memoryBlock); 59 | newNode->lastIndex = ptr->firstIndex + size + sizeof(struct memoryBlock); 60 | newNode->avail = 0; 61 | 62 | newNode1 = newNode->lastPoint; 63 | newNode1->next = ptr->next; 64 | 65 | newNode1 = newNode->lastPoint; 66 | newNode1->firstPoint = newNode->lastPoint; 67 | newNode1->firstIndex = newNode->lastIndex + 1; 68 | newNode1->avail = 1; 69 | newNode1->lastIndex = f; 70 | newNode1->lastPoint = la; 71 | newNode->next = newNode1; 72 | newNode1->next = na; 73 | ptr3 = newNode1->next; 74 | find = 1; 75 | } 76 | } 77 | ptr = ptr->next; 78 | } 79 | 80 | // If current block is not free, the program creates a new block and connect it into current block 81 | if(find == 0){ 82 | newNode = ptr->lastPoint; 83 | ptr->next = newNode; 84 | newNode = ptr->lastPoint; 85 | sPoint = (char*) ptr->lastPoint; 86 | newNode->firstPoint = ptr->lastPoint; 87 | newNode->firstIndex = ptr->lastIndex + 1; 88 | newNode->lastIndex = size + sizeof(struct memoryBlock) + ptr->lastIndex + 1; 89 | ptr = ptr->lastPoint; 90 | newNode->lastPoint = sPoint + size + sizeof(struct memoryBlock); 91 | newNode->avail = 0; 92 | newNode->next = NULL; 93 | } 94 | } 95 | 96 | currentPosition = newNode->lastIndex + 1; 97 | return newNode; 98 | } 99 | 100 | void traverse(){ 101 | ptr = start; 102 | while(ptr->next != NULL){ 103 | printf("\n------ %4d %4d %4d", ptr->avail, ptr->firstIndex, ptr->lastIndex); 104 | ptr = ptr->next; 105 | } 106 | } 107 | 108 | void merge(){ 109 | ptr = start; 110 | while(ptr->next != NULL){ 111 | if(ptr->avail == 1 && ptr->next->avail == 1){ 112 | ptr->lastPoint = ptr->next->lastPoint; 113 | ptr->lastIndex = ptr->next->lastIndex; 114 | ptr->next = ptr->next->next; 115 | } 116 | ptr = ptr->next; 117 | } 118 | } 119 | 120 | void MyFree(void * pointer){ 121 | ptr = start; 122 | while(ptr->next != pointer){ 123 | ptr = ptr->next; 124 | } 125 | 126 | ptr->next->avail = 1; 127 | merge(); 128 | } 129 | 130 | int main() { 131 | printf("%llu\n", sizeof(struct memoryBlock)); 132 | 133 | int *w = (int*) MyMalloc(5); 134 | printf("%p %p\n", w, memory); 135 | 136 | int *r = (int*) MyMalloc(10); 137 | printf("%p %p\n", r, memory); 138 | 139 | int *k = (int*) MyMalloc(5); 140 | printf("%p %p\n", k, memory); 141 | 142 | char *g = (char*) MyMalloc(50); 143 | printf("%p %p\n", g, memory); 144 | 145 | float *t = (float*) MyMalloc(100); 146 | printf("%p %p\n", t, memory); 147 | 148 | double *l = (double*) MyMalloc(60); 149 | printf("%p %p\n", l, memory); 150 | 151 | traverse(); 152 | printf("\n"); 153 | 154 | MyFree(g); 155 | 156 | traverse(); 157 | printf("\n"); 158 | 159 | float *f = (float*) MyMalloc(200); 160 | printf("%p %p\n", f, memory); 161 | 162 | traverse(); 163 | printf("\n"); 164 | 165 | return 0; 166 | } 167 | --------------------------------------------------------------------------------