Просмотр исходного кода

malloc: use uClibc_mutex.h provided macros consequently

use the __UCLIBC_MUTEX macros
remove unused code
remove duplicated code (likely,unlikely)
hide internal __x() functions (mainly debug related)

Signed-off-by: Peter S. Mazinger <ps.m@gmx.net>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Peter S. Mazinger 13 лет назад
Родитель
Сommit
3b879e2627
4 измененных файлов с 38 добавлено и 73 удалено
  1. 1 1
      libc/stdlib/malloc/free.c
  2. 10 30
      libc/stdlib/malloc/heap.h
  3. 4 4
      libc/stdlib/malloc/malloc.c
  4. 23 38
      libc/stdlib/malloc/malloc.h

+ 1 - 1
libc/stdlib/malloc/free.c

@@ -28,7 +28,7 @@
 static void
 static void
 __free_to_heap (void *mem, struct heap_free_area **heap
 __free_to_heap (void *mem, struct heap_free_area **heap
 #ifdef HEAP_USE_LOCKING
 #ifdef HEAP_USE_LOCKING
-		, malloc_mutex_t *heap_lock
+		, __UCLIBC_MUTEX_TYPE *heap_lock
 #endif
 #endif
 	       )
 	       )
 {
 {

+ 10 - 30
libc/stdlib/malloc/heap.h

@@ -13,18 +13,13 @@
 
 
 #include <features.h>
 #include <features.h>
 
 
-
-/* On multi-threaded systems, the heap includes a lock.  */
+#include <bits/uClibc_mutex.h>
 #ifdef __UCLIBC_HAS_THREADS__
 #ifdef __UCLIBC_HAS_THREADS__
-# include <bits/uClibc_mutex.h>
 # define HEAP_USE_LOCKING
 # define HEAP_USE_LOCKING
-# define __heap_lock(heap_lock) __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(*(heap_lock))
-# define __heap_unlock(heap_lock) __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(*(heap_lock))
-#else
-# define __heap_lock(heap_lock)
-# define __heap_unlock(heap_lock)
 #endif
 #endif
 
 
+#define __heap_lock(heap_lock) __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(*(heap_lock))
+#define __heap_unlock(heap_lock) __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(*(heap_lock))
 
 
 /* The heap allocates in multiples of, and aligned to, HEAP_GRANULARITY.
 /* The heap allocates in multiples of, and aligned to, HEAP_GRANULARITY.
    HEAP_GRANULARITY must be a power of 2.  Malloc depends on this being the
    HEAP_GRANULARITY must be a power of 2.  Malloc depends on this being the
@@ -35,11 +30,9 @@
 
 
 
 
 
 
-/* The HEAP_INIT macro can be used as a static initializer for a heap
-   variable.  The HEAP_INIT_WITH_FA variant is used to initialize a heap
+/* The HEAP_INIT_WITH_FA variant is used to initialize a heap
    with an initial static free-area; its argument FA should be declared
    with an initial static free-area; its argument FA should be declared
    using HEAP_DECLARE_STATIC_FREE_AREA.  */
    using HEAP_DECLARE_STATIC_FREE_AREA.  */
-# define HEAP_INIT 		0
 # define HEAP_INIT_WITH_FA(fa)	&fa._fa
 # define HEAP_INIT_WITH_FA(fa)	&fa._fa
 
 
 /* A free-list area `header'.  These are actually stored at the _ends_ of
 /* A free-list area `header'.  These are actually stored at the _ends_ of
@@ -92,34 +85,21 @@ struct heap_free_area
 #define HEAP_MIN_FREE_AREA_SIZE  \
 #define HEAP_MIN_FREE_AREA_SIZE  \
   HEAP_ADJUST_SIZE (sizeof (struct heap_free_area) + 32)
   HEAP_ADJUST_SIZE (sizeof (struct heap_free_area) + 32)
 
 
-
-/* branch-prediction macros; they may already be defined by libc.  */
-#ifndef likely
-#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
-#define likely(cond)	__builtin_expect(!!(int)(cond), 1)
-#define unlikely(cond)	__builtin_expect((int)(cond), 0)
-#else
-#define likely(cond)	(cond)
-#define unlikely(cond)	(cond)
-#endif
-#endif /* !likely */
-
-
 /* Define HEAP_DEBUGGING to cause the heap routines to emit debugging info
 /* Define HEAP_DEBUGGING to cause the heap routines to emit debugging info
    to stderr when the variable __heap_debug is set to true.  */
    to stderr when the variable __heap_debug is set to true.  */
 #ifdef HEAP_DEBUGGING
 #ifdef HEAP_DEBUGGING
-extern int __heap_debug;
+extern int __heap_debug attribute_hidden;
 #define HEAP_DEBUG(heap, str) (__heap_debug ? __heap_dump (heap, str) : 0)
 #define HEAP_DEBUG(heap, str) (__heap_debug ? __heap_dump (heap, str) : 0)
 #else
 #else
 #define HEAP_DEBUG(heap, str) (void)0
 #define HEAP_DEBUG(heap, str) (void)0
 #endif
 #endif
 
 
 /* Output a text representation of HEAP to stderr, labelling it with STR.  */
 /* Output a text representation of HEAP to stderr, labelling it with STR.  */
-extern void __heap_dump (struct heap_free_area *heap, const char *str);
+extern void __heap_dump (struct heap_free_area *heap, const char *str) attribute_hidden;
 
 
 /* Do some consistency checks on HEAP.  If they fail, output an error
 /* Do some consistency checks on HEAP.  If they fail, output an error
    message to stderr, and exit.  STR is printed with the failure message.  */
    message to stderr, and exit.  STR is printed with the failure message.  */
-extern void __heap_check (struct heap_free_area *heap, const char *str);
+extern void __heap_check (struct heap_free_area *heap, const char *str) attribute_hidden;
 
 
 
 
 /* Delete the free-area FA from HEAP.  */
 /* Delete the free-area FA from HEAP.  */
@@ -216,16 +196,16 @@ __heap_free_area_alloc (struct heap_free_area **heap,
 /* Allocate and return a block at least *SIZE bytes long from HEAP.
 /* Allocate and return a block at least *SIZE bytes long from HEAP.
    *SIZE is adjusted to reflect the actual amount allocated (which may be
    *SIZE is adjusted to reflect the actual amount allocated (which may be
    greater than requested).  */
    greater than requested).  */
-extern void *__heap_alloc (struct heap_free_area **heap, size_t *size);
+extern void *__heap_alloc (struct heap_free_area **heap, size_t *size) attribute_hidden;
 
 
 /* Allocate SIZE bytes at address MEM in HEAP.  Return the actual size
 /* Allocate SIZE bytes at address MEM in HEAP.  Return the actual size
    allocated, or 0 if we failed.  */
    allocated, or 0 if we failed.  */
-extern size_t __heap_alloc_at (struct heap_free_area **heap, void *mem, size_t size);
+extern size_t __heap_alloc_at (struct heap_free_area **heap, void *mem, size_t size) attribute_hidden;
 
 
 /* Return the memory area MEM of size SIZE to HEAP.
 /* Return the memory area MEM of size SIZE to HEAP.
    Returns the heap free area into which the memory was placed.  */
    Returns the heap free area into which the memory was placed.  */
 extern struct heap_free_area *__heap_free (struct heap_free_area **heap,
 extern struct heap_free_area *__heap_free (struct heap_free_area **heap,
-					   void *mem, size_t size);
+					   void *mem, size_t size) attribute_hidden;
 
 
 /* Return true if HEAP contains absolutely no memory.  */
 /* Return true if HEAP contains absolutely no memory.  */
 #define __heap_is_empty(heap) (! (heap))
 #define __heap_is_empty(heap) (! (heap))

+ 4 - 4
libc/stdlib/malloc/malloc.c

@@ -26,12 +26,12 @@
 HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
 HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
 struct heap_free_area *__malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
 struct heap_free_area *__malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
 #ifdef HEAP_USE_LOCKING
 #ifdef HEAP_USE_LOCKING
-malloc_mutex_t __malloc_heap_lock = PTHREAD_MUTEX_INITIALIZER;
+__UCLIBC_MUTEX_INIT(__malloc_heap_lock,PTHREAD_MUTEX_INITIALIZER);
 #endif
 #endif
 
 
 #if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_SBRK)
 #if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_SBRK)
 /* A lock protecting our use of sbrk.  */
 /* A lock protecting our use of sbrk.  */
-malloc_mutex_t __malloc_sbrk_lock;
+__UCLIBC_MUTEX(__malloc_sbrk_lock);
 #endif /* MALLOC_USE_LOCKING && MALLOC_USE_SBRK */
 #endif /* MALLOC_USE_LOCKING && MALLOC_USE_SBRK */
 
 
 
 
@@ -46,7 +46,7 @@ struct malloc_mmb *__malloc_mmapped_blocks = 0;
 HEAP_DECLARE_STATIC_FREE_AREA (initial_mmb_fa, 48); /* enough for 3 mmbs */
 HEAP_DECLARE_STATIC_FREE_AREA (initial_mmb_fa, 48); /* enough for 3 mmbs */
 struct heap_free_area *__malloc_mmb_heap = HEAP_INIT_WITH_FA (initial_mmb_fa);
 struct heap_free_area *__malloc_mmb_heap = HEAP_INIT_WITH_FA (initial_mmb_fa);
 #ifdef HEAP_USE_LOCKING
 #ifdef HEAP_USE_LOCKING
-malloc_mutex_t __malloc_mmb_heap_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+__UCLIBC_MUTEX_INIT(__malloc_mmb_heap_lock,PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
 #endif
 #endif
 #endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
 #endif /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
 
 
@@ -59,7 +59,7 @@ malloc_mutex_t __malloc_mmb_heap_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 static void *
 static void *
 __malloc_from_heap (size_t size, struct heap_free_area **heap
 __malloc_from_heap (size_t size, struct heap_free_area **heap
 #ifdef HEAP_USE_LOCKING
 #ifdef HEAP_USE_LOCKING
-		, malloc_mutex_t *heap_lock
+		, __UCLIBC_MUTEX_TYPE *heap_lock
 #endif
 #endif
 		)
 		)
 {
 {

+ 23 - 38
libc/stdlib/malloc/malloc.h

@@ -128,69 +128,46 @@ extern int __malloc_mmb_debug;
 /* Return the size of a malloc allocation, given the user address.  */
 /* Return the size of a malloc allocation, given the user address.  */
 #define MALLOC_SIZE(addr)	(*(size_t *)MALLOC_BASE(addr))
 #define MALLOC_SIZE(addr)	(*(size_t *)MALLOC_BASE(addr))
 
 
+#include <bits/uClibc_mutex.h>
 
 
-/* Locking for multithreaded apps.  */
 #ifdef __UCLIBC_HAS_THREADS__
 #ifdef __UCLIBC_HAS_THREADS__
-
-# include <bits/uClibc_mutex.h>
-
 # define MALLOC_USE_LOCKING
 # define MALLOC_USE_LOCKING
+#endif
 
 
-typedef __UCLIBC_MUTEX_TYPE malloc_mutex_t;
-# define MALLOC_MUTEX_INIT	__UCLIBC_MUTEX_INITIALIZER
-
-# ifdef MALLOC_USE_SBRK
+#ifdef MALLOC_USE_SBRK
 /* This lock is used to serialize uses of the `sbrk' function (in both
 /* This lock is used to serialize uses of the `sbrk' function (in both
    malloc and free, sbrk may be used several times in succession, and
    malloc and free, sbrk may be used several times in succession, and
    things will break if these multiple calls are interleaved with another
    things will break if these multiple calls are interleaved with another
    thread's use of sbrk!).  */
    thread's use of sbrk!).  */
-extern malloc_mutex_t __malloc_sbrk_lock;
+__UCLIBC_MUTEX_EXTERN(__malloc_sbrk_lock) attribute_hidden;
 #  define __malloc_lock_sbrk()	__UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE (__malloc_sbrk_lock)
 #  define __malloc_lock_sbrk()	__UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE (__malloc_sbrk_lock)
 #  define __malloc_unlock_sbrk() __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE (__malloc_sbrk_lock)
 #  define __malloc_unlock_sbrk() __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE (__malloc_sbrk_lock)
-# endif /* MALLOC_USE_SBRK */
-
-#else /* !__UCLIBC_HAS_THREADS__ */
-
-/* Without threads, mutex operations are a nop.  */
+#else
 # define __malloc_lock_sbrk()	(void)0
 # define __malloc_lock_sbrk()	(void)0
 # define __malloc_unlock_sbrk()	(void)0
 # define __malloc_unlock_sbrk()	(void)0
-
-#endif /* __UCLIBC_HAS_THREADS__ */
-
-
-/* branch-prediction macros; they may already be defined by libc.  */
-#ifndef likely
-#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
-#define likely(cond)	__builtin_expect(!!(int)(cond), 1)
-#define unlikely(cond)	__builtin_expect((int)(cond), 0)
-#else
-#define likely(cond)	(cond)
-#define unlikely(cond)	(cond)
-#endif
-#endif /* !likely */
-
+#endif /* MALLOC_USE_SBRK */
 
 
 /* Define MALLOC_DEBUGGING to cause malloc to emit debugging info to stderr
 /* Define MALLOC_DEBUGGING to cause malloc to emit debugging info to stderr
    when the variable __malloc_debug is set to true. */
    when the variable __malloc_debug is set to true. */
 #ifdef MALLOC_DEBUGGING
 #ifdef MALLOC_DEBUGGING
 
 
-extern void __malloc_debug_init (void);
+extern void __malloc_debug_init (void) attribute_hidden;
 
 
 /* The number of spaces in a malloc debug indent level.  */
 /* The number of spaces in a malloc debug indent level.  */
 #define MALLOC_DEBUG_INDENT_SIZE 3
 #define MALLOC_DEBUG_INDENT_SIZE 3
 
 
-extern int __malloc_debug, __malloc_check;
+extern int __malloc_debug attribute_hidden, __malloc_check attribute_hidden;
 
 
 # define MALLOC_DEBUG(indent, fmt, args...)				      \
 # define MALLOC_DEBUG(indent, fmt, args...)				      \
    (__malloc_debug ? __malloc_debug_printf (indent, fmt , ##args) : 0)
    (__malloc_debug ? __malloc_debug_printf (indent, fmt , ##args) : 0)
 # define MALLOC_DEBUG_INDENT(indent)					      \
 # define MALLOC_DEBUG_INDENT(indent)					      \
    (__malloc_debug ? __malloc_debug_indent (indent) : 0)
    (__malloc_debug ? __malloc_debug_indent (indent) : 0)
 
 
-extern int __malloc_debug_cur_indent;
+extern int __malloc_debug_cur_indent attribute_hidden;
 
 
 /* Print FMT and args indented at the current debug print level, followed
 /* Print FMT and args indented at the current debug print level, followed
    by a newline, and change the level by INDENT.  */
    by a newline, and change the level by INDENT.  */
-extern void __malloc_debug_printf (int indent, const char *fmt, ...);
+extern void __malloc_debug_printf (int indent, const char *fmt, ...) attribute_hidden;
 
 
 /* Change the current debug print level by INDENT, and return the value.  */
 /* Change the current debug print level by INDENT, and return the value.  */
 #define __malloc_debug_indent(indent) (__malloc_debug_cur_indent += indent)
 #define __malloc_debug_indent(indent) (__malloc_debug_cur_indent += indent)
@@ -220,10 +197,18 @@ extern void __malloc_debug_printf (int indent, const char *fmt, ...);
 
 
 
 
 /* The malloc heap.  */
 /* The malloc heap.  */
-extern struct heap_free_area *__malloc_heap;
+extern struct heap_free_area *__malloc_heap attribute_hidden;
 #ifdef __UCLIBC_HAS_THREADS__
 #ifdef __UCLIBC_HAS_THREADS__
-extern malloc_mutex_t __malloc_heap_lock;
-#ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
-extern malloc_mutex_t __malloc_mmb_heap_lock;
-#endif
+__UCLIBC_MUTEX_EXTERN(__malloc_heap_lock)
+# ifndef __LINUXTHREADS_OLD__
+	attribute_hidden
+# endif
+	;
+# ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
+__UCLIBC_MUTEX_EXTERN(__malloc_mmb_heap_lock)
+#  ifndef __LINUXTHREADS_OLD__
+	attribute_hidden
+#  endif
+	;
+# endif
 #endif
 #endif