2009-12-08 18:46:35 +00:00
|
|
|
/*
|
|
|
|
** Userdata handling.
|
2021-01-02 20:49:41 +00:00
|
|
|
** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
|
2009-12-08 18:46:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define lj_udata_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lj_obj.h"
|
|
|
|
#include "lj_gc.h"
|
2021-03-25 01:15:26 +00:00
|
|
|
#include "lj_err.h"
|
2009-12-08 18:46:35 +00:00
|
|
|
#include "lj_udata.h"
|
|
|
|
|
|
|
|
GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env)
|
|
|
|
{
|
|
|
|
GCudata *ud = lj_mem_newt(L, sizeof(GCudata) + sz, GCudata);
|
|
|
|
global_State *g = G(L);
|
|
|
|
newwhite(g, ud); /* Not finalized. */
|
|
|
|
ud->gct = ~LJ_TUDATA;
|
2009-12-08 19:35:29 +00:00
|
|
|
ud->udtype = UDTYPE_USERDATA;
|
2009-12-08 18:46:35 +00:00
|
|
|
ud->len = sz;
|
|
|
|
/* NOBARRIER: The GCudata is new (marked white). */
|
|
|
|
setgcrefnull(ud->metatable);
|
|
|
|
setgcref(ud->env, obj2gco(env));
|
|
|
|
/* Chain to userdata list (after main thread). */
|
|
|
|
setgcrefr(ud->nextgc, mainthread(g)->nextgc);
|
|
|
|
setgcref(mainthread(g)->nextgc, obj2gco(ud));
|
|
|
|
return ud;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud)
|
|
|
|
{
|
|
|
|
lj_mem_free(g, ud, sizeudata(ud));
|
|
|
|
}
|
|
|
|
|
2021-03-25 01:15:26 +00:00
|
|
|
#if LJ_64
|
|
|
|
void *lj_lightud_intern(lua_State *L, void *p)
|
|
|
|
{
|
|
|
|
global_State *g = G(L);
|
|
|
|
uint64_t u = (uint64_t)p;
|
|
|
|
uint32_t up = lightudup(u);
|
|
|
|
uint32_t *segmap = mref(g->gc.lightudseg, uint32_t);
|
|
|
|
MSize segnum = g->gc.lightudnum;
|
|
|
|
if (segmap) {
|
|
|
|
MSize seg;
|
|
|
|
for (seg = 0; seg <= segnum; seg++)
|
|
|
|
if (segmap[seg] == up) /* Fast path. */
|
|
|
|
return (void *)(((uint64_t)seg << LJ_LIGHTUD_BITS_LO) | lightudlo(u));
|
|
|
|
segnum++;
|
|
|
|
}
|
|
|
|
if (!((segnum-1) & segnum) && segnum != 1) {
|
|
|
|
if (segnum >= (1 << LJ_LIGHTUD_BITS_SEG)) lj_err_msg(L, LJ_ERR_BADLU);
|
|
|
|
lj_mem_reallocvec(L, segmap, segnum, segnum ? 2*segnum : 2u, uint32_t);
|
|
|
|
setmref(g->gc.lightudseg, segmap);
|
|
|
|
}
|
|
|
|
g->gc.lightudnum = segnum;
|
|
|
|
segmap[segnum] = up;
|
|
|
|
return (void *)(((uint64_t)segnum << LJ_LIGHTUD_BITS_LO) | lightudlo(u));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|