memlib.c 패키지에서 제공하는 메모리 시스템의 모델 사용 (목적 : 설계한 할당기가 기존 시스템 수준의 malloc과 충돌하지 않는 것)
🔍 memlib.c
/* Private global variables */
static char *mem_heap; // Points to first byte of heap
static char *mem_brk; // Points to last byte of heap plus 1
static char *mem_max_addr; // Max legal heap addr plus 1
/*
* mem_init : Initialize the memory system model
* double-word alignment
*/
void mem_init(void)
{
mem_heap = (char *)Malloc(MAX_HEAP);
mem_brk = (char *)mem_heap;
mem_max_addr = (char *)(mem_heap + MAX_HEAP);
}
/*
* mem_sbrk : Simple model of the sbrk function. Extends the heap by incr bytes
* and returns the start address of the new(??) area. (The heap cannot be shrunk.)
*/
void *mem_sbrk(int incr)
{
char *old_brk = mem_brk;
if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) {
errno = ENOMEM;
fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory...\\n");
return (void *)-1;
}
mem_brk += incr;
return (void *)old_brk;
}
🔍 중요한 세 개의 함수
extern int mm_init(void);
extern void *mm_malloc(size_t size); //Returns pointer to an allocated block
extern void mm_free(void *ptr); //ptr : pointer to an allocated block
mm_init