Skip to content

Commit

Permalink
Reorganize lightuserdata interning code.
Browse files Browse the repository at this point in the history
(cherry picked from commit 836fb5b)

This patch only contains a code movement of lightuserdata interning to
<lj_udata.c> file and does nothing else. This patch is backported to
simplify syncing with the upstream.

Sergey Kaplun:
* added the description for the patch

Part of tarantool/tarantool#6548
Needed for tarantool/tarantool#2712

Reviewed-by: Sergey Ostanevich <sergos@tarantool.org>
Reviewed-by: Igor Munkin <imun@tarantool.org>
Signed-off-by: Igor Munkin <imun@tarantool.org>
(cherry picked from commit 2754da4)
  • Loading branch information
Mike Pall authored and igormunkin committed Jun 29, 2022
1 parent bec55f2 commit 5c9a24c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
30 changes: 2 additions & 28 deletions src/lj_api.c
Expand Up @@ -716,36 +716,10 @@ LUA_API void lua_pushboolean(lua_State *L, int b)
incr_top(L);
}

#if LJ_64
static void *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

LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
{
#if LJ_64
p = lightud_intern(L, p);
p = lj_lightud_intern(L, p);
#endif
setrawlightudV(L->top, p);
incr_top(L);
Expand Down Expand Up @@ -1199,7 +1173,7 @@ static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
setfuncV(L, top++, fn);
if (LJ_FR2) setnilV(top++);
#if LJ_64
ud = lightud_intern(L, ud);
ud = lj_lightud_intern(L, ud);
#endif
setrawlightudV(top++, ud);
cframe_nres(L->cframe) = 1+0; /* Zero results. */
Expand Down
27 changes: 27 additions & 0 deletions src/lj_udata.c
Expand Up @@ -8,6 +8,7 @@

#include "lj_obj.h"
#include "lj_gc.h"
#include "lj_err.h"
#include "lj_udata.h"

GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env)
Expand All @@ -34,3 +35,29 @@ void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud)
lj_mem_free(g, ud, sizeudata(ud));
}

#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

3 changes: 3 additions & 0 deletions src/lj_udata.h
Expand Up @@ -10,5 +10,8 @@

LJ_FUNC GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env);
LJ_FUNC void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud);
#if LJ_64
LJ_FUNC void * LJ_FASTCALL lj_lightud_intern(lua_State *L, void *p);
#endif

#endif

0 comments on commit 5c9a24c

Please sign in to comment.