Availability:C-language interface function
bool PL_get_pointer(term_t
+t, void **ptr)Together with PL_put_pointer()
and PL_unify_pointer(),
these functions allow representing a C pointer as a Prolog integer. PL_put_pointer()
and PL_unify_pointer()
create a pointer integer: an integer that behaves as any other
integer, but is a different term than the plain integer with the same
value: the two do not unify and
compare/3
orders the pointer integer directly after the plain integer. Pointer
integers survive assert/1,
recorded databases,
findall/3, copy_term/2,
tries and tables. They become plain integers if they are written and
read, serialized with fast_term_serialized/2
or PL_record_external(),
saved in a .qlf file or used in arithmetic. PL_get_pointer()
only succeeds on a pointer integer, so a plain integer typed by the user
or computed using arithmetic is not taken for a pointer. A pointer must
fit in a small integer (max_tagged_integer);
otherwise PL_put_pointer()
and
PL_unify_pointer()
raise a representation error. The exception is 64-bit Android, which
keeps a tag in the top byte of heap pointers: there such pointers are
represented as plain integers and
PL_get_pointer()
accepts integers that are too large to be small. Representing C pointers
as integers is unsafe: the pointer may be used after the object
it points at is deallocated. The
blob API described in section
12.4.10 provides a safe way for handling foreign resources that
cooperates with Prolog garbage collection.