diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-02-12 15:16:11 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-02-12 15:16:11 -0300 |
commit | c8121ce34b39c6fd31899f4da91e26063c8af54f (patch) | |
tree | 5ce96fbe65d5f16f87c816f88bab2df25975ba5d /lundump.c | |
parent | 7360f8d0fd91344deb583ff76b8250a1883dcd4c (diff) | |
download | lua-c8121ce34b39c6fd31899f4da91e26063c8af54f.tar.gz lua-c8121ce34b39c6fd31899f4da91e26063c8af54f.tar.bz2 lua-c8121ce34b39c6fd31899f4da91e26063c8af54f.zip |
Revising code for Varint encoding in dumps
- Usign lua_Unsigned to count strings.
- Varint uses a type large enough both for size_t and lua_Unsigned.
- Most-Significant Bit 0 means last byte, to conform to common usage.
- (unrelated) Change in macro 'getaddr' so that multiplication is
by constants.
Diffstat (limited to 'lundump.c')
-rw-r--r-- | lundump.c | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -37,7 +37,7 @@ typedef struct { | |||
37 | const char *name; | 37 | const char *name; |
38 | Table *h; /* list for string reuse */ | 38 | Table *h; /* list for string reuse */ |
39 | size_t offset; /* current position relative to beginning of dump */ | 39 | size_t offset; /* current position relative to beginning of dump */ |
40 | lua_Integer nstr; /* number of strings in the list */ | 40 | lua_Unsigned nstr; /* number of strings in the list */ |
41 | lu_byte fixed; /* dump is fixed in memory */ | 41 | lu_byte fixed; /* dump is fixed in memory */ |
42 | } LoadState; | 42 | } LoadState; |
43 | 43 | ||
@@ -71,10 +71,9 @@ static void loadAlign (LoadState *S, int align) { | |||
71 | } | 71 | } |
72 | 72 | ||
73 | 73 | ||
74 | #define getaddr(S,n,t) cast(t *, getaddr_(S,n,sizeof(t))) | 74 | #define getaddr(S,n,t) cast(t *, getaddr_(S,(n) * sizeof(t))) |
75 | 75 | ||
76 | static const void *getaddr_ (LoadState *S, int n, size_t sz) { | 76 | static const void *getaddr_ (LoadState *S, size_t size) { |
77 | size_t size = n * sz; | ||
78 | const void *block = luaZ_getaddr(S->Z, size); | 77 | const void *block = luaZ_getaddr(S->Z, size); |
79 | S->offset += size; | 78 | S->offset += size; |
80 | if (block == NULL) | 79 | if (block == NULL) |
@@ -95,8 +94,8 @@ static lu_byte loadByte (LoadState *S) { | |||
95 | } | 94 | } |
96 | 95 | ||
97 | 96 | ||
98 | static size_t loadUnsigned (LoadState *S, size_t limit) { | 97 | static varint_t loadVarint (LoadState *S, varint_t limit) { |
99 | size_t x = 0; | 98 | varint_t x = 0; |
100 | int b; | 99 | int b; |
101 | limit >>= 7; | 100 | limit >>= 7; |
102 | do { | 101 | do { |
@@ -104,18 +103,18 @@ static size_t loadUnsigned (LoadState *S, size_t limit) { | |||
104 | if (x >= limit) | 103 | if (x >= limit) |
105 | error(S, "integer overflow"); | 104 | error(S, "integer overflow"); |
106 | x = (x << 7) | (b & 0x7f); | 105 | x = (x << 7) | (b & 0x7f); |
107 | } while ((b & 0x80) == 0); | 106 | } while ((b & 0x80) != 0); |
108 | return x; | 107 | return x; |
109 | } | 108 | } |
110 | 109 | ||
111 | 110 | ||
112 | static size_t loadSize (LoadState *S) { | 111 | static size_t loadSize (LoadState *S) { |
113 | return loadUnsigned(S, MAX_SIZET); | 112 | return cast_sizet(loadVarint(S, MAX_SIZET)); |
114 | } | 113 | } |
115 | 114 | ||
116 | 115 | ||
117 | static int loadInt (LoadState *S) { | 116 | static int loadInt (LoadState *S) { |
118 | return cast_int(loadUnsigned(S, INT_MAX)); | 117 | return cast_int(loadVarint(S, INT_MAX)); |
119 | } | 118 | } |
120 | 119 | ||
121 | 120 | ||
@@ -149,9 +148,10 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { | |||
149 | return; | 148 | return; |
150 | } | 149 | } |
151 | else if (size == 1) { /* previously saved string? */ | 150 | else if (size == 1) { /* previously saved string? */ |
152 | int idx = loadInt(S); /* get its index */ | 151 | /* get its index */ |
152 | lua_Unsigned idx = cast(lua_Unsigned, loadVarint(S, LUA_MAXUNSIGNED)); | ||
153 | TValue stv; | 153 | TValue stv; |
154 | luaH_getint(S->h, idx, &stv); | 154 | luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */ |
155 | *sl = ts = tsvalue(&stv); | 155 | *sl = ts = tsvalue(&stv); |
156 | luaC_objbarrier(L, p, ts); | 156 | luaC_objbarrier(L, p, ts); |
157 | return; /* do not save it again */ | 157 | return; /* do not save it again */ |
@@ -175,7 +175,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { | |||
175 | /* add string to list of saved strings */ | 175 | /* add string to list of saved strings */ |
176 | S->nstr++; | 176 | S->nstr++; |
177 | setsvalue(L, &sv, ts); | 177 | setsvalue(L, &sv, ts); |
178 | luaH_setint(L, S->h, S->nstr, &sv); | 178 | luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); |
179 | luaC_objbarrierback(L, obj2gco(S->h), ts); | 179 | luaC_objbarrierback(L, obj2gco(S->h), ts); |
180 | } | 180 | } |
181 | 181 | ||