bca718
commit 5782a80f9f8ca86899b30161166f044b0b6b8590
bca718
Author: Ondřej Bílka <neleai@seznam.cz>
bca718
Date:   Tue Dec 10 17:56:59 2013 +0100
bca718
bca718
    Drop PER_THREAD conditionals from malloc.
bca718
bca718
Index: glibc-2.17-c758a686/malloc/Makefile
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/malloc/Makefile
bca718
+++ glibc-2.17-c758a686/malloc/Makefile
bca718
@@ -121,7 +121,6 @@ endif
bca718
 tst-mcheck-ENV = MALLOC_CHECK_=3
bca718
 tst-malloc-usable-ENV = MALLOC_CHECK_=3
bca718
 
bca718
-CPPFLAGS-malloc.c += -DPER_THREAD
bca718
 # Uncomment this for test releases.  For public releases it is too expensive.
bca718
 #CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1
bca718
 
bca718
Index: glibc-2.17-c758a686/malloc/arena.c
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/malloc/arena.c
bca718
+++ glibc-2.17-c758a686/malloc/arena.c
bca718
@@ -78,10 +78,8 @@ extern int sanity_check_heap_info_alignm
bca718
 
bca718
 static tsd_key_t arena_key;
bca718
 static mutex_t list_lock = MUTEX_INITIALIZER;
bca718
-#ifdef PER_THREAD
bca718
 static size_t narenas = 1;
bca718
 static mstate free_list;
bca718
-#endif
bca718
 
bca718
 #if THREAD_STATS
bca718
 static int stat_n_heaps;
bca718
@@ -117,21 +115,12 @@ int __malloc_initialized = -1;
bca718
   ptr = (mstate)tsd_getspecific(arena_key, vptr); \
bca718
 } while(0)
bca718
 
bca718
-#ifdef PER_THREAD
bca718
 # define arena_lock(ptr, size) do { \
bca718
   if(ptr && !arena_is_corrupt (ptr)) \
bca718
     (void)mutex_lock(&ptr->mutex); \
bca718
   else \
bca718
     ptr = arena_get2(ptr, (size), NULL); \
bca718
 } while(0)
bca718
-#else
bca718
-# define arena_lock(ptr, size) do { \
bca718
-  if(ptr && !mutex_trylock(&ptr->mutex)) { \
bca718
-    THREAD_STAT(++(ptr->stat_lock_direct)); \
bca718
-  } else \
bca718
-    ptr = arena_get2(ptr, (size), NULL); \
bca718
-} while(0)
bca718
-#endif
bca718
 
bca718
 /* find the heap and corresponding arena for a given ptr */
bca718
 
bca718
@@ -297,17 +286,13 @@ ptmalloc_unlock_all2 (void)
bca718
   tsd_setspecific(arena_key, save_arena);
bca718
   __malloc_hook = save_malloc_hook;
bca718
   __free_hook = save_free_hook;
bca718
-#ifdef PER_THREAD
bca718
   free_list = NULL;
bca718
-#endif
bca718
   for(ar_ptr = &main_arena;;) {
bca718
     mutex_init(&ar_ptr->mutex);
bca718
-#ifdef PER_THREAD
bca718
     if (ar_ptr != save_arena) {
bca718
       ar_ptr->next_free = free_list;
bca718
       free_list = ar_ptr;
bca718
     }
bca718
-#endif
bca718
     ar_ptr = ar_ptr->next;
bca718
     if(ar_ptr == &main_arena) break;
bca718
   }
bca718
@@ -428,13 +413,10 @@ ptmalloc_init (void)
bca718
 		{
bca718
 		  if (memcmp (envline, "MMAP_MAX_", 9) == 0)
bca718
 		    __libc_mallopt(M_MMAP_MAX, atoi(&envline[10]));
bca718
-#ifdef PER_THREAD
bca718
 		  else if (memcmp (envline, "ARENA_MAX", 9) == 0)
bca718
 		    __libc_mallopt(M_ARENA_MAX, atoi(&envline[10]));
bca718
-#endif
bca718
 		}
bca718
 	      break;
bca718
-#ifdef PER_THREAD
bca718
 	    case 10:
bca718
 	      if (! __builtin_expect (__libc_enable_secure, 0))
bca718
 		{
bca718
@@ -442,7 +424,6 @@ ptmalloc_init (void)
bca718
 		    __libc_mallopt(M_ARENA_TEST, atoi(&envline[11]));
bca718
 		}
bca718
 	      break;
bca718
-#endif
bca718
 	    case 15:
bca718
 	      if (! __builtin_expect (__libc_enable_secure, 0))
bca718
 		{
bca718
@@ -750,18 +731,14 @@ _int_new_arena(size_t size)
bca718
   mutex_init(&a->mutex);
bca718
   (void)mutex_lock(&a->mutex);
bca718
 
bca718
-#ifdef PER_THREAD
bca718
   (void)mutex_lock(&list_lock);
bca718
-#endif
bca718
 
bca718
   /* Add the new arena to the global list.  */
bca718
   a->next = main_arena.next;
bca718
   atomic_write_barrier ();
bca718
   main_arena.next = a;
bca718
 
bca718
-#ifdef PER_THREAD
bca718
   (void)mutex_unlock(&list_lock);
bca718
-#endif
bca718
 
bca718
   THREAD_STAT(++(a->stat_lock_loop));
bca718
 
bca718
@@ -769,7 +746,6 @@ _int_new_arena(size_t size)
bca718
 }
bca718
 
bca718
 
bca718
-#ifdef PER_THREAD
bca718
 static mstate
bca718
 get_free_list (void)
bca718
 {
bca718
@@ -846,7 +822,6 @@ reused_arena (mstate avoid_arena)
bca718
 
bca718
   return result;
bca718
 }
bca718
-#endif
bca718
 
bca718
 static mstate
bca718
 internal_function
bca718
@@ -854,7 +829,6 @@ arena_get2(mstate a_tsd, size_t size, ms
bca718
 {
bca718
   mstate a;
bca718
 
bca718
-#ifdef PER_THREAD
bca718
   static size_t narenas_limit;
bca718
 
bca718
   a = get_free_list ();
bca718
@@ -897,54 +871,6 @@ arena_get2(mstate a_tsd, size_t size, ms
bca718
       else
bca718
 	a = reused_arena (avoid_arena);
bca718
     }
bca718
-#else
bca718
-  if(!a_tsd)
bca718
-    a = a_tsd = &main_arena;
bca718
-  else {
bca718
-    a = a_tsd->next;
bca718
-    if(!a) {
bca718
-      /* This can only happen while initializing the new arena. */
bca718
-      (void)mutex_lock(&main_arena.mutex);
bca718
-      THREAD_STAT(++(main_arena.stat_lock_wait));
bca718
-      return &main_arena;
bca718
-    }
bca718
-  }
bca718
-
bca718
-  /* Check the global, circularly linked list for available arenas. */
bca718
-  bool retried = false;
bca718
- repeat:
bca718
-  do {
bca718
-    if(!mutex_trylock(&a->mutex)) {
bca718
-      if (retried)
bca718
-	(void)mutex_unlock(&list_lock);
bca718
-      THREAD_STAT(++(a->stat_lock_loop));
bca718
-      LIBC_PROBE (memory_arena_reuse, 2, a, a_tsd);
bca718
-      tsd_setspecific(arena_key, (void *)a);
bca718
-      return a;
bca718
-    }
bca718
-    a = a->next;
bca718
-  } while(a != a_tsd);
bca718
-
bca718
-  /* If not even the list_lock can be obtained, try again.  This can
bca718
-     happen during `atfork', or for example on systems where thread
bca718
-     creation makes it temporarily impossible to obtain _any_
bca718
-     locks. */
bca718
-  if(!retried && mutex_trylock(&list_lock)) {
bca718
-    /* We will block to not run in a busy loop.  */
bca718
-    LIBC_PROBE (memory_arena_reuse_wait, 3, &list_lock, NULL, a_tsd);
bca718
-    (void)mutex_lock(&list_lock);
bca718
-
bca718
-    /* Since we blocked there might be an arena available now.  */
bca718
-    retried = true;
bca718
-    a = a_tsd;
bca718
-    goto repeat;
bca718
-  }
bca718
-
bca718
-  /* Nothing immediately available, so generate a new arena.  */
bca718
-  a = _int_new_arena(size);
bca718
-  (void)mutex_unlock(&list_lock);
bca718
-#endif
bca718
-
bca718
   return a;
bca718
 }
bca718
 
bca718
@@ -970,7 +896,6 @@ arena_get_retry (mstate ar_ptr, size_t b
bca718
   return ar_ptr;
bca718
 }
bca718
 
bca718
-#ifdef PER_THREAD
bca718
 static void __attribute__ ((section ("__libc_thread_freeres_fn")))
bca718
 arena_thread_freeres (void)
bca718
 {
bca718
@@ -987,7 +912,6 @@ arena_thread_freeres (void)
bca718
     }
bca718
 }
bca718
 text_set_element (__libc_thread_subfreeres, arena_thread_freeres);
bca718
-#endif
bca718
 
bca718
 /*
bca718
  * Local variables:
bca718
Index: glibc-2.17-c758a686/malloc/hooks.c
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/malloc/hooks.c
bca718
+++ glibc-2.17-c758a686/malloc/hooks.c
bca718
@@ -464,11 +464,9 @@ __malloc_get_state(void)
bca718
   ms->max_mmapped_mem = mp_.max_mmapped_mem;
bca718
   ms->using_malloc_checking = using_malloc_checking;
bca718
   ms->max_fast = get_max_fast();
bca718
-#ifdef PER_THREAD
bca718
   ms->arena_test = mp_.arena_test;
bca718
   ms->arena_max = mp_.arena_max;
bca718
   ms->narenas = narenas;
bca718
-#endif
bca718
   (void)mutex_unlock(&main_arena.mutex);
bca718
   return (void*)ms;
bca718
 }
bca718
@@ -565,11 +563,9 @@ __malloc_set_state(void* msptr)
bca718
     }
bca718
   }
bca718
   if (ms->version >= 4) {
bca718
-#ifdef PER_THREAD
bca718
     mp_.arena_test = ms->arena_test;
bca718
     mp_.arena_max = ms->arena_max;
bca718
     narenas = ms->narenas;
bca718
-#endif
bca718
   }
bca718
   check_malloc_state(&main_arena);
bca718
 
bca718
Index: glibc-2.17-c758a686/malloc/malloc.c
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/malloc/malloc.c
bca718
+++ glibc-2.17-c758a686/malloc/malloc.c
bca718
@@ -1727,10 +1727,8 @@ struct malloc_state {
bca718
   /* Linked list */
bca718
   struct malloc_state *next;
bca718
 
bca718
-#ifdef PER_THREAD
bca718
   /* Linked list for free arenas.  */
bca718
   struct malloc_state *next_free;
bca718
-#endif
bca718
 
bca718
   /* Memory allocated from the system in this arena.  */
bca718
   INTERNAL_SIZE_T system_mem;
bca718
@@ -1742,10 +1740,8 @@ struct malloc_par {
bca718
   unsigned long    trim_threshold;
bca718
   INTERNAL_SIZE_T  top_pad;
bca718
   INTERNAL_SIZE_T  mmap_threshold;
bca718
-#ifdef PER_THREAD
bca718
   INTERNAL_SIZE_T  arena_test;
bca718
   INTERNAL_SIZE_T  arena_max;
bca718
-#endif
bca718
 
bca718
   /* Memory map support */
bca718
   int              n_mmaps;
bca718
@@ -1787,18 +1783,14 @@ static struct malloc_par mp_ =
bca718
     .n_mmaps_max    = DEFAULT_MMAP_MAX,
bca718
     .mmap_threshold = DEFAULT_MMAP_THRESHOLD,
bca718
     .trim_threshold = DEFAULT_TRIM_THRESHOLD,
bca718
-#ifdef PER_THREAD
bca718
 # define NARENAS_FROM_NCORES(n) ((n) * (sizeof(long) == 4 ? 2 : 8))
bca718
     .arena_test     = NARENAS_FROM_NCORES (1)
bca718
-#endif
bca718
   };
bca718
 
bca718
 
bca718
-#ifdef PER_THREAD
bca718
 /*  Non public mallopt parameters.  */
bca718
 #define M_ARENA_TEST -7
bca718
 #define M_ARENA_MAX  -8
bca718
-#endif
bca718
 
bca718
 
bca718
 /* Maximum size of memory handled in fastbins.  */
bca718
@@ -3008,11 +3000,6 @@ __libc_realloc(void* oldmem, size_t byte
bca718
   (void)mutex_lock(&ar_ptr->mutex);
bca718
 #endif
bca718
 
bca718
-#if !defined PER_THREAD
bca718
-  LIBC_PROBE (memory_arena_reuse_realloc, 1, ar_ptr);
bca718
-  /* As in malloc(), remember this arena for the next allocation. */
bca718
-  tsd_setspecific(arena_key, (void *)ar_ptr);
bca718
-#endif
bca718
 
bca718
   newp = _int_realloc(ar_ptr, oldp, oldsize, nb);
bca718
 
bca718
@@ -4823,7 +4810,6 @@ int __libc_mallopt(int param_number, int
bca718
     perturb_byte = value;
bca718
     break;
bca718
 
bca718
-#ifdef PER_THREAD
bca718
   case M_ARENA_TEST:
bca718
     if (value > 0)
bca718
       {
bca718
@@ -4839,7 +4825,6 @@ int __libc_mallopt(int param_number, int
bca718
 	mp_.arena_max = value;
bca718
       }
bca718
     break;
bca718
-#endif
bca718
   }
bca718
   (void)mutex_unlock(&av->mutex);
bca718
   return res;