A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h
5** luaS_newllocstr is adapted from "elua -- pseudo RO strings"
6** by bogdanm, distributed under a MIT license.
7*/
8
9
10#include <string.h>
11
12#define lstring_c
13#define LUA_CORE
14
15#include "lua.h"
16
17#include "lmem.h"
18#include "lobject.h"
19#include "lstate.h"
20#include "lstring.h"
21
22
23
24void luaS_resize (lua_State *L, int newsize) {
25 stringtable *tb;
26 int i;
27 tb = &G(L)->strt;
28 if (luaC_sweepstrgc(L) || newsize == tb->size || is_resizing_strings_gc(L))
29 return; /* cannot resize during GC traverse or doesn't need to be resized */
30 set_resizing_strings_gc(L);
31 if (newsize > tb->size) {
32 luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
33 for (i=tb->size; i<newsize; i++) tb->hash[i] = NULL;
34 }
35 /* rehash */
36 for (i=0; i<tb->size; i++) {
37 GCObject *p = tb->hash[i];
38 tb->hash[i] = NULL;
39 while (p) { /* for each node in the list */
40 GCObject *next = p->gch.next; /* save next */
41 unsigned int h = gco2ts(p)->hash;
42 int h1 = lmod(h, newsize); /* new position */
43 lua_assert(cast_int(h%newsize) == lmod(h, newsize));
44 p->gch.next = tb->hash[h1]; /* chain it */
45 tb->hash[h1] = p;
46 p = next;
47 }
48 }
49 if (newsize < tb->size)
50 luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
51 tb->size = newsize;
52 unset_resizing_strings_gc(L);
53}
54
55
56static TString *newlstr (lua_State *L, const char *str, size_t l,
57 unsigned int h, char type) {
58 TString *ts;
59 stringtable *tb;
60 if (l > ((MAX_SIZET - sizeof(TString))/sizeof(char)) - sizeof(""))
61 luaM_toobig(L);
62 tb = &G(L)->strt;
63 if ((tb->nuse + 1) > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
64 luaS_resize(L, tb->size*2); /* too crowded */
65 ts = cast(TString *, luaM_malloc(L, sizetstring(type, l)));
66 ts->tsv.len = l;
67 ts->tsv.hash = h;
68 ts->tsv.marked = luaC_white(G(L));
69 if (testbits(type, TSTR_FIXED))
70 luaS_fix(ts);
71 ts->tsv.tt = LUA_TSTRING;
72 ts->tsv.reserved = 0;
73 ts->tsv.type = cast_byte(type);
74 if (testbits(type, TSTR_INBIN)) /* ROCKLUA ADDED */
75 *(const char **)(ts+1) = str; /* store a pointer to the string instead */
76 else {
77 memcpy(ts+1, str, l*sizeof(char));
78 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
79 }
80 h = lmod(h, tb->size);
81 ts->tsv.next = tb->hash[h]; /* chain new entry */
82 tb->hash[h] = obj2gco(ts);
83 tb->nuse++;
84 return ts;
85}
86
87
88TString *luaS_newllocstr (lua_State *L, const char *str, size_t l, char type) {
89 GCObject *o;
90 if (testbits(type, TSTR_CHKSZ))
91 l = strlen(str);
92#ifdef INBINARYSTRINGS
93 else if (!testbits(type, TSTR_ISLIT))
94#else
95 if (true)
96#endif
97 resetbits(type, TSTR_INBIN); /* only whole strings can be used inbin */
98 unsigned int h = cast(unsigned int, l); /* seed */
99 size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
100 size_t l1;
101 for (l1=l; l1>=step; l1-=step) /* compute hash */
102 h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
103 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
104 o != NULL;
105 o = o->gch.next) {
106 TString *ts = rawgco2ts(o);
107 if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
108 /* string may be dead */
109 if (isdead(G(L), o)) changewhite(o);
110 if (testbits(type, TSTR_FIXED))
111 luaS_fix(ts);
112 return ts;
113 }
114 }
115 return newlstr(L, str, l, h, type); /* not found */
116}
117
118
119Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
120 Udata *u;
121 if (s > MAX_SIZET - sizeof(Udata))
122 luaM_toobig(L);
123 u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
124 u->uv.marked = luaC_white(G(L)); /* is not finalized */
125 u->uv.tt = LUA_TUSERDATA;
126 u->uv.len = s;
127 u->uv.metatable = NULL;
128 u->uv.env = e;
129 /* chain it on udata list (after main thread) */
130 u->uv.next = G(L)->mainthread->next;
131 G(L)->mainthread->next = obj2gco(u);
132 return u;
133}
134