lj_tab.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. ** Table handling.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_TAB_H
  6. #define _LJ_TAB_H
  7. #include "lj_obj.h"
  8. /* Hash constants. Tuned using a brute force search. */
  9. #define HASH_BIAS (-0x04c11db7)
  10. #define HASH_ROT1 14
  11. #define HASH_ROT2 5
  12. #define HASH_ROT3 13
  13. /* Scramble the bits of numbers and pointers. */
  14. static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi)
  15. {
  16. #if LJ_TARGET_X86ORX64
  17. /* Prefer variant that compiles well for a 2-operand CPU. */
  18. lo ^= hi; hi = lj_rol(hi, HASH_ROT1);
  19. lo -= hi; hi = lj_rol(hi, HASH_ROT2);
  20. hi ^= lo; hi -= lj_rol(lo, HASH_ROT3);
  21. #else
  22. lo ^= hi;
  23. lo = lo - lj_rol(hi, HASH_ROT1);
  24. hi = lo ^ lj_rol(hi, HASH_ROT1 + HASH_ROT2);
  25. hi = hi - lj_rol(lo, HASH_ROT3);
  26. #endif
  27. return hi;
  28. }
  29. /* Hash values are masked with the table hash mask and used as an index. */
  30. static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash)
  31. {
  32. Node *n = noderef(t->node);
  33. return &n[hash & t->hmask];
  34. }
  35. /* String IDs are generated when a string is interned. */
  36. #define hashstr(t, s) hashmask(t, (s)->sid)
  37. #define hashlohi(t, lo, hi) hashmask((t), hashrot((lo), (hi)))
  38. #define hashnum(t, o) hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1))
  39. #if LJ_GC64
  40. #define hashgcref(t, r) \
  41. hashlohi((t), (uint32_t)gcrefu(r), (uint32_t)(gcrefu(r) >> 32))
  42. #else
  43. #define hashgcref(t, r) hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS)
  44. #endif
  45. #define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0)
  46. LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits);
  47. LJ_FUNC GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h);
  48. #if LJ_HASJIT
  49. LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize);
  50. #endif
  51. LJ_FUNCA GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt);
  52. LJ_FUNC void LJ_FASTCALL lj_tab_clear(GCtab *t);
  53. LJ_FUNC void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t);
  54. #if LJ_HASFFI
  55. LJ_FUNC void lj_tab_rehash(lua_State *L, GCtab *t);
  56. #endif
  57. LJ_FUNC void lj_tab_resize(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits);
  58. LJ_FUNCA void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize);
  59. /* Caveat: all getters except lj_tab_get() can return NULL! */
  60. LJ_FUNCA cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key);
  61. LJ_FUNC cTValue *lj_tab_getstr(GCtab *t, const GCstr *key);
  62. LJ_FUNCA cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key);
  63. /* Caveat: all setters require a write barrier for the stored value. */
  64. LJ_FUNCA TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key);
  65. LJ_FUNCA TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key);
  66. LJ_FUNC TValue *lj_tab_setstr(lua_State *L, GCtab *t, const GCstr *key);
  67. LJ_FUNC TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key);
  68. #define inarray(t, key) ((MSize)(key) < (MSize)(t)->asize)
  69. #define arrayslot(t, i) (&tvref((t)->array)[(i)])
  70. #define lj_tab_getint(t, key) \
  71. (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_getinth((t), (key)))
  72. #define lj_tab_setint(L, t, key) \
  73. (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_setinth(L, (t), (key)))
  74. LJ_FUNC uint32_t LJ_FASTCALL lj_tab_keyindex(GCtab *t, cTValue *key);
  75. LJ_FUNCA int lj_tab_next(GCtab *t, cTValue *key, TValue *o);
  76. LJ_FUNCA MSize LJ_FASTCALL lj_tab_len(GCtab *t);
  77. #if LJ_HASJIT
  78. LJ_FUNC MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint);
  79. #endif
  80. #endif