The Meta-Environment API
#include "safio.h"
#include "byteencoding.h"
#include "memory.h"
#include "encoding.h"
#include <string.h>
#include <stdlib.h>
Include dependency graph for safio.c:

Go to the source code of this file.
Data Structures | |
| struct | _BufferNode |
| This is a node we need to build a linked list of ByteBuffers while writing SAF to a string. More... | |
Defines | |
| #define | ANNOSFLAG 0x00000010U |
| #define | APPLQUOTED 0x00000020U |
| #define | DEFAULTSHAREDSYMBOLARRAYSIZE 1024 |
| #define | DEFAULTSHAREDTERMARRAYSIZE 1024 |
| #define | DEFAULTSTACKSIZE 256 |
| #define | FUNSHARED 0x00000040U |
| #define | inline |
| #define | INTEGERSTOREBLOCKSINCREMENT 16 |
| #define | INTEGERSTOREBLOCKSINCREMENTMASK 0x0000000fU |
| #define | INTEGERSTOREBLOCKSIZE 1024 |
| #define | INTEGERSTOREBLOCKSIZEBITS 10 |
| #define | INTEGERSTOREBLOCKSIZEMASK 0x000003ffU |
| #define | ISSHAREDFLAG 0x00000080U |
| #define | MINIMUMFREEBUFFERSPACE 10 |
| #define | PROTECTEDMEMORYSTACKBLOCKSINCREMENT 16 |
| #define | PROTECTEDMEMORYSTACKBLOCKSINCREMENTMASK 0x0000000fU |
| #define | PROTECTEDMEMORYSTACKBLOCKSIZE 1024 |
| #define | SHAREDSYMBOLARRAYINCREMENT 2048 |
| #define | SHAREDTERMARRAYINCREMENT 2048 |
| #define | STACKSIZEINCREMENT 512 |
| #define | TEMPNAMEPAGESIZE 4096 |
| #define | TYPEMASK 0x0000000fU |
Typedefs | |
| typedef _BufferNode | BufferNode |
| This is a node we need to build a linked list of ByteBuffers while writing SAF to a string. | |
Functions | |
| BinaryReader | ATcreateBinaryReader () |
| Constructs a binary reader that can interpret a SAF stream. | |
| BinaryWriter | ATcreateBinaryWriter (ATerm term) |
| Constructs a binary writer that is responsible for serializing the given ATerm. | |
| ByteBuffer | ATcreateByteBuffer (unsigned int capacity) |
| Creates a byte buffer, with the given capacity. | |
| void | ATdeserialize (BinaryReader binaryReader, ByteBuffer byteBuffer) |
| Continues the deserialization process, who's state is described in the binary reader with the data in the byte buffer. | |
| void | ATdestroyBinaryReader (BinaryReader binaryReader) |
| Frees the memory associated with the given binary reader. | |
| void | ATdestroyBinaryWriter (BinaryWriter binaryWriter) |
| Frees the memory associated with the given binary writer. | |
| void | ATdestroyByteBuffer (ByteBuffer byteBuffer) |
| Frees the memory associated with the given byte buffer. | |
| void | ATflipByteBuffer (ByteBuffer byteBuffer) |
| Flips the byte buffer. | |
| unsigned int | ATgetRemainingBufferSpace (ByteBuffer byteBuffer) |
| Returns the amount of space left in the given byte buffer. | |
| ATerm | ATgetRoot (BinaryReader binaryReader) |
| Returns the root of the deserialized tree, present in the given binary writer. | |
| int | ATisFinishedReading (BinaryReader binaryReader) |
| Checks if the binary reader is done with the deserialization process. | |
| int | ATisFinishedWriting (BinaryWriter binaryWriter) |
| Checks if the given binary writer is finished with the serialization process. | |
| ATerm | ATreadFromNamedSAFFile (const char *filename) |
| Interprets the content of the SAF file with the given name and returns the constructed ATerm. | |
| ATerm | ATreadFromSAFFile (FILE *file) |
| Interprets the content of the given SAF file and returns the constructed ATerm. | |
| ATerm | ATreadFromSAFString (char *data, int length) |
| Interprets the given string in SAF format and returns the constructed ATerm. | |
| void | ATresetByteBuffer (ByteBuffer byteBuffer) |
| Resets the given byte buffer. | |
| void | ATserialize (BinaryWriter binaryWriter, ByteBuffer byteBuffer) |
| Serializes the next part of the ATerm tree associated with the given binary writers to the byte buffer. | |
| ByteBuffer | ATwrapBuffer (char *buffer, unsigned int capacity) |
| Encapsulates the given string in a byte buffer. | |
| ATbool | ATwriteToNamedSAFFile (ATerm aTerm, const char *filename) |
| Writes the given ATerm in SAF format to the file with the given name. | |
| ATbool | ATwriteToSAFFile (ATerm aTerm, FILE *file) |
| Writes the given ATerm in SAF format to the given file. | |
| char * | ATwriteToSAFString (ATerm aTerm, int *length) |
| Writes the given ATerm to a string in SAF format. | |
| static ATerm | buildTerm (BinaryReader binaryReader, ATermConstruct *parent) |
| Constructs the term associated with the given ATerm construct. | |
| static ProtectedMemoryStack | createProtectedMemoryStack () |
| Creates a protected memory stack. | |
| static void | destroyProtectedMemoryStack (ProtectedMemoryStack protectedMemoryStack) |
| Frees the memory associated with the given protected memory stack. | |
| static void | ensureReadSharedSymbolCapacity (BinaryReader binaryReader) |
| Ensures that there is enough space left in the shared signatures array of the binary reader after teh invocation of this function. | |
| static void | ensureReadSharedTermCapacity (BinaryReader binaryReader) |
| Ensures that there is enough space left in the shared terms array of the binary reader after the invocation of this function. | |
| static void | ensureReadStackCapacity (BinaryReader binaryReader) |
| Ensures that there is enough space left on the stack of the binary reader after the invocation of this function. | |
| static void | ensureWriteStackCapacity (BinaryWriter binaryWriter) |
| Ensures that there is enough space left on the stack of the binary writer after the invocation of this function. | |
| static void | expandProtectedMemoryStack (ProtectedMemoryStack protectedMemoryStack) |
| Adds an additional block of memory to the given protected memory stack. | |
| static unsigned int | getHeader (ATerm aTerm) |
| Constructs the header for the given ATerm. | |
| static ATerm | getNextTerm (BinaryWriter binaryWriter) |
| Returns a reference to the next ATerm that needs to be serialized to the stream the binary writer is working on. | |
| static unsigned int | getNrOfSubTerms (ATerm term) |
| Returns the number of subTerms the given term has. | |
| static ATerm * | getProtectedMemoryBlock (ProtectedMemoryStack protectedMemoryStack, unsigned int size) |
| Returns an array of ATerms from the given protected memory stack. | |
| static void | linkTerm (BinaryReader binaryReader, ATerm aTerm) |
| Links the given (deserialized) term with it's parent. | |
| static void | readData (BinaryReader binaryReader, ByteBuffer byteBuffer) |
| Reads bytes from the byte buffer until we either run out of data or the name of the function symbol or BLOB we are reconstructing is complete. | |
| static double | readDouble (ByteBuffer byteBuffer) |
| Reads a double from the given byte buffer. | |
| static int | readInt (ByteBuffer byteBuffer) |
| Reads an integer from the given byte buffer. | |
| static void | releaseProtectedMemoryBlock (ProtectedMemoryStack protectedMemoryStack, ATerm *ptr, unsigned int size) |
| Releases a certain amount of bytes in the given protected memory stack. | |
| static void | resetTempReaderData (BinaryReader binaryReader) |
| Resets the temporary reader data of the given binary reader. | |
| static void | shareTerm (BinaryReader binaryReader, ATermConstruct *ac, ATerm term) |
| Adds the given term to the shared terms list. | |
| static void | touchAppl (BinaryReader binaryReader, ByteBuffer byteBuffer, unsigned int header) |
| Starts the deserialization of an ATermAppl. | |
| static void | touchBlob (BinaryReader binaryReader, ByteBuffer byteBuffer) |
| Starts the deserialization of an ATermBlob. | |
| static void | touchInt (BinaryReader binaryReader, ByteBuffer byteBuffer) |
| Starts the deserialization of an ATermInt. | |
| static void | touchList (BinaryReader binaryReader, ByteBuffer byteBuffer) |
| Starts the deserialization of a ATermList. | |
| static void | touchPlaceholder (BinaryReader binaryReader) |
| Starts the deserialization of a ATermPlaceholder. | |
| static void | touchReal (BinaryReader binaryReader, ByteBuffer byteBuffer) |
| Starts the deserialization of an ATermReal. | |
| static void | visitAppl (BinaryWriter binaryWriter, ATermAppl arg, ByteBuffer byteBuffer) |
| Serializes the given ATermAppl. | |
| static void | visitBlob (BinaryWriter binaryWriter, ATermBlob arg, ByteBuffer byteBuffer) |
| Serializes the given ATermBlob. | |
| static void | visitInt (ATermInt arg, ByteBuffer byteBuffer) |
| Serializes the given ATermInt. | |
| static void | visitList (ATermList arg, ByteBuffer byteBuffer) |
| Serializes the given ATermList. | |
| static void | visitPlaceholder (ATermPlaceholder arg, ByteBuffer byteBuffer) |
| Serializes the given ATermPlaceholder. | |
| static void | visitReal (ATermReal arg, ByteBuffer byteBuffer) |
| Serializes the given ATermReal. | |
| static void | writeDouble (double value, ByteBuffer byteBuffer) |
| Writes the given double to the byte buffer. | |
| static void | writeInt (int value, ByteBuffer byteBuffer) |
| Writes the given integer to the byte buffer. | |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This is a node we need to build a linked list of ByteBuffers while writing SAF to a string. By using this strategy, we avoid the unnecessary reallocation of a temporary buffer. |
|
|
Constructs a binary reader that can interpret a SAF stream.
|
|
|
Constructs a binary writer that is responsible for serializing the given ATerm.
|
|
|
Creates a byte buffer, with the given capacity.
|
|
||||||||||||
|
Continues the deserialization process, who's state is described in the binary reader with the data in the byte buffer.
|
|
|
Frees the memory associated with the given binary reader. NOTE: Calling this function with a binary reader that has started, but not completed, a deserialization process as argument has undefined behavior (protected zones will not be unprotected or freed). |
|
|
Frees the memory associated with the given binary writer.
|
|
|
Frees the memory associated with the given byte buffer.
|
|
|
Flips the byte buffer. Setting the limit to the amount of bytes that are currently present in the buffer and setting the current position to the start of the buffer. |
|
|
Returns the amount of space left in the given byte buffer.
|
|
|
Returns the root of the deserialized tree, present in the given binary writer. NOTE: If the deserialization process is incomplete a warning will be issued and NULL returned. |
|
|
Checks if the binary reader is done with the deserialization process. Returns > 0 if true, 0 otherwise. |
|
|
Checks if the given binary writer is finished with the serialization process. Returns > 0 is true, 0 otherwise. |
|
|
Interprets the content of the SAF file with the given name and returns the constructed ATerm.
|
|
|
Interprets the content of the given SAF file and returns the constructed ATerm. NOTE: The given file must be opened in binary mode (at least on Win32 this is required) |
|
||||||||||||
|
Interprets the given string in SAF format and returns the constructed ATerm.
|
|
|
Resets the given byte buffer. The current position will be reset to the start of the buffer and the limit will be set to the capacity. NOTE: Keep in mind that the data in the buffer will not be erased (not set to 0). |
|
||||||||||||
|
Serializes the next part of the ATerm tree associated with the given binary writers to the byte buffer. This function returns when the byte buffer is full; the buffer will be flipped before returning. |
|
||||||||||||
|
Encapsulates the given string in a byte buffer. The position will be set at the start and the limit to the capacity. NOTE: The given string MUST have been allocated using malloc, so it can be passed to free() when destroying this bytebuffer. Alternatively you could set the buffer field to NULL, before passing this buffer to the ATdestroyByteBuffer function and handle the freeing of the buffer manually. |
|
||||||||||||
|
Writes the given ATerm in SAF format to the file with the given name.
|
|
||||||||||||
|
Writes the given ATerm in SAF format to the given file. NOTE: The given file must be opened in binary mode (at least on Win32 this is required). |
|
||||||||||||
|
Writes the given ATerm to a string in SAF format. Since the string will contain bytes, the value the length parameter has after this function returns will specify the number of bytes that were written. Note that the resulting string has been malloced and will need to be freed by the user. |
|
||||||||||||
|
Constructs the term associated with the given ATerm construct.
|
|
|
Creates a protected memory stack. The memory this store holds will be returned in the order it was requested (last out, first in). Additionally, all the blocks that are allocated by this store are protected, so all ATerms that are referenced from inside one of the memory blocks this store holds won't be collected before it is destroyed. Because all memory is pre-allocated, so we only need to increment and decrement a pointer for allocating and freeing memory, which is WAY faster then calling malloc + free and ATprotect + ATunprotect for every few term pointers we need. |
|
|
Frees the memory associated with the given protected memory stack. Additionally the protection of the memory blocks that are present in the store will be removed. All references that point to data in this store will be invalid after invoking this method. |
|
|
Ensures that there is enough space left in the shared signatures array of the binary reader after teh invocation of this function.
|
|
|
Ensures that there is enough space left in the shared terms array of the binary reader after the invocation of this function.
|
|
|
Ensures that there is enough space left on the stack of the binary reader after the invocation of this function.
|
|
|
Ensures that there is enough space left on the stack of the binary writer after the invocation of this function.
|
|
|
Adds an additional block of memory to the given protected memory stack. A previously allocated block will be reused if possible; otherwise a new block of memory is allocated. |
|
|
Constructs the header for the given ATerm.
|
|
|
Returns a reference to the next ATerm that needs to be serialized to the stream the binary writer is working on.
|
|
|
Returns the number of subTerms the given term has.
|
|
||||||||||||
|
Returns an array of ATerms from the given protected memory stack. NOTE: If we request a ATerm array that is larger then PROTECTEDMEMORYSTACKBLOCKSIZE, fall back to using malloc. |
|
||||||||||||
|
Links the given (deserialized) term with it's parent.
|
|
||||||||||||
|
Reads bytes from the byte buffer until we either run out of data or the name of the function symbol or BLOB we are reconstructing is complete.
|
|
|
Reads a double from the given byte buffer. The decoding will be done in 'byteencoding.c'. |
|
|
Reads an integer from the given byte buffer. The decoding will be done in 'byteencoding.c'. |
|
||||||||||||||||
|
Releases a certain amount of bytes in the given protected memory stack. NOTE: releasing memory will not 'free' it, but it will make it possible to reuse it. For this reason the protected memory stack will never shrink, but will always remain as large as it was at it's maximum size. This is intended by design and will never cause a problem. NOTE2: If we want to release an ATerm array that is larger then PROTECTEDMEMORYSTACKBLOCKSIZE, it was allocated using malloc and WILL be freed. |
|
|
Resets the temporary reader data of the given binary reader. This temporary data is used when reading 'chunkified types' (name of a function symbol or BLOB); These types can be deserialized in pieces. |
|
||||||||||||||||
|
Adds the given term to the shared terms list.
|
|
||||||||||||||||
|
Starts the deserialization of an ATermAppl.
|
|
||||||||||||
|
Starts the deserialization of an ATermBlob.
|
|
||||||||||||
|
Starts the deserialization of an ATermInt.
|
|
||||||||||||
|
Starts the deserialization of a ATermList.
|
|
|
Starts the deserialization of a ATermPlaceholder.
|
|
||||||||||||
|
Starts the deserialization of an ATermReal.
|
|
||||||||||||||||
|
Serializes the given ATermAppl.
|
|
||||||||||||||||
|
Serializes the given ATermBlob.
|
|
||||||||||||
|
Serializes the given ATermInt.
|
|
||||||||||||
|
Serializes the given ATermList.
|
|
||||||||||||
|
Serializes the given ATermPlaceholder.
|
|
||||||||||||
|
Serializes the given ATermReal.
|
|
||||||||||||
|
Writes the given double to the byte buffer. The encoding will be done in 'byteenconding.c'. |
|
||||||||||||
|
Writes the given integer to the byte buffer. The encoding will be done in 'byteenconding.c'. |
1.4.6