Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / external / squirrel / squirrel / squserdata.h
1 /*      see copyright notice in squirrel.h */
2 #ifndef _SQUSERDATA_H_
3 #define _SQUSERDATA_H_
4
5 struct SQUserData : SQDelegable
6 {
7         SQUserData(SQSharedState *ss){ _delegate = 0; _hook = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain, this); }
8         ~SQUserData()
9         {
10                 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain, this);
11                 SetDelegate(NULL);
12         }
13         static SQUserData* Create(SQSharedState *ss, SQInteger size)
14         {
15                 SQUserData* ud = (SQUserData*)SQ_MALLOC(sizeof(SQUserData)+(size-1));
16                 new (ud) SQUserData(ss);
17                 ud->_size = size;
18                 ud->_typetag = 0;
19                 return ud;
20         }
21 #ifndef NO_GARBAGE_COLLECTOR
22         void Mark(SQCollectable **chain);
23         void Finalize(){SetDelegate(NULL);}
24 #endif
25         void Release() {
26                 if (_hook) _hook(_val,_size);
27                 SQInteger tsize = _size - 1;
28                 this->~SQUserData();
29                 SQ_FREE(this, sizeof(SQUserData) + tsize);
30         }
31                 
32         SQInteger _size;
33         SQRELEASEHOOK _hook;
34         SQUserPointer _typetag;
35         SQChar _val[1];
36 };
37
38 #endif //_SQUSERDATA_H_