data.h

00001 #ifndef _DATA_H
00002 #define _DATA_H
00003 
00004 #include "nasal.h"
00005 
00006 #if defined(NASAL_NAN64)
00007 
00008 // On 64 bit systems, Nasal non-numeric references are stored with a
00009 // bitmask that sets the top 16 bits.  As a double, this is a
00010 // signalling NaN that cannot itself be produced by normal numerics
00011 // code.  The pointer value can be reconstructed if (and only if) we
00012 // are guaranteed that all memory that can be pointed to by a naRef
00013 // (i.e. all memory returned by naAlloc) lives in the bottom 48 bits
00014 // of memory.  Linux on x86_64, Win64, Solaris and Irix all have such
00015 // policies with address spaces:
00016 //
00017 // http://msdn.microsoft.com/library/en-us/win64/win64/virtual_address_space.asp
00018 // http://docs.sun.com/app/docs/doc/816-5138/6mba6ua5p?a=view
00019 // http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi/
00020 //  ...   0650/bks/SGI_Developer/books/T_IRIX_Prog/sgi_html/ch01.html
00021 //
00022 // In the above, MS guarantees 44 bits of process address space, SGI
00023 // 40, and Sun 43 (Solaris *does* place the stack in the "negative"
00024 // address space at 0xffff..., but we don't care as naRefs will never
00025 // point there).  Linux doesn't document this rigorously, but testing
00026 // shows that it allows 47 bits of address space (and current x86_64
00027 // implementations are limited to 48 bits of virtual space anyway). So
00028 // we choose 48 as the conservative compromise.
00029 
00030 #define REFMAGIC ((1UL<<48) - 1)
00031 
00032 #define _ULP(r) ((unsigned long long)((r).ptr))
00033 #define REFPTR(r) (_ULP(r) & REFMAGIC)
00034 #define IS_REF(r) ((_ULP(r) & ~REFMAGIC) == ~REFMAGIC)
00035 
00036 // Portability note: this cast from a pointer type to naPtr (a union)
00037 // is not defined in ISO C, it's a GCC extention that doesn't work on
00038 // (at least) either the SUNWspro or MSVC compilers.  Unfortunately,
00039 // fixing this would require abandoning the naPtr union for a set of
00040 // PTR_<type>() macros, which is a ton of work and a lot of extra
00041 // code.  And as all enabled 64 bit platforms are gcc anyway, and the
00042 // 32 bit fallback code works in any case, this is acceptable for now.
00043 #define PTR(r) ((naPtr)((struct naObj*)(_ULP(r) & REFMAGIC)))
00044 
00045 #define SETPTR(r, p) ((r).ptr = (void*)((unsigned long long)p | ~REFMAGIC))
00046 #define SETNUM(r, n) ((r).num = n)
00047 
00048 #else
00049 
00050 // On 32 bit systems where the pointer is half the width of the
00051 // double, we store a special magic number in the structure to make
00052 // the double a NaN.  This must appear in the top bits of the double,
00053 // which is why the structure layout is endianness-dependent.
00054 
00055 #define NASAL_REFTAG 0x7ff56789 // == 2,146,789,257 decimal
00056 #define IS_REF(r) ((r).ref.reftag == NASAL_REFTAG)
00057 #define PTR(r) ((r).ref.ptr)
00058 
00059 #define SETPTR(r, p) ((r).ref.ptr.obj = (void*)p, (r).ref.reftag = NASAL_REFTAG)
00060 #define SETNUM(r, n) ((r).ref.reftag = ~NASAL_REFTAG, (r).num = n)
00061 
00062 #endif /* platform stuff */
00063 
00064 enum { T_STR, T_VEC, T_HASH, T_CODE, T_FUNC, T_CCODE, T_GHOST,
00065        NUM_NASAL_TYPES }; // V. important that this come last!
00066 
00067 #define IS_NUM(r) (!IS_REF(r))
00068 #define IS_OBJ(r) (IS_REF(r) && PTR(r).obj != 0)
00069 #define IS_NIL(r) (IS_REF(r) && PTR(r).obj == 0)
00070 #define IS_STR(r) (IS_OBJ(r) && PTR(r).obj->type == T_STR)
00071 #define IS_VEC(r) (IS_OBJ(r) && PTR(r).obj->type == T_VEC)
00072 #define IS_HASH(r) (IS_OBJ(r) && PTR(r).obj->type == T_HASH)
00073 #define IS_CODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CODE)
00074 #define IS_FUNC(r) (IS_OBJ(r) && PTR(r).obj->type == T_FUNC)
00075 #define IS_CCODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CCODE)
00076 #define IS_GHOST(r) (IS_OBJ(r) && PTR(r).obj->type == T_GHOST)
00077 #define IS_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
00078 #define IS_SCALAR(r) (IS_NUM(r) || IS_STR(r))
00079 #define IDENTICAL(a, b) (IS_REF(a) && IS_REF(b) \
00080                          && PTR(a).obj == PTR(b).obj)
00081 
00082 #define MUTABLE(r) (IS_STR(r) && PTR(r).str->hashcode == 0)
00083 
00084 // This is a macro instead of a separate struct to allow compilers to
00085 // avoid padding.  GCC on x86, at least, will always pad the size of
00086 // an embedded struct up to 32 bits.  Doing it this way allows the
00087 // implementing objects to pack in 16 bits worth of data "for free".
00088 #define GC_HEADER \
00089     unsigned char mark; \
00090     unsigned char type
00091 
00092 struct naObj {
00093     GC_HEADER;
00094 };
00095 
00096 struct naStr {
00097     GC_HEADER;
00098     int len;
00099     unsigned char* data;
00100     unsigned int hashcode;
00101 };
00102 
00103 struct VecRec {
00104     int size;
00105     int alloced;
00106     naRef array[];
00107 };
00108 
00109 struct naVec {
00110     GC_HEADER;
00111     struct VecRec* rec;
00112 };
00113 
00114 struct HashNode {
00115     naRef key;
00116     naRef val;
00117     struct HashNode* next;
00118 };
00119 
00120 struct HashRec {
00121     int size;
00122     int dels;
00123     int lgalloced;
00124     struct HashNode* nodes;
00125     struct HashNode* table[];
00126 };
00127 
00128 struct naHash {
00129     GC_HEADER;
00130     struct HashRec* rec;
00131 };
00132 
00133 struct naCode {
00134     GC_HEADER;
00135     unsigned char nArgs;
00136     unsigned char nOptArgs;
00137     unsigned char needArgVector;
00138     unsigned short nConstants;
00139     unsigned short nLines;
00140     unsigned short codesz;
00141     unsigned short* byteCode;
00142     naRef* constants;
00143     int* argSyms; // indices into constants
00144     int* optArgSyms;
00145     int* optArgVals;
00146     unsigned short* lineIps; // pairs of {ip, line}
00147     naRef srcFile;
00148     naRef restArgSym; // The "..." vector name, defaults to "arg"
00149 };
00150 
00151 struct naFunc {
00152     GC_HEADER;
00153     naRef code;
00154     naRef namespace;
00155     naRef next; // parent closure
00156 };
00157 
00158 struct naCCode {
00159     GC_HEADER;
00160     naCFunction fptr;
00161 };
00162 
00163 struct naGhost {
00164     GC_HEADER;
00165     naGhostType* gtype;
00166     void* ptr;
00167 };
00168 
00169 struct naPool {
00170     int           type;
00171     int           elemsz;
00172     struct Block* blocks;
00173     void**   free0; // pointer to the alloced buffer
00174     int     freesz; // size of the alloced buffer
00175     void**    free; // current "free frame"
00176     int      nfree; // down-counting index within the free frame
00177     int    freetop; // curr. top of the free list
00178 };
00179 
00180 void naFree(void* m);
00181 void* naAlloc(int n);
00182 void* naRealloc(void* buf, int sz);
00183 void naBZero(void* m, int n);
00184 
00185 int naTypeSize(int type);
00186 naRef naObj(int type, struct naObj* o);
00187 naRef naNew(naContext c, int type);
00188 naRef naNewCode(naContext c);
00189 
00190 int naStr_equal(naRef s1, naRef s2);
00191 naRef naStr_fromnum(naRef dest, double num);
00192 int naStr_numeric(naRef str);
00193 int naStr_parsenum(char* str, int len, double* result);
00194 int naStr_tonum(naRef str, double* out);
00195 naRef naStr_buf(naRef str, int len);
00196 
00197 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
00198 int naHash_sym(struct naHash* h, struct naStr* sym, naRef* out);
00199 void naHash_newsym(struct naHash* h, naRef* sym, naRef* val);
00200 
00201 void naGC_init(struct naPool* p, int type);
00202 struct naObj** naGC_get(struct naPool* p, int n, int* nout);
00203 void naGC_swapfree(void** target, void* val);
00204 void naGC_freedead();
00205 
00206 void naStr_gcclean(struct naStr* s);
00207 void naVec_gcclean(struct naVec* s);
00208 void naHash_gcclean(struct naHash* s);
00209 
00210 #endif // _DATA_H

Generated on Mon Dec 17 09:30:54 2007 for SimGear by  doxygen 1.5.1