From baa2be0bcf86c435c999bbdcff2a6aa40d5ed12c Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 18 Apr 2016 22:07:57 -0400 Subject: [PATCH] Added opaque types --- include/cyclone/types.h | 10 ++++++++++ runtime.c | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/cyclone/types.h b/include/cyclone/types.h index 70c2461b..808c45b5 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -197,6 +197,7 @@ typedef long tag_type; #define mutex_tag 19 #define cond_var_tag 20 #define bytevector_tag 21 +#define c_opaque_tag 22 #define nil NULL #define eq(x,y) (x == y) @@ -239,6 +240,15 @@ typedef struct {gc_header_type hdr; tag_type tag; object *pvar;} cvar_type; typedef cvar_type *cvar; #define make_cvar(n,v) cvar_type n; n.hdr.mark = gc_color_red; n.hdr.grayed = 0; n.tag = cvar_tag; n.pvar = v; +/* C Opaque type - a wrapper around a pointer of any type. + Note this requires application code to free any memory + before an object is collected by GC. */ +typedef struct {gc_header_type hdr; tag_type tag; void *ptr;} c_opaque_type; +typedef c_opaque_type *c_opaque; +#define make_c_opaque(var, ptr) c_opaque_type var; var.hdr.mark = gc_color_red; n.ndr.grayed = 0; n.tag = c_opaque_type; n.ptr = ptr; + +#define opaque_ptr(x) (((c_opaque)x)->ptr) + /* Define mutex type */ typedef struct {gc_header_type hdr; tag_type tag; pthread_mutex_t lock;} mutex_type; typedef mutex_type *mutex; diff --git a/runtime.c b/runtime.c index 80da55d5..9bbfe4b8 100644 --- a/runtime.c +++ b/runtime.c @@ -27,7 +27,7 @@ object Cyc_global_set(void *thd, object *glo, object value) /* Error checking section - type mismatch, num args, etc */ /* Type names to use for error messages */ -const char *tag_names[23] = { \ +const char *tag_names[24] = { \ "pair" \ , "symbol" \ , "" \ @@ -50,6 +50,7 @@ const char *tag_names[23] = { \ , "mutex" \ , "condition variable" \ , "bytevector" \ + , "opaque" \ , "Reserved for future use" }; void Cyc_invalid_type_error(void *data, int tag, object found) { @@ -565,6 +566,9 @@ object Cyc_display(object x, FILE *port) case cvar_tag: Cyc_display(Cyc_get_cvar(x), port); break; + case c_opaque_tag: + fprintf(port, "", opaque_ptr(x)); + break; case mutex_tag: fprintf(port, "", x); break;