]>
git.saurik.com Git - redis.git/blob - deps/jemalloc/src/base.c
b1a5945efaa449e65ead1f94cc29e6deceb0ffa6
1 #define JEMALLOC_BASE_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
4 /******************************************************************************/
7 static malloc_mutex_t base_mtx
;
10 * Current pages that are being used for internal memory allocations. These
11 * pages are carved up in cacheline-size quanta, so that there is no chance of
12 * false cache line sharing.
14 static void *base_pages
;
15 static void *base_next_addr
;
16 static void *base_past_addr
; /* Addr immediately past base_pages. */
17 static extent_node_t
*base_nodes
;
19 /******************************************************************************/
20 /* Function prototypes for non-inline static functions. */
22 static bool base_pages_alloc(size_t minsize
);
24 /******************************************************************************/
27 base_pages_alloc(size_t minsize
)
33 csize
= CHUNK_CEILING(minsize
);
35 base_pages
= chunk_alloc(csize
, chunksize
, true, &zero
,
36 chunk_dss_prec_get());
37 if (base_pages
== NULL
)
39 base_next_addr
= base_pages
;
40 base_past_addr
= (void *)((uintptr_t)base_pages
+ csize
);
46 base_alloc(size_t size
)
51 /* Round size up to nearest multiple of the cacheline size. */
52 csize
= CACHELINE_CEILING(size
);
54 malloc_mutex_lock(&base_mtx
);
55 /* Make sure there's enough space for the allocation. */
56 if ((uintptr_t)base_next_addr
+ csize
> (uintptr_t)base_past_addr
) {
57 if (base_pages_alloc(csize
)) {
58 malloc_mutex_unlock(&base_mtx
);
64 base_next_addr
= (void *)((uintptr_t)base_next_addr
+ csize
);
65 malloc_mutex_unlock(&base_mtx
);
71 base_calloc(size_t number
, size_t size
)
73 void *ret
= base_alloc(number
* size
);
76 memset(ret
, 0, number
* size
);
86 malloc_mutex_lock(&base_mtx
);
87 if (base_nodes
!= NULL
) {
89 base_nodes
= *(extent_node_t
**)ret
;
90 malloc_mutex_unlock(&base_mtx
);
92 malloc_mutex_unlock(&base_mtx
);
93 ret
= (extent_node_t
*)base_alloc(sizeof(extent_node_t
));
100 base_node_dealloc(extent_node_t
*node
)
103 malloc_mutex_lock(&base_mtx
);
104 *(extent_node_t
**)node
= base_nodes
;
106 malloc_mutex_unlock(&base_mtx
);
114 if (malloc_mutex_init(&base_mtx
))
124 malloc_mutex_prefork(&base_mtx
);
128 base_postfork_parent(void)
131 malloc_mutex_postfork_parent(&base_mtx
);
135 base_postfork_child(void)
138 malloc_mutex_postfork_child(&base_mtx
);