summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc
diff options
context:
space:
mode:
Diffstat (limited to 'libc/stdlib/malloc')
-rw-r--r--libc/stdlib/malloc/free.c2
-rw-r--r--libc/stdlib/malloc/heap.h40
-rw-r--r--libc/stdlib/malloc/malloc.c8
-rw-r--r--libc/stdlib/malloc/malloc.h61
4 files changed, 38 insertions, 73 deletions
diff --git a/libc/stdlib/malloc/free.c b/libc/stdlib/malloc/free.c
index e7b6a290a..14d110153 100644
--- a/libc/stdlib/malloc/free.c
+++ b/libc/stdlib/malloc/free.c
@@ -28,7 +28,7 @@
static void
__free_to_heap (void *mem, struct heap_free_area **heap
#ifdef HEAP_USE_LOCKING
- , malloc_mutex_t *heap_lock
+ , __UCLIBC_MUTEX_TYPE *heap_lock
#endif
)
{
diff --git a/libc/stdlib/malloc/heap.h b/libc/stdlib/malloc/heap.h
index 2f06ab17c..11bd49ffe 100644
--- a/libc/stdlib/malloc/heap.h
+++ b/libc/stdlib/malloc/heap.h
@@ -13,18 +13,13 @@
#include <features.h>
-
-/* On multi-threaded systems, the heap includes a lock. */
+#include <bits/uClibc_mutex.h>
#ifdef __UCLIBC_HAS_THREADS__
-# include <bits/uClibc_mutex.h>
# 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
+#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.
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
using HEAP_DECLARE_STATIC_FREE_AREA. */
-# define HEAP_INIT 0
# define HEAP_INIT_WITH_FA(fa) &fa._fa
/* 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 \
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
to stderr when the variable __heap_debug is set to true. */
#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)
#else
#define HEAP_DEBUG(heap, str) (void)0
#endif
/* 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
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. */
@@ -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.
*SIZE is adjusted to reflect the actual amount allocated (which may be
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
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.
Returns the heap free area into which the memory was placed. */
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. */
#define __heap_is_empty(heap) (! (heap))
diff --git a/libc/stdlib/malloc/malloc.c b/libc/stdlib/malloc/malloc.c
index d58a7d0ee..550d5b994 100644
--- a/libc/stdlib/malloc/malloc.c
+++ b/libc/stdlib/malloc/malloc.c
@@ -26,12 +26,12 @@
HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256);
struct heap_free_area *__malloc_heap = HEAP_INIT_WITH_FA (initial_fa);
#ifdef HEAP_USE_LOCKING
-malloc_mutex_t __malloc_heap_lock = PTHREAD_MUTEX_INITIALIZER;
+__UCLIBC_MUTEX_INIT(__malloc_heap_lock,PTHREAD_MUTEX_INITIALIZER);
#endif
#if defined(MALLOC_USE_LOCKING) && defined(MALLOC_USE_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 */
@@ -46,7 +46,7 @@ struct malloc_mmb *__malloc_mmapped_blocks = 0;
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);
#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 /* __UCLIBC_UCLINUX_BROKEN_MUNMAP__ */
@@ -59,7 +59,7 @@ malloc_mutex_t __malloc_mmb_heap_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static void *
__malloc_from_heap (size_t size, struct heap_free_area **heap
#ifdef HEAP_USE_LOCKING
- , malloc_mutex_t *heap_lock
+ , __UCLIBC_MUTEX_TYPE *heap_lock
#endif
)
{
diff --git a/libc/stdlib/malloc/malloc.h b/libc/stdlib/malloc/malloc.h
index 25f7409bf..1ce19f1b3 100644
--- a/libc/stdlib/malloc/malloc.h
+++ b/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. */
#define MALLOC_SIZE(addr) (*(size_t *)MALLOC_BASE(addr))
+#include <bits/uClibc_mutex.h>
-/* Locking for multithreaded apps. */
#ifdef __UCLIBC_HAS_THREADS__
-
-# include <bits/uClibc_mutex.h>
-
# 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
malloc and free, sbrk may be used several times in succession, and
things will break if these multiple calls are interleaved with another
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_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_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
when the variable __malloc_debug is set to true. */
#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. */
#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...) \
(__malloc_debug ? __malloc_debug_printf (indent, fmt , ##args) : 0)
# define MALLOC_DEBUG_INDENT(indent) \
(__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
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. */
#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. */
-extern struct heap_free_area *__malloc_heap;
+extern struct heap_free_area *__malloc_heap attribute_hidden;
#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