diff options
Diffstat (limited to 'lmem.c')
| -rw-r--r-- | lmem.c | 65 |
1 files changed, 27 insertions, 38 deletions
| @@ -1,11 +1,11 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.c,v 1.60 2002/11/21 14:14:42 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.61 2002/12/04 17:38:31 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 | */ |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | #include <stdlib.h> | 8 | #include <stddef.h> |
| 9 | 9 | ||
| 10 | #define lmem_c | 10 | #define lmem_c |
| 11 | 11 | ||
| @@ -20,21 +20,23 @@ | |||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | /* | 22 | /* |
| 23 | ** definition for realloc function. It must assure that l_realloc(NULL, | 23 | ** About the realloc function: |
| 24 | ** 0, x) allocates a new block (ANSI C assures that). (`os' is the old | 24 | ** void * realloc (void *ud, void *ptr, size_t osize, size_t nsize); |
| 25 | ** block size; some allocators may use that.) | 25 | ** (`osize' is the old size, `nsize' is the new size) |
| 26 | ** | ||
| 27 | ** Lua ensures that (ptr == NULL) iff (osize == 0). | ||
| 28 | ** | ||
| 29 | ** * realloc(ud, NULL, 0, x) creates a new block of size `x' | ||
| 30 | ** | ||
| 31 | ** * realloc(ud, p, x, 0) frees the block `p' | ||
| 32 | ** (in this specific case, realloc must return NULL). | ||
| 33 | ** particularly, realloc(ud, NULL, 0, 0) does nothing | ||
| 34 | ** (which is equivalent to free(NULL) in ANSI C) | ||
| 35 | ** | ||
| 36 | ** realloc returns NULL if it cannot create or reallocate the area | ||
| 37 | ** (any reallocation to an equal or smaller size cannot fail!) | ||
| 26 | */ | 38 | */ |
| 27 | #ifndef l_realloc | ||
| 28 | #define l_realloc(b,os,s) realloc(b,s) | ||
| 29 | #endif | ||
| 30 | 39 | ||
| 31 | /* | ||
| 32 | ** definition for free function. (`os' is the old block size; some | ||
| 33 | ** allocators may use that.) | ||
| 34 | */ | ||
| 35 | #ifndef l_free | ||
| 36 | #define l_free(b,os) free(b) | ||
| 37 | #endif | ||
| 38 | 40 | ||
| 39 | 41 | ||
| 40 | #define MINSIZEARRAY 4 | 42 | #define MINSIZEARRAY 4 |
| @@ -62,30 +64,17 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, | |||
| 62 | /* | 64 | /* |
| 63 | ** generic allocation routine. | 65 | ** generic allocation routine. |
| 64 | */ | 66 | */ |
| 65 | void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) { | 67 | void *luaM_realloc (lua_State *L, void *block, lu_mem osize, lu_mem nsize) { |
| 66 | lua_assert((oldsize == 0) == (block == NULL)); | 68 | global_State *g = G(L); |
| 67 | if (size == 0) { | 69 | lua_assert((osize == 0) == (block == NULL)); |
| 68 | if (block != NULL) { | 70 | if (nsize >= MAX_SIZET) |
| 69 | l_free(block, oldsize); | ||
| 70 | block = NULL; | ||
| 71 | } | ||
| 72 | else return NULL; /* avoid `nblocks' computations when oldsize==size==0 */ | ||
| 73 | } | ||
| 74 | else if (size >= MAX_SIZET) | ||
| 75 | luaG_runerror(L, "memory allocation error: block too big"); | 71 | luaG_runerror(L, "memory allocation error: block too big"); |
| 76 | else { | 72 | block = (*g->realloc)(g->ud, block, osize, nsize); |
| 77 | block = l_realloc(block, oldsize, size); | 73 | if (block == NULL && nsize > 0) |
| 78 | if (block == NULL) { | 74 | luaD_throw(L, LUA_ERRMEM); |
| 79 | if (L) | 75 | lua_assert((nsize == 0) == (block == NULL)); |
| 80 | luaD_throw(L, LUA_ERRMEM); | 76 | g->nblocks -= osize; |
| 81 | else return NULL; /* error before creating state! */ | 77 | g->nblocks += nsize; |
| 82 | } | ||
| 83 | } | ||
| 84 | if (L) { | ||
| 85 | lua_assert(G(L) != NULL && G(L)->nblocks > 0); | ||
| 86 | G(L)->nblocks -= oldsize; | ||
| 87 | G(L)->nblocks += size; | ||
| 88 | } | ||
| 89 | return block; | 78 | return block; |
| 90 | } | 79 | } |
| 91 | 80 | ||
