feat: update structure

This commit is contained in:
2024-01-22 14:27:40 +08:00
parent 7836c9185c
commit 3544a28a2e
559 changed files with 120846 additions and 4102 deletions

View File

@@ -0,0 +1,40 @@
#include "mymalloc.h"
#include "bitmap.h"
#include <stdio.h>
#include <stdlib.h>
char _heap[MEMSIZE] = {0};
unsigned char _bitmap[BITMAP_SIZE] = {};
char lens[MEMSIZE] = {0};
// Do not change this. Used by the test harness.
// You may however use this function in your code if necessary.
long get_index(void *ptr) {
if (ptr == NULL)
return -1;
else
return (long)((char *)ptr - &_heap[0]);
}
void print_memlist() { print_map(_bitmap, BITMAP_SIZE); }
// Allocates size bytes of memory and returns a pointer
// to the first byte.
void *mymalloc(size_t size) {
long start = search_map(_bitmap, BITMAP_SIZE, size);
if (start == -1)
return NULL;
allocate_map(_bitmap, start, size);
lens[start] = size;
return (void *)(&_heap[start]);
}
// Frees memory pointer to by ptr.
void myfree(void *ptr) {
if (ptr == NULL)
return;
long ptrIdx = get_index(ptr);
long size = lens[ptrIdx];
free_map(_bitmap, ptrIdx, size);
lens[ptrIdx] = 0;
}