• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/lib/ffi/uc_cdata.h

  1 /*
  2 ** C data management.
  3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice below.
  4 **
  5 ** This header contains derived work from LuaJIT's FFI C data objects.
  6 **
  7 ** Modifications:
  8 ** - Adapted for ucode VM integration (uc_cdata_new, uc_cdata_dataptr, etc.)
  9 ** - Removed JIT-specific dependencies
 10 **
 11 ** See NOTICE and ATTRIBUTION.md for complete attribution details.
 12 */
 13 
 14 #ifndef _UC_CDATA_H
 15 #define _UC_CDATA_H
 16 
 17 #include "uc_ctype.h"
 18 
 19 /* Get C data pointer. */
 20 static UC_AINLINE void *cdata_getptr(void *p, CTSize sz)
 21 {
 22         if (UC_64 && sz == 4)
 23         { /* Support 32 bit pointers on 64 bit targets. */
 24                 return ((void *)(uintptr_t) * (uint32_t *)p);
 25         }
 26         else
 27         {
 28                 uc_assertX(sz == CTSIZE_PTR, "bad pointer size %d", sz);
 29                 return *(void **)p;
 30         }
 31 }
 32 
 33 /* Set C data pointer. */
 34 static UC_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v)
 35 {
 36         if (UC_64 && sz == 4)
 37         { /* Support 32 bit pointers on 64 bit targets. */
 38                 *(uint32_t *)p = (uint32_t)(uintptr_t)v;
 39         }
 40         else
 41         {
 42                 uc_assertX(sz == CTSIZE_PTR, "bad pointer size %d", sz);
 43                 *(void **)p = (void *)v;
 44         }
 45 }
 46 
 47 /* Allocate fixed-size C data object. */
 48 static inline uc_value_t *
 49 uc_cdata_new(uc_vm_t *vm, CTypeID id, CTSize sz)
 50 {
 51         CTState *cts = ctype_cts(vm);
 52         uc_resource_t *res;
 53         GCcdata *cd;
 54 
 55 #ifdef LUA_USE_ASSERT
 56         CType *ct = ctype_raw(cts, id);
 57         uc_assertCTS((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz,
 58                                  "inconsistent size of fixed-size cdata alloc");
 59 #endif
 60 
 61         res = xalloc(ALIGN(sizeof(*res)) + sizeof(*cd) + sz);
 62         res->header.type = UC_RESOURCE;
 63         res->header.refcount = 1;
 64         res->type = ucv_resource_type_lookup(vm, "ffi.ctype");
 65         res->data = (char *)res + ALIGN(sizeof(*res));
 66 
 67         cd = res->data;
 68         cd->ctypeid = ctype_check(cts, id);
 69         cd->isvla = 0;
 70         cd->refs = NULL;
 71 
 72         return &res->header;
 73 }
 74 
 75 /* Variant which works without a valid CTState. */
 76 static UC_AINLINE uc_value_t *uc_cdata_new_(uc_vm_t *vm, CTypeID id, CTSize sz)
 77 {
 78         GCcdata *cd;
 79 
 80         cd = xalloc(sizeof(*cd) + sz);
 81         cd->ctypeid = id;
 82         cd->isvla = 0;
 83         cd->refs = NULL;
 84 
 85         return ucv_resource_new(ucv_resource_type_lookup(vm, "ffi.ctype"), cd);
 86 }
 87 
 88 UC_NOAPI uc_value_t *uc_cdata_newref(uc_vm_t *vm, const void *pp, CTypeID id);
 89 UC_NOAPI uc_value_t *uc_cdata_newv(uc_vm_t *vm, CTypeID id, CTSize sz,
 90                                   CTSize align);
 91 UC_NOAPI uc_value_t *uc_cdata_newx(uc_vm_t *vm, CTypeID id, CTSize sz,
 92                                   CTInfo info);
 93 
 94 static inline void *
 95 uc_cdata_dataptr(uc_value_t *val)
 96 {
 97         if (ucv_type(val) != UC_RESOURCE)
 98                 return NULL;
 99 
100         uc_resource_t *res = (uc_resource_t *)val;
101 
102         if (!res->type || strcmp(res->type->name, "ffi.ctype") != 0)
103                 return NULL;
104 
105         return cdataptr((GCcdata *)res->data);
106 }
107 
108 #define uc_cdataptr(res) cdataptr(((uc_resource_t *)res)->data)
109 
110 
111 UC_NOAPI CType *uc_cdata_index(CTState *cts, GCcdata *cd, uc_value_t *key,
112                                                           uint8_t **pp, CTInfo *qual);
113 UC_NOAPI int uc_cdata_get(CTState *cts, CType *s, uc_value_t **uv, uint8_t *sp);
114 UC_NOAPI void uc_cdata_set(CTState *cts, CType *d, uint8_t *dp, uc_value_t *uv,
115                                                   CTInfo qual, uc_value_t **refs);
116 
117 #endif
118 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt