diff options
Diffstat (limited to 'lmem.c')
-rw-r--r-- | lmem.c | 24 |
1 files changed, 13 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmem.c,v 1.18 1999/08/16 20:52:00 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.19 1999/10/19 13:33:22 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 | */ |
@@ -7,6 +7,8 @@ | |||
7 | 7 | ||
8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
9 | 9 | ||
10 | #define LUA_REENTRANT | ||
11 | |||
10 | #include "lmem.h" | 12 | #include "lmem.h" |
11 | #include "lstate.h" | 13 | #include "lstate.h" |
12 | #include "lua.h" | 14 | #include "lua.h" |
@@ -34,15 +36,15 @@ static unsigned long power2 (unsigned long n) { | |||
34 | } | 36 | } |
35 | 37 | ||
36 | 38 | ||
37 | void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, | 39 | void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size, |
38 | const char *errormsg, unsigned long limit) { | 40 | const char *errormsg, unsigned long limit) { |
39 | unsigned long newn = nelems+inc; | 41 | unsigned long newn = nelems+inc; |
40 | if (newn >= limit) lua_error(errormsg); | 42 | if (newn >= limit) lua_error(L, errormsg); |
41 | if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */ | 43 | if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */ |
42 | (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */ | 44 | (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */ |
43 | return block; /* do not need to reallocate */ | 45 | return block; /* do not need to reallocate */ |
44 | else /* it crossed a power of 2 boundary; grow to next power */ | 46 | else /* it crossed a power of 2 boundary; grow to next power */ |
45 | return luaM_realloc(block, power2(newn)*size); | 47 | return luaM_realloc(L, block, power2(newn)*size); |
46 | } | 48 | } |
47 | 49 | ||
48 | 50 | ||
@@ -51,17 +53,17 @@ void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, | |||
51 | /* | 53 | /* |
52 | ** generic allocation routine. | 54 | ** generic allocation routine. |
53 | */ | 55 | */ |
54 | void *luaM_realloc (void *block, unsigned long size) { | 56 | void *luaM_realloc (lua_State *L, void *block, unsigned long size) { |
55 | size_t s = (size_t)size; | 57 | size_t s = (size_t)size; |
56 | if (s != size) | 58 | if (s != size) |
57 | lua_error("memory allocation error: block too big"); | 59 | lua_error(L, "memory allocation error: block too big"); |
58 | if (size == 0) { | 60 | if (size == 0) { |
59 | free(block); /* block may be NULL, that is OK for free */ | 61 | free(block); /* block may be NULL, that is OK for free */ |
60 | return NULL; | 62 | return NULL; |
61 | } | 63 | } |
62 | block = realloc(block, s); | 64 | block = realloc(block, s); |
63 | if (block == NULL) | 65 | if (block == NULL) |
64 | lua_error(memEM); | 66 | lua_error(L, memEM); |
65 | return block; | 67 | return block; |
66 | } | 68 | } |
67 | 69 | ||
@@ -90,7 +92,7 @@ static void *checkblock (void *block) { | |||
90 | unsigned long size = *b; | 92 | unsigned long size = *b; |
91 | int i; | 93 | int i; |
92 | for (i=0;i<MARKSIZE;i++) | 94 | for (i=0;i<MARKSIZE;i++) |
93 | LUA_ASSERT(*(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block"); | 95 | LUA_ASSERT(L, *(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block"); |
94 | numblocks--; | 96 | numblocks--; |
95 | totalmem -= size; | 97 | totalmem -= size; |
96 | return b; | 98 | return b; |
@@ -106,10 +108,10 @@ static void freeblock (void *block) { | |||
106 | } | 108 | } |
107 | 109 | ||
108 | 110 | ||
109 | void *luaM_realloc (void *block, unsigned long size) { | 111 | void *luaM_realloc (lua_State *L, void *block, unsigned long size) { |
110 | unsigned long realsize = HEADER+size+MARKSIZE; | 112 | unsigned long realsize = HEADER+size+MARKSIZE; |
111 | if (realsize != (size_t)realsize) | 113 | if (realsize != (size_t)realsize) |
112 | lua_error("memory allocation error: block too big"); | 114 | lua_error(L, "memory allocation error: block too big"); |
113 | if (size == 0) { | 115 | if (size == 0) { |
114 | freeblock(block); | 116 | freeblock(block); |
115 | return NULL; | 117 | return NULL; |
@@ -124,7 +126,7 @@ void *luaM_realloc (void *block, unsigned long size) { | |||
124 | freeblock(block); /* erase (and check) old copy */ | 126 | freeblock(block); /* erase (and check) old copy */ |
125 | } | 127 | } |
126 | if (newblock == NULL) | 128 | if (newblock == NULL) |
127 | lua_error(memEM); | 129 | lua_error(L, memEM); |
128 | totalmem += size; | 130 | totalmem += size; |
129 | numblocks++; | 131 | numblocks++; |
130 | *(unsigned long *)newblock = size; | 132 | *(unsigned long *)newblock = size; |