aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lauxlib.c6
-rw-r--r--ltests.c32
-rw-r--r--ltests.h4
-rw-r--r--onelua.c5
4 files changed, 34 insertions, 13 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 7f33f0ad..adb3851e 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1177,7 +1177,11 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
1177} 1177}
1178 1178
1179 1179
1180LUALIB_API lua_State *luaL_newstate (void) { 1180/*
1181** Use the name with parentheses so that headers can redefine it
1182** as a macro.
1183*/
1184LUALIB_API lua_State *(luaL_newstate) (void) {
1181 lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); 1185 lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed());
1182 if (l_likely(L)) { 1186 if (l_likely(L)) {
1183 lua_atpanic(L, &panic); 1187 lua_atpanic(L, &panic);
diff --git a/ltests.c b/ltests.c
index d92cd6c5..c4905f94 100644
--- a/ltests.c
+++ b/ltests.c
@@ -164,13 +164,13 @@ static void warnf (void *ud, const char *msg, int tocont) {
164 164
165#define MARK 0x55 /* 01010101 (a nice pattern) */ 165#define MARK 0x55 /* 01010101 (a nice pattern) */
166 166
167typedef union Header { 167typedef union memHeader {
168 LUAI_MAXALIGN; 168 LUAI_MAXALIGN;
169 struct { 169 struct {
170 size_t size; 170 size_t size;
171 int type; 171 int type;
172 } d; 172 } d;
173} Header; 173} memHeader;
174 174
175 175
176#if !defined(EXTERNMEMCHECK) 176#if !defined(EXTERNMEMCHECK)
@@ -193,14 +193,14 @@ Memcontrol l_memcontrol =
193 {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; 193 {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
194 194
195 195
196static void freeblock (Memcontrol *mc, Header *block) { 196static void freeblock (Memcontrol *mc, memHeader *block) {
197 if (block) { 197 if (block) {
198 size_t size = block->d.size; 198 size_t size = block->d.size;
199 int i; 199 int i;
200 for (i = 0; i < MARKSIZE; i++) /* check marks after block */ 200 for (i = 0; i < MARKSIZE; i++) /* check marks after block */
201 lua_assert(*(cast_charp(block + 1) + size + i) == MARK); 201 lua_assert(*(cast_charp(block + 1) + size + i) == MARK);
202 mc->objcount[block->d.type]--; 202 mc->objcount[block->d.type]--;
203 fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */ 203 fillmem(block, sizeof(memHeader) + size + MARKSIZE); /* erase block */
204 free(block); /* actually free block */ 204 free(block); /* actually free block */
205 mc->numblocks--; /* update counts */ 205 mc->numblocks--; /* update counts */
206 mc->total -= size; 206 mc->total -= size;
@@ -210,7 +210,7 @@ static void freeblock (Memcontrol *mc, Header *block) {
210 210
211void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { 211void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
212 Memcontrol *mc = cast(Memcontrol *, ud); 212 Memcontrol *mc = cast(Memcontrol *, ud);
213 Header *block = cast(Header *, b); 213 memHeader *block = cast(memHeader *, b);
214 int type; 214 int type;
215 if (mc->memlimit == 0) { /* first time? */ 215 if (mc->memlimit == 0) { /* first time? */
216 char *limit = getenv("MEMLIMIT"); /* initialize memory limit */ 216 char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
@@ -241,12 +241,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
241 if (size > oldsize && mc->total+size-oldsize > mc->memlimit) 241 if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
242 return NULL; /* fake a memory allocation error */ 242 return NULL; /* fake a memory allocation error */
243 else { 243 else {
244 Header *newblock; 244 memHeader *newblock;
245 int i; 245 int i;
246 size_t commonsize = (oldsize < size) ? oldsize : size; 246 size_t commonsize = (oldsize < size) ? oldsize : size;
247 size_t realsize = sizeof(Header) + size + MARKSIZE; 247 size_t realsize = sizeof(memHeader) + size + MARKSIZE;
248 if (realsize < size) return NULL; /* arithmetic overflow! */ 248 if (realsize < size) return NULL; /* arithmetic overflow! */
249 newblock = cast(Header *, malloc(realsize)); /* alloc a new block */ 249 newblock = cast(memHeader *, malloc(realsize)); /* alloc a new block */
250 if (newblock == NULL) 250 if (newblock == NULL)
251 return NULL; /* really out of memory? */ 251 return NULL; /* really out of memory? */
252 if (block) { 252 if (block) {
@@ -480,7 +480,7 @@ static int lua_checkpc (CallInfo *ci) {
480} 480}
481 481
482 482
483static void checkstack (global_State *g, lua_State *L1) { 483static void check_stack (global_State *g, lua_State *L1) {
484 StkId o; 484 StkId o;
485 CallInfo *ci; 485 CallInfo *ci;
486 UpVal *uv; 486 UpVal *uv;
@@ -517,7 +517,7 @@ static void checkrefs (global_State *g, GCObject *o) {
517 break; 517 break;
518 } 518 }
519 case LUA_VTHREAD: { 519 case LUA_VTHREAD: {
520 checkstack(g, gco2th(o)); 520 check_stack(g, gco2th(o));
521 break; 521 break;
522 } 522 }
523 case LUA_VLCL: { 523 case LUA_VLCL: {
@@ -908,6 +908,17 @@ static int get_limits (lua_State *L) {
908} 908}
909 909
910 910
911static int get_sizes (lua_State *L) {
912 lua_newtable(L);
913 setnameval(L, "Lua state", sizeof(lua_State));
914 setnameval(L, "global state", sizeof(global_State));
915 setnameval(L, "TValue", sizeof(TValue));
916 setnameval(L, "Node", sizeof(Node));
917 setnameval(L, "stack Value", sizeof(StackValue));
918 return 1;
919}
920
921
911static int mem_query (lua_State *L) { 922static int mem_query (lua_State *L) {
912 if (lua_isnone(L, 1)) { 923 if (lua_isnone(L, 1)) {
913 lua_pushinteger(L, cast_Integer(l_memcontrol.total)); 924 lua_pushinteger(L, cast_Integer(l_memcontrol.total));
@@ -2171,6 +2182,7 @@ static const struct luaL_Reg tests_funcs[] = {
2171 {"s2d", s2d}, 2182 {"s2d", s2d},
2172 {"sethook", sethook}, 2183 {"sethook", sethook},
2173 {"stacklevel", stacklevel}, 2184 {"stacklevel", stacklevel},
2185 {"sizes", get_sizes},
2174 {"testC", testC}, 2186 {"testC", testC},
2175 {"makeCfunc", makeCfunc}, 2187 {"makeCfunc", makeCfunc},
2176 {"totalmem", mem_query}, 2188 {"totalmem", mem_query},
diff --git a/ltests.h b/ltests.h
index d34e9d42..c825bdcf 100644
--- a/ltests.h
+++ b/ltests.h
@@ -122,14 +122,14 @@ LUA_API int luaB_opentests (lua_State *L);
122LUA_API void *debug_realloc (void *ud, void *block, 122LUA_API void *debug_realloc (void *ud, void *block,
123 size_t osize, size_t nsize); 123 size_t osize, size_t nsize);
124 124
125#if defined(lua_c) 125
126#define luaL_newstate() \ 126#define luaL_newstate() \
127 lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL)) 127 lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL))
128#define luai_openlibs(L) \ 128#define luai_openlibs(L) \
129 { luaL_openlibs(L); \ 129 { luaL_openlibs(L); \
130 luaL_requiref(L, "T", luaB_opentests, 1); \ 130 luaL_requiref(L, "T", luaB_opentests, 1); \
131 lua_pop(L, 1); } 131 lua_pop(L, 1); }
132#endif 132
133 133
134 134
135 135
diff --git a/onelua.c b/onelua.c
index 2a434961..cc639494 100644
--- a/onelua.c
+++ b/onelua.c
@@ -110,6 +110,11 @@
110#include "linit.c" 110#include "linit.c"
111#endif 111#endif
112 112
113/* test library -- used only for internal development */
114#if defined(LUA_DEBUG)
115#include "ltests.c"
116#endif
117
113/* lua */ 118/* lua */
114#ifdef MAKE_LUA 119#ifdef MAKE_LUA
115#include "lua.c" 120#include "lua.c"