1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/* enable mremap on GNU/Linux */
#ifdef __linux__
# define _GNU_SOURCE
#endif
/* enable the POSIX prototypes of mmap/munmap on Solaris */
#ifdef __sun
# if __STDC_VERSION__ >= 199901L
# define _XOPEN_SOURCE 600
# else
# define _XOPEN_SOURCE 500
# endif
#endif
#include "emman.h"
#include <ecb.h>
#include <string.h>
#ifndef _WIN32
# include <unistd.h>
#endif
#if _POSIX_MAPPED_FILES > 0
# define USE_MMAP 1
# include <sys/mman.h>
# ifndef MAP_FAILED
# define MAP_FAILED ((void *)-1)
# endif
# ifndef MAP_ANONYMOUS
# ifdef MAP_ANON
# define MAP_ANONYMOUS MAP_ANON
# else
# undef USE_MMAP
# endif
# endif
# include <limits.h>
# ifndef PAGESIZE
static uint32_t pagesize;
# define BOOT_PAGESIZE if (!pagesize) pagesize = sysconf (_SC_PAGESIZE)
# define PAGESIZE pagesize
# else
# define BOOT_PAGESIZE
# endif
#else
# define PAGESIZE 1
# define BOOT_PAGESIZE
#endif
size_t
chunk_round (size_t size)
{
BOOT_PAGESIZE;
return (size + (PAGESIZE - 1)) & ~(size_t)(PAGESIZE - 1);
}
size_t
chunk_fit (size_t header, size_t element_size, size_t max_increase)
{
uint32_t fill, maximum_fill = 0;
size_t minimum_size;
max_increase += header + element_size;
BOOT_PAGESIZE;
do
{
header += element_size;
fill = (uint32_t)header & (PAGESIZE - 1);
if (fill >= maximum_fill)
{
maximum_fill = fill + 16; /* size increase results in at least 16 bytes improvement */
minimum_size = header;
}
}
while (header < max_increase);
return minimum_size;
}
void *
chunk_alloc (size_t size, int populate)
{
#if USE_MMAP
void *ptr = MAP_FAILED;
#ifdef MAP_POPULATE
if (populate & 1)
ptr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
#endif
if (ptr == MAP_FAILED)
ptr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED)
return 0;
return ptr;
#else
return malloc (size);
#endif
}
void *
chunk_realloc (void *ptr, size_t old_size, size_t new_size)
{
#if USE_MMAP
#ifdef MREMAP_MAYMOVE /* requires _GNU_SOURCE */
void *ptr2 = mremap (ptr, old_size, new_size, MREMAP_MAYMOVE);
if (ptr2 == MAP_FAILED)
return 0;
return ptr2;
#else
void *ptr2 = chunk_alloc (new_size, 0);
if (!ptr2)
return ptr2;
/* TODO: prepopulate old_size pages instead of faulting them in */
memcpy (ptr2, ptr, old_size);
munmap (ptr, old_size);
return ptr2;
#endif
#else
return realloc (ptr, new_size);
#endif
}
void
chunk_free (void *ptr, size_t size)
{
#if USE_MMAP
/* we assume the OS never mmaps at address 0 */
if (ptr)
munmap (ptr, size);
#else
return free (ptr);
#endif
}
|