]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/jemalloc/src/chunk_mmap.c
Update libc++ to 3.7.0 release.
[FreeBSD/FreeBSD.git] / contrib / jemalloc / src / chunk_mmap.c
1 #define JEMALLOC_CHUNK_MMAP_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
3
4 /******************************************************************************/
5
6 static void *
7 chunk_alloc_mmap_slow(size_t size, size_t alignment, bool *zero, bool *commit)
8 {
9         void *ret, *pages;
10         size_t alloc_size, leadsize;
11
12         alloc_size = size + alignment - PAGE;
13         /* Beware size_t wrap-around. */
14         if (alloc_size < size)
15                 return (NULL);
16         do {
17                 pages = pages_map(NULL, alloc_size);
18                 if (pages == NULL)
19                         return (NULL);
20                 leadsize = ALIGNMENT_CEILING((uintptr_t)pages, alignment) -
21                     (uintptr_t)pages;
22                 ret = pages_trim(pages, alloc_size, leadsize, size);
23         } while (ret == NULL);
24
25         assert(ret != NULL);
26         *zero = true;
27         if (!*commit)
28                 *commit = pages_decommit(ret, size);
29         return (ret);
30 }
31
32 void *
33 chunk_alloc_mmap(size_t size, size_t alignment, bool *zero, bool *commit)
34 {
35         void *ret;
36         size_t offset;
37
38         /*
39          * Ideally, there would be a way to specify alignment to mmap() (like
40          * NetBSD has), but in the absence of such a feature, we have to work
41          * hard to efficiently create aligned mappings.  The reliable, but
42          * slow method is to create a mapping that is over-sized, then trim the
43          * excess.  However, that always results in one or two calls to
44          * pages_unmap().
45          *
46          * Optimistically try mapping precisely the right amount before falling
47          * back to the slow method, with the expectation that the optimistic
48          * approach works most of the time.
49          */
50
51         assert(alignment != 0);
52         assert((alignment & chunksize_mask) == 0);
53
54         ret = pages_map(NULL, size);
55         if (ret == NULL)
56                 return (NULL);
57         offset = ALIGNMENT_ADDR2OFFSET(ret, alignment);
58         if (offset != 0) {
59                 pages_unmap(ret, size);
60                 return (chunk_alloc_mmap_slow(size, alignment, zero, commit));
61         }
62
63         assert(ret != NULL);
64         *zero = true;
65         if (!*commit)
66                 *commit = pages_decommit(ret, size);
67         return (ret);
68 }
69
70 bool
71 chunk_dalloc_mmap(void *chunk, size_t size)
72 {
73
74         if (config_munmap)
75                 pages_unmap(chunk, size);
76
77         return (!config_munmap);
78 }