Profile

youngsouk

youngsouk

__libc_free함수 분석

  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;

NULL 메모리를 free하는 것은 효과가 없다,

  p = mem2chunk (mem);

  if (chunk_is_mmapped (p))                       /* release mmapped memory. */
    {
      /* See if the dynamic brk/mmap threshold needs adjusting.
	 Dumped fake mmapped chunks do not affect the threshold.  */
      if (!mp_.no_dyn_threshold
          && chunksize_nomask (p) > mp_.mmap_threshold
          && chunksize_nomask (p) <= DEFAULT_MMAP_THRESHOLD_MAX
	  && !DUMPED_MAIN_ARENA_CHUNK (p))
        {
          mp_.mmap_threshold = chunksize (p);
          mp_.trim_threshold = 2 * mp_.mmap_threshold;
          LIBC_PROBE (memory_mallopt_free_dyn_thresholds, 2,
                      mp_.mmap_threshold, mp_.trim_threshold);
        }
      munmap_chunk (p);
      return;
    }

mmap으로 할당받은 청크들은 조정이 필요할 때가 있을 수 있는데 그 조정을 해주는 것 같다,

  MAYBE_INIT_TCACHE ();

  ar_ptr = arena_for_chunk (p);
  _int_free (ar_ptr, p, 0);
}

MAYBE_INIT_TCACHE()는 

# define MAYBE_INIT_TCACHE() \
  if (__glibc_unlikely (tcache == NULL)) \
    tcache_init();

이렇게 정의되는데 즉 tcache가 시작?되지 않았으면 tcache를 시작하라는 것이다.

 

tcache에 관련된 자세한 것은

2019/08/08 - [힙(heap)] - tcache 분석

 

tcache 분석

/* We overlay this structure on the user-data portion of a chunk when the chunk is stored in the per-thread cache. */ typedef struct tcache_entry { struct tcache_entry *next; } tcache_entry; tcache_..

youngsouk-hack.tistory.com

unlikely에 대한것은 

2019/08/09 - [힙(heap)] - __builtin_expect, likely, unlikely

 

__builtin_expect, likely, unlikely

malloc관련 문서나 커널 문서에 가면 많이 보이는 이 함수들은 기능이 결과적으로 보면 비슷하다. 다 if문을 좀 더 효율적으로 동작시키기 위한 장치이다. 기본적으로 __builtin_expect는 __builtin_expect(조건식..

youngsouk-hack.tistory.com

  ar_ptr = arena_for_chunk (p);
  _int_free (ar_ptr, p, 0);

다음은 청크의 아레나를 얻어와서 free시키는 것 뿐이다.

'힙(heap) > glibc' 카테고리의 다른 글

_int_malloc 함수 분석 (2)  (0) 2019.08.09
_int_malloc 함수 분석 (1)  (0) 2019.08.09
__libc_malloc함수 분석  (0) 2019.08.09
__builtin_expect, likely, unlikely  (0) 2019.08.09
__libc_calloc 함수 분석  (0) 2019.08.09