Dynamic memory allocation (강의자료 정리)

이후띵·2021년 12월 10일
0

컴퓨터시스템

목록 보기
1/8

하단에 유튜브 영상자료 있음.

The malloc package

#include <stdlib.h>
void *malloc (size_t size)

  • Successful :
    - Retruns a pointer to a memory block of at least size bytes aligned to an 8-byte (x86) or 16-byte (x86-64) boundary.
    - if size == 0, return NULL

  • Unsuccessful :
    - Returns NULL(0) and set errno


void free(void *p)

  • Returns the block pointed at by p to pool of available memory.
  • p must come from a previous call to malloc or realloc.

other functions

  • calloc : Version of malloc that initializes allocated block to zero.
  • realloc : Changes the size of a previously allocated block.
  • sbrk : Used internally by allocators to grow or shrink heap.

Allocation Example

  • p1 = malloc(4)

  • p2 = malloc(5)

  • p3 = malloc(6)

  • free(p2)

  • p4 = malloc(2)

Constraints

  • Applications
    - Can issue arbitrary sequence of malloc and free requests
    - free request must be to a malloc’d block
  • Allocators
    - Can’t control number or size of allocated blocks
    - Must respond immediately to malloc requests
    	> i.e., can only place allocated blocks in free memory
    - Must align blocks so they satisfy all alignment requirements
    	> 8-byte (x86) or 16-byte (x86-64) alignment on Linux boxes
    - Can manipulate and modify only free memory
    - Can’t move the allocated blocks once they are malloc’d
    	> i.e., compaction is not allowed

Allocators are really interesting objects because they combine a trade-off of sort of running time sort of speed and space so it's kinda space and performance trade-off. you are trying to optimize both you want it to run as quickly as possible but you want to use you want it to use the VM in the heap as efficiently as possible.

It's very easy to make a really fast malloc that that has terrible memory utilization.

Performance Goal :

Maximize throughput & Peak Memory Utilization.

  • These goals are often conflicting

one obvious thing is that since blocks have to be aligned to some .. you know if there's sixteen by two lines then blocks have to start on 16 by boundaries and they have to be at least 16 bytes so if you were to request a payload of 2 bytes you'd have a lot of wasted bytes right that would sort of decrease the utilazation. This overhead is unavoidable but your job as a someone who writes a implements malloc is to try to keep that as small as possible.

Fragmentation

  • Internal Fragmentation
  • External Fragmentation

---따로 필기한 자료---




동적 메모리 할당 카네기 대학 강의 영상.

profile
이후띵's 개발일지

0개의 댓글