Fix shrinking of direct mapped block in builtin allocator.
[luajit-2.0/celess22.git] / src / lj_cdata.h
blob3132926622cf4fa9a18b316d37d74e11702f33a0
1 /*
2 ** C data management.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_CDATA_H
7 #define _LJ_CDATA_H
9 #include "lj_obj.h"
10 #include "lj_gc.h"
11 #include "lj_ctype.h"
13 #if LJ_HASFFI
15 /* Get C data pointer. */
16 static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz)
18 if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */
19 return ((void *)(uintptr_t)*(uint32_t *)p);
20 } else {
21 lua_assert(sz == CTSIZE_PTR);
22 return *(void **)p;
26 /* Set C data pointer. */
27 static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v)
29 if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */
30 *(uint32_t *)p = (uint32_t)(uintptr_t)v;
31 } else {
32 lua_assert(sz == CTSIZE_PTR);
33 *(void **)p = (void *)v;
37 /* Allocate fixed-size C data object. */
38 static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz)
40 GCcdata *cd;
41 #ifdef LUA_USE_ASSERT
42 CType *ct = ctype_raw(cts, id);
43 lua_assert((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz);
44 #endif
45 cd = (GCcdata *)lj_mem_newgco(cts->L, sizeof(GCcdata) + sz);
46 cd->gct = ~LJ_TCDATA;
47 cd->ctypeid = ctype_check(cts, id);
48 return cd;
51 /* Variant which works without a valid CTState. */
52 static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz)
54 GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz);
55 cd->gct = ~LJ_TCDATA;
56 cd->ctypeid = id;
57 return cd;
60 LJ_FUNC GCcdata *lj_cdata_newref(CTState *cts, const void *pp, CTypeID id);
61 LJ_FUNC GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz,
62 CTSize align);
64 LJ_FUNC void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd);
65 LJ_FUNCA TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd);
67 LJ_FUNC CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key,
68 uint8_t **pp, CTInfo *qual);
69 LJ_FUNC int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp);
70 LJ_FUNC void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o,
71 CTInfo qual);
73 #endif
75 #endif