diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-25 12:16:26 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-25 12:16:26 -0300 |
| commit | 26d1e21c89a481c2368ba934da8e192a164d8f99 (patch) | |
| tree | ea2eaaade79264b71d0e18fef75bca70fc1f4766 /lmem.c | |
| parent | 24a2c08145ecc9b5c528a46ba83bdd7b95dbdc02 (diff) | |
| download | lua-26d1e21c89a481c2368ba934da8e192a164d8f99.tar.gz lua-26d1e21c89a481c2368ba934da8e192a164d8f99.tar.bz2 lua-26d1e21c89a481c2368ba934da8e192a164d8f99.zip | |
new way to handle "growing" vectors
Diffstat (limited to 'lmem.c')
| -rw-r--r-- | lmem.c | 47 |
1 files changed, 25 insertions, 22 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.c,v 1.9 1999/01/22 18:08:57 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.10 1999/02/24 17:55:51 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 | */ |
| @@ -23,21 +23,36 @@ | |||
| 23 | #endif | 23 | #endif |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | #define MINSIZE 16 /* minimum size for "growing" vectors */ | ||
| 26 | 27 | ||
| 27 | #ifndef DEBUG | ||
| 28 | 28 | ||
| 29 | int luaM_growaux (void **block, unsigned long nelems, int size, | 29 | static unsigned long power2 (unsigned long n) { |
| 30 | unsigned long p = MINSIZE; | ||
| 31 | while (p<=n) p<<=1; | ||
| 32 | return p; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, | ||
| 30 | char *errormsg, unsigned long limit) { | 37 | char *errormsg, unsigned long limit) { |
| 31 | if (nelems >= limit) | 38 | unsigned long newn = nelems+inc; |
| 32 | lua_error(errormsg); | 39 | if ((newn ^ nelems) > nelems) { /* cross a power of 2 boundary? */ |
| 33 | nelems = (nelems == 0) ? 32 : nelems*2; | 40 | if (newn >= limit) |
| 34 | if (nelems > limit) | 41 | lua_error(errormsg); |
| 35 | nelems = limit; | 42 | newn = power2(newn); |
| 36 | *block = luaM_realloc(*block, nelems*size); | 43 | if (newn > limit) |
| 37 | return (int)nelems; | 44 | newn = limit; |
| 45 | return luaM_realloc(block, newn*size); | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | LUA_ASSERT(power2(nelems) == power2(newn), "bad arithmetic"); | ||
| 49 | return block; | ||
| 50 | } | ||
| 38 | } | 51 | } |
| 39 | 52 | ||
| 40 | 53 | ||
| 54 | #ifndef DEBUG | ||
| 55 | |||
| 41 | /* | 56 | /* |
| 42 | ** generic allocation routine. | 57 | ** generic allocation routine. |
| 43 | */ | 58 | */ |
| @@ -63,18 +78,6 @@ void *luaM_realloc (void *block, unsigned long size) { | |||
| 63 | #include <string.h> | 78 | #include <string.h> |
| 64 | 79 | ||
| 65 | 80 | ||
| 66 | int luaM_growaux (void **block, unsigned long nelems, int size, | ||
| 67 | char *errormsg, unsigned long limit) { | ||
| 68 | if (nelems >= limit) | ||
| 69 | lua_error(errormsg); | ||
| 70 | nelems = nelems+1; | ||
| 71 | if (nelems > limit) | ||
| 72 | nelems = limit; | ||
| 73 | *block = luaM_realloc(*block, nelems*size); | ||
| 74 | return (int)nelems; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | #define HEADER (sizeof(double)) | 81 | #define HEADER (sizeof(double)) |
| 79 | 82 | ||
| 80 | #define MARK 55 | 83 | #define MARK 55 |
