diff options
Diffstat (limited to 'lmem.c')
-rw-r--r-- | lmem.c | 21 |
1 files changed, 13 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmem.c,v 1.64 2004/04/30 20:13:38 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.65 2004/08/30 13:44:44 roberto Exp roberto $ |
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 | */ |
@@ -43,10 +43,12 @@ | |||
43 | #define MINSIZEARRAY 4 | 43 | #define MINSIZEARRAY 4 |
44 | 44 | ||
45 | 45 | ||
46 | void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, | 46 | void *luaM_growaux (lua_State *L, void *block, int *size, size_t size_elems, |
47 | int limit, const char *errormsg) { | 47 | int limit, const char *errormsg) { |
48 | void *newblock; | 48 | void *newblock; |
49 | int newsize; | 49 | int newsize; |
50 | if (cast(size_t, limit) > MAX_SIZET/size_elems) | ||
51 | limit = cast(int, MAX_SIZET/size_elems); | ||
50 | if (*size >= limit/2) { /* cannot double it? */ | 52 | if (*size >= limit/2) { /* cannot double it? */ |
51 | if (*size >= limit - MINSIZEARRAY) /* try something smaller... */ | 53 | if (*size >= limit - MINSIZEARRAY) /* try something smaller... */ |
52 | luaG_runerror(L, errormsg); | 54 | luaG_runerror(L, errormsg); |
@@ -57,22 +59,25 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, | |||
57 | if (newsize < MINSIZEARRAY) | 59 | if (newsize < MINSIZEARRAY) |
58 | newsize = MINSIZEARRAY; /* minimum size */ | 60 | newsize = MINSIZEARRAY; /* minimum size */ |
59 | } | 61 | } |
60 | newblock = luaM_realloc(L, block, | 62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); |
61 | cast(lu_mem, *size)*cast(lu_mem, size_elems), | ||
62 | cast(lu_mem, newsize)*cast(lu_mem, size_elems)); | ||
63 | *size = newsize; /* update only when everything else is OK */ | 63 | *size = newsize; /* update only when everything else is OK */ |
64 | return newblock; | 64 | return newblock; |
65 | } | 65 | } |
66 | 66 | ||
67 | 67 | ||
68 | void *luaM_toobig (lua_State *L) { | ||
69 | luaG_runerror(L, "memory allocation error: block too big"); | ||
70 | return NULL; /* to avoid warnings */ | ||
71 | } | ||
72 | |||
73 | |||
74 | |||
68 | /* | 75 | /* |
69 | ** generic allocation routine. | 76 | ** generic allocation routine. |
70 | */ | 77 | */ |
71 | void *luaM_realloc (lua_State *L, void *block, lu_mem osize, lu_mem nsize) { | 78 | void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) { |
72 | global_State *g = G(L); | 79 | global_State *g = G(L); |
73 | lua_assert((osize == 0) == (block == NULL)); | 80 | lua_assert((osize == 0) == (block == NULL)); |
74 | if (nsize >= MAX_SIZET) | ||
75 | luaG_runerror(L, "memory allocation error: block too big"); | ||
76 | block = (*g->realloc)(g->ud, block, osize, nsize); | 81 | block = (*g->realloc)(g->ud, block, osize, nsize); |
77 | if (block == NULL && nsize > 0) | 82 | if (block == NULL && nsize > 0) |
78 | luaD_throw(L, LUA_ERRMEM); | 83 | luaD_throw(L, LUA_ERRMEM); |