From 9100e797f839e5a231fa29d2d05a29cf4f4eb4f8 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 8 Nov 2017 10:26:51 +0100 Subject: [PATCH] opaque terms --- C/bignum.c | 10 +++++----- C/c_interface.c | 16 +++++++++------- H/TermExt.h | 8 ++++---- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/C/bignum.c b/C/bignum.c index 9da08c0a6..b9a3a46ef 100644 --- a/C/bignum.c +++ b/C/bignum.c @@ -167,7 +167,7 @@ int Yap_CleanOpaqueVariable(CELL d) { "clean opaque: bad blob with tag " UInt_FORMAT, blob_tag); return FALSE; } - blob_info = blob_tag - USER_BLOB_START; + blob_info = blob_tag; if (!GLOBAL_OpaqueHandlers) return false; if (!GLOBAL_OpaqueHandlers[blob_info].fail_handler) @@ -192,7 +192,7 @@ YAP_Opaque_CallOnWrite Yap_blob_write_handler(Term t) { "clean opaque: bad blob with tag " UInt_FORMAT, blob_tag); return FALSE; } - blob_info = blob_tag - USER_BLOB_START; + blob_info = blob_tag; if (!GLOBAL_OpaqueHandlers) { return NULL; } @@ -214,7 +214,7 @@ YAP_Opaque_CallOnGCMark Yap_blob_gc_mark_handler(Term t) { if (blob_tag < USER_BLOB_START || blob_tag >= USER_BLOB_END) { return NULL; } - blob_info = blob_tag - USER_BLOB_START; + blob_info = blob_tag; if (!GLOBAL_OpaqueHandlers) return NULL; return GLOBAL_OpaqueHandlers[blob_info].mark_handler; @@ -237,7 +237,7 @@ YAP_Opaque_CallOnGCRelocate Yap_blob_gc_relocate_handler(Term t) { "clean opaque: bad blob with tag " UInt_FORMAT, blob_tag); return FALSE; } - blob_info = blob_tag - USER_BLOB_START; + blob_info = blob_tag; if (!GLOBAL_OpaqueHandlers) return NULL; return GLOBAL_OpaqueHandlers[blob_info].relocate_handler; @@ -509,4 +509,4 @@ void Yap_InitBigNums(void) { Yap_InitCPred("string", 1, p_is_string, SafePredFlag); Yap_InitCPred("opaque", 1, p_is_opaque, SafePredFlag); Yap_InitCPred("nb_set_bit", 2, p_nb_set_bit, SafePredFlag); -} \ No newline at end of file +} diff --git a/C/c_interface.c b/C/c_interface.c index 22c0e2043..c0f03a315 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1893,34 +1893,36 @@ X_API void *YAP_ExternalDataInStackFromTerm(Term t) { X_API YAP_opaque_tag_t YAP_NewOpaqueType(struct YAP_opaque_handler_struct *f) { int i; - if (!GLOBAL_OpaqueHandlers) { + if (!GLOBAL_OpaqueHandlersCount) { GLOBAL_OpaqueHandlers = - malloc(sizeof(YAP_opaque_handler_t) * (USER_BLOB_END - USER_BLOB_START)); + malloc(sizeof(YAP_opaque_handler_t) *USER_BLOB_END ); if (!GLOBAL_OpaqueHandlers) { /* no room */ return -1; } - } else if (GLOBAL_OpaqueHandlersCount == USER_BLOB_END - USER_BLOB_START) { + GLOBAL_OpaqueHandlersCount = USER_BLOB_START; + } else if (GLOBAL_OpaqueHandlersCount == USER_BLOB_END) { /* all types used */ return -1; } i = GLOBAL_OpaqueHandlersCount++; memcpy(GLOBAL_OpaqueHandlers + i, f, sizeof(YAP_opaque_handler_t)); - return i + USER_BLOB_START; + return i; } X_API Term YAP_NewOpaqueObject(YAP_opaque_tag_t blob_tag, size_t bytes) { CELL *pt; Term t = Yap_AllocExternalDataInStack((CELL) blob_tag, bytes, &pt); - if (t == TermNil) - return 0L; + if (t == TermNil) + return 0L; + pt = RepAppl(t); blob_tag = pt[1]; if (blob_tag < USER_BLOB_START || blob_tag >= USER_BLOB_END) { Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag " UInt_FORMAT ,blob_tag); return FALSE; } - YAP_opaque_tag_t blob_info = blob_tag - USER_BLOB_START; + YAP_opaque_tag_t blob_info = blob_tag; if (GLOBAL_OpaqueHandlers[blob_info].cut_handler || GLOBAL_OpaqueHandlers[blob_info].fail_handler ) { *HR++ = t; diff --git a/H/TermExt.h b/H/TermExt.h index 8fe99d5e6..0f2669596 100755 --- a/H/TermExt.h +++ b/H/TermExt.h @@ -95,10 +95,10 @@ typedef enum { ARRAY_INT = 0x21, ARRAY_FLOAT = 0x22, CLAUSE_LIST = 0x40, - EXTERNAL_BLOB = 0x100, /* generic data */ - GOAL_CUT_POINT = 0x200, - USER_BLOB_START = 0x1000, /* user defined blob */ - USER_BLOB_END = 0x1100 /* end of user defined blob */ + EXTERNAL_BLOB = 0x0A0, /* generic data */ + GOAL_CUT_POINT = 0x0A1, + USER_BLOB_START = 0x0100, /* user defined blob */ + USER_BLOB_END = 0x0200 /* end of user defined blob */ } big_blob_type; INLINE_ONLY inline EXTERN blob_type BlobOfFunctor(Functor f);