Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8140805
  • 博文数量: 594
  • 博客积分: 13065
  • 博客等级: 上将
  • 技术积分: 10324
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-26 16:44
个人简介

推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html

文章分类

全部博文(594)

分类: C/C++

2015-10-09 15:50:17

glibc-2.14中的arean.c源代码,供研究malloc和free实现使用:

  1. /* Malloc implementation for multiple threads without lock contention.
  2.    Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2009,2010
  3.    Free Software Foundation, Inc.
  4.    This file is part of the GNU C Library.
  5.    Contributed by Wolfram Gloger <wg@malloc.de>, 2001.

  6.    The GNU C Library is free software; you can redistribute it and/or
  7.    modify it under the terms of the GNU Lesser General Public License as
  8.    published by the Free Software Foundation; either version 2.1 of the
  9.    License, or (at your option) any later version.

  10.    The GNU C Library is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13.    Lesser General Public License for more details.

  14.    You should have received a copy of the GNU Lesser General Public
  15.    License along with the GNU C Library; see the file COPYING.LIB. If not,
  16.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.    Boston, MA 02111-1307, USA. */

  18. #include <stdbool.h>

  19. /* Compile-time constants. */

  20. #define HEAP_MIN_SIZE (32*1024)
  21. #ifndef HEAP_MAX_SIZE
  22. # ifdef DEFAULT_MMAP_THRESHOLD_MAX
  23. # define HEAP_MAX_SIZE (2 * DEFAULT_MMAP_THRESHOLD_MAX)
  24. # else
  25. # define HEAP_MAX_SIZE (1024*1024) /* must be a power of two */
  26. # endif
  27. #endif

  28. /* HEAP_MIN_SIZE and HEAP_MAX_SIZE limit the size of mmap()ed heaps
  29.    that are dynamically created for multi-threaded programs. The
  30.    maximum size must be a power of two, for fast determination of
  31.    which heap belongs to a chunk. It should be much larger than the
  32.    mmap threshold, so that requests with a size just below that
  33.    threshold can be fulfilled without creating too many heaps. */


  34. #ifndef THREAD_STATS
  35. #define THREAD_STATS 0
  36. #endif

  37. /* If THREAD_STATS is non-zero, some statistics on mutex locking are
  38.    computed. */

  39. /***************************************************************************/

  40. #define top(ar_ptr) ((ar_ptr)->top)

  41. /* A heap is a single contiguous memory region holding (coalesceable)
  42.    malloc_chunks. It is allocated with mmap() and always starts at an
  43.    address aligned to HEAP_MAX_SIZE. Not used unless compiling with
  44.    USE_ARENAS. */

  45. typedef struct _heap_info {
  46.   mstate ar_ptr; /* Arena for this heap. */
  47.   struct _heap_info *prev; /* Previous heap. */
  48.   size_t size; /* Current size in bytes. */
  49.   size_t mprotect_size;    /* Size in bytes that has been mprotected
  50.              PROT_READ|PROT_WRITE. */
  51.   /* Make sure the following data is properly aligned, particularly
  52.      that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
  53.      MALLOC_ALIGNMENT. */
  54.   char pad[-6 * SIZE_SZ & MALLOC_ALIGN_MASK];
  55. } heap_info;

  56. /* Get a compile-time error if the heap_info padding is not correct
  57.    to make alignment work as expected in sYSMALLOc. */
  58. extern int sanity_check_heap_info_alignment[(sizeof (heap_info)
  59.                      + 2 * SIZE_SZ) % MALLOC_ALIGNMENT
  60.                      ? -1 : 1];

  61. /* Thread specific data */

  62. static tsd_key_t arena_key;
  63. static mutex_t list_lock;
  64. #ifdef PER_THREAD
  65. static size_t narenas;
  66. static mstate free_list;
  67. #endif

  68. #if THREAD_STATS
  69. static int stat_n_heaps;
  70. #define THREAD_STAT(x) x
  71. #else
  72. #define THREAD_STAT(x) do ; while(0)
  73. #endif

  74. /* Mapped memory in non-main arenas (reliable only for NO_THREADS). */
  75. static unsigned long arena_mem;

  76. /* Already initialized? */
  77. int __malloc_initialized = -1;

  78. /**************************************************************************/

  79. #if USE_ARENAS

  80. /* arena_get() acquires an arena and locks the corresponding mutex.
  81.    First, try the one last locked successfully by this thread. (This
  82.    is the common case and handled with a macro for speed.) Then, loop
  83.    once over the circularly linked list of arenas. If no arena is
  84.    readily available, create a new one. In this latter case, `size'
  85.    is just a hint as to how much memory will be required immediately
  86.    in the new arena. */

  87. #define arena_get(ptr, size) do { \
  88.   arena_lookup(ptr); \
  89.   arena_lock(ptr, size); \
  90. } while(0)

  91. #define arena_lookup(ptr) do { \
  92.   Void_t *vptr = NULL; \
  93.   ptr = (mstate)tsd_getspecific(arena_key, vptr); \
  94. } while(0)

  95. #ifdef PER_THREAD
  96. #define arena_lock(ptr, size) do { \
  97.   if(ptr) \
  98.     (void)mutex_lock(&ptr->mutex); \
  99.   else \
  100.     ptr = arena_get2(ptr, (size)); \
  101. } while(0)
  102. #else
  103. #define arena_lock(ptr, size) do { \
  104.   if(ptr && !mutex_trylock(&ptr->mutex)) { \
  105.     THREAD_STAT(++(ptr->stat_lock_direct)); \
  106.   } else \
  107.     ptr = arena_get2(ptr, (size)); \
  108. } while(0)
  109. #endif

  110. /* find the heap and corresponding arena for a given ptr */

  111. #define heap_for_ptr(ptr) \
  112.  ((heap_info *)((unsigned long)(ptr) & ~(HEAP_MAX_SIZE-1)))
  113. #define arena_for_chunk(ptr) \
  114.  (chunk_non_main_arena(ptr) ? heap_for_ptr(ptr)->ar_ptr : &main_arena)

  115. #else /* !USE_ARENAS */

  116. /* There is only one arena, main_arena. */

  117. #if THREAD_STATS
  118. #define arena_get(ar_ptr, sz) do { \
  119.   ar_ptr = &main_arena; \
  120.   if(!mutex_trylock(&ar_ptr->mutex)) \
  121.     ++(ar_ptr->stat_lock_direct); \
  122.   else { \
  123.     (void)mutex_lock(&ar_ptr->mutex); \
  124.     ++(ar_ptr->stat_lock_wait); \
  125.   } \
  126. } while(0)
  127. #else
  128. #define arena_get(ar_ptr, sz) do { \
  129.   ar_ptr = &main_arena; \
  130.   (void)mutex_lock(&ar_ptr->mutex); \
  131. } while(0)
  132. #endif
  133. #define arena_for_chunk(ptr) (&main_arena)

  134. #endif /* USE_ARENAS */

  135. /**************************************************************************/

  136. #ifndef NO_THREADS

  137. /* atfork support. */

  138. static __malloc_ptr_t (*save_malloc_hook) (size_t __size,
  139.                      __const __malloc_ptr_t);
  140. # if !defined _LIBC || (defined SHARED && !USE___THREAD)
  141. static __malloc_ptr_t (*save_memalign_hook) (size_t __align, size_t __size,
  142.                      __const __malloc_ptr_t);
  143. # endif
  144. static void (*save_free_hook) (__malloc_ptr_t __ptr,
  145.                      __const __malloc_ptr_t);
  146. static Void_t* save_arena;

  147. #ifdef ATFORK_MEM
  148. ATFORK_MEM;
  149. #endif

  150. /* Magic value for the thread-specific arena pointer when
  151.    malloc_atfork() is in use. */

  152. #define ATFORK_ARENA_PTR ((Void_t*)-1)

  153. /* The following hooks are used while the `atfork' handling mechanism
  154.    is active. */

  155. static Void_t*
  156. malloc_atfork(size_t sz, const Void_t *caller)
  157. {
  158.   Void_t *vptr = NULL;
  159.   Void_t *victim;

  160.   tsd_getspecific(arena_key, vptr);
  161.   if(vptr == ATFORK_ARENA_PTR) {
  162.     /* We are the only thread that may allocate at all. */
  163.     if(save_malloc_hook != malloc_check) {
  164.       return _int_malloc(&main_arena, sz);
  165.     } else {
  166.       if(top_check()<0)
  167.     return 0;
  168.       victim = _int_malloc(&main_arena, sz+1);
  169.       return mem2mem_check(victim, sz);
  170.     }
  171.   } else {
  172.     /* Suspend the thread until the `atfork' handlers have completed.
  173.        By that time, the hooks will have been reset as well, so that
  174.        mALLOc() can be used again. */
  175.     (void)mutex_lock(&list_lock);
  176.     (void)mutex_unlock(&list_lock);
  177.     return public_mALLOc(sz);
  178.   }
  179. }

  180. static void
  181. free_atfork(Void_t* mem, const Void_t *caller)
  182. {
  183.   Void_t *vptr = NULL;
  184.   mstate ar_ptr;
  185.   mchunkptr p; /* chunk corresponding to mem */

  186.   if (mem == 0) /* free(0) has no effect */
  187.     return;

  188.   p = mem2chunk(mem); /* do not bother to replicate free_check here */

  189. #if HAVE_MMAP
  190.   if (chunk_is_mmapped(p)) /* release mmapped memory. */
  191.   {
  192.     munmap_chunk(p);
  193.     return;
  194.   }
  195. #endif

  196. #ifdef ATOMIC_FASTBINS
  197.   ar_ptr = arena_for_chunk(p);
  198.   tsd_getspecific(arena_key, vptr);
  199.   _int_free(ar_ptr, p, vptr == ATFORK_ARENA_PTR);
  200. #else
  201.   ar_ptr = arena_for_chunk(p);
  202.   tsd_getspecific(arena_key, vptr);
  203.   if(vptr != ATFORK_ARENA_PTR)
  204.     (void)mutex_lock(&ar_ptr->mutex);
  205.   _int_free(ar_ptr, p);
  206.   if(vptr != ATFORK_ARENA_PTR)
  207.     (void)mutex_unlock(&ar_ptr->mutex);
  208. #endif
  209. }


  210. /* Counter for number of times the list is locked by the same thread. */
  211. static unsigned int atfork_recursive_cntr;

  212. /* The following two functions are registered via thread_atfork() to
  213.    make sure that the mutexes remain in a consistent state in the
  214.    fork()ed version of a thread. Also adapt the malloc and free hooks
  215.    temporarily, because the `atfork' handler mechanism may use
  216.    malloc/free internally (e.g. in LinuxThreads). */

  217. static void
  218. ptmalloc_lock_all (void)
  219. {
  220.   mstate ar_ptr;

  221.   if(__malloc_initialized < 1)
  222.     return;
  223.   if (mutex_trylock(&list_lock))
  224.     {
  225.       Void_t *my_arena;
  226.       tsd_getspecific(arena_key, my_arena);
  227.       if (my_arena == ATFORK_ARENA_PTR)
  228.     /* This is the same thread which already locks the global list.
  229.      Just bump the counter. */
  230.     goto out;

  231.       /* This thread has to wait its turn. */
  232.       (void)mutex_lock(&list_lock);
  233.     }
  234.   for(ar_ptr = &main_arena;;) {
  235.     (void)mutex_lock(&ar_ptr->mutex);
  236.     ar_ptr = ar_ptr->next;
  237.     if(ar_ptr == &main_arena) break;
  238.   }
  239.   save_malloc_hook = __malloc_hook;
  240.   save_free_hook = __free_hook;
  241.   __malloc_hook = malloc_atfork;
  242.   __free_hook = free_atfork;
  243.   /* Only the current thread may perform malloc/free calls now. */
  244.   tsd_getspecific(arena_key, save_arena);
  245.   tsd_setspecific(arena_key, ATFORK_ARENA_PTR);
  246.  out:
  247.   ++atfork_recursive_cntr;
  248. }

  249. static void
  250. ptmalloc_unlock_all (void)
  251. {
  252.   mstate ar_ptr;

  253.   if(__malloc_initialized < 1)
  254.     return;
  255.   if (--atfork_recursive_cntr != 0)
  256.     return;
  257.   tsd_setspecific(arena_key, save_arena);
  258.   __malloc_hook = save_malloc_hook;
  259.   __free_hook = save_free_hook;
  260.   for(ar_ptr = &main_arena;;) {
  261.     (void)mutex_unlock(&ar_ptr->mutex);
  262.     ar_ptr = ar_ptr->next;
  263.     if(ar_ptr == &main_arena) break;
  264.   }
  265.   (void)mutex_unlock(&list_lock);
  266. }

  267. #ifdef __linux__

  268. /* In NPTL, unlocking a mutex in the child process after a
  269.    fork() is currently unsafe, whereas re-initializing it is safe and
  270.    does not leak resources. Therefore, a special atfork handler is
  271.    installed for the child. */

  272. static void
  273. ptmalloc_unlock_all2 (void)
  274. {
  275.   mstate ar_ptr;

  276.   if(__malloc_initialized < 1)
  277.     return;
  278. #if defined _LIBC || defined MALLOC_HOOKS
  279.   tsd_setspecific(arena_key, save_arena);
  280.   __malloc_hook = save_malloc_hook;
  281.   __free_hook = save_free_hook;
  282. #endif
  283. #ifdef PER_THREAD
  284.   free_list = NULL;
  285. #endif
  286.   for(ar_ptr = &main_arena;;) {
  287.     mutex_init(&ar_ptr->mutex);
  288. #ifdef PER_THREAD
  289.     if (ar_ptr != save_arena) {
  290.       ar_ptr->next_free = free_list;
  291.       free_list = ar_ptr;
  292.     }
  293. #endif
  294.     ar_ptr = ar_ptr->next;
  295.     if(ar_ptr == &main_arena) break;
  296.   }
  297.   mutex_init(&list_lock);
  298.   atfork_recursive_cntr = 0;
  299. }

  300. #else

  301. #define ptmalloc_unlock_all2 ptmalloc_unlock_all

  302. #endif

  303. #endif /* !defined NO_THREADS */

  304. /* Initialization routine. */
  305. #ifdef _LIBC
  306. #include <string.h>
  307. extern char **_environ;

  308. static char *
  309. internal_function
  310. next_env_entry (char ***position)
  311. {
  312.   char **current = *position;
  313.   char *result = NULL;

  314.   while (*current != NULL)
  315.     {
  316.       if (__builtin_expect ((*current)[0] == 'M', 0)
  317.      && (*current)[1] == 'A'
  318.      && (*current)[2] == 'L'
  319.      && (*current)[3] == 'L'
  320.      && (*current)[4] == 'O'
  321.      && (*current)[5] == 'C'
  322.      && (*current)[6] == '_')
  323.     {
  324.      result = &(*current)[7];

  325.      /* Save current position for next visit. */
  326.      *position = ++current;

  327.      break;
  328.     }

  329.       ++current;
  330.     }

  331.   return result;
  332. }
  333. #endif /* _LIBC */

  334. /* Set up basic state so that _int_malloc et al can work. */
  335. static void
  336. ptmalloc_init_minimal (void)
  337. {
  338. #if DEFAULT_TOP_PAD != 0
  339.   mp_.top_pad = DEFAULT_TOP_PAD;
  340. #endif
  341.   mp_.n_mmaps_max = DEFAULT_MMAP_MAX;
  342.   mp_.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
  343.   mp_.trim_threshold = DEFAULT_TRIM_THRESHOLD;
  344.   mp_.pagesize = malloc_getpagesize;
  345. #ifdef PER_THREAD
  346. # define NARENAS_FROM_NCORES(n) ((n) * (sizeof(long) == 4 ? 2 : 8))
  347.   mp_.arena_test = NARENAS_FROM_NCORES (1);
  348.   narenas = 1;
  349. #endif
  350. }


  351. #ifdef _LIBC
  352. # ifdef SHARED
  353. static void *
  354. __failing_morecore (ptrdiff_t d)
  355. {
  356.   return (void *) MORECORE_FAILURE;
  357. }

  358. extern struct dl_open_hook *_dl_open_hook;
  359. libc_hidden_proto (_dl_open_hook);
  360. # endif

  361. # if defined SHARED && !USE___THREAD
  362. /* This is called by __pthread_initialize_minimal when it needs to use
  363.    malloc to set up the TLS state. We cannot do the full work of
  364.    ptmalloc_init (below) until __pthread_initialize_minimal has finished,
  365.    so it has to switch to using the special startup-time hooks while doing
  366.    those allocations. */
  367. void
  368. __libc_malloc_pthread_startup (bool first_time)
  369. {
  370.   if (first_time)
  371.     {
  372.       ptmalloc_init_minimal ();
  373.       save_malloc_hook = __malloc_hook;
  374.       save_memalign_hook = __memalign_hook;
  375.       save_free_hook = __free_hook;
  376.       __malloc_hook = malloc_starter;
  377.       __memalign_hook = memalign_starter;
  378.       __free_hook = free_starter;
  379.     }
  380.   else
  381.     {
  382.       __malloc_hook = save_malloc_hook;
  383.       __memalign_hook = save_memalign_hook;
  384.       __free_hook = save_free_hook;
  385.     }
  386. }
  387. # endif
  388. #endif

  389. static void
  390. ptmalloc_init (void)
  391. {
  392. #if __STD_C
  393.   const char* s;
  394. #else
  395.   char* s;
  396. #endif
  397.   int secure = 0;

  398.   if(__malloc_initialized >= 0) return;
  399.   __malloc_initialized = 0;

  400. #ifdef _LIBC
  401. # if defined SHARED && !USE___THREAD
  402.   /* ptmalloc_init_minimal may already have been called via
  403.      __libc_malloc_pthread_startup, above. */
  404.   if (mp_.pagesize == 0)
  405. # endif
  406. #endif
  407.     ptmalloc_init_minimal();

  408. #ifndef NO_THREADS
  409. # if defined _LIBC
  410.   /* We know __pthread_initialize_minimal has already been called,
  411.      and that is enough. */
  412. # define NO_STARTER
  413. # endif
  414. # ifndef NO_STARTER
  415.   /* With some threads implementations, creating thread-specific data
  416.      or initializing a mutex may call malloc() itself. Provide a
  417.      simple starter version (realloc() won't work). */
  418.   save_malloc_hook = __malloc_hook;
  419.   save_memalign_hook = __memalign_hook;
  420.   save_free_hook = __free_hook;
  421.   __malloc_hook = malloc_starter;
  422.   __memalign_hook = memalign_starter;
  423.   __free_hook = free_starter;
  424. # ifdef _LIBC
  425.   /* Initialize the pthreads interface. */
  426.   if (__pthread_initialize != NULL)
  427.     __pthread_initialize();
  428. # endif /* !defined _LIBC */
  429. # endif    /* !defined NO_STARTER */
  430. #endif /* !defined NO_THREADS */
  431.   mutex_init(&main_arena.mutex);
  432.   main_arena.next = &main_arena;

  433. #if defined _LIBC && defined SHARED
  434.   /* In case this libc copy is in a non-default namespace, never use brk.
  435.      Likewise if dlopened from statically linked program. */
  436.   Dl_info di;
  437.   struct link_map *l;

  438.   if (_dl_open_hook != NULL
  439.       || (_dl_addr (ptmalloc_init, &di, &l, NULL) != 0
  440.      && l->l_ns != LM_ID_BASE))
  441.     __morecore = __failing_morecore;
  442. #endif

  443.   mutex_init(&list_lock);
  444.   tsd_key_create(&arena_key, NULL);
  445.   tsd_setspecific(arena_key, (Void_t *)&main_arena);
  446.   thread_atfork(ptmalloc_lock_all, ptmalloc_unlock_all, ptmalloc_unlock_all2);
  447. #ifndef NO_THREADS
  448. # ifndef NO_STARTER
  449.   __malloc_hook = save_malloc_hook;
  450.   __memalign_hook = save_memalign_hook;
  451.   __free_hook = save_free_hook;
  452. # else
  453. # undef NO_STARTER
  454. # endif
  455. #endif
  456. #ifdef _LIBC
  457.   secure = __libc_enable_secure;
  458.   s = NULL;
  459.   if (__builtin_expect (_environ != NULL, 1))
  460.     {
  461.       char **runp = _environ;
  462.       char *envline;

  463.       while (__builtin_expect ((envline = next_env_entry (&runp)) != NULL,
  464.              0))
  465.     {
  466.      size_t len = strcspn (envline, "=");

  467.      if (envline[len] != '=')
  468.      /* This is a "MALLOC_" variable at the end of the string
  469.      without a '=' character. Ignore it since otherwise we
  470.      will access invalid memory below. */
  471.      continue;

  472.      switch (len)
  473.      {
  474.      case 6:
  475.      if (memcmp (envline, "CHECK_", 6) == 0)
  476.         s = &envline[7];
  477.      break;
  478.      case 8:
  479.      if (! secure)
  480.         {
  481.          if (memcmp (envline, "TOP_PAD_", 8) == 0)
  482.          mALLOPt(M_TOP_PAD, atoi(&envline[9]));
  483.          else if (memcmp (envline, "PERTURB_", 8) == 0)
  484.          mALLOPt(M_PERTURB, atoi(&envline[9]));
  485.         }
  486.      break;
  487.      case 9:
  488.      if (! secure)
  489.         {
  490.          if (memcmp (envline, "MMAP_MAX_", 9) == 0)
  491.          mALLOPt(M_MMAP_MAX, atoi(&envline[10]));
  492. #ifdef PER_THREAD
  493.          else if (memcmp (envline, "ARENA_MAX", 9) == 0)
  494.          mALLOPt(M_ARENA_MAX, atoi(&envline[10]));
  495. #endif
  496.         }
  497.      break;
  498. #ifdef PER_THREAD
  499.      case 10:
  500.      if (! secure)
  501.         {
  502.          if (memcmp (envline, "ARENA_TEST", 10) == 0)
  503.          mALLOPt(M_ARENA_TEST, atoi(&envline[11]));
  504.         }
  505.      break;
  506. #endif
  507.      case 15:
  508.      if (! secure)
  509.         {
  510.          if (memcmp (envline, "TRIM_THRESHOLD_", 15) == 0)
  511.          mALLOPt(M_TRIM_THRESHOLD, atoi(&envline[16]));
  512.          else if (memcmp (envline, "MMAP_THRESHOLD_", 15) == 0)
  513.          mALLOPt(M_MMAP_THRESHOLD, atoi(&envline[16]));
  514.         }
  515.      break;
  516.      default:
  517.      break;
  518.      }
  519.     }
  520.     }
  521. #else
  522.   if (! secure)
  523.     {
  524.       if((s = getenv("MALLOC_TRIM_THRESHOLD_")))
  525.     mALLOPt(M_TRIM_THRESHOLD, atoi(s));
  526.       if((s = getenv("MALLOC_TOP_PAD_")))
  527.     mALLOPt(M_TOP_PAD, atoi(s));
  528.       if((s = getenv("MALLOC_PERTURB_")))
  529.     mALLOPt(M_PERTURB, atoi(s));
  530.       if((s = getenv("MALLOC_MMAP_THRESHOLD_")))
  531.     mALLOPt(M_MMAP_THRESHOLD, atoi(s));
  532.       if((s = getenv("MALLOC_MMAP_MAX_")))
  533.     mALLOPt(M_MMAP_MAX, atoi(s));
  534.     }
  535.   s = getenv("MALLOC_CHECK_");
  536. #endif
  537.   if(s && s[0]) {
  538.     mALLOPt(M_CHECK_ACTION, (int)(s[0] - '0'));
  539.     if (check_action != 0)
  540.       __malloc_check_init();
  541.   }
  542.   void (*hook) (void) = force_reg (__malloc_initialize_hook);
  543.   if (hook != NULL)
  544.     (*hook)();
  545.   __malloc_initialized = 1;
  546. }

  547. /* There are platforms (e.g. Hurd) with a link-time hook mechanism. */
  548. #ifdef thread_atfork_static
  549. thread_atfork_static(ptmalloc_lock_all, ptmalloc_unlock_all, \
  550.          ptmalloc_unlock_all2)
  551. #endif



  552. /* Managing heaps and arenas (for concurrent threads) */

  553. #if USE_ARENAS

  554. #if MALLOC_DEBUG > 1

  555. /* Print the complete contents of a single heap to stderr. */

  556. static void
  557. #if __STD_C
  558. dump_heap(heap_info *heap)
  559. #else
  560. dump_heap(heap) heap_info *heap;
  561. #endif
  562. {
  563.   char *ptr;
  564.   mchunkptr p;

  565.   fprintf(stderr, "Heap %p, size %10lx:\n", heap, (long)heap->size);
  566.   ptr = (heap->ar_ptr != (mstate)(heap+1)) ?
  567.     (char*)(heap + 1) : (char*)(heap + 1) + sizeof(struct malloc_state);
  568.   p = (mchunkptr)(((unsigned long)ptr + MALLOC_ALIGN_MASK) &
  569.          ~MALLOC_ALIGN_MASK);
  570.   for(;;) {
  571.     fprintf(stderr, "chunk %p size %10lx", p, (long)p->size);
  572.     if(p == top(heap->ar_ptr)) {
  573.       fprintf(stderr, " (top)\n");
  574.       break;
  575.     } else if(p->size == (0|PREV_INUSE)) {
  576.       fprintf(stderr, " (fence)\n");
  577.       break;
  578.     }
  579.     fprintf(stderr, "\n");
  580.     p = next_chunk(p);
  581.   }
  582. }

  583. #endif /* MALLOC_DEBUG > 1 */

  584. /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) calls return decreasing
  585.    addresses as opposed to increasing, new_heap would badly fragment the
  586.    address space. In that case remember the second HEAP_MAX_SIZE part
  587.    aligned to HEAP_MAX_SIZE from last mmap (0, HEAP_MAX_SIZE << 1, ...)
  588.    call (if it is already aligned) and try to reuse it next time. We need
  589.    no locking for it, as kernel ensures the atomicity for us - worst case
  590.    we'll call mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in
  591.    multiple threads, but only one will succeed. */
  592. static char *aligned_heap_area;

  593. /* Create a new heap. size is automatically rounded up to a multiple
  594.    of the page size. */

  595. static heap_info *
  596. internal_function
  597. #if __STD_C
  598. new_heap(size_t size, size_t top_pad)
  599. #else
  600. new_heap(size, top_pad) size_t size, top_pad;
  601. #endif
  602. {
  603.   size_t page_mask = malloc_getpagesize - 1;
  604.   char *p1, *p2;
  605.   unsigned long ul;
  606.   heap_info *h;

  607.   if(size+top_pad < HEAP_MIN_SIZE)
  608.     size = HEAP_MIN_SIZE;
  609.   else if(size+top_pad <= HEAP_MAX_SIZE)
  610.     size += top_pad;
  611.   else if(size > HEAP_MAX_SIZE)
  612.     return 0;
  613.   else
  614.     size = HEAP_MAX_SIZE;
  615.   size = (size + page_mask) & ~page_mask;

  616.   /* A memory region aligned to a multiple of HEAP_MAX_SIZE is needed.
  617.      No swap space needs to be reserved for the following large
  618.      mapping (on Linux, this is the case for all non-writable mappings
  619.      anyway). */
  620.   p2 = MAP_FAILED;
  621.   if(aligned_heap_area) {
  622.     p2 = (char *)MMAP(aligned_heap_area, HEAP_MAX_SIZE, PROT_NONE,
  623.          MAP_PRIVATE|MAP_NORESERVE);
  624.     aligned_heap_area = NULL;
  625.     if (p2 != MAP_FAILED && ((unsigned long)p2 & (HEAP_MAX_SIZE-1))) {
  626.       munmap(p2, HEAP_MAX_SIZE);
  627.       p2 = MAP_FAILED;
  628.     }
  629.   }
  630.   if(p2 == MAP_FAILED) {
  631.     p1 = (char *)MMAP(0, HEAP_MAX_SIZE<<1, PROT_NONE,
  632.          MAP_PRIVATE|MAP_NORESERVE);
  633.     if(p1 != MAP_FAILED) {
  634.       p2 = (char *)(((unsigned long)p1 + (HEAP_MAX_SIZE-1))
  635.          & ~(HEAP_MAX_SIZE-1));
  636.       ul = p2 - p1;
  637.       if (ul)
  638.     munmap(p1, ul);
  639.       else
  640.     aligned_heap_area = p2 + HEAP_MAX_SIZE;
  641.       munmap(p2 + HEAP_MAX_SIZE, HEAP_MAX_SIZE - ul);
  642.     } else {
  643.       /* Try to take the chance that an allocation of only HEAP_MAX_SIZE
  644.      is already aligned. */
  645.       p2 = (char *)MMAP(0, HEAP_MAX_SIZE, PROT_NONE, MAP_PRIVATE|MAP_NORESERVE);
  646.       if(p2 == MAP_FAILED)
  647.     return 0;
  648.       if((unsigned long)p2 & (HEAP_MAX_SIZE-1)) {
  649.     munmap(p2, HEAP_MAX_SIZE);
  650.     return 0;
  651.       }
  652.     }
  653.   }
  654.   if(mprotect(p2, size, PROT_READ|PROT_WRITE) != 0) {
  655.     munmap(p2, HEAP_MAX_SIZE);
  656.     return 0;
  657.   }
  658.   h = (heap_info *)p2;
  659.   h->size = size;
  660.   h->mprotect_size = size;
  661.   THREAD_STAT(stat_n_heaps++);
  662.   return h;
  663. }

  664. /* Grow a heap. size is automatically rounded up to a
  665.    multiple of the page size. */

  666. static int
  667. #if __STD_C
  668. grow_heap(heap_info *h, long diff)
  669. #else
  670. grow_heap(h, diff) heap_info *h; long diff;
  671. #endif
  672. {
  673.   size_t page_mask = malloc_getpagesize - 1;
  674.   long new_size;

  675.   diff = (diff + page_mask) & ~page_mask;
  676.   new_size = (long)h->size + diff;
  677.   if((unsigned long) new_size > (unsigned long) HEAP_MAX_SIZE)
  678.     return -1;
  679.   if((unsigned long) new_size > h->mprotect_size) {
  680.     if (mprotect((char *)h + h->mprotect_size,
  681.          (unsigned long) new_size - h->mprotect_size,
  682.          PROT_READ|PROT_WRITE) != 0)
  683.       return -2;
  684.     h->mprotect_size = new_size;
  685.   }

  686.   h->size = new_size;
  687.   return 0;
  688. }

  689. /* Shrink a heap. */

  690. static int
  691. #if __STD_C
  692. shrink_heap(heap_info *h, long diff)
  693. #else
  694. shrink_heap(h, diff) heap_info *h; long diff;
  695. #endif
  696. {
  697.   long new_size;

  698.   new_size = (long)h->size - diff;
  699.   if(new_size < (long)sizeof(*h))
  700.     return -1;
  701.   /* Try to re-map the extra heap space freshly to save memory, and
  702.      make it inaccessible. */
  703. #ifdef _LIBC
  704.   if (__builtin_expect (__libc_enable_secure, 0))
  705. #else
  706.   if (1)
  707. #endif
  708.     {
  709.       if((char *)MMAP((char *)h + new_size, diff, PROT_NONE,
  710.          MAP_PRIVATE|MAP_FIXED) == (char *) MAP_FAILED)
  711.     return -2;
  712.       h->mprotect_size = new_size;
  713.     }
  714. #ifdef _LIBC
  715.   else
  716.     madvise ((char *)h + new_size, diff, MADV_DONTNEED);
  717. #endif
  718.   /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/

  719.   h->size = new_size;
  720.   return 0;
  721. }

  722. /* Delete a heap. */

  723. #define delete_heap(heap) \
  724.   do {                                \
  725.     if ((char *)(heap) + HEAP_MAX_SIZE == aligned_heap_area)    \
  726.       aligned_heap_area = NULL;                    \
  727.     munmap((char*)(heap), HEAP_MAX_SIZE);            \
  728.   } while (0)

  729. static int
  730. internal_function
  731. #if __STD_C
  732. heap_trim(heap_info *heap, size_t pad)
  733. #else
  734. heap_trim(heap, pad) heap_info *heap; size_t pad;
  735. #endif
  736. {
  737.   mstate ar_ptr = heap->ar_ptr;
  738.   unsigned long pagesz = mp_.pagesize;
  739.   mchunkptr top_chunk = top(ar_ptr), p, bck, fwd;
  740.   heap_info *prev_heap;
  741.   long new_size, top_size, extra;

  742.   /* Can this heap go away completely? */
  743.   while(top_chunk == chunk_at_offset(heap, sizeof(*heap))) {
  744.     prev_heap = heap->prev;
  745.     p = chunk_at_offset(prev_heap, prev_heap->size - (MINSIZE-2*SIZE_SZ));
  746.     assert(p->size == (0|PREV_INUSE)); /* must be fencepost */
  747.     p = prev_chunk(p);
  748.     new_size = chunksize(p) + (MINSIZE-2*SIZE_SZ);
  749.     assert(new_size>0 && new_size<(long)(2*MINSIZE));
  750.     if(!prev_inuse(p))
  751.       new_size += p->prev_size;
  752.     assert(new_size>0 && new_size<HEAP_MAX_SIZE);
  753.     if(new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz)
  754.       break;
  755.     ar_ptr->system_mem -= heap->size;
  756.     arena_mem -= heap->size;
  757.     delete_heap(heap);
  758.     heap = prev_heap;
  759.     if(!prev_inuse(p)) { /* consolidate backward */
  760.       p = prev_chunk(p);
  761.       unlink(p, bck, fwd);
  762.     }
  763.     assert(((unsigned long)((char*)p + new_size) & (pagesz-1)) == 0);
  764.     assert( ((char*)p + new_size) == ((char*)heap + heap->size) );
  765.     top(ar_ptr) = top_chunk = p;
  766.     set_head(top_chunk, new_size | PREV_INUSE);
  767.     /*check_chunk(ar_ptr, top_chunk);*/
  768.   }
  769.   top_size = chunksize(top_chunk);
  770.   extra = (top_size - pad - MINSIZE - 1) & ~(pagesz - 1);
  771.   if(extra < (long)pagesz)
  772.     return 0;
  773.   /* Try to shrink. */
  774.   if(shrink_heap(heap, extra) != 0)
  775.     return 0;
  776.   ar_ptr->system_mem -= extra;
  777.   arena_mem -= extra;

  778.   /* Success. Adjust top accordingly. */
  779.   set_head(top_chunk, (top_size - extra) | PREV_INUSE);
  780.   /*check_chunk(ar_ptr, top_chunk);*/
  781.   return 1;
  782. }

  783. /* Create a new arena with initial size "size". */

  784. static mstate
  785. _int_new_arena(size_t size)
  786. {
  787.   mstate a;
  788.   heap_info *h;
  789.   char *ptr;
  790.   unsigned long misalign;

  791.   h = new_heap(size + (sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT),
  792.      mp_.top_pad);
  793.   if(!h) {
  794.     /* Maybe size is too large to fit in a single heap. So, just try
  795.        to create a minimally-sized arena and let _int_malloc() attempt
  796.        to deal with the large request via mmap_chunk(). */
  797.     h = new_heap(sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT, mp_.top_pad);
  798.     if(!h)
  799.       return 0;
  800.   }
  801.   a = h->ar_ptr = (mstate)(h+1);
  802.   malloc_init_state(a);
  803.   /*a->next = NULL;*/
  804.   a->system_mem = a->max_system_mem = h->size;
  805.   arena_mem += h->size;
  806. #ifdef NO_THREADS
  807.   if((unsigned long)(mp_.mmapped_mem + arena_mem + main_arena.system_mem) >
  808.      mp_.max_total_mem)
  809.     mp_.max_total_mem = mp_.mmapped_mem + arena_mem + main_arena.system_mem;
  810. #endif

  811.   /* Set up the top chunk, with proper alignment. */
  812.   ptr = (char *)(a + 1);
  813.   misalign = (unsigned long)chunk2mem(ptr) & MALLOC_ALIGN_MASK;
  814.   if (misalign > 0)
  815.     ptr += MALLOC_ALIGNMENT - misalign;
  816.   top(a) = (mchunkptr)ptr;
  817.   set_head(top(a), (((char*)h + h->size) - ptr) | PREV_INUSE);

  818.   tsd_setspecific(arena_key, (Void_t *)a);
  819.   mutex_init(&a->mutex);
  820.   (void)mutex_lock(&a->mutex);

  821. #ifdef PER_THREAD
  822.   (void)mutex_lock(&list_lock);
  823. #endif

  824.   /* Add the new arena to the global list. */
  825.   a->next = main_arena.next;
  826.   atomic_write_barrier ();
  827.   main_arena.next = a;

  828. #ifdef PER_THREAD
  829.   ++narenas;

  830.   (void)mutex_unlock(&list_lock);
  831. #endif

  832.   THREAD_STAT(++(a->stat_lock_loop));

  833.   return a;
  834. }


  835. #ifdef PER_THREAD
  836. static mstate
  837. get_free_list (void)
  838. {
  839.   mstate result = free_list;
  840.   if (result != NULL)
  841.     {
  842.       (void)mutex_lock(&list_lock);
  843.       result = free_list;
  844.       if (result != NULL)
  845.     free_list = result->next_free;
  846.       (void)mutex_unlock(&list_lock);

  847.       if (result != NULL)
  848.     {
  849.      (void)mutex_lock(&result->mutex);
  850.      tsd_setspecific(arena_key, (Void_t *)result);
  851.      THREAD_STAT(++(result->stat_lock_loop));
  852.     }
  853.     }

  854.   return result;
  855. }


  856. static mstate
  857. reused_arena (void)
  858. {
  859.   if (narenas <= mp_.arena_test)
  860.     return NULL;

  861.   static int narenas_limit;
  862.   if (narenas_limit == 0)
  863.     {
  864.       if (mp_.arena_max != 0)
  865.     narenas_limit = mp_.arena_max;
  866.       else
  867.     {
  868.      int n = __get_nprocs ();

  869.      if (n >= 1)
  870.      narenas_limit = NARENAS_FROM_NCORES (n);
  871.      else
  872.      /* We have no information about the system. Assume two
  873.      cores. */
  874.      narenas_limit = NARENAS_FROM_NCORES (2);
  875.     }
  876.     }

  877.   if (narenas < narenas_limit)
  878.     return NULL;

  879.   mstate result;
  880.   static mstate next_to_use;
  881.   if (next_to_use == NULL)
  882.     next_to_use = &main_arena;

  883.   result = next_to_use;
  884.   do
  885.     {
  886.       if (!mutex_trylock(&result->mutex))
  887.     goto out;

  888.       result = result->next;
  889.     }
  890.   while (result != next_to_use);

  891.   /* No arena available. Wait for the next in line. */
  892.   (void)mutex_lock(&result->mutex);

  893.  out:
  894.   tsd_setspecific(arena_key, (Void_t *)result);
  895.   THREAD_STAT(++(result->stat_lock_loop));
  896.   next_to_use = result->next;

  897.   return result;
  898. }
  899. #endif

  900. static mstate
  901. internal_function
  902. #if __STD_C
  903. arena_get2(mstate a_tsd, size_t size)
  904. #else
  905. arena_get2(a_tsd, size) mstate a_tsd; size_t size;
  906. #endif
  907. {
  908.   mstate a;

  909. #ifdef PER_THREAD
  910.   if ((a = get_free_list ()) == NULL
  911.       && (a = reused_arena ()) == NULL)
  912.     /* Nothing immediately available, so generate a new arena. */
  913.     a = _int_new_arena(size);
  914. #else
  915.   if(!a_tsd)
  916.     a = a_tsd = &main_arena;
  917.   else {
  918.     a = a_tsd->next;
  919.     if(!a) {
  920.       /* This can only happen while initializing the new arena. */
  921.       (void)mutex_lock(&main_arena.mutex);
  922.       THREAD_STAT(++(main_arena.stat_lock_wait));
  923.       return &main_arena;
  924.     }
  925.   }

  926.   /* Check the global, circularly linked list for available arenas. */
  927.   bool retried = false;
  928.  repeat:
  929.   do {
  930.     if(!mutex_trylock(&a->mutex)) {
  931.       if (retried)
  932.     (void)mutex_unlock(&list_lock);
  933.       THREAD_STAT(++(a->stat_lock_loop));
  934.       tsd_setspecific(arena_key, (Void_t *)a);
  935.       return a;
  936.     }
  937.     a = a->next;
  938.   } while(a != a_tsd);

  939.   /* If not even the list_lock can be obtained, try again. This can
  940.      happen during `atfork', or for example on systems where thread
  941.      creation makes it temporarily impossible to obtain _any_
  942.      locks. */
  943.   if(!retried && mutex_trylock(&list_lock)) {
  944.     /* We will block to not run in a busy loop. */
  945.     (void)mutex_lock(&list_lock);

  946.     /* Since we blocked there might be an arena available now. */
  947.     retried = true;
  948.     a = a_tsd;
  949.     goto repeat;
  950.   }

  951.   /* Nothing immediately available, so generate a new arena. */
  952.   a = _int_new_arena(size);
  953.   (void)mutex_unlock(&list_lock);
  954. #endif

  955.   return a;
  956. }

  957. #ifdef PER_THREAD
  958. static void __attribute__ ((section ("__libc_thread_freeres_fn")))
  959. arena_thread_freeres (void)
  960. {
  961.   Void_t *vptr = NULL;
  962.   mstate a = tsd_getspecific(arena_key, vptr);
  963.   tsd_setspecific(arena_key, NULL);

  964.   if (a != NULL)
  965.     {
  966.       (void)mutex_lock(&list_lock);
  967.       a->next_free = free_list;
  968.       free_list = a;
  969.       (void)mutex_unlock(&list_lock);
  970.     }
  971. }
  972. text_set_element (__libc_thread_subfreeres, arena_thread_freeres);
  973. #endif

  974. #endif /* USE_ARENAS */

  975. /*
  976.  * Local variables:
  977.  * c-basic-offset: 2
  978.  * End:
  979.  */

阅读(2580) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~