as_globalproperty.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2015 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. #include "as_config.h"
  24. #include "as_property.h"
  25. #include "as_scriptengine.h"
  26. BEGIN_AS_NAMESPACE
  27. asCGlobalProperty::asCGlobalProperty()
  28. {
  29. memory = &storage;
  30. memoryAllocated = false;
  31. realAddress = 0;
  32. initFunc = 0;
  33. accessMask = 0xFFFFFFFF;
  34. refCount.set(1);
  35. }
  36. asCGlobalProperty::~asCGlobalProperty()
  37. {
  38. #ifndef WIP_16BYTE_ALIGNED
  39. if( memoryAllocated ) { asDELETEARRAY(memory); }
  40. #else
  41. if( memoryAllocated ) { asDELETEARRAYALIGNED(memory); }
  42. #endif
  43. if( initFunc )
  44. initFunc->ReleaseInternal();
  45. }
  46. void asCGlobalProperty::AddRef()
  47. {
  48. refCount.atomicInc();
  49. }
  50. void asCGlobalProperty::Release()
  51. {
  52. if( refCount.atomicDec() == 0 )
  53. asDELETE(this, asCGlobalProperty);
  54. }
  55. void asCGlobalProperty::DestroyInternal()
  56. {
  57. if( initFunc )
  58. {
  59. initFunc->ReleaseInternal();
  60. initFunc = 0;
  61. }
  62. }
  63. void *asCGlobalProperty::GetAddressOfValue()
  64. {
  65. return memory;
  66. }
  67. // The global property structure is responsible for allocating the storage
  68. // method for script declared variables. Each allocation is independent of
  69. // other global properties, so that variables can be added and removed at
  70. // any time.
  71. void asCGlobalProperty::AllocateMemory()
  72. {
  73. if( type.GetSizeOnStackDWords() > 2 )
  74. {
  75. #ifndef WIP_16BYTE_ALIGNED
  76. memory = asNEWARRAY(asDWORD, type.GetSizeOnStackDWords());
  77. #else
  78. // TODO: Avoid aligned allocation if not needed to reduce the waste of memory for the alignment
  79. memory = asNEWARRAYALIGNED(asDWORD, type.GetSizeOnStackDWords(), type.GetAlignment());
  80. #endif
  81. memoryAllocated = true;
  82. }
  83. }
  84. void asCGlobalProperty::SetRegisteredAddress(void *p)
  85. {
  86. realAddress = p;
  87. if( type.IsObject() && !type.IsReference() && !type.IsObjectHandle() )
  88. {
  89. // The global property is a pointer to a pointer
  90. memory = &realAddress;
  91. }
  92. else
  93. memory = p;
  94. }
  95. void *asCGlobalProperty::GetRegisteredAddress() const
  96. {
  97. return realAddress;
  98. }
  99. void asCGlobalProperty::SetInitFunc(asCScriptFunction *in_initFunc)
  100. {
  101. // This should only be done once
  102. asASSERT( initFunc == 0 );
  103. initFunc = in_initFunc;
  104. initFunc->AddRefInternal();
  105. }
  106. asCScriptFunction *asCGlobalProperty::GetInitFunc()
  107. {
  108. return initFunc;
  109. }
  110. END_AS_NAMESPACE