opaque terms

This commit is contained in:
Vitor Santos Costa 2017-11-08 10:26:51 +01:00
parent 7cd3fffe13
commit 9100e797f8
3 changed files with 18 additions and 16 deletions

View File

@ -167,7 +167,7 @@ int Yap_CleanOpaqueVariable(CELL d) {
"clean opaque: bad blob with tag " UInt_FORMAT, blob_tag); "clean opaque: bad blob with tag " UInt_FORMAT, blob_tag);
return FALSE; return FALSE;
} }
blob_info = blob_tag - USER_BLOB_START; blob_info = blob_tag;
if (!GLOBAL_OpaqueHandlers) if (!GLOBAL_OpaqueHandlers)
return false; return false;
if (!GLOBAL_OpaqueHandlers[blob_info].fail_handler) 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); "clean opaque: bad blob with tag " UInt_FORMAT, blob_tag);
return FALSE; return FALSE;
} }
blob_info = blob_tag - USER_BLOB_START; blob_info = blob_tag;
if (!GLOBAL_OpaqueHandlers) { if (!GLOBAL_OpaqueHandlers) {
return NULL; 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) { if (blob_tag < USER_BLOB_START || blob_tag >= USER_BLOB_END) {
return NULL; return NULL;
} }
blob_info = blob_tag - USER_BLOB_START; blob_info = blob_tag;
if (!GLOBAL_OpaqueHandlers) if (!GLOBAL_OpaqueHandlers)
return NULL; return NULL;
return GLOBAL_OpaqueHandlers[blob_info].mark_handler; 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); "clean opaque: bad blob with tag " UInt_FORMAT, blob_tag);
return FALSE; return FALSE;
} }
blob_info = blob_tag - USER_BLOB_START; blob_info = blob_tag;
if (!GLOBAL_OpaqueHandlers) if (!GLOBAL_OpaqueHandlers)
return NULL; return NULL;
return GLOBAL_OpaqueHandlers[blob_info].relocate_handler; return GLOBAL_OpaqueHandlers[blob_info].relocate_handler;

View File

@ -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) { X_API YAP_opaque_tag_t YAP_NewOpaqueType(struct YAP_opaque_handler_struct *f) {
int i; int i;
if (!GLOBAL_OpaqueHandlers) { if (!GLOBAL_OpaqueHandlersCount) {
GLOBAL_OpaqueHandlers = 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) { if (!GLOBAL_OpaqueHandlers) {
/* no room */ /* no room */
return -1; 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 */ /* all types used */
return -1; return -1;
} }
i = GLOBAL_OpaqueHandlersCount++; i = GLOBAL_OpaqueHandlersCount++;
memcpy(GLOBAL_OpaqueHandlers + i, f, sizeof(YAP_opaque_handler_t)); 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) { X_API Term YAP_NewOpaqueObject(YAP_opaque_tag_t blob_tag, size_t bytes) {
CELL *pt; CELL *pt;
Term t = Yap_AllocExternalDataInStack((CELL) blob_tag, bytes, &pt); Term t = Yap_AllocExternalDataInStack((CELL) blob_tag, bytes, &pt);
if (t == TermNil) if (t == TermNil)
return 0L; return 0L;
pt = RepAppl(t);
blob_tag = pt[1]; blob_tag = pt[1];
if (blob_tag < USER_BLOB_START || if (blob_tag < USER_BLOB_START ||
blob_tag >= USER_BLOB_END) { blob_tag >= USER_BLOB_END) {
Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag " UInt_FORMAT ,blob_tag); Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag " UInt_FORMAT ,blob_tag);
return FALSE; 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 || if (GLOBAL_OpaqueHandlers[blob_info].cut_handler ||
GLOBAL_OpaqueHandlers[blob_info].fail_handler ) { GLOBAL_OpaqueHandlers[blob_info].fail_handler ) {
*HR++ = t; *HR++ = t;

View File

@ -95,10 +95,10 @@ typedef enum {
ARRAY_INT = 0x21, ARRAY_INT = 0x21,
ARRAY_FLOAT = 0x22, ARRAY_FLOAT = 0x22,
CLAUSE_LIST = 0x40, CLAUSE_LIST = 0x40,
EXTERNAL_BLOB = 0x100, /* generic data */ EXTERNAL_BLOB = 0x0A0, /* generic data */
GOAL_CUT_POINT = 0x200, GOAL_CUT_POINT = 0x0A1,
USER_BLOB_START = 0x1000, /* user defined blob */ USER_BLOB_START = 0x0100, /* user defined blob */
USER_BLOB_END = 0x1100 /* end of user defined blob */ USER_BLOB_END = 0x0200 /* end of user defined blob */
} big_blob_type; } big_blob_type;
INLINE_ONLY inline EXTERN blob_type BlobOfFunctor(Functor f); INLINE_ONLY inline EXTERN blob_type BlobOfFunctor(Functor f);