1
#define JEMALLOC_CHUNK_MMAP_C_
2
#include "jemalloc/internal/jemalloc_internal.h"
4
/******************************************************************************/
5
/* Function prototypes for non-inline static functions. */
7
static void *pages_map(void *addr, size_t size);
8
static void pages_unmap(void *addr, size_t size);
9
static void *chunk_alloc_mmap_slow(size_t size, size_t alignment,
12
/******************************************************************************/
15
pages_map(void *addr, size_t size)
23
* If VirtualAlloc can't allocate at the given address when one is
24
* given, it fails and returns NULL.
26
ret = VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE,
30
* We don't use MAP_FIXED here, because it can cause the *replacement*
31
* of existing mappings, and we only want to create new mappings.
33
ret = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
37
if (ret == MAP_FAILED)
39
else if (addr != NULL && ret != addr) {
41
* We succeeded in mapping memory, but not in the right place.
43
if (munmap(ret, size) == -1) {
44
char buf[BUFERROR_BUF];
46
buferror(buf, sizeof(buf));
47
malloc_printf("<jemalloc: Error in munmap(): %s\n",
55
assert(ret == NULL || (addr == NULL && ret != addr)
56
|| (addr != NULL && ret == addr));
61
pages_unmap(void *addr, size_t size)
65
if (VirtualFree(addr, 0, MEM_RELEASE) == 0)
67
if (munmap(addr, size) == -1)
70
char buf[BUFERROR_BUF];
72
buferror(buf, sizeof(buf));
73
malloc_printf("<jemalloc>: Error in "
86
pages_trim(void *addr, size_t alloc_size, size_t leadsize, size_t size)
88
void *ret = (void *)((uintptr_t)addr + leadsize);
90
assert(alloc_size >= leadsize + size);
95
pages_unmap(addr, alloc_size);
96
new_addr = pages_map(ret, size);
100
pages_unmap(new_addr, size);
105
size_t trailsize = alloc_size - leadsize - size;
108
pages_unmap(addr, leadsize);
110
pages_unmap((void *)((uintptr_t)ret + size), trailsize);
117
pages_purge(void *addr, size_t length)
122
VirtualAlloc(addr, length, MEM_RESET, PAGE_READWRITE);
125
# ifdef JEMALLOC_PURGE_MADVISE_DONTNEED
126
# define JEMALLOC_MADV_PURGE MADV_DONTNEED
127
# define JEMALLOC_MADV_ZEROS true
128
# elif defined(JEMALLOC_PURGE_MADVISE_FREE)
129
# define JEMALLOC_MADV_PURGE MADV_FREE
130
# define JEMALLOC_MADV_ZEROS false
132
# error "No method defined for purging unused dirty pages."
134
int err = madvise(addr, length, JEMALLOC_MADV_PURGE);
135
unzeroed = (JEMALLOC_MADV_ZEROS == false || err != 0);
136
# undef JEMALLOC_MADV_PURGE
137
# undef JEMALLOC_MADV_ZEROS
143
chunk_alloc_mmap_slow(size_t size, size_t alignment, bool *zero)
146
size_t alloc_size, leadsize;
148
alloc_size = size + alignment - PAGE;
149
/* Beware size_t wrap-around. */
150
if (alloc_size < size)
153
pages = pages_map(NULL, alloc_size);
156
leadsize = ALIGNMENT_CEILING((uintptr_t)pages, alignment) -
158
ret = pages_trim(pages, alloc_size, leadsize, size);
159
} while (ret == NULL);
167
chunk_alloc_mmap(size_t size, size_t alignment, bool *zero)
173
* Ideally, there would be a way to specify alignment to mmap() (like
174
* NetBSD has), but in the absence of such a feature, we have to work
175
* hard to efficiently create aligned mappings. The reliable, but
176
* slow method is to create a mapping that is over-sized, then trim the
177
* excess. However, that always results in one or two calls to
180
* Optimistically try mapping precisely the right amount before falling
181
* back to the slow method, with the expectation that the optimistic
182
* approach works most of the time.
185
assert(alignment != 0);
186
assert((alignment & chunksize_mask) == 0);
188
ret = pages_map(NULL, size);
191
offset = ALIGNMENT_ADDR2OFFSET(ret, alignment);
193
pages_unmap(ret, size);
194
return (chunk_alloc_mmap_slow(size, alignment, zero));
203
chunk_dealloc_mmap(void *chunk, size_t size)
207
pages_unmap(chunk, size);
209
return (config_munmap == false);