Path: blob/main/sys/contrib/openzfs/module/lua/lstring.c
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $3** String table (keeps all strings handled by Lua)4** See Copyright Notice in lua.h5*/678#define lstring_c9#define LUA_CORE1011#include <sys/lua/lua.h>1213#include "lmem.h"14#include "lobject.h"15#include "lstate.h"16#include "lstring.h"171819/*20** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to21** compute its hash22*/23#if !defined(LUAI_HASHLIMIT)24#define LUAI_HASHLIMIT 525#endif262728/*29** equality for long strings30*/31int luaS_eqlngstr (TString *a, TString *b) {32size_t len = a->tsv.len;33lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);34return (a == b) || /* same instance or... */35((len == b->tsv.len) && /* equal length and ... */36(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */37}383940/*41** equality for strings42*/43int luaS_eqstr (TString *a, TString *b) {44return (a->tsv.tt == b->tsv.tt) &&45(a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));46}474849unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {50unsigned int h = seed ^ cast(unsigned int, l);51size_t l1;52size_t step = (l >> LUAI_HASHLIMIT) + 1;53for (l1 = l; l1 >= step; l1 -= step)54h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));55return h;56}575859/*60** resizes the string table61*/62void luaS_resize (lua_State *L, int newsize) {63int i;64stringtable *tb = &G(L)->strt;65/* cannot resize while GC is traversing strings */66luaC_runtilstate(L, ~bitmask(GCSsweepstring));67if (newsize > tb->size) {68luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);69for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;70}71/* rehash */72for (i=0; i<tb->size; i++) {73GCObject *p = tb->hash[i];74tb->hash[i] = NULL;75while (p) { /* for each node in the list */76GCObject *next = gch(p)->next; /* save next */77unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */78gch(p)->next = tb->hash[h]; /* chain it */79tb->hash[h] = p;80resetoldbit(p); /* see MOVE OLD rule */81p = next;82}83}84if (newsize < tb->size) {85/* shrinking slice must be empty */86lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);87luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);88}89tb->size = newsize;90}919293/*94** creates a new string object95*/96static TString *createstrobj (lua_State *L, const char *str, size_t l,97int tag, unsigned int h, GCObject **list) {98TString *ts;99char *sbuf;100size_t totalsize; /* total size of TString object */101totalsize = sizeof(TString) + ((l + 1) * sizeof(char));102ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts;103ts->tsv.len = l;104ts->tsv.hash = h;105ts->tsv.extra = 0;106sbuf = ts->contents;107memcpy(sbuf, str, l*sizeof(char));108sbuf[l] = '\0'; /* ending 0 */109return ts;110}111112113/*114** creates a new short string, inserting it into string table115*/116static TString *newshrstr (lua_State *L, const char *str, size_t l,117unsigned int h) {118GCObject **list; /* (pointer to) list where it will be inserted */119stringtable *tb = &G(L)->strt;120TString *s;121if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)122luaS_resize(L, tb->size*2); /* too crowded */123list = &tb->hash[lmod(h, tb->size)];124s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);125tb->nuse++;126return s;127}128129130/*131** checks whether short string exists and reuses it or creates a new one132*/133static TString *internshrstr (lua_State *L, const char *str, size_t l) {134GCObject *o;135global_State *g = G(L);136unsigned int h = luaS_hash(str, l, g->seed);137for (o = g->strt.hash[lmod(h, g->strt.size)];138o != NULL;139o = gch(o)->next) {140TString *ts = rawgco2ts(o);141if (h == ts->tsv.hash &&142l == ts->tsv.len &&143(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {144if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */145changewhite(o); /* resurrect it */146return ts;147}148}149return newshrstr(L, str, l, h); /* not found; create a new string */150}151152153/*154** new string (with explicit length)155*/156TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {157if (l <= LUAI_MAXSHORTLEN) /* short string? */158return internshrstr(L, str, l);159else {160if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char))161luaM_toobig(L);162return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);163}164}165166167/*168** new zero-terminated string169*/170TString *luaS_new (lua_State *L, const char *str) {171return luaS_newlstr(L, str, strlen(str));172}173174175Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {176Udata *u;177if (s > MAX_SIZET - sizeof(Udata))178luaM_toobig(L);179u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;180u->uv.len = s;181u->uv.metatable = NULL;182u->uv.env = e;183return u;184}185186187