aboutsummaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-12-10 10:13:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-12-10 10:13:36 -0200
commit47fc57a2529c83376883f36954082cfe80ae588f (patch)
treec2e57e2f9f7d78279144bfd9cbd04a3b1b131f12 /ltable.c
parent4d5fe1f54bc00850f77a7c42f9e95d0ff3f1ab5b (diff)
downloadlua-47fc57a2529c83376883f36954082cfe80ae588f.tar.gz
lua-47fc57a2529c83376883f36954082cfe80ae588f.tar.bz2
lua-47fc57a2529c83376883f36954082cfe80ae588f.zip
`TObject' renamed to `TValue' + other name changes and better assertions
for incremental garbage collection
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c72
1 files changed, 36 insertions, 36 deletions
diff --git a/ltable.c b/ltable.c
index b0db3bce..797690b4 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.137 2003/12/01 18:22:56 roberto Exp roberto $ 2** $Id: ltable.c,v 1.138 2003/12/09 16:56:11 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -91,12 +91,12 @@ static Node *hashnum (const Table *t, lua_Number n) {
91** returns the `main' position of an element in a table (that is, the index 91** returns the `main' position of an element in a table (that is, the index
92** of its hash value) 92** of its hash value)
93*/ 93*/
94Node *luaH_mainposition (const Table *t, const TObject *key) { 94Node *luaH_mainposition (const Table *t, const TValue *key) {
95 switch (ttype(key)) { 95 switch (ttype(key)) {
96 case LUA_TNUMBER: 96 case LUA_TNUMBER:
97 return hashnum(t, nvalue(key)); 97 return hashnum(t, nvalue(key));
98 case LUA_TSTRING: 98 case LUA_TSTRING:
99 return hashstr(t, tsvalue(key)); 99 return hashstr(t, rawtsvalue(key));
100 case LUA_TBOOLEAN: 100 case LUA_TBOOLEAN:
101 return hashboolean(t, bvalue(key)); 101 return hashboolean(t, bvalue(key));
102 case LUA_TLIGHTUSERDATA: 102 case LUA_TLIGHTUSERDATA:
@@ -111,7 +111,7 @@ Node *luaH_mainposition (const Table *t, const TObject *key) {
111** returns the index for `key' if `key' is an appropriate key to live in 111** returns the index for `key' if `key' is an appropriate key to live in
112** the array part of the table, -1 otherwise. 112** the array part of the table, -1 otherwise.
113*/ 113*/
114static int arrayindex (const TObject *key, lua_Number lim) { 114static int arrayindex (const TValue *key, lua_Number lim) {
115 if (ttisnumber(key)) { 115 if (ttisnumber(key)) {
116 lua_Number n = nvalue(key); 116 lua_Number n = nvalue(key);
117 int k; 117 int k;
@@ -137,7 +137,7 @@ static int luaH_index (lua_State *L, Table *t, StkId key) {
137 return i-1; /* yes; that's the index (corrected to C) */ 137 return i-1; /* yes; that's the index (corrected to C) */
138 } 138 }
139 else { 139 else {
140 const TObject *v = luaH_get(t, key); 140 const TValue *v = luaH_get(t, key);
141 if (v == &luaO_nilobject) 141 if (v == &luaO_nilobject)
142 luaG_runerror(L, "invalid key for `next'"); 142 luaG_runerror(L, "invalid key for `next'");
143 i = cast(int, (cast(const lu_byte *, v) - 143 i = cast(int, (cast(const lu_byte *, v) -
@@ -152,14 +152,14 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
152 for (i++; i < t->sizearray; i++) { /* try first array part */ 152 for (i++; i < t->sizearray; i++) { /* try first array part */
153 if (!ttisnil(&t->array[i])) { /* a non-nil value? */ 153 if (!ttisnil(&t->array[i])) { /* a non-nil value? */
154 setnvalue(key, cast(lua_Number, i+1)); 154 setnvalue(key, cast(lua_Number, i+1));
155 setobj2s(key+1, &t->array[i]); 155 setobj2s(L, key+1, &t->array[i]);
156 return 1; 156 return 1;
157 } 157 }
158 } 158 }
159 for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ 159 for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
160 if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ 160 if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
161 setobj2s(key, gkey(gnode(t, i))); 161 setobj2s(L, key, gkey(gnode(t, i)));
162 setobj2s(key+1, gval(gnode(t, i))); 162 setobj2s(L, key+1, gval(gnode(t, i)));
163 return 1; 163 return 1;
164 } 164 }
165 } 165 }
@@ -239,7 +239,7 @@ static void numuse (const Table *t, int *narray, int *nhash) {
239 239
240static void setarrayvector (lua_State *L, Table *t, int size) { 240static void setarrayvector (lua_State *L, Table *t, int size) {
241 int i; 241 int i;
242 luaM_reallocvector(L, t->array, t->sizearray, size, TObject); 242 luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
243 for (i=t->sizearray; i<size; i++) 243 for (i=t->sizearray; i<size; i++)
244 setnilvalue(&t->array[i]); 244 setnilvalue(&t->array[i]);
245 t->sizearray = size; 245 t->sizearray = size;
@@ -296,16 +296,16 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
296 /* re-insert elements from vanishing slice */ 296 /* re-insert elements from vanishing slice */
297 for (i=nasize; i<oldasize; i++) { 297 for (i=nasize; i<oldasize; i++) {
298 if (!ttisnil(&t->array[i])) 298 if (!ttisnil(&t->array[i]))
299 setobjt2t(luaH_setnum(L, t, i+1), &t->array[i]); 299 setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
300 } 300 }
301 /* shrink array */ 301 /* shrink array */
302 luaM_reallocvector(L, t->array, oldasize, nasize, TObject); 302 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
303 } 303 }
304 /* re-insert elements in hash part */ 304 /* re-insert elements in hash part */
305 for (i = twoto(oldhsize) - 1; i >= 0; i--) { 305 for (i = twoto(oldhsize) - 1; i >= 0; i--) {
306 Node *old = nold+i; 306 Node *old = nold+i;
307 if (!ttisnil(gval(old))) 307 if (!ttisnil(gval(old)))
308 setobjt2t(luaH_set(L, t, gkey(old)), gval(old)); 308 setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
309 } 309 }
310 if (oldhsize) 310 if (oldhsize)
311 luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ 311 luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
@@ -327,7 +327,7 @@ static void rehash (lua_State *L, Table *t) {
327 327
328Table *luaH_new (lua_State *L, int narray, int lnhash) { 328Table *luaH_new (lua_State *L, int narray, int lnhash) {
329 Table *t = luaM_new(L, Table); 329 Table *t = luaM_new(L, Table);
330 luaC_link(L, valtogco(t), LUA_TTABLE); 330 luaC_link(L, obj2gco(t), LUA_TTABLE);
331 t->metatable = NULL; 331 t->metatable = NULL;
332 t->flags = cast(lu_byte, ~0); 332 t->flags = cast(lu_byte, ~0);
333 /* temporary values (kept only if some malloc fails) */ 333 /* temporary values (kept only if some malloc fails) */
@@ -344,7 +344,7 @@ Table *luaH_new (lua_State *L, int narray, int lnhash) {
344void luaH_free (lua_State *L, Table *t) { 344void luaH_free (lua_State *L, Table *t) {
345 if (t->lsizenode) 345 if (t->lsizenode)
346 luaM_freearray(L, t->node, sizenode(t), Node); 346 luaM_freearray(L, t->node, sizenode(t), Node);
347 luaM_freearray(L, t->array, t->sizearray, TObject); 347 luaM_freearray(L, t->array, t->sizearray, TValue);
348 luaM_freelem(L, t); 348 luaM_freelem(L, t);
349} 349}
350 350
@@ -377,8 +377,8 @@ void luaH_remove (Table *t, Node *e) {
377** put new key in its main position; otherwise (colliding node is in its main 377** put new key in its main position; otherwise (colliding node is in its main
378** position), new key goes to an empty position. 378** position), new key goes to an empty position.
379*/ 379*/
380static TObject *newkey (lua_State *L, Table *t, const TObject *key) { 380static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
381 TObject *val; 381 TValue *val;
382 Node *mp = luaH_mainposition(t, key); 382 Node *mp = luaH_mainposition(t, key);
383 if (!ttisnil(gval(mp))) { /* main position is not free? */ 383 if (!ttisnil(gval(mp))) { /* main position is not free? */
384 Node *othern = luaH_mainposition(t, gkey(mp)); /* `mp' of colliding node */ 384 Node *othern = luaH_mainposition(t, gkey(mp)); /* `mp' of colliding node */
@@ -398,7 +398,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
398 mp = n; 398 mp = n;
399 } 399 }
400 } 400 }
401 setobj2t(gkey(mp), key); 401 setobj2t(L, gkey(mp), key);
402 luaC_barrier(L, t, key); 402 luaC_barrier(L, t, key);
403 lua_assert(ttisnil(gval(mp))); 403 lua_assert(ttisnil(gval(mp)));
404 for (;;) { /* correct `firstfree' */ 404 for (;;) { /* correct `firstfree' */
@@ -410,7 +410,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
410 /* no more free places; must create one */ 410 /* no more free places; must create one */
411 setbvalue(gval(mp), 0); /* avoid new key being removed */ 411 setbvalue(gval(mp), 0); /* avoid new key being removed */
412 rehash(L, t); /* grow table */ 412 rehash(L, t); /* grow table */
413 val = cast(TObject *, luaH_get(t, key)); /* get new position */ 413 val = cast(TValue *, luaH_get(t, key)); /* get new position */
414 lua_assert(ttisboolean(val)); 414 lua_assert(ttisboolean(val));
415 setnilvalue(val); 415 setnilvalue(val);
416 return val; 416 return val;
@@ -420,7 +420,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
420/* 420/*
421** generic search function 421** generic search function
422*/ 422*/
423static const TObject *luaH_getany (Table *t, const TObject *key) { 423static const TValue *luaH_getany (Table *t, const TValue *key) {
424 if (!ttisnil(key)) { 424 if (!ttisnil(key)) {
425 Node *n = luaH_mainposition(t, key); 425 Node *n = luaH_mainposition(t, key);
426 do { /* check whether `key' is somewhere in the chain */ 426 do { /* check whether `key' is somewhere in the chain */
@@ -435,7 +435,7 @@ static const TObject *luaH_getany (Table *t, const TObject *key) {
435/* 435/*
436** search function for integers 436** search function for integers
437*/ 437*/
438const TObject *luaH_getnum (Table *t, int key) { 438const TValue *luaH_getnum (Table *t, int key) {
439 if (1 <= key && key <= t->sizearray) 439 if (1 <= key && key <= t->sizearray)
440 return &t->array[key-1]; 440 return &t->array[key-1];
441 else { 441 else {
@@ -454,10 +454,10 @@ const TObject *luaH_getnum (Table *t, int key) {
454/* 454/*
455** search function for strings 455** search function for strings
456*/ 456*/
457const TObject *luaH_getstr (Table *t, TString *key) { 457const TValue *luaH_getstr (Table *t, TString *key) {
458 Node *n = hashstr(t, key); 458 Node *n = hashstr(t, key);
459 do { /* check whether `key' is somewhere in the chain */ 459 do { /* check whether `key' is somewhere in the chain */
460 if (ttisstring(gkey(n)) && tsvalue(gkey(n)) == key) 460 if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
461 return gval(n); /* that's it */ 461 return gval(n); /* that's it */
462 else n = n->next; 462 else n = n->next;
463 } while (n); 463 } while (n);
@@ -468,9 +468,9 @@ const TObject *luaH_getstr (Table *t, TString *key) {
468/* 468/*
469** main search function 469** main search function
470*/ 470*/
471const TObject *luaH_get (Table *t, const TObject *key) { 471const TValue *luaH_get (Table *t, const TValue *key) {
472 switch (ttype(key)) { 472 switch (ttype(key)) {
473 case LUA_TSTRING: return luaH_getstr(t, tsvalue(key)); 473 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
474 case LUA_TNUMBER: { 474 case LUA_TNUMBER: {
475 int k; 475 int k;
476 lua_number2int(k, (nvalue(key))); 476 lua_number2int(k, (nvalue(key)));
@@ -483,11 +483,11 @@ const TObject *luaH_get (Table *t, const TObject *key) {
483} 483}
484 484
485 485
486TObject *luaH_set (lua_State *L, Table *t, const TObject *key) { 486TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
487 const TObject *p = luaH_get(t, key); 487 const TValue *p = luaH_get(t, key);
488 t->flags = 0; 488 t->flags = 0;
489 if (p != &luaO_nilobject) 489 if (p != &luaO_nilobject)
490 return cast(TObject *, p); 490 return cast(TValue *, p);
491 else { 491 else {
492 if (ttisnil(key)) luaG_runerror(L, "table index is nil"); 492 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
493 else if (ttisnumber(key) && nvalue(key) != nvalue(key)) 493 else if (ttisnumber(key) && nvalue(key) != nvalue(key))
@@ -497,25 +497,25 @@ TObject *luaH_set (lua_State *L, Table *t, const TObject *key) {
497} 497}
498 498
499 499
500TObject *luaH_setnum (lua_State *L, Table *t, int key) { 500TValue *luaH_setnum (lua_State *L, Table *t, int key) {
501 const TObject *p = luaH_getnum(t, key); 501 const TValue *p = luaH_getnum(t, key);
502 if (p != &luaO_nilobject) 502 if (p != &luaO_nilobject)
503 return cast(TObject *, p); 503 return cast(TValue *, p);
504 else { 504 else {
505 TObject k; 505 TValue k;
506 setnvalue(&k, cast(lua_Number, key)); 506 setnvalue(&k, cast(lua_Number, key));
507 return newkey(L, t, &k); 507 return newkey(L, t, &k);
508 } 508 }
509} 509}
510 510
511 511
512TObject *luaH_setstr (lua_State *L, Table *t, TString *key) { 512TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
513 const TObject *p = luaH_getstr(t, key); 513 const TValue *p = luaH_getstr(t, key);
514 if (p != &luaO_nilobject) 514 if (p != &luaO_nilobject)
515 return cast(TObject *, p); 515 return cast(TValue *, p);
516 else { 516 else {
517 TObject k; 517 TValue k;
518 setsvalue(&k, key); 518 setsvalue(L, &k, key);
519 return newkey(L, t, &k); 519 return newkey(L, t, &k);
520 } 520 }
521} 521}