forked from LuaJIT/LuaJIT
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_memory_allocator.patch
More file actions
187 lines (170 loc) · 6.35 KB
/
custom_memory_allocator.patch
File metadata and controls
187 lines (170 loc) · 6.35 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
diff --git a/src/lauxlib.h b/src/lauxlib.h
index fed1491..a2a65c3 100644
--- a/src/lauxlib.h
+++ b/src/lauxlib.h
@@ -67,6 +67,8 @@ LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
LUALIB_API lua_State *(luaL_newstate) (void);
+LUALIB_API lua_State *(luaL_newstate_mmap) (lua_Mmap cust_mmap,
+ lua_Munmap cust_munmap);
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
diff --git a/src/lib_aux.c b/src/lib_aux.c
index 4a1b70d..b083b5d 100644
--- a/src/lib_aux.c
+++ b/src/lib_aux.c
@@ -331,8 +331,13 @@ LUALIB_API lua_State *luaL_newstate(void)
LUALIB_API lua_State *luaL_newstate(void)
{
+ return luaL_newstate_mmap(0, 0);
+}
+
+LUALIB_API lua_State *luaL_newstate_mmap(lua_Mmap cust_mmap, lua_Munmap cust_munmap)
+{
lua_State *L;
- void *ud = lj_alloc_create();
+ void *ud = lj_alloc_create(cust_mmap, cust_munmap);
if (ud == NULL) return NULL;
#if LJ_64
L = lj_state_newstate(lj_alloc_f, ud);
diff --git a/src/lj_alloc.c b/src/lj_alloc.c
index 7c7ec67..be1ce80 100644
--- a/src/lj_alloc.c
+++ b/src/lj_alloc.c
@@ -84,34 +84,43 @@ typedef long (*PNTAVM)(HANDLE handle, void **addr, ULONG zbits,
size_t *size, ULONG alloctype, ULONG prot);
static PNTAVM ntavm;
+static PMMAP custom_mmap;
+static PMUNMAP custom_munmap;
+
/* Number of top bits of the lower 32 bits of an address that must be zero.
** Apparently 0 gives us full 64 bit addresses and 1 gives us the lower 2GB.
*/
#define NTAVM_ZEROBITS 1
-static void INIT_MMAP(void)
+static void INIT_MMAP(PMMAP cust_mmap, PMUNMAP cust_munmap)
{
ntavm = (PNTAVM)GetProcAddress(GetModuleHandleA("ntdll.dll"),
"NtAllocateVirtualMemory");
+ custom_mmap = cust_mmap;
+ custom_munmap = cust_munmap;
}
/* Win64 32 bit MMAP via NtAllocateVirtualMemory. */
-static LJ_AINLINE void *CALL_MMAP(size_t size)
+static LJ_AINLINE void *CALL_MMAP(size_t* size)
{
+ if (custom_mmap)
+ return custom_mmap(size);
DWORD olderr = GetLastError();
void *ptr = NULL;
- long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size,
+ long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, size,
MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
SetLastError(olderr);
return st == 0 ? ptr : MFAIL;
}
/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
-static LJ_AINLINE void *DIRECT_MMAP(size_t size)
+static LJ_AINLINE void *DIRECT_MMAP(size_t* size)
{
+ if (custom_mmap)
+ return custom_mmap(size);
DWORD olderr = GetLastError();
void *ptr = NULL;
- long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size,
+ long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, size,
MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE);
SetLastError(olderr);
return st == 0 ? ptr : MFAIL;
@@ -119,22 +128,22 @@ static LJ_AINLINE void *DIRECT_MMAP(size_t size)
#else
-#define INIT_MMAP() ((void)0)
+#define INIT_MMAP(a,b) ((void)0)
/* Win32 MMAP via VirtualAlloc */
-static LJ_AINLINE void *CALL_MMAP(size_t size)
+static LJ_AINLINE void *CALL_MMAP(size_t* size)
{
DWORD olderr = GetLastError();
- void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
+ void *ptr = VirtualAlloc(0, *size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
SetLastError(olderr);
return ptr ? ptr : MFAIL;
}
/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
-static LJ_AINLINE void *DIRECT_MMAP(size_t size)
+static LJ_AINLINE void *DIRECT_MMAP(size_t* size)
{
DWORD olderr = GetLastError();
- void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN,
+ void *ptr = VirtualAlloc(0, *size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN,
PAGE_READWRITE);
SetLastError(olderr);
return ptr ? ptr : MFAIL;
@@ -145,6 +154,10 @@ static LJ_AINLINE void *DIRECT_MMAP(size_t size)
/* This function supports releasing coalesed segments */
static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size)
{
+#if LJ_64
+ if (custom_munmap)
+ return custom_munmap(ptr, size);
+#endif
DWORD olderr = GetLastError();
MEMORY_BASIC_INFORMATION minfo;
char *cptr = (char *)ptr;
@@ -745,7 +758,7 @@ static void *direct_alloc(size_t nb)
{
size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
if (LJ_LIKELY(mmsize > nb)) { /* Check for wrap around 0 */
- char *mm = (char *)(DIRECT_MMAP(mmsize));
+ char *mm = (char *)(DIRECT_MMAP(&mmsize));
if (mm != CMFAIL) {
size_t offset = align_offset(chunk2mem(mm));
size_t psize = mmsize - offset - DIRECT_FOOT_PAD;
@@ -914,7 +927,7 @@ static void *alloc_sys(mstate m, size_t nb)
size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;
size_t rsize = granularity_align(req);
if (LJ_LIKELY(rsize > nb)) { /* Fail if wraps around zero */
- char *mp = (char *)(CALL_MMAP(rsize));
+ char *mp = (char *)(CALL_MMAP(&rsize));
if (mp != CMFAIL) {
tbase = mp;
tsize = rsize;
@@ -1141,12 +1154,12 @@ static void *tmalloc_small(mstate m, size_t nb)
/* ----------------------------------------------------------------------- */
-void *lj_alloc_create(void)
+void *lj_alloc_create(PMMAP cust_mmap, PMUNMAP cust_munmap)
{
size_t tsize = DEFAULT_GRANULARITY;
char *tbase;
- INIT_MMAP();
- tbase = (char *)(CALL_MMAP(tsize));
+ INIT_MMAP(cust_mmap, cust_munmap);
+ tbase = (char *)(CALL_MMAP(&tsize));
if (tbase != CMFAIL) {
size_t msize = pad_request(sizeof(struct malloc_state));
mchunkptr mn;
diff --git a/src/lj_alloc.h b/src/lj_alloc.h
index f87a7cf..3950679 100644
--- a/src/lj_alloc.h
+++ b/src/lj_alloc.h
@@ -9,7 +9,10 @@
#include "lj_def.h"
#ifndef LUAJIT_USE_SYSMALLOC
-LJ_FUNC void *lj_alloc_create(void);
+typedef void* (*PMMAP)(size_t* size);
+typedef int (*PMUNMAP)(void *ptr, size_t size);
+
+LJ_FUNC void *lj_alloc_create(PMMAP cust_mmap, PMUNMAP cust_munmap);
LJ_FUNC void lj_alloc_destroy(void *msp);
LJ_FUNC void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize);
#endif
diff --git a/src/lua.h b/src/lua.h
index c83fd3b..2abb3a6 100644
--- a/src/lua.h
+++ b/src/lua.h
@@ -65,6 +65,8 @@ typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
*/
typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
+typedef void* (*lua_Mmap)(size_t* size);
+typedef int (*lua_Munmap)(void *ptr, size_t size);
/*
** basic types