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