diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-06 14:01:29 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-06 14:01:29 -0200 |
| commit | 1f917e709ca3fe41cf07fd4bf99a080883521394 (patch) | |
| tree | 00e89f7218a38e028bebe111dbf682925fe4cca0 | |
| parent | d444153dbea006f5e0547f27a11256af96ba1829 (diff) | |
| download | lua-1f917e709ca3fe41cf07fd4bf99a080883521394.tar.gz lua-1f917e709ca3fe41cf07fd4bf99a080883521394.tar.bz2 lua-1f917e709ca3fe41cf07fd4bf99a080883521394.zip | |
better use of extra include files (both for tests and for old_ansi)
Diffstat (limited to '')
| -rw-r--r-- | liolib.c | 34 | ||||
| -rw-r--r-- | lmem.c | 115 | ||||
| -rw-r--r-- | ltests.c | 86 | ||||
| -rw-r--r-- | ltests.h | 12 | ||||
| -rw-r--r-- | lvm.c | 7 |
5 files changed, 118 insertions, 136 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.102 2001/01/26 12:12:16 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.103 2001/02/02 19:02:40 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -21,19 +21,13 @@ | |||
| 21 | #ifndef OLD_ANSI | 21 | #ifndef OLD_ANSI |
| 22 | #include <errno.h> | 22 | #include <errno.h> |
| 23 | #include <locale.h> | 23 | #include <locale.h> |
| 24 | #define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s)) | 24 | #endif |
| 25 | #define free(b) if (b) (free)(b) | 25 | |
| 26 | #else | 26 | |
| 27 | /* no support for locale and for strerror: fake them */ | 27 | #ifndef l_realloc |
| 28 | #define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL) | 28 | #define l_malloc(s) malloc(s) |
| 29 | #define LC_ALL 0 | 29 | #define l_realloc(b,os,s) realloc(b, s) |
| 30 | #define LC_COLLATE 0 | 30 | #define l_free(b, os) free(b) |
| 31 | #define LC_CTYPE 0 | ||
| 32 | #define LC_MONETARY 0 | ||
| 33 | #define LC_NUMERIC 0 | ||
| 34 | #define LC_TIME 0 | ||
| 35 | #define strerror(e) "I/O error" | ||
| 36 | #define errno (-1) | ||
| 37 | #endif | 31 | #endif |
| 38 | 32 | ||
| 39 | 33 | ||
| @@ -258,20 +252,22 @@ static int read_line (lua_State *L, FILE *f) { | |||
| 258 | static void read_file (lua_State *L, FILE *f) { | 252 | static void read_file (lua_State *L, FILE *f) { |
| 259 | size_t len = 0; | 253 | size_t len = 0; |
| 260 | size_t size = LUAL_BUFFERSIZE; | 254 | size_t size = LUAL_BUFFERSIZE; |
| 255 | size_t oldsize = 0; | ||
| 261 | char *buffer = NULL; | 256 | char *buffer = NULL; |
| 262 | for (;;) { | 257 | for (;;) { |
| 263 | char *newbuffer = (char *)realloc(buffer, size); | 258 | char *newbuffer = (char *)l_realloc(buffer, oldsize, size); |
| 264 | if (newbuffer == NULL) { | 259 | if (newbuffer == NULL) { |
| 265 | free(buffer); | 260 | l_free(buffer, oldsize); |
| 266 | lua_error(L, "not enough memory to read a file"); | 261 | lua_error(L, "not enough memory to read a file"); |
| 267 | } | 262 | } |
| 268 | buffer = newbuffer; | 263 | buffer = newbuffer; |
| 269 | len += fread(buffer+len, sizeof(char), size-len, f); | 264 | len += fread(buffer+len, sizeof(char), size-len, f); |
| 270 | if (len < size) break; /* did not read all it could */ | 265 | if (len < size) break; /* did not read all it could */ |
| 266 | oldsize = size; | ||
| 271 | size *= 2; | 267 | size *= 2; |
| 272 | } | 268 | } |
| 273 | lua_pushlstring(L, buffer, len); | 269 | lua_pushlstring(L, buffer, len); |
| 274 | free(buffer); | 270 | l_free(buffer, size); |
| 275 | } | 271 | } |
| 276 | 272 | ||
| 277 | 273 | ||
| @@ -282,13 +278,13 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { | |||
| 282 | if (n <= LUAL_BUFFERSIZE) | 278 | if (n <= LUAL_BUFFERSIZE) |
| 283 | buffer = statbuff; | 279 | buffer = statbuff; |
| 284 | else { | 280 | else { |
| 285 | buffer = (char *)malloc(n); | 281 | buffer = (char *)l_malloc(n); |
| 286 | if (buffer == NULL) | 282 | if (buffer == NULL) |
| 287 | lua_error(L, "not enough memory to read a file"); | 283 | lua_error(L, "not enough memory to read a file"); |
| 288 | } | 284 | } |
| 289 | n1 = fread(buffer, sizeof(char), n, f); | 285 | n1 = fread(buffer, sizeof(char), n, f); |
| 290 | lua_pushlstring(L, buffer, n1); | 286 | lua_pushlstring(L, buffer, n1); |
| 291 | if (buffer != statbuff) free(buffer); | 287 | if (buffer != statbuff) l_free(buffer, n); |
| 292 | return (n1 > 0 || n == 0); | 288 | return (n1 > 0 || n == 0); |
| 293 | } | 289 | } |
| 294 | 290 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.c,v 1.44 2001/01/24 15:45:33 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.45 2001/02/05 19:08:01 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 | */ |
| @@ -16,115 +16,12 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | 19 | #ifndef l_realloc | |
| 20 | #ifdef LUA_DEBUG | 20 | #define l_realloc(b,os,s) realloc(b,s) |
| 21 | /* | 21 | #define l_free(b,s) free(b) |
| 22 | ** {====================================================================== | ||
| 23 | ** Controlled version for realloc. | ||
| 24 | ** ======================================================================= | ||
| 25 | */ | ||
| 26 | |||
| 27 | |||
| 28 | #include <assert.h> | ||
| 29 | #include <limits.h> | ||
| 30 | #include <string.h> | ||
| 31 | |||
| 32 | #define basicrealloc(b, os, s) debug_realloc(b, os, s) | ||
| 33 | #define basicfree(b, s) debug_realloc(b, s, 0) | ||
| 34 | |||
| 35 | |||
| 36 | /* ensures maximum alignment for HEADER */ | ||
| 37 | #define HEADER (sizeof(union L_Umaxalign)) | ||
| 38 | |||
| 39 | #define MARKSIZE 32 | ||
| 40 | #define MARK 0x55 /* 01010101 (a nice pattern) */ | ||
| 41 | |||
| 42 | |||
| 43 | #define blocksize(b) ((size_t *)((char *)(b) - HEADER)) | ||
| 44 | |||
| 45 | unsigned long memdebug_numblocks = 0; | ||
| 46 | unsigned long memdebug_total = 0; | ||
| 47 | unsigned long memdebug_maxmem = 0; | ||
| 48 | unsigned long memdebug_memlimit = ULONG_MAX; | ||
| 49 | |||
| 50 | |||
| 51 | static void *checkblock (void *block) { | ||
| 52 | size_t *b = blocksize(block); | ||
| 53 | size_t size = *b; | ||
| 54 | int i; | ||
| 55 | for (i=0;i<MARKSIZE;i++) | ||
| 56 | assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */ | ||
| 57 | return b; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | static void freeblock (void *block) { | ||
| 62 | if (block) { | ||
| 63 | size_t size = *blocksize(block); | ||
| 64 | block = checkblock(block); | ||
| 65 | memset(block, -1, size+HEADER+MARKSIZE); /* erase block */ | ||
| 66 | free(block); /* free original block */ | ||
| 67 | memdebug_numblocks--; | ||
| 68 | memdebug_total -= size; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | static void *debug_realloc (void *block, size_t oldsize, size_t size) { | ||
| 74 | assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block)); | ||
| 75 | if (size == 0) { | ||
| 76 | freeblock(block); | ||
| 77 | return NULL; | ||
| 78 | } | ||
| 79 | else if (memdebug_total+size > memdebug_memlimit) | ||
| 80 | return NULL; /* to test memory allocation errors */ | ||
| 81 | else { | ||
| 82 | char *newblock; | ||
| 83 | int i; | ||
| 84 | size_t realsize = HEADER+size+MARKSIZE; | ||
| 85 | if (realsize < size) return NULL; /* overflow! */ | ||
| 86 | newblock = (char *)malloc(realsize); /* alloc a new block */ | ||
| 87 | if (newblock == NULL) return NULL; | ||
| 88 | if (oldsize > size) oldsize = size; | ||
| 89 | if (block) { | ||
| 90 | memcpy(newblock+HEADER, block, oldsize); | ||
| 91 | freeblock(block); /* erase (and check) old copy */ | ||
| 92 | } | ||
| 93 | /* initialize new part of the block with something `weird' */ | ||
| 94 | memset(newblock+HEADER+oldsize, -MARK, size-oldsize); | ||
| 95 | memdebug_total += size; | ||
| 96 | if (memdebug_total > memdebug_maxmem) | ||
| 97 | memdebug_maxmem = memdebug_total; | ||
| 98 | memdebug_numblocks++; | ||
| 99 | *(size_t *)newblock = size; | ||
| 100 | for (i=0;i<MARKSIZE;i++) | ||
| 101 | *(newblock+HEADER+size+i) = (char)(MARK+i); | ||
| 102 | return newblock+HEADER; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | /* }====================================================================== */ | ||
| 108 | |||
| 109 | #else | ||
| 110 | /* no debug */ | ||
| 111 | |||
| 112 | /* | ||
| 113 | ** Real ISO (ANSI) systems do not need these tests; | ||
| 114 | ** but some systems (Sun OS) are not that ISO... | ||
| 115 | */ | ||
| 116 | #ifdef OLD_ANSI | ||
| 117 | #define basicrealloc(b,os,s) ((b) == NULL ? malloc(s) : realloc(b, s)) | ||
| 118 | #define basicfree(b,s) if (b) free(b) | ||
| 119 | #else | ||
| 120 | #define basicrealloc(b,os,s) realloc(b,s) | ||
| 121 | #define basicfree(b,s) free(b) | ||
| 122 | |||
| 123 | #endif | 22 | #endif |
| 124 | 23 | ||
| 125 | 24 | ||
| 126 | #endif | ||
| 127 | |||
| 128 | void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, | 25 | void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, |
| 129 | int limit, const char *errormsg) { | 26 | int limit, const char *errormsg) { |
| 130 | void *newblock; | 27 | void *newblock; |
| @@ -148,13 +45,13 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, | |||
| 148 | */ | 45 | */ |
| 149 | void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) { | 46 | void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) { |
| 150 | if (size == 0) { | 47 | if (size == 0) { |
| 151 | basicfree(block, oldsize); /* block may be NULL; that is OK for free */ | 48 | l_free(block, oldsize); /* block may be NULL; that is OK for free */ |
| 152 | block = NULL; | 49 | block = NULL; |
| 153 | } | 50 | } |
| 154 | else if (size >= MAX_SIZET) | 51 | else if (size >= MAX_SIZET) |
| 155 | luaD_error(L, "memory allocation error: block too big"); | 52 | luaD_error(L, "memory allocation error: block too big"); |
| 156 | else { | 53 | else { |
| 157 | block = basicrealloc(block, oldsize, size); | 54 | block = l_realloc(block, oldsize, size); |
| 158 | if (block == NULL) { | 55 | if (block == NULL) { |
| 159 | if (L) | 56 | if (L) |
| 160 | luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */ | 57 | luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */ |
| @@ -1,11 +1,12 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 1.61 2001/02/01 16:03:38 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 1.62 2001/02/02 15:13:05 roberto Exp roberto $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | #include <ctype.h> | 8 | #include <ctype.h> |
| 9 | #include <limits.h> | ||
| 9 | #include <stdio.h> | 10 | #include <stdio.h> |
| 10 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| 11 | #include <string.h> | 12 | #include <string.h> |
| @@ -34,6 +35,7 @@ | |||
| 34 | */ | 35 | */ |
| 35 | #ifdef LUA_DEBUG | 36 | #ifdef LUA_DEBUG |
| 36 | 37 | ||
| 38 | |||
| 37 | lua_State *lua_state = NULL; | 39 | lua_State *lua_state = NULL; |
| 38 | 40 | ||
| 39 | int islocked = 0; | 41 | int islocked = 0; |
| @@ -48,6 +50,88 @@ static void setnameval (lua_State *L, const char *name, int val) { | |||
| 48 | 50 | ||
| 49 | 51 | ||
| 50 | /* | 52 | /* |
| 53 | ** {====================================================================== | ||
| 54 | ** Controlled version for realloc. | ||
| 55 | ** ======================================================================= | ||
| 56 | */ | ||
| 57 | |||
| 58 | |||
| 59 | /* ensures maximum alignment for HEADER */ | ||
| 60 | #define HEADER (sizeof(union L_Umaxalign)) | ||
| 61 | |||
| 62 | #define MARKSIZE 32 | ||
| 63 | #define MARK 0x55 /* 01010101 (a nice pattern) */ | ||
| 64 | |||
| 65 | |||
| 66 | #define blocksize(b) ((size_t *)((char *)(b) - HEADER)) | ||
| 67 | |||
| 68 | unsigned long memdebug_numblocks = 0; | ||
| 69 | unsigned long memdebug_total = 0; | ||
| 70 | unsigned long memdebug_maxmem = 0; | ||
| 71 | unsigned long memdebug_memlimit = ULONG_MAX; | ||
| 72 | |||
| 73 | |||
| 74 | static void *checkblock (void *block) { | ||
| 75 | size_t *b = blocksize(block); | ||
| 76 | size_t size = *b; | ||
| 77 | int i; | ||
| 78 | for (i=0;i<MARKSIZE;i++) | ||
| 79 | lua_assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */ | ||
| 80 | return b; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | static void freeblock (void *block) { | ||
| 85 | if (block) { | ||
| 86 | size_t size = *blocksize(block); | ||
| 87 | block = checkblock(block); | ||
| 88 | memset(block, -1, size+HEADER+MARKSIZE); /* erase block */ | ||
| 89 | free(block); /* free original block */ | ||
| 90 | memdebug_numblocks--; | ||
| 91 | memdebug_total -= size; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | void *debug_realloc (void *block, size_t oldsize, size_t size) { | ||
| 97 | lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block)); | ||
| 98 | if (size == 0) { | ||
| 99 | freeblock(block); | ||
| 100 | return NULL; | ||
| 101 | } | ||
| 102 | else if (memdebug_total+size > memdebug_memlimit) | ||
| 103 | return NULL; /* to test memory allocation errors */ | ||
| 104 | else { | ||
| 105 | char *newblock; | ||
| 106 | int i; | ||
| 107 | size_t realsize = HEADER+size+MARKSIZE; | ||
| 108 | if (realsize < size) return NULL; /* overflow! */ | ||
| 109 | newblock = (char *)malloc(realsize); /* alloc a new block */ | ||
| 110 | if (newblock == NULL) return NULL; | ||
| 111 | if (oldsize > size) oldsize = size; | ||
| 112 | if (block) { | ||
| 113 | memcpy(newblock+HEADER, block, oldsize); | ||
| 114 | freeblock(block); /* erase (and check) old copy */ | ||
| 115 | } | ||
| 116 | /* initialize new part of the block with something `weird' */ | ||
| 117 | memset(newblock+HEADER+oldsize, -MARK, size-oldsize); | ||
| 118 | memdebug_total += size; | ||
| 119 | if (memdebug_total > memdebug_maxmem) | ||
| 120 | memdebug_maxmem = memdebug_total; | ||
| 121 | memdebug_numblocks++; | ||
| 122 | *(size_t *)newblock = size; | ||
| 123 | for (i=0;i<MARKSIZE;i++) | ||
| 124 | *(newblock+HEADER+size+i) = (char)(MARK+i); | ||
| 125 | return newblock+HEADER; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | /* }====================================================================== */ | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | /* | ||
| 51 | ** {====================================================== | 135 | ** {====================================================== |
| 52 | ** Disassembler | 136 | ** Disassembler |
| 53 | ** ======================================================= | 137 | ** ======================================================= |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.h,v 1.1 2001/02/02 15:12:25 roberto Exp roberto $ | 2 | ** $Id: ltests.h,v 1.2 2001/02/05 19:08:01 roberto Exp roberto $ |
| 3 | ** Internal Header for Debugging of the Lua Implementation | 3 | ** Internal Header for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -8,6 +8,8 @@ | |||
| 8 | #define ltests_h | 8 | #define ltests_h |
| 9 | 9 | ||
| 10 | 10 | ||
| 11 | #include <stdlib.h> | ||
| 12 | |||
| 11 | 13 | ||
| 12 | #define LUA_DEBUG | 14 | #define LUA_DEBUG |
| 13 | 15 | ||
| @@ -27,6 +29,14 @@ extern unsigned long memdebug_maxmem; | |||
| 27 | extern unsigned long memdebug_memlimit; | 29 | extern unsigned long memdebug_memlimit; |
| 28 | 30 | ||
| 29 | 31 | ||
| 32 | #define l_malloc(s) debug_realloc(NULL, 0, s) | ||
| 33 | #define l_realloc(b, os, s) debug_realloc(b, os, s) | ||
| 34 | #define l_free(b, s) debug_realloc(b, s, 0) | ||
| 35 | |||
| 36 | void *debug_realloc (void *block, size_t oldsize, size_t size); | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 30 | /* test for lock/unlock */ | 40 | /* test for lock/unlock */ |
| 31 | #define LUA_USERSTATE int *lock; | 41 | #define LUA_USERSTATE int *lock; |
| 32 | extern int islocked; | 42 | extern int islocked; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.163 2001/02/01 17:39:55 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.164 2001/02/02 15:13:05 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -25,11 +25,6 @@ | |||
| 25 | #include "lvm.h" | 25 | #include "lvm.h" |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | #ifdef OLD_ANSI | ||
| 29 | #define strcoll(a,b) strcmp(a,b) | ||
| 30 | #endif | ||
| 31 | |||
| 32 | |||
| 33 | 28 | ||
| 34 | /* | 29 | /* |
| 35 | ** Extra stack size to run a function: | 30 | ** Extra stack size to run a function: |
