본문 바로가기

전체 글

(186)
(glibc 2.26) _int_malloc glibc 2.23버전에서 malloc의 동작과정을 살펴보았기 때문에 해당 과정에 대한 이해가 있다라고 생각하고 바뀐 코드 위주로 정리할 것이다 각 소제목에 해당되는 상세 설명은 각 리스트 아래 더보기 참고 전체 코드는 아래 더보기 더보기 static void * _int_malloc (mstate av, size_t bytes) { INTERNAL_SIZE_T nb; /* normalized request size */ unsigned int idx; /* associated bin index */ mbinptr bin; /* associated bin */ mchunkptr victim; /* inspected/selected chunk */ INTERNAL_SIZE_T size; /* its siz..
(glibc 2.26) tcache_put /* Caller must ensure that we know tc_idx is valid and there's room for more chunks. */ static void tcache_put (mchunkptr chunk, size_t tc_idx) { tcache_entry *e = (tcache_entry *) chunk2mem (chunk); assert (tc_idx next = tcache->entries[tc_idx]; tcache->entries[tc_idx] = e; ++(tcache->counts[tc_idx]); } tcache에 freed 청크를 넣는 작업을 하는 함수이다 포인터 e에 chunk주소 저장 tc_idx가 TCACHE_MAX..
(glibc 2.26) tcache_get /* Caller must ensure that we know tc_idx is valid and there's available chunks to remove. */ static void * tcache_get (size_t tc_idx) { tcache_entry *e = tcache->entries[tc_idx]; assert (tc_idx entries[tc_idx] > 0); tcache->entries[tc_idx] = e->next; --(tcache->counts[tc_idx]); return (void *) e; } 인자로 들어온 tc_idx 인덱스에 있는 freed chunk를 반환하는 함수이다 get address poi..
Heap / 4. tcache 보호되어 있는 글입니다.
(glibc 2.26) tcache_init 전체 코드는 아래와 같다 더보기 static void tcache_init(void) { mstate ar_ptr; void *victim = 0; const size_t bytes = sizeof (tcache_perthread_struct); if (tcache_shutting_down) return; arena_get (ar_ptr, bytes); victim = _int_malloc (ar_ptr, bytes); if (!victim && ar_ptr != NULL) { ar_ptr = arena_get_retry (ar_ptr, bytes); victim = _int_malloc (ar_ptr, bytes); } if (ar_ptr != NULL) __libc_lock_unlock (ar_ptr..
(glibc 2.26) 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)); #if USE_TCACHE /* int_free also calls request2size, be careful to not pad twice. */ size_t tbytes = request2size (bytes); size_t tc_idx = csize2tidx (tbyte..
(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..