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 /ltests.c | |
| 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 'ltests.c')
| -rw-r--r-- | ltests.c | 86 |
1 files changed, 85 insertions, 1 deletions
| @@ -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 | ** ======================================================= |
