diff options
Diffstat (limited to 'lmem.h')
-rw-r--r-- | lmem.h | 24 |
1 files changed, 15 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmem.h,v 1.39 2012/11/14 17:21:34 roberto Exp roberto $ | 2 | ** $Id: lmem.h,v 1.40 2013/02/20 14:08:21 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 | */ |
@@ -15,20 +15,26 @@ | |||
15 | 15 | ||
16 | 16 | ||
17 | /* | 17 | /* |
18 | ** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is | 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where |
19 | ** always constant. | 19 | ** each element has size 'e'. In case of arithmetic overflow of the |
20 | ** The macro is somewhat complex to avoid warnings: | 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because |
21 | ** +1 avoids warnings of "comparison has constant result"; | 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). |
22 | ** cast to 'void' avoids warnings of "value unused". | 22 | ** |
23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' | ||
24 | ** comparison avoids a runtime comparison when overflow cannot occur. | ||
25 | ** The compiler should be able to optimize the real test by itself, but | ||
26 | ** when it does it, it may give a warning about "comparison is always | ||
27 | ** false due to limited range of data type"; the +1 tricks the compiler, | ||
28 | ** avoiding this warning but also this optimization.) | ||
23 | */ | 29 | */ |
24 | #define luaM_reallocv(L,b,on,n,e) \ | 30 | #define luaM_reallocv(L,b,on,n,e) \ |
25 | (cast(void, \ | 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ |
26 | (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ | 32 | ? luaM_toobig(L) : cast_void(0)) , \ |
27 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) | 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) |
28 | 34 | ||
29 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) | 35 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) |
30 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) | 36 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) |
31 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) | 37 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) |
32 | 38 | ||
33 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) | 39 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) |
34 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) | 40 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) |