The Meta-Environment API

safio.c File Reference

#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 ATermgetProtectedMemoryBlock (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.


Define Documentation

#define ANNOSFLAG   0x00000010U
 

Definition at line 22 of file safio.c.

#define APPLQUOTED   0x00000020U
 

Definition at line 25 of file safio.c.

#define DEFAULTSHAREDSYMBOLARRAYSIZE   1024
 

Definition at line 36 of file safio.c.

#define DEFAULTSHAREDTERMARRAYSIZE   1024
 

Definition at line 34 of file safio.c.

#define DEFAULTSTACKSIZE   256
 

Definition at line 27 of file safio.c.

#define FUNSHARED   0x00000040U
 

Definition at line 24 of file safio.c.

#define inline
 

Definition at line 16 of file safio.c.

#define INTEGERSTOREBLOCKSINCREMENT   16
 

Definition at line 39 of file safio.c.

#define INTEGERSTOREBLOCKSINCREMENTMASK   0x0000000fU
 

Definition at line 40 of file safio.c.

#define INTEGERSTOREBLOCKSIZE   1024
 

Definition at line 41 of file safio.c.

#define INTEGERSTOREBLOCKSIZEBITS   10
 

Definition at line 43 of file safio.c.

#define INTEGERSTOREBLOCKSIZEMASK   0x000003ffU
 

Definition at line 42 of file safio.c.

#define ISSHAREDFLAG   0x00000080U
 

Definition at line 20 of file safio.c.

#define MINIMUMFREEBUFFERSPACE   10
 

Definition at line 30 of file safio.c.

#define PROTECTEDMEMORYSTACKBLOCKSINCREMENT   16
 

Definition at line 45 of file safio.c.

#define PROTECTEDMEMORYSTACKBLOCKSINCREMENTMASK   0x0000000fU
 

Definition at line 46 of file safio.c.

#define PROTECTEDMEMORYSTACKBLOCKSIZE   1024
 

Definition at line 47 of file safio.c.

#define SHAREDSYMBOLARRAYINCREMENT   2048
 

Definition at line 37 of file safio.c.

#define SHAREDTERMARRAYINCREMENT   2048
 

Definition at line 35 of file safio.c.

#define STACKSIZEINCREMENT   512
 

Definition at line 28 of file safio.c.

#define TEMPNAMEPAGESIZE   4096
 

Definition at line 32 of file safio.c.

#define TYPEMASK   0x0000000fU
 

Definition at line 21 of file safio.c.


Typedef Documentation

typedef struct _BufferNode BufferNode
 

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.


Function Documentation

BinaryReader ATcreateBinaryReader  ) 
 

Constructs a binary reader that can interpret a SAF stream.

Definition at line 1098 of file safio.c.

BinaryWriter ATcreateBinaryWriter ATerm  term  ) 
 

Constructs a binary writer that is responsible for serializing the given ATerm.

Definition at line 522 of file safio.c.

ByteBuffer ATcreateByteBuffer unsigned int  capacity  ) 
 

Creates a byte buffer, with the given capacity.

Definition at line 190 of file safio.c.

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.

Definition at line 1031 of file safio.c.

void ATdestroyBinaryReader BinaryReader  binaryReader  ) 
 

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).

Definition at line 1166 of file safio.c.

void ATdestroyBinaryWriter BinaryWriter  binaryWriter  ) 
 

Frees the memory associated with the given binary writer.

Definition at line 567 of file safio.c.

void ATdestroyByteBuffer ByteBuffer  byteBuffer  ) 
 

Frees the memory associated with the given byte buffer.

Definition at line 255 of file safio.c.

void ATflipByteBuffer ByteBuffer  byteBuffer  )  [inline]
 

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.

Definition at line 237 of file safio.c.

unsigned int ATgetRemainingBufferSpace ByteBuffer  byteBuffer  )  [inline]
 

Returns the amount of space left in the given byte buffer.

Definition at line 229 of file safio.c.

ATerm ATgetRoot BinaryReader  binaryReader  ) 
 

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.

Definition at line 1153 of file safio.c.

int ATisFinishedReading BinaryReader  binaryReader  ) 
 

Checks if the binary reader is done with the deserialization process.

Returns > 0 if true, 0 otherwise.

Definition at line 1145 of file safio.c.

int ATisFinishedWriting BinaryWriter  binaryWriter  ) 
 

Checks if the given binary writer is finished with the serialization process.

Returns > 0 is true, 0 otherwise.

Definition at line 557 of file safio.c.

ATerm ATreadFromNamedSAFFile const char *  filename  ) 
 

Interprets the content of the SAF file with the given name and returns the constructed ATerm.

Definition at line 1354 of file safio.c.

ATerm ATreadFromSAFFile FILE *  file  ) 
 

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)

Definition at line 1284 of file safio.c.

ATerm ATreadFromSAFString char *  data,
int  length
 

Interprets the given string in SAF format and returns the constructed ATerm.

Definition at line 1449 of file safio.c.

void ATresetByteBuffer ByteBuffer  byteBuffer  )  [inline]
 

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).

Definition at line 247 of file safio.c.

void ATserialize BinaryWriter  binaryWriter,
ByteBuffer  byteBuffer
 

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.

Definition at line 581 of file safio.c.

ByteBuffer ATwrapBuffer char *  buffer,
unsigned int  capacity
 

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.

Definition at line 213 of file safio.c.

ATbool ATwriteToNamedSAFFile ATerm  aTerm,
const char *  filename
 

Writes the given ATerm in SAF format to the file with the given name.

Definition at line 1259 of file safio.c.

ATbool ATwriteToSAFFile ATerm  aTerm,
FILE *  file
 

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).

Definition at line 1196 of file safio.c.

char* ATwriteToSAFString ATerm  aTerm,
int *  length
 

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.

Definition at line 1389 of file safio.c.

static ATerm buildTerm BinaryReader  binaryReader,
ATermConstruct parent
[static]
 

Constructs the term associated with the given ATerm construct.

Definition at line 719 of file safio.c.

static ProtectedMemoryStack createProtectedMemoryStack  )  [static]
 

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.

Definition at line 60 of file safio.c.

static void destroyProtectedMemoryStack ProtectedMemoryStack  protectedMemoryStack  )  [static]
 

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.

Definition at line 91 of file safio.c.

static void ensureReadSharedSymbolCapacity BinaryReader  binaryReader  )  [static]
 

Ensures that there is enough space left in the shared signatures array of the binary reader after teh invocation of this function.

Definition at line 683 of file safio.c.

static void ensureReadSharedTermCapacity BinaryReader  binaryReader  )  [static]
 

Ensures that there is enough space left in the shared terms array of the binary reader after the invocation of this function.

Definition at line 673 of file safio.c.

static void ensureReadStackCapacity BinaryReader  binaryReader  )  [static]
 

Ensures that there is enough space left on the stack of the binary reader after the invocation of this function.

Definition at line 663 of file safio.c.

static void ensureWriteStackCapacity BinaryWriter  binaryWriter  )  [static]
 

Ensures that there is enough space left on the stack of the binary writer after the invocation of this function.

Definition at line 300 of file safio.c.

static void expandProtectedMemoryStack ProtectedMemoryStack  protectedMemoryStack  )  [static]
 

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.

Definition at line 111 of file safio.c.

static unsigned int getHeader ATerm  aTerm  )  [inline, static]
 

Constructs the header for the given ATerm.

Definition at line 375 of file safio.c.

static ATerm getNextTerm BinaryWriter  binaryWriter  )  [static]
 

Returns a reference to the next ATerm that needs to be serialized to the stream the binary writer is working on.

Definition at line 310 of file safio.c.

static unsigned int getNrOfSubTerms ATerm  term  )  [inline, static]
 

Returns the number of subTerms the given term has.

Definition at line 284 of file safio.c.

static ATerm* getProtectedMemoryBlock ProtectedMemoryStack  protectedMemoryStack,
unsigned int  size
[static]
 

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.

Definition at line 144 of file safio.c.

static void linkTerm BinaryReader  binaryReader,
ATerm  aTerm
[static]
 

Links the given (deserialized) term with it's parent.

Definition at line 781 of file safio.c.

static void readData BinaryReader  binaryReader,
ByteBuffer  byteBuffer
[static]
 

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.

Definition at line 815 of file safio.c.

static double readDouble ByteBuffer  byteBuffer  )  [inline, static]
 

Reads a double from the given byte buffer.

The decoding will be done in 'byteencoding.c'.

Definition at line 653 of file safio.c.

static int readInt ByteBuffer  byteBuffer  )  [inline, static]
 

Reads an integer from the given byte buffer.

The decoding will be done in 'byteencoding.c'.

Definition at line 641 of file safio.c.

static void releaseProtectedMemoryBlock ProtectedMemoryStack  protectedMemoryStack,
ATerm ptr,
unsigned int  size
[static]
 

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.

Definition at line 168 of file safio.c.

static void resetTempReaderData BinaryReader  binaryReader  )  [inline, static]
 

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.

Definition at line 695 of file safio.c.

static void shareTerm BinaryReader  binaryReader,
ATermConstruct ac,
ATerm  term
[inline, static]
 

Adds the given term to the shared terms list.

Definition at line 712 of file safio.c.

static void touchAppl BinaryReader  binaryReader,
ByteBuffer  byteBuffer,
unsigned int  header
[static]
 

Starts the deserialization of an ATermAppl.

Definition at line 885 of file safio.c.

static void touchBlob BinaryReader  binaryReader,
ByteBuffer  byteBuffer
[static]
 

Starts the deserialization of an ATermBlob.

Definition at line 1004 of file safio.c.

static void touchInt BinaryReader  binaryReader,
ByteBuffer  byteBuffer
[static]
 

Starts the deserialization of an ATermInt.

Definition at line 962 of file safio.c.

static void touchList BinaryReader  binaryReader,
ByteBuffer  byteBuffer
[static]
 

Starts the deserialization of a ATermList.

Definition at line 942 of file safio.c.

static void touchPlaceholder BinaryReader  binaryReader  )  [static]
 

Starts the deserialization of a ATermPlaceholder.

Definition at line 1019 of file safio.c.

static void touchReal BinaryReader  binaryReader,
ByteBuffer  byteBuffer
[static]
 

Starts the deserialization of an ATermReal.

Definition at line 983 of file safio.c.

static void visitAppl BinaryWriter  binaryWriter,
ATermAppl  arg,
ByteBuffer  byteBuffer
[static]
 

Serializes the given ATermAppl.

Definition at line 385 of file safio.c.

static void visitBlob BinaryWriter  binaryWriter,
ATermBlob  arg,
ByteBuffer  byteBuffer
[static]
 

Serializes the given ATermBlob.

Definition at line 454 of file safio.c.

static void visitInt ATermInt  arg,
ByteBuffer  byteBuffer
[static]
 

Serializes the given ATermInt.

Definition at line 482 of file safio.c.

static void visitList ATermList  arg,
ByteBuffer  byteBuffer
[static]
 

Serializes the given ATermList.

Definition at line 492 of file safio.c.

static void visitPlaceholder ATermPlaceholder  arg,
ByteBuffer  byteBuffer
[static]
 

Serializes the given ATermPlaceholder.

Definition at line 502 of file safio.c.

static void visitReal ATermReal  arg,
ByteBuffer  byteBuffer
[static]
 

Serializes the given ATermReal.

Definition at line 512 of file safio.c.

static void writeDouble double  value,
ByteBuffer  byteBuffer
[inline, static]
 

Writes the given double to the byte buffer.

The encoding will be done in 'byteenconding.c'.

Definition at line 276 of file safio.c.

static void writeInt int  value,
ByteBuffer  byteBuffer
[inline, static]
 

Writes the given integer to the byte buffer.

The encoding will be done in 'byteenconding.c'.

Definition at line 268 of file safio.c.


Generated on Mon Sep 21 16:12:33 2009 for aterm by  doxygen 1.4.6