From 097db7317b0fad3a63093370593c772eb1cca189 Mon Sep 17 00:00:00 2001
From: Mike Pall <mike>
Date: Mon, 22 Mar 2010 15:59:50 +0100
Subject: Move colocated array part after GCtab (now properly aligned).

---
 src/lj_asm.c |  2 +-
 src/lj_gc.h  |  8 ++++++--
 src/lj_tab.c | 32 ++++++++++----------------------
 3 files changed, 17 insertions(+), 25 deletions(-)

(limited to 'src')

diff --git a/src/lj_asm.c b/src/lj_asm.c
index aa42d677..e2fddbe9 100644
--- a/src/lj_asm.c
+++ b/src/lj_asm.c
@@ -1122,7 +1122,7 @@ static void asm_fusearef(ASMState *as, IRIns *ir, RegSet allow)
       noconflict(as, irb->op1, IR_NEWREF)) {
     /* We can avoid the FLOAD of t->array for colocated arrays. */
     as->mrm.base = (uint8_t)ra_alloc1(as, irb->op1, allow);  /* Table obj. */
-    as->mrm.ofs = -(int32_t)(ira->op1*sizeof(TValue));  /* Ofs to colo array. */
+    as->mrm.ofs = (int32_t)sizeof(GCtab);  /* Ofs to colocated array. */
   } else {
     as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow);  /* Array base. */
     as->mrm.ofs = 0;
diff --git a/src/lj_gc.h b/src/lj_gc.h
index 3c4f2d24..228835ac 100644
--- a/src/lj_gc.h
+++ b/src/lj_gc.h
@@ -91,8 +91,12 @@ LJ_FUNC void *lj_mem_grow(lua_State *L, void *p,
 			  MSize *szp, MSize lim, MSize esz);
 
 #define lj_mem_new(L, s)	lj_mem_realloc(L, NULL, 0, (s))
-#define lj_mem_free(g, p, osize) \
-  (g->gc.total -= (MSize)(osize), g->allocf(g->allocd, (p), (osize), 0))
+
+static LJ_AINLINE void lj_mem_free(global_State *g, void *p, size_t osize)
+{
+  g->gc.total -= (MSize)osize;
+  g->allocf(g->allocd, p, osize, 0);
+}
 
 #define lj_mem_newvec(L, n, t)	((t *)lj_mem_new(L, (MSize)((n)*sizeof(t))))
 #define lj_mem_reallocvec(L, p, on, n, t) \
diff --git a/src/lj_tab.c b/src/lj_tab.c
index be26bdda..d77aa05a 100644
--- a/src/lj_tab.c
+++ b/src/lj_tab.c
@@ -98,24 +98,18 @@ static LJ_AINLINE void clearapart(GCtab *t)
 static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
 {
   GCtab *t;
-  global_State *g;
   /* First try to colocate the array part. */
   if (LJ_MAX_COLOSIZE && asize > 0 && asize <= LJ_MAX_COLOSIZE) {
-    /* This is ugly. (sizeof(GCtab)&7) != 0. So prepend the colocated array. */
-    TValue *array = lj_mem_newt(L, sizetabcolo(asize), TValue);
-    t = cast(GCtab *, array + asize);
-    g = G(L);
-    setgcrefr(t->nextgc, g->gc.root);
-    setgcref(g->gc.root, obj2gco(t));
-    newwhite(g, t);
+    lua_assert((sizeof(GCtab) & 7) == 0);
+    t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize));
     t->gct = ~LJ_TTAB;
     t->nomm = cast_byte(~0);
     t->colo = (int8_t)asize;
-    setmref(t->array, array);
+    setmref(t->array, (TValue *)((char *)t + sizeof(GCtab)));
     setgcrefnull(t->metatable);
     t->asize = asize;
     t->hmask = 0;
-    setmref(t->node, &g->nilnode);
+    setmref(t->node, &G(L)->nilnode);
   } else {  /* Otherwise separately allocate the array part. */
     t = lj_mem_newobj(L, GCtab);
     t->gct = ~LJ_TTAB;
@@ -125,8 +119,7 @@ static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
     setgcrefnull(t->metatable);
     t->asize = 0;  /* In case the array allocation fails. */
     t->hmask = 0;
-    g = G(L);
-    setmref(t->node, &g->nilnode);
+    setmref(t->node, &G(L)->nilnode);
     if (asize > 0) {
       if (asize > LJ_MAX_ASIZE)
 	lj_err_msg(L, LJ_ERR_TABOV);
@@ -212,17 +205,12 @@ void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t)
 {
   if (t->hmask > 0)
     lj_mem_freevec(g, noderef(t->node), t->hmask+1, Node);
-  if (LJ_MAX_COLOSIZE && t->colo) {
-    ptrdiff_t n;
-    if (t->colo < 0 && t->asize > 0)  /* Array part was separated. */
-      lj_mem_freevec(g, tvref(t->array), t->asize, TValue);
-    n = t->colo & 0x7f;
-    lj_mem_free(g, (TValue *)t - n, sizetabcolo((uint32_t)n));
-  } else {
-    if (t->asize > 0)
-      lj_mem_freevec(g, tvref(t->array), t->asize, TValue);
+  if (t->asize > 0 && LJ_MAX_COLOSIZE && t->colo <= 0)
+    lj_mem_freevec(g, tvref(t->array), t->asize, TValue);
+  if (LJ_MAX_COLOSIZE && t->colo)
+    lj_mem_free(g, t, sizetabcolo((uint32_t)t->colo & 0x7f));
+  else
     lj_mem_freet(g, t);
-  }
 }
 
 /* -- Table resizing ------------------------------------------------------ */
-- 
cgit v1.2.3-55-g6feb