1 | /* |
2 | ** Userdata handling. |
3 | ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h |
4 | */ |
5 | |
6 | #define lj_udata_c |
7 | #define LUA_CORE |
8 | |
9 | #include "lj_obj.h" |
10 | #include "lj_gc.h" |
11 | #include "lj_err.h" |
12 | #include "lj_udata.h" |
13 | |
14 | GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env) |
15 | { |
16 | GCudata *ud = lj_mem_newt(L, sizeof(GCudata) + sz, GCudata); |
17 | global_State *g = G(L); |
18 | newwhite(g, ud); /* Not finalized. */ |
19 | ud->gct = ~LJ_TUDATA; |
20 | ud->udtype = UDTYPE_USERDATA; |
21 | ud->len = sz; |
22 | /* NOBARRIER: The GCudata is new (marked white). */ |
23 | setgcrefnull(ud->metatable); |
24 | setgcref(ud->env, obj2gco(env)); |
25 | /* Chain to userdata list (after main thread). */ |
26 | setgcrefr(ud->nextgc, mainthread(g)->nextgc); |
27 | setgcref(mainthread(g)->nextgc, obj2gco(ud)); |
28 | return ud; |
29 | } |
30 | |
31 | void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud) |
32 | { |
33 | lj_mem_free(g, ud, sizeudata(ud)); |
34 | } |
35 | |
36 | #if LJ_64 |
37 | void *lj_lightud_intern(lua_State *L, void *p) |
38 | { |
39 | global_State *g = G(L); |
40 | uint64_t u = (uint64_t)p; |
41 | uint32_t up = lightudup(u); |
42 | uint32_t *segmap = mref(g->gc.lightudseg, uint32_t); |
43 | MSize segnum = g->gc.lightudnum; |
44 | if (segmap) { |
45 | MSize seg; |
46 | for (seg = 0; seg <= segnum; seg++) |
47 | if (segmap[seg] == up) /* Fast path. */ |
48 | return (void *)(((uint64_t)seg << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); |
49 | segnum++; |
50 | } |
51 | if (!((segnum-1) & segnum) && segnum != 1) { |
52 | if (segnum >= (1 << LJ_LIGHTUD_BITS_SEG)) lj_err_msg(L, LJ_ERR_BADLU); |
53 | lj_mem_reallocvec(L, segmap, segnum, segnum ? 2*segnum : 2u, uint32_t); |
54 | setmref(g->gc.lightudseg, segmap); |
55 | } |
56 | g->gc.lightudnum = segnum; |
57 | segmap[segnum] = up; |
58 | return (void *)(((uint64_t)segnum << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); |
59 | } |
60 | #endif |
61 | |
62 | |