next up previous contents
Next: Integers and Big Integers Up: Strings Previous: Strings

Implementation

There is nothing much to say about string implementation. The corresponding structure holds a pointer to a string-element. The string-elements are allocated from a pre-allocated (either static or dynamic, depending on what the compiler flag DSTACK says) byte array. It is something very common that due to garbage collection some gaps may occur in this array. So the famous problem of fragmentation arises.To solve this problem a Compactification is performed on this byte array, when it is necessary. The back-pointer that one observes in the string-element serves for this purpose. The last field of a string-element is declared as you see to be a character array of size 254, which is of course nothing real. It will never be the case that such a string-element is created using the C systems variable creation mechanisms. This structure declaration will always be used as type casting. So a good controlled pointer that points to the first free position in the string-byte array (where all of the string elements live) will be type casted to be a STRELEMENT and will be advanced such an amount that the actual string will exactly fit into the place just following the back-pointer. So it will also be possible to index the actual string bytes through the field identifier (which was declared to be an array) Xrealstr, cute, isn't it?. For the IBMPC implementation the string-byte elements are casted with a structure that we named struct Xstrelmnt . But as this code was ported to other computers this casting proved itself to be non-portable. The reason was that some (non - 80x86) processors (like the 680xx, or the SPARC) did not accept pointer indirections if the pointer is sitting in the memory at an address that is not zero (modulo 4). For these cases the string-byte array is not type casted but the fields are accessed (harshly) by movmem calls. Please note that the underlying data-representation is exactly the same in both cases. If you have a (80x86) processor then you may turn on the IBMPC flag, so the fields will be accessed by type-casting and (hopefully) a faster code will be generated. Below is the picture for the IBMPC case. For the non IBMPC situation consider the second picture.

IBMPC case

     struct Xstring { char Xtype;
                      struct Xstrelmnt *Xstrbody; } ;

     struct Xstrelmnt { char Xlength;
                        STRING* Xbackpointer;
char Xrealstr[254]; } ;

figure258

non - IBMPC case

     struct Xstring { char Xtype;
                      char *Xstrelement; } ;

figure303


next up previous contents
Next: Integers and Big Integers Up: Strings Previous: Strings

Gokturk UCOLUK
Fri Nov 1 21:52:13 EET 1996