diff options
Diffstat (limited to 'apps/plugins/lua/lmem.c')
-rw-r--r-- | apps/plugins/lua/lmem.c | 45 |
1 files changed, 16 insertions, 29 deletions
diff --git a/apps/plugins/lua/lmem.c b/apps/plugins/lua/lmem.c index ee343e3e03..ae7d8c965f 100644 --- a/apps/plugins/lua/lmem.c +++ b/apps/plugins/lua/lmem.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ | 2 | ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ |
3 | ** Interface to Memory Manager | 3 | ** Interface to Memory Manager |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -14,7 +14,6 @@ | |||
14 | 14 | ||
15 | #include "ldebug.h" | 15 | #include "ldebug.h" |
16 | #include "ldo.h" | 16 | #include "ldo.h" |
17 | #include "lgc.h" | ||
18 | #include "lmem.h" | 17 | #include "lmem.h" |
19 | #include "lobject.h" | 18 | #include "lobject.h" |
20 | #include "lstate.h" | 19 | #include "lstate.h" |
@@ -26,11 +25,12 @@ | |||
26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); | 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); |
27 | ** (`osize' is the old size, `nsize' is the new size) | 26 | ** (`osize' is the old size, `nsize' is the new size) |
28 | ** | 27 | ** |
29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no | 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). |
30 | ** matter 'x'). | 29 | ** |
30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' | ||
31 | ** | 31 | ** |
32 | ** * frealloc(ud, p, x, 0) frees the block `p' | 32 | ** * frealloc(ud, p, x, 0) frees the block `p' |
33 | ** (in this specific case, frealloc must return NULL); | 33 | ** (in this specific case, frealloc must return NULL). |
34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing | 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing |
35 | ** (which is equivalent to free(NULL) in ANSI C) | 35 | ** (which is equivalent to free(NULL) in ANSI C) |
36 | ** | 36 | ** |
@@ -44,12 +44,12 @@ | |||
44 | 44 | ||
45 | 45 | ||
46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, |
47 | int limit, const char *what) { | 47 | int limit, const char *errormsg) { |
48 | void *newblock; | 48 | void *newblock; |
49 | int newsize; | 49 | int newsize; |
50 | if (*size >= limit/2) { /* cannot double it? */ | 50 | if (*size >= limit/2) { /* cannot double it? */ |
51 | if (*size >= limit) /* cannot grow even a little? */ | 51 | if (*size >= limit) /* cannot grow even a little? */ |
52 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); | 52 | luaG_runerror(L, errormsg); |
53 | newsize = limit; /* still have at least one free place */ | 53 | newsize = limit; /* still have at least one free place */ |
54 | } | 54 | } |
55 | else { | 55 | else { |
@@ -63,8 +63,9 @@ void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, | |||
63 | } | 63 | } |
64 | 64 | ||
65 | 65 | ||
66 | l_noret luaM_toobig (lua_State *L) { | 66 | void *luaM_toobig (lua_State *L) { |
67 | luaG_runerror(L, "memory allocation error: block too big"); | 67 | luaG_runerror(L, "memory allocation error: block too big"); |
68 | return NULL; /* to avoid warnings */ | ||
68 | } | 69 | } |
69 | 70 | ||
70 | 71 | ||
@@ -73,27 +74,13 @@ l_noret luaM_toobig (lua_State *L) { | |||
73 | ** generic allocation routine. | 74 | ** generic allocation routine. |
74 | */ | 75 | */ |
75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { | 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { |
76 | void *newblock; | ||
77 | global_State *g = G(L); | 77 | global_State *g = G(L); |
78 | size_t realosize = (block) ? osize : 0; | 78 | lua_assert((osize == 0) == (block == NULL)); |
79 | lua_assert((realosize == 0) == (block == NULL)); | 79 | block = (*g->frealloc)(g->ud, block, osize, nsize); |
80 | #if defined(HARDMEMTESTS) | 80 | if (block == NULL && nsize > 0) |
81 | if (nsize > realosize && g->gcrunning) | 81 | luaD_throw(L, LUA_ERRMEM); |
82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ | 82 | lua_assert((nsize == 0) == (block == NULL)); |
83 | #endif | 83 | g->totalbytes = (g->totalbytes - osize) + nsize; |
84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); | 84 | return block; |
85 | if (newblock == NULL && nsize > 0) { | ||
86 | api_check(L, nsize > realosize, | ||
87 | "realloc cannot fail when shrinking a block"); | ||
88 | if (g->gcrunning) { | ||
89 | luaC_fullgc(L, 1); /* try to free some memory... */ | ||
90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ | ||
91 | } | ||
92 | if (newblock == NULL) | ||
93 | luaD_throw(L, LUA_ERRMEM); | ||
94 | } | ||
95 | lua_assert((nsize == 0) == (newblock == NULL)); | ||
96 | g->GCdebt = (g->GCdebt + nsize) - realosize; | ||
97 | return newblock; | ||
98 | } | 85 | } |
99 | 86 | ||