diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-12-08 15:28:25 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-12-08 15:28:25 -0200 |
| commit | e663a24ab03a54fa221c20a793812e5c5ffdf94f (patch) | |
| tree | 8fbd40f779f0eed29d46f26c07e1234fd5df8bae /lmem.h | |
| parent | 40f823ec907fd725617e37199199b3ed424bd88c (diff) | |
| download | lua-e663a24ab03a54fa221c20a793812e5c5ffdf94f.tar.gz lua-e663a24ab03a54fa221c20a793812e5c5ffdf94f.tar.bz2 lua-e663a24ab03a54fa221c20a793812e5c5ffdf94f.zip | |
more freedom in handling memory-allocation errors (not all allocations
automatically raise an error), which allows fixing a bug when resizing
a table.
Diffstat (limited to 'lmem.h')
| -rw-r--r-- | lmem.h | 23 |
1 files changed, 14 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.h,v 1.44 2017/12/06 18:36:31 roberto Exp roberto $ | 2 | ** $Id: lmem.h,v 1.45 2017/12/07 18:59:52 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 | */ |
| @@ -14,6 +14,9 @@ | |||
| 14 | #include "lua.h" | 14 | #include "lua.h" |
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) | ||
| 18 | |||
| 19 | |||
| 17 | /* | 20 | /* |
| 18 | ** This macro tests whether it is safe to multiply 'n' by the size of | 21 | ** This macro tests whether it is safe to multiply 'n' by the size of |
| 19 | ** type 't' without overflows. Because 'e' is always constant, it avoids | 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids |
| @@ -45,26 +48,26 @@ | |||
| 45 | ** Arrays of chars do not need any test | 48 | ** Arrays of chars do not need any test |
| 46 | */ | 49 | */ |
| 47 | #define luaM_reallocvchar(L,b,on,n) \ | 50 | #define luaM_reallocvchar(L,b,on,n) \ |
| 48 | cast(char *, luaM_realloc(L, (b), (on)*sizeof(char), (n)*sizeof(char))) | 51 | cast(char *, luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) |
| 49 | 52 | ||
| 50 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) | 53 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) |
| 51 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) | 54 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) |
| 52 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) | 55 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) |
| 53 | 56 | ||
| 54 | #define luaM_new(L,t) cast(t*, luaM_malloc(L, sizeof(t), 0)) | 57 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) |
| 55 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc(L, (n)*sizeof(t), 0)) | 58 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) |
| 56 | #define luaM_newvectorchecked(L,n,t) \ | 59 | #define luaM_newvectorchecked(L,n,t) \ |
| 57 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) | 60 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) |
| 58 | 61 | ||
| 59 | #define luaM_newobject(L,tag,s) luaM_malloc(L, (s), tag) | 62 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) |
| 60 | 63 | ||
| 61 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ | 64 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ |
| 62 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ | 65 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ |
| 63 | luaM_limitN(limit,t),e))) | 66 | luaM_limitN(limit,t),e))) |
| 64 | 67 | ||
| 65 | #define luaM_reallocvector(L, v,oldn,n,t) \ | 68 | #define luaM_reallocvector(L, v,oldn,n,t) \ |
| 66 | ((v)=cast(t *, luaM_realloc(L, v, cast(size_t, oldn) * sizeof(t), \ | 69 | (cast(t *, luaM_realloc_(L, v, cast(size_t, oldn) * sizeof(t), \ |
| 67 | cast(size_t, n) * sizeof(t)))) | 70 | cast(size_t, n) * sizeof(t)))) |
| 68 | 71 | ||
| 69 | #define luaM_shrinkvector(L,v,size,fs,t) \ | 72 | #define luaM_shrinkvector(L,v,size,fs,t) \ |
| 70 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) | 73 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) |
| @@ -72,15 +75,17 @@ | |||
| 72 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); | 75 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); |
| 73 | 76 | ||
| 74 | /* not to be called directly */ | 77 | /* not to be called directly */ |
| 75 | LUAI_FUNC void *luaM_realloc (lua_State *L, void *block, size_t oldsize, | 78 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, |
| 76 | size_t size); | 79 | size_t size); |
| 80 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, | ||
| 81 | size_t size); | ||
| 77 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); | 82 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); |
| 78 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, | 83 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, |
| 79 | int *size, int size_elem, int limit, | 84 | int *size, int size_elem, int limit, |
| 80 | const char *what); | 85 | const char *what); |
| 81 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, | 86 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, |
| 82 | int final_n, int size_elem); | 87 | int final_n, int size_elem); |
| 83 | LUAI_FUNC void *luaM_malloc (lua_State *L, size_t size, int tag); | 88 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); |
| 84 | 89 | ||
| 85 | #endif | 90 | #endif |
| 86 | 91 | ||
