]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/data.h
From Benoit Laniel: replace SG threading constructs with those from OpenThreads
[simgear.git] / simgear / nasal / data.h
1 #ifndef _DATA_H
2 #define _DATA_H
3
4 #include "nasal.h"
5
6 #if defined(NASAL_NAN64)
7
8 // On 64 bit systems, Nasal non-numeric references are stored with a
9 // bitmask that sets the top 16 bits.  As a double, this is a
10 // signalling NaN that cannot itself be produced by normal numerics
11 // code.  The pointer value can be reconstructed if (and only if) we
12 // are guaranteed that all memory that can be pointed to by a naRef
13 // (i.e. all memory returned by naAlloc) lives in the bottom 48 bits
14 // of memory.  Linux on x86_64, Win64, Solaris and Irix all have such
15 // policies with address spaces:
16 //
17 // http://msdn.microsoft.com/library/en-us/win64/win64/virtual_address_space.asp
18 // http://docs.sun.com/app/docs/doc/816-5138/6mba6ua5p?a=view
19 // http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi/
20 //  ...   0650/bks/SGI_Developer/books/T_IRIX_Prog/sgi_html/ch01.html
21 //
22 // In the above, MS guarantees 44 bits of process address space, SGI
23 // 40, and Sun 43 (Solaris *does* place the stack in the "negative"
24 // address space at 0xffff..., but we don't care as naRefs will never
25 // point there).  Linux doesn't document this rigorously, but testing
26 // shows that it allows 47 bits of address space (and current x86_64
27 // implementations are limited to 48 bits of virtual space anyway). So
28 // we choose 48 as the conservative compromise.
29
30 #define REFMAGIC ((1UL<<48) - 1)
31
32 #define _ULP(r) ((unsigned long long)((r).ptr))
33 #define REFPTR(r) (_ULP(r) & REFMAGIC)
34 #define IS_REF(r) ((_ULP(r) & ~REFMAGIC) == ~REFMAGIC)
35
36 // Portability note: this cast from a pointer type to naPtr (a union)
37 // is not defined in ISO C, it's a GCC extention that doesn't work on
38 // (at least) either the SUNWspro or MSVC compilers.  Unfortunately,
39 // fixing this would require abandoning the naPtr union for a set of
40 // PTR_<type>() macros, which is a ton of work and a lot of extra
41 // code.  And as all enabled 64 bit platforms are gcc anyway, and the
42 // 32 bit fallback code works in any case, this is acceptable for now.
43 #define PTR(r) ((naPtr)((struct naObj*)(_ULP(r) & REFMAGIC)))
44
45 #define SETPTR(r, p) ((r).ptr = (void*)((unsigned long long)p | ~REFMAGIC))
46 #define SETNUM(r, n) ((r).num = n)
47
48 #else
49
50 // On 32 bit systems where the pointer is half the width of the
51 // double, we store a special magic number in the structure to make
52 // the double a NaN.  This must appear in the top bits of the double,
53 // which is why the structure layout is endianness-dependent.
54
55 #define NASAL_REFTAG 0x7ff56789 // == 2,146,789,257 decimal
56 #define IS_REF(r) ((r).ref.reftag == NASAL_REFTAG)
57 #define PTR(r) ((r).ref.ptr)
58
59 #define SETPTR(r, p) ((r).ref.ptr.obj = (void*)p, (r).ref.reftag = NASAL_REFTAG)
60 #define SETNUM(r, n) ((r).ref.reftag = ~NASAL_REFTAG, (r).num = n)
61
62 #endif /* platform stuff */
63
64 enum { T_STR, T_VEC, T_HASH, T_CODE, T_FUNC, T_CCODE, T_GHOST,
65        NUM_NASAL_TYPES }; // V. important that this come last!
66
67 #define IS_NUM(r) (!IS_REF(r))
68 #define IS_OBJ(r) (IS_REF(r) && PTR(r).obj != 0)
69 #define IS_NIL(r) (IS_REF(r) && PTR(r).obj == 0)
70 #define IS_STR(r) (IS_OBJ(r) && PTR(r).obj->type == T_STR)
71 #define IS_VEC(r) (IS_OBJ(r) && PTR(r).obj->type == T_VEC)
72 #define IS_HASH(r) (IS_OBJ(r) && PTR(r).obj->type == T_HASH)
73 #define IS_CODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CODE)
74 #define IS_FUNC(r) (IS_OBJ(r) && PTR(r).obj->type == T_FUNC)
75 #define IS_CCODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CCODE)
76 #define IS_GHOST(r) (IS_OBJ(r) && PTR(r).obj->type == T_GHOST)
77 #define IS_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
78 #define IS_SCALAR(r) (IS_NUM(r) || IS_STR(r))
79 #define IDENTICAL(a, b) (IS_REF(a) && IS_REF(b) \
80                          && PTR(a).obj == PTR(b).obj)
81
82 #define MUTABLE(r) (IS_STR(r) && PTR(r).str->hashcode == 0)
83
84 // This is a macro instead of a separate struct to allow compilers to
85 // avoid padding.  GCC on x86, at least, will always pad the size of
86 // an embedded struct up to 32 bits.  Doing it this way allows the
87 // implementing objects to pack in 16 bits worth of data "for free".
88 #define GC_HEADER \
89     unsigned char mark; \
90     unsigned char type
91
92 struct naObj {
93     GC_HEADER;
94 };
95
96 struct naStr {
97     GC_HEADER;
98     int len;
99     unsigned char* data;
100     unsigned int hashcode;
101 };
102
103 struct VecRec {
104     int size;
105     int alloced;
106     naRef array[];
107 };
108
109 struct naVec {
110     GC_HEADER;
111     struct VecRec* rec;
112 };
113
114 struct HashNode {
115     naRef key;
116     naRef val;
117     struct HashNode* next;
118 };
119
120 struct HashRec {
121     int size;
122     int dels;
123     int lgalloced;
124     struct HashNode* nodes;
125     struct HashNode* table[];
126 };
127
128 struct naHash {
129     GC_HEADER;
130     struct HashRec* rec;
131 };
132
133 struct naCode {
134     GC_HEADER;
135     unsigned char nArgs;
136     unsigned char nOptArgs;
137     unsigned char needArgVector;
138     unsigned short nConstants;
139     unsigned short nLines;
140     unsigned short codesz;
141     unsigned short* byteCode;
142     naRef* constants;
143     int* argSyms; // indices into constants
144     int* optArgSyms;
145     int* optArgVals;
146     unsigned short* lineIps; // pairs of {ip, line}
147     naRef srcFile;
148     naRef restArgSym; // The "..." vector name, defaults to "arg"
149 };
150
151 struct naFunc {
152     GC_HEADER;
153     naRef code;
154     naRef namespace;
155     naRef next; // parent closure
156 };
157
158 struct naCCode {
159     GC_HEADER;
160     naCFunction fptr;
161 };
162
163 struct naGhost {
164     GC_HEADER;
165     naGhostType* gtype;
166     void* ptr;
167 };
168
169 struct naPool {
170     int           type;
171     int           elemsz;
172     struct Block* blocks;
173     void**   free0; // pointer to the alloced buffer
174     int     freesz; // size of the alloced buffer
175     void**    free; // current "free frame"
176     int      nfree; // down-counting index within the free frame
177     int    freetop; // curr. top of the free list
178 };
179
180 void naFree(void* m);
181 void* naAlloc(int n);
182 void* naRealloc(void* buf, int sz);
183 void naBZero(void* m, int n);
184
185 int naTypeSize(int type);
186 naRef naObj(int type, struct naObj* o);
187 naRef naNew(naContext c, int type);
188 naRef naNewCode(naContext c);
189
190 int naStr_equal(naRef s1, naRef s2);
191 naRef naStr_fromnum(naRef dest, double num);
192 int naStr_numeric(naRef str);
193 int naStr_parsenum(char* str, int len, double* result);
194 int naStr_tonum(naRef str, double* out);
195 naRef naStr_buf(naRef str, int len);
196
197 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
198 int naHash_sym(struct naHash* h, struct naStr* sym, naRef* out);
199 void naHash_newsym(struct naHash* h, naRef* sym, naRef* val);
200
201 void naGC_init(struct naPool* p, int type);
202 struct naObj** naGC_get(struct naPool* p, int n, int* nout);
203 void naGC_swapfree(void** target, void* val);
204 void naGC_freedead();
205
206 void naStr_gcclean(struct naStr* s);
207 void naVec_gcclean(struct naVec* s);
208 void naHash_gcclean(struct naHash* s);
209
210 #endif // _DATA_H