diff options
| -rw-r--r-- | lauxlib.c | 6 | ||||
| -rw-r--r-- | ltests.c | 32 | ||||
| -rw-r--r-- | ltests.h | 4 | ||||
| -rw-r--r-- | onelua.c | 5 |
4 files changed, 34 insertions, 13 deletions
| @@ -1177,7 +1177,11 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) { | |||
| 1177 | } | 1177 | } |
| 1178 | 1178 | ||
| 1179 | 1179 | ||
| 1180 | LUALIB_API lua_State *luaL_newstate (void) { | 1180 | /* |
| 1181 | ** Use the name with parentheses so that headers can redefine it | ||
| 1182 | ** as a macro. | ||
| 1183 | */ | ||
| 1184 | LUALIB_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); |
| @@ -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 | ||
| 167 | typedef union Header { | 167 | typedef 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 | ||
| 196 | static void freeblock (Memcontrol *mc, Header *block) { | 196 | static 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 | ||
| 211 | void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { | 211 | void *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 | ||
| 483 | static void checkstack (global_State *g, lua_State *L1) { | 483 | static 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 | ||
| 911 | static 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 | |||
| 911 | static int mem_query (lua_State *L) { | 922 | static 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}, |
| @@ -122,14 +122,14 @@ LUA_API int luaB_opentests (lua_State *L); | |||
| 122 | LUA_API void *debug_realloc (void *ud, void *block, | 122 | LUA_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 | ||
| @@ -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" |
