Heap analysis/glibc 2.23 (11) 썸네일형 리스트형 (glibc 2.23) malloc_consolidate 전체 코드 아래 더보기 더보기 인자 및 변수 static void malloc_consolidate(mstate av) { mfastbinptr* fb; /* current fastbin being consolidated */ mfastbinptr* maxfb; /* last fastbin (for loop control) */ mchunkptr p; /* current chunk being consolidated */ mchunkptr nextp; /* next chunk to consolidate */ mchunkptr unsorted_bin; /* bin header */ mchunkptr first_unsorted; /* chunk to link to */ /* These have same use.. (glibc 2.23) _int_malloc _int_malloc에서 수행되는 코드의 흐름을 최대한 간략화하면 아래와 같다 fastbin과 small bin에 재할당할 수 있는 청크 있으면 재할당 unsorted bin에 last_remainder만이 존재할 경우 last_remainder를 재할당 시도 unsortd bin을 차례차례 재할당 시도하고 실패 시 small bin과 large bin에 연결 (2와 3번 과정 unsorted bin 비워질 때까지 또는 10000번 반복할 때까지 반복) large bin으로 재할당 시도 binmap을 검사하여 재할당 시도 top 청크를 사용하여 할당 시도 sysmalloc으로 할당 전체 코드는 아래 더보기 (코드 양이 꽤 많다 에디터로 따로 코드를 볼수 있으면 열어보지 말자) 더보기 static void.. (glibc 2.23) malloc 전체 코드는 더보기 참고 더보기 void * __libc_malloc (size_t bytes) { mstate ar_ptr; void *victim; void *(*hook) (size_t, const void *) = atomic_forced_read (__malloc_hook); if (__builtin_expect (hook != NULL, 0)) return (*hook)(bytes, RETURN_ADDRESS (0)); arena_get (ar_ptr, bytes); victim = _int_malloc (ar_ptr, bytes); /* Retry with another arena only if we were able to find a usable arena before. */ if (!vi.. (glibc 2.23) _int_free 전체 코드는 아래 더보기 참고 더보기 static void _int_free (mstate av, mchunkptr p, int have_lock) { INTERNAL_SIZE_T size; /* its size */ mfastbinptr *fb; /* associated fastbin */ mchunkptr nextchunk; /* next contiguous chunk */ INTERNAL_SIZE_T nextsize; /* its size */ int nextinuse; /* true if nextchunk is used */ INTERNAL_SIZE_T prevsize; /* size of previous contiguous chunk */ mchunkptr bck; /* misc temp for.. (glibc 2.23) unlink 사실 unlink에 대해 매우 잘 정리되어 있는 블로그 글이 많다 그냥 공부한 것을 다시 복기하고 제대로 익히기 위해 정리하려한다 전체 코드는 아래 더보기 더보기 /* Take a chunk off a bin list */ #define unlink(AV, P, BK, FD) { \ FD = P->fd; \ BK = P->bk; \ if (__builtin_expect (FD->bk != P || BK->fd != P, 0)) \ malloc_printerr (check_action, "corrupted double-linked list", P, AV); \ else { \ FD->bk = BK; \ BK->fd = FD; \ if (!in_smallbin_range (P->size) \ && __bui.. (glibc 2.23)check_malloced_chunk 해당 함수를 malloc함수에서 재할당할 청크의 주소를 반환하기 전에 호출된다 do_check_remalloced_chunk를 호출하고 청크에 PREV_INUSE비트가 설정되어 있는지 확인한다 /* Properties of nonrecycled chunks at the point they are malloced */ static void do_check_malloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s) { /* same as recycled case ... */ do_check_remalloced_chunk (av, p, s); /* ... plus, must obey implementation invariant that prev_inuse is a.. (glibc 2.23) free 힙의 할당한 메모리를 해제할 때 사용되는 함수이다 전체 코드 아래 더보기 더보기 void __libc_free (void *mem) { mstate ar_ptr; mchunkptr p; /* chunk corresponding to mem */ void (*hook) (void *, const void *) = atomic_forced_read (__free_hook); if (__builtin_expect (hook != NULL, 0)) { (*hook)(mem, RETURN_ADDRESS (0)); return; } if (mem == 0) /* free(0) has no effect */ return; p = mem2chunk (mem); if (chunk_is_mmapped (p)) /* rel.. (glibc 2.23) do_check_free_chunk 전체 코드는 아래 더보기 더보기 /* Properties of free chunks */ static void do_check_free_chunk (mstate av, mchunkptr p) { INTERNAL_SIZE_T sz = p->size & ~(PREV_INUSE | NON_MAIN_ARENA); mchunkptr next = chunk_at_offset (p, sz); do_check_chunk (av, p); /* Chunk must claim to be free ... */ assert (!inuse (p)); assert (!chunk_is_mmapped (p)); /* Unless a special marker, must have OK fields */ if ((unsigned long.. 이전 1 2 다음 목록 더보기