├── README.md └── main.c /README.md: -------------------------------------------------------------------------------- 1 | # malloc -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define u8 uint8_t 7 | #define u16 uint16_t 8 | #define STACK_SIZE 32 9 | #define HEAP_SIZE STACK_SIZE * 4 10 | #define HEADER 4 11 | 12 | static u16 IN_USE; 13 | 14 | typedef struct virtual_memory 15 | { 16 | u8 stack[STACK_SIZE]; 17 | char** unmapped; 18 | u8 heap[HEAP_SIZE]; 19 | 20 | struct 21 | { 22 | char** data; 23 | char** bss; 24 | char* text; 25 | }data_t; 26 | }virtual_memory_t; 27 | 28 | 29 | typedef struct entity 30 | { 31 | u8* ptr; 32 | int size; 33 | }entity_t; 34 | 35 | 36 | entity_t LIST[40]; 37 | 38 | void LOG() 39 | { 40 | printf("OUR LIST\n"); 41 | for (unsigned i = 0; i < IN_USE; i++) 42 | { 43 | printf("Data + HEADER. [%p]. Memory of our heap free: [%u]\n", LIST[i].ptr, LIST[i].size); 44 | } 45 | printf("Entities in use:[%d]\n", (sizeof(LIST) / sizeof(LIST[0]) - IN_USE)); 46 | } 47 | 48 | 49 | entity_t* new_entity(size_t size) 50 | { 51 | if (LIST[0].ptr == NULL && LIST[0].size == 0) 52 | { 53 | static virtual_memory_t vm; 54 | LIST[0].ptr = vm.heap; 55 | LIST[0].size = HEAP_SIZE; 56 | IN_USE++; 57 | LOG(); 58 | } 59 | 60 | entity_t* best = LIST; 61 | 62 | for (unsigned i = 0; i < IN_USE; i++) 63 | { 64 | if (LIST[i].size >= size && LIST[i].size < best->size) 65 | { 66 | best = &LIST[i]; 67 | } 68 | } 69 | 70 | return best; 71 | } 72 | 73 | void* w_malloc(size_t size) 74 | { 75 | assert((size+HEADER) <= HEAP_SIZE); 76 | 77 | size += HEADER; 78 | 79 | entity_t* e = new_entity(size); 80 | 81 | u8* start = e->ptr; 82 | u8* user_ptr = start + HEADER; 83 | 84 | *start = size; 85 | 86 | e->ptr += size; 87 | e->size -= size; 88 | 89 | assert(e->size >= 0); 90 | 91 | LOG(); 92 | return user_ptr; 93 | } 94 | 95 | 96 | void w_free(void* ptr) 97 | { 98 | u8* start = (u8*)ptr - HEADER; 99 | 100 | LIST[IN_USE].ptr = &(*start); 101 | LIST[IN_USE].size = (u8)*((u8*)ptr - HEADER); 102 | IN_USE++; 103 | LOG(); 104 | } 105 | 106 | 107 | 108 | void test() 109 | { 110 | typedef struct foo 111 | { 112 | int a; 113 | int b; 114 | }foo_t; 115 | 116 | foo_t* foo; 117 | int* bazz; 118 | char* bar; 119 | 120 | foo = w_malloc(sizeof(foo_t)); 121 | bar = w_malloc(5); 122 | bazz = w_malloc(sizeof(int)); 123 | 124 | foo->a = 5; 125 | foo->b = 10; 126 | 127 | strcpy_s(bar, "bar"); 128 | 129 | memcpy(bazz, &foo->a, sizeof(int)); 130 | 131 | printf("Address: [%p], data: [%d] [%d]\n", foo, foo->a, foo->b); 132 | printf("Address: [%p], data: [%s] \n", bar, bar); 133 | printf("Address: [%p], data: [%d] \n", bazz, *bazz); 134 | 135 | 136 | w_free(foo); 137 | w_free(bar); 138 | 139 | char* other = w_malloc(96); 140 | strcpy(other, "other"); 141 | printf("Address: [%p], data: [%s] \n", other, other); 142 | } 143 | 144 | int main(int argc, char** argv) 145 | { 146 | test(); 147 | return 0; 148 | } --------------------------------------------------------------------------------