12.4.8.1 Defining a BLOB type
The type PL_blob_t
represents a structure with the
layout displayed below. The structure contains additional fields at the
... for internal bookkeeping as well as future extensions.
typedef struct PL_blob_t { uintptr_t magic; /* PL_BLOB_MAGIC */ uintptr_t flags; /* Bitwise or of PL_BLOB_* */ char * name; /* name of the type */ int (*release)(atom_t a); int (*compare)(atom_t a, atom_t b); int (*write)(IOSTREAM *s, atom_t a, int flags); void (*acquire)(atom_t a); ... } PL_blob_t;
For each type, exactly one such structure should be allocated. Its
first field must be initialised to PL_BLOB_MAGIC
. The
flags is a bitwise or of the following constants:
- PL_BLOB_TEXT
- If specified the blob is assumed to contain text and is considered a normal Prolog atom.
- PL_BLOB_UNIQUE
- If specified the system ensures that the blob-handle is a unique reference for a blob with the given type, length and content. If this flag is not specified, each lookup creates a new blob.
- PL_BLOB_NOCOPY
- By default the content of the blob is copied. Using this flag the blob
references the external data directly. The user must ensure the provided
pointer is valid as long as the atom lives. If
PL_BLOB_UNIQUE
is also specified, uniqueness is determined by comparing the pointer rather than the data pointed at.
The name field represents the type name as available to
Prolog. See also current_blob/2.
The other fields are function pointers that must be initialised to
proper functions or NULL
to get the default behaviour of
built-in atoms. Below are the defined member functions:
- void acquire(atom_t a)
- Called if a new blob of this type is created through PL_put_blob() or PL_unify_blob(). This callback may be used together with the release hook to deal with reference-counted external objects.
- int release(atom_t a)
- The blob (atom) a is about to be released. This function can
retrieve the data of the blob using PL_blob_data().
If it returns
FALSE
the atom garbage collector will not reclaim the atom. - int compare(atom_t a, atom_t b)
- Compare the blobs a and b, both of which are of the type associated to this blob type. Return values are, as memcmp(), < 0 if a is less than b, = 0 if both are equal, and > 0 otherwise.
- int write(IOSTREAM *s, atom_t a, int flags)
- Write the content of the blob a to the stream s
respecting the flags. The flags are a bitwise
or of zero or more of the
PL_WRT_*
flags defined inSWI-Prolog.h
. This prototype is available if the undocumentedSWI-Stream.h
is included beforeSWI-Prolog.h
.If this function is not provided, write/1 emits the content of the blob for blobs of type
PL_BLOB_TEXT
or a string of the format<#
hex data>
for binary blobs.
If a blob type is registered from a loadable object (shared object or DLL) the blob type must be deregistered before the object may be released.
- int PL_unregister_blob_type(PL_blob_t *type)
- Unlink the blob type from the registered type and transform the type of
possible living blobs to
unregistered
, avoiding further reference to the type structure, functions referred by it, as well as the data. This function returnsTRUE
if no blobs of this type existed andFALSE
otherwise. PL_unregister_blob_type() is intended for the uninstall() hook of foreign modules, avoiding further references to the module.