squserdata.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* see copyright notice in squirrel.h */
  2. #ifndef _SQUSERDATA_H_
  3. #define _SQUSERDATA_H_
  4. struct SQUserData : SQDelegable
  5. {
  6. SQUserData(SQSharedState *ss){ _delegate = 0; _hook = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain, this); }
  7. ~SQUserData()
  8. {
  9. REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain, this);
  10. SetDelegate(NULL);
  11. }
  12. static SQUserData* Create(SQSharedState *ss, SQInteger size)
  13. {
  14. SQUserData* ud = (SQUserData*)SQ_MALLOC(sq_aligning(sizeof(SQUserData))+size);
  15. new (ud) SQUserData(ss);
  16. ud->_size = size;
  17. ud->_typetag = 0;
  18. return ud;
  19. }
  20. #ifndef NO_GARBAGE_COLLECTOR
  21. void Mark(SQCollectable **chain);
  22. void Finalize(){SetDelegate(NULL);}
  23. SQObjectType GetType(){ return OT_USERDATA;}
  24. #endif
  25. void Release() {
  26. if (_hook) {
  27. SQDelayedReleseHook dh;
  28. dh.hook = _hook;
  29. dh.ptr = (SQUserPointer)sq_aligning(this + 1);
  30. dh.size = _size;
  31. _sharedstate->_delayed_release_hook.push_back(dh);
  32. //_hook((SQUserPointer)sq_aligning(this + 1),_size, 0);
  33. }
  34. SQInteger tsize = _size;
  35. this->~SQUserData();
  36. SQ_FREE(this, sq_aligning(sizeof(SQUserData)) + tsize);
  37. }
  38. SQInteger _size;
  39. SQRELEASEHOOK _hook;
  40. SQUserPointer _typetag;
  41. //SQChar _val[1];
  42. };
  43. #endif //_SQUSERDATA_H_