diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-15 10:51:04 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-15 10:51:04 -0300 |
commit | 88e5c6b80bd8dac1b4a6edc8401c012a409b0d7c (patch) | |
tree | 3253ccbb58d8a377f218e72a3b5e57ca4f375197 /lmem.c | |
parent | e33e1bda97871c17c1ba23ea508ce295125a65e9 (diff) | |
parent | 35e01ed21d018e1c6428ae0351d8597e53a620b3 (diff) | |
download | lua-88e5c6b80bd8dac1b4a6edc8401c012a409b0d7c.tar.gz lua-88e5c6b80bd8dac1b4a6edc8401c012a409b0d7c.tar.bz2 lua-88e5c6b80bd8dac1b4a6edc8401c012a409b0d7c.zip |
Merge branch 'master' into nextversion
Diffstat (limited to 'lmem.c')
-rw-r--r-- | lmem.c | 68 |
1 files changed, 41 insertions, 27 deletions
@@ -22,25 +22,6 @@ | |||
22 | #include "lstate.h" | 22 | #include "lstate.h" |
23 | 23 | ||
24 | 24 | ||
25 | #if defined(EMERGENCYGCTESTS) | ||
26 | /* | ||
27 | ** First allocation will fail whenever not building initial state. | ||
28 | ** (This fail will trigger 'tryagain' and a full GC cycle at every | ||
29 | ** allocation.) | ||
30 | */ | ||
31 | static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { | ||
32 | if (completestate(g) && ns > 0) /* frees never fail */ | ||
33 | return NULL; /* fail */ | ||
34 | else /* normal allocation */ | ||
35 | return (*g->frealloc)(g->ud, block, os, ns); | ||
36 | } | ||
37 | #else | ||
38 | #define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) | ||
39 | #endif | ||
40 | |||
41 | |||
42 | |||
43 | |||
44 | 25 | ||
45 | /* | 26 | /* |
46 | ** About the realloc function: | 27 | ** About the realloc function: |
@@ -60,6 +41,43 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { | |||
60 | */ | 41 | */ |
61 | 42 | ||
62 | 43 | ||
44 | /* | ||
45 | ** Macro to call the allocation function. | ||
46 | */ | ||
47 | #define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) | ||
48 | |||
49 | |||
50 | /* | ||
51 | ** When an allocation fails, it will try again after an emergency | ||
52 | ** collection, except when it cannot run a collection. The GC should | ||
53 | ** not be called while the state is not fully built, as the collector | ||
54 | ** is not yet fully initialized. Also, it should not be called when | ||
55 | ** 'gcstopem' is true, because then the interpreter is in the middle of | ||
56 | ** a collection step. | ||
57 | */ | ||
58 | #define cantryagain(g) (completestate(g) && !g->gcstopem) | ||
59 | |||
60 | |||
61 | |||
62 | |||
63 | #if defined(EMERGENCYGCTESTS) | ||
64 | /* | ||
65 | ** First allocation will fail except when freeing a block (frees never | ||
66 | ** fail) and when it cannot try again; this fail will trigger 'tryagain' | ||
67 | ** and a full GC cycle at every allocation. | ||
68 | */ | ||
69 | static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { | ||
70 | if (ns > 0 && cantryagain(g)) | ||
71 | return NULL; /* fail */ | ||
72 | else /* normal allocation */ | ||
73 | return callfrealloc(g, block, os, ns); | ||
74 | } | ||
75 | #else | ||
76 | #define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns) | ||
77 | #endif | ||
78 | |||
79 | |||
80 | |||
63 | 81 | ||
64 | 82 | ||
65 | /* | 83 | /* |
@@ -132,7 +150,7 @@ l_noret luaM_toobig (lua_State *L) { | |||
132 | void luaM_free_ (lua_State *L, void *block, size_t osize) { | 150 | void luaM_free_ (lua_State *L, void *block, size_t osize) { |
133 | global_State *g = G(L); | 151 | global_State *g = G(L); |
134 | lua_assert((osize == 0) == (block == NULL)); | 152 | lua_assert((osize == 0) == (block == NULL)); |
135 | (*g->frealloc)(g->ud, block, osize, 0); | 153 | callfrealloc(g, block, osize, 0); |
136 | g->totalbytes -= osize; | 154 | g->totalbytes -= osize; |
137 | } | 155 | } |
138 | 156 | ||
@@ -140,19 +158,15 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { | |||
140 | /* | 158 | /* |
141 | ** In case of allocation fail, this function will do an emergency | 159 | ** In case of allocation fail, this function will do an emergency |
142 | ** collection to free some memory and then try the allocation again. | 160 | ** collection to free some memory and then try the allocation again. |
143 | ** The GC should not be called while state is not fully built, as the | ||
144 | ** collector is not yet fully initialized. Also, it should not be called | ||
145 | ** when 'gcstopem' is true, because then the interpreter is in the | ||
146 | ** middle of a collection step. | ||
147 | */ | 161 | */ |
148 | static void *tryagain (lua_State *L, void *block, | 162 | static void *tryagain (lua_State *L, void *block, |
149 | size_t osize, size_t nsize) { | 163 | size_t osize, size_t nsize) { |
150 | global_State *g = G(L); | 164 | global_State *g = G(L); |
151 | if (completestate(g) && !g->gcstopem) { | 165 | if (cantryagain(g)) { |
152 | luaC_fullgc(L, 1); /* try to free some memory... */ | 166 | luaC_fullgc(L, 1); /* try to free some memory... */ |
153 | return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ | 167 | return callfrealloc(g, block, osize, nsize); /* try again */ |
154 | } | 168 | } |
155 | else return NULL; /* cannot free any memory without a full state */ | 169 | else return NULL; /* cannot run an emergency collection */ |
156 | } | 170 | } |
157 | 171 | ||
158 | 172 | ||