xref: /third_party/musl/src/malloc/mallocng/glue.h (revision 570af302)
1#ifndef MALLOC_GLUE_H
2#define MALLOC_GLUE_H
3
4#include <stdint.h>
5#include <sys/mman.h>
6#include <pthread.h>
7#include <unistd.h>
8#include <elf.h>
9#include <string.h>
10#include "atomic.h"
11#include "syscall.h"
12#include "libc.h"
13#include "lock.h"
14#include "dynlink.h"
15
16// use macros to appropriately namespace these.
17#define size_classes __malloc_size_classes
18#define ctx __malloc_context
19#define alloc_meta __malloc_alloc_meta
20#define is_allzero __malloc_allzerop
21#define dump_heap __dump_heap
22
23#define malloc __libc_malloc_impl
24#define realloc __libc_realloc
25#define free __libc_free
26
27#define USE_MADV_FREE 0
28
29#if USE_REAL_ASSERT
30#include <assert.h>
31#else
32#undef assert
33#define assert(x) do { if (!(x)) a_crash(); } while(0)
34#endif
35
36#define brk(p) ((uintptr_t)__syscall(SYS_brk, p))
37
38#define mmap __mmap
39#define madvise __madvise
40#define mremap __mremap
41
42#define DISABLE_ALIGNED_ALLOC (__malloc_replaced && !__aligned_alloc_replaced)
43
44static inline uint64_t get_random_secret()
45{
46	uint64_t secret = (uintptr_t)&secret * 1103515245;
47	for (size_t i=0; libc.auxv[i]; i+=2)
48		if (libc.auxv[i]==AT_RANDOM)
49			memcpy(&secret, (char *)libc.auxv[i+1]+8, sizeof secret);
50	return secret;
51}
52
53#ifndef PAGESIZE
54#define PAGESIZE PAGE_SIZE
55#endif
56
57#define MT (libc.need_locks)
58
59#define RDLOCK_IS_EXCLUSIVE 1
60
61__attribute__((__visibility__("hidden")))
62extern int __malloc_lock[1];
63
64#define LOCK_OBJ_DEF \
65int __malloc_lock[1]; \
66void __malloc_atfork(int who) { malloc_atfork(who); }
67
68static inline void rdlock()
69{
70	if (MT) LOCK(__malloc_lock);
71}
72static inline void wrlock()
73{
74	if (MT) LOCK(__malloc_lock);
75}
76static inline void unlock()
77{
78	UNLOCK(__malloc_lock);
79}
80static inline void upgradelock()
81{
82}
83static inline void resetlock()
84{
85	__malloc_lock[0] = 0;
86}
87
88static inline void malloc_atfork(int who)
89{
90	if (who<0) rdlock();
91	else if (who>0) resetlock();
92	else unlock();
93}
94
95#endif
96