1 | /* |
2 | ** C data management. |
3 | ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h |
4 | */ |
5 | |
6 | #ifndef _LJ_CDATA_H |
7 | #define _LJ_CDATA_H |
8 | |
9 | #include "lj_obj.h" |
10 | #include "lj_gc.h" |
11 | #include "lj_ctype.h" |
12 | |
13 | #if LJ_HASFFI |
14 | |
15 | /* Get C data pointer. */ |
16 | static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz) |
17 | { |
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; |
23 | } |
24 | } |
25 | |
26 | /* Set C data pointer. */ |
27 | static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v) |
28 | { |
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; |
34 | } |
35 | } |
36 | |
37 | /* Allocate fixed-size C data object. */ |
38 | static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz) |
39 | { |
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; |
49 | } |
50 | |
51 | /* Variant which works without a valid CTState. */ |
52 | static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz) |
53 | { |
54 | GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz); |
55 | cd->gct = ~LJ_TCDATA; |
56 | cd->ctypeid = id; |
57 | return cd; |
58 | } |
59 | |
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); |
63 | |
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); |
66 | |
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); |
72 | |
73 | #endif |
74 | |
75 | #endif |
76 | |