Malloc 정리 실패..

hodeethelion·2023년 4월 6일
0

CSAPP

목록 보기
5/6

처음 들어가는 거니까 먼저 github readme부터 찬찬히 읽어보도록 하자~

  • Main Files
  1. mm.cmm.h만 건드는 solution malloc package이다.

  2. 실행방법

make
./mdriver -V

  1. 설정해야 할것!

malloc 패키지 안에 다음과 같이 들어가 있을 것이다.

int mm_init(void); 
/* perform initialization and allocate the initial heap area
   return value shoud be -1 or 0
*/

void *mm_malloc(size_t size); 
/* returns pointer to an allocated block payload
libc 에 있는 malloc이랑 비교할 것입니다.
*/


void mm_free(void *ptr);
/* frees the block pointed to by ptr. return nothing. 
only work when the ptr was by an earlier call to mm_malloc or mm_realloc.

*/
void *mm_realloc(void * ptr, size_t size);
/* ptr is null, the call = mm_malloc(size)

*/

The heap is the diametrical opposite of the stack. The heap is a large pool of memory that can be used dynamically – it is also known as the “free store”

Heap consistency checker

• Is every block in the free list marked as free?
• Are there any contiguous free blocks that somehow escaped coalescing?
• Is every free block actually in the free list?
• Do the pointers in the free list point to valid free blocks?
• Do any allocated blocks overlap?
• Do the pointers in a heap block point to valid heap addresses?


우리가 어디에 메모리를 할당해야되는 지 부터 알아보자면...!

이렇게 프로세스가 구성 되어 있는데 동적 메모리 할당에 접근해야 하는데 이걸 어캐? ...


방법이 있다! 즉 sbrk를 이용하는 것이다.

#include <unistd.h>

int main()
{
    void *p = sbrk(sizeof(int));
    if (p == (void*) -1) {
        // error occurred
        return 1;
    }
    int *q = (int*) p;
    *q = 42;
    // release memory when done
    sbrk(-sizeof(int));
    return 0;
}

이런 방식으로 접근하면 되는데? ㅎㅎ 시발무튼


CSAPP 책으로 돌아가서 먼저 explicit에 대한 정의, implicit list의 블록 구조를 보여 준다.
블록 크기의 하위3비트는 항상 0으로 설정. header 부분의 a를 보면 1은 할당 상태 0일때는 써도 되는 상태를 의미한다.

이거 이렇게 대충 정리하면 안될 것 같고 하나씩 영어로 보면서 정리하는 게 훨씬 나을 것 같다는 생각이다 든다.

profile
가슴을 따라가자

0개의 댓글

관련 채용 정보