summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lmem.c')
-rw-r--r--apps/plugins/lua/lmem.c45
1 files changed, 29 insertions, 16 deletions
diff --git a/apps/plugins/lua/lmem.c b/apps/plugins/lua/lmem.c
index ae7d8c965f..ee343e3e03 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.70.1.1 2007/12/27 13:02:25 roberto Exp $ 2** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 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,6 +14,7 @@
14 14
15#include "ldebug.h" 15#include "ldebug.h"
16#include "ldo.h" 16#include "ldo.h"
17#include "lgc.h"
17#include "lmem.h" 18#include "lmem.h"
18#include "lobject.h" 19#include "lobject.h"
19#include "lstate.h" 20#include "lstate.h"
@@ -25,12 +26,11 @@
25** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
26** (`osize' is the old size, `nsize' is the new size) 27** (`osize' is the old size, `nsize' is the new size)
27** 28**
28** Lua ensures that (ptr == NULL) iff (osize == 0). 29** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
29** 30** matter 'x').
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
46void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 46void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
47 int limit, const char *errormsg) { 47 int limit, const char *what) {
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, errormsg); 52 luaG_runerror(L, "too many %s (limit is %d)", what, limit);
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,9 +63,8 @@ void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
63} 63}
64 64
65 65
66void *luaM_toobig (lua_State *L) { 66l_noret 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 */
69} 68}
70 69
71 70
@@ -74,13 +73,27 @@ void *luaM_toobig (lua_State *L) {
74** generic allocation routine. 73** generic allocation routine.
75*/ 74*/
76void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 75void *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 lua_assert((osize == 0) == (block == NULL)); 78 size_t realosize = (block) ? osize : 0;
79 block = (*g->frealloc)(g->ud, block, osize, nsize); 79 lua_assert((realosize == 0) == (block == NULL));
80 if (block == NULL && nsize > 0) 80#if defined(HARDMEMTESTS)
81 luaD_throw(L, LUA_ERRMEM); 81 if (nsize > realosize && g->gcrunning)
82 lua_assert((nsize == 0) == (block == NULL)); 82 luaC_fullgc(L, 1); /* force a GC whenever possible */
83 g->totalbytes = (g->totalbytes - osize) + nsize; 83#endif
84 return block; 84 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
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;
85} 98}
86 99