Buffers

SB-Prolog supports the concept of buffers. A buffer is actually a constant and the characters that make up the buffer is the name of the constant. However, the symbol table entry for a buffer is not hashed and thus is not added to the obj-list, so two different buffers will never unify. Buffers can be allocated either in permanent space or on the heap. Buffers in permanent space stay there forever; buffers on the heap are deallocated when the ``allocate buffer'' goal is backtracked over.

A buffer allocated on the heap can either be a simple buffer, or it can be allocated as a subbuffer of another buffer already on the heap. A subbuffer will be deallocated when its superbuffer is deallocated.

There are occasions when it is not known, in advance, exactly how much space will be required and so how big a buffer should be allocated. Sometimes this problem can be overcome by allocating a large buffer and then, after using as much as is needed, returning the rest of the buffer to the system. This can be done, but only under very limited circumstances: a buffer is allocated from the end of the permanent space, the top of the heap, or from the next available space in the superbuffer; if no more space has been used beyond the end of the buffer, a tail portion of the buffer can be returned to the system. This operation is called ``trimming'' the buffer.

The following is a list of library predicates for buffer management:

alloc_perm(Size, Buff)
alloc_perm/2 (L) Allocates a buffer with a length Size in the permanent (i.e. program) area. Size must be bound to a number. On successful return, Buff will be bound to the allocated buffer. The buffer, being in the permanent area, is never de-allocated.

alloc_heap(Size, Buff)
alloc_heap/2 (L) Allocates a buffer of size Size on the heap and binds Buff to it. Since it is on the heap, it will be deallocated on backtracking.

trimbuff(Type, Buff, Newlen)
trimbuff/3 (L) This allows (in some very restricted circumstances) the changing of the size of a buffer. Type is 0 if the buffer is permanent, 1 if the buffer is on the heap. Buff is the buffer. Newlen is an integer: the size (which should be smaller than the original length of the buffer) to make the buffer. If the buffer is at the top of the heap (if heap buffer) or the end of the program area (if permanent) then the heap-top (or program area top) will be readjusted down. The length of the buffer will be modified to Newlen. This is (obviously) a very low-level primitive and is for hackers only to implement grungy stuff.

conlength(Constant,Length)
conlength/2 (L) Succeeds if the length of the print name of the constant Constant (which can be an atom, buffer or integer), in bytes, is Length. If Constant is a buffer, it is the length of the buffer; if Constant is an integer, it is the length of the decimal representation of that integer, i.e., the number of bytes that a $writename will use.