1 martin 1.21 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.22 //
|
3 martin 1.21 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
|
9 martin 1.22 //
|
10 martin 1.21 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
16 martin 1.22 //
|
17 martin 1.21 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.22 //
|
20 martin 1.21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 martin 1.22 //
|
28 martin 1.21 //////////////////////////////////////////////////////////////////////////
|
29 kumpf 1.1 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_ArrayRep_h
33 #define Pegasus_ArrayRep_h
34
35 #include <new>
36 #include <Pegasus/Common/Config.h>
37 #include <Pegasus/Common/Memory.h>
|
38 mike 1.11 #include <Pegasus/Common/AtomicInt.h>
39 #include <Pegasus/Common/Linkage.h>
40
41 #define Array_rep (static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep))
42 #define Array_size (Array_rep)->size
43 #define Array_data (Array_rep)->data()
44 #define Array_capacity (Array_rep)->capacity
45 #define Array_refs (Array_rep)->refs
|
46 kumpf 1.1
47 PEGASUS_NAMESPACE_BEGIN
48
|
49 mike 1.11 struct PEGASUS_COMMON_LINKAGE ArrayRepBase
50 {
51 // We put this first to avoid gaps in this structure. Some compilers may
52 // align it on a large boundary.
53 AtomicInt refs;
54
55 Uint32 size;
56
57 union
58 {
|
59 kumpf 1.17 Uint32 capacity;
60 Uint64 alignment;
|
61 mike 1.11 };
62
63 // Called only only _empty_rep object. We set the reference count to
64 // two and it will remain two for the lifetime of the process. Anything
65 // other than one will do. If the _empty_rep.refs were one, an Array
66 // object may think it is the sole owner and attempt to modify it.
67
68 ArrayRepBase() : refs(2), size(0), capacity(0) { }
69
70 static ArrayRepBase _empty_rep;
71 };
72
|
73 kumpf 1.1 /* ArrayRep<T>
74 The ArrayRep object represents the array size, capacity,
75 and elements in one contiguous chunk of memory. The elements
76 follow immediately after the end of the ArrayRep structure in memory.
77 The union is used to force 64 bit alignment of these elements. This is
78 a private class and should not be accessed directly by the user.
79 */
80 template<class T>
|
81 mike 1.11 struct ArrayRep : public ArrayRepBase
|
82 kumpf 1.1 {
83 // Obtains a pointer to the first element in the array.
84 T* data() { return (T*)(void*)(this + 1); }
85
86 // Same as method above but returns a constant pointer.
87 const T* data() const { return (const T*)(void*)(this + 1); }
88
|
89 mike 1.11 /* Make a new copy */
90 static ArrayRep<T>* copy_on_write(ArrayRep<T>* rep);
|
91 kumpf 1.1
92 /* Create and initialize a ArrayRep instance. Note that the
93 memory for the elements is unitialized and must be initialized by
94 the caller.
95 */
|
96 marek 1.16 static ArrayRep<T>* alloc(Uint32 size);
|
97 mike 1.11
98 static void ref(const ArrayRep<T>* rep);
|
99 kumpf 1.1
|
100 mike 1.11 static void unref(const ArrayRep<T>* rep);
|
101 kumpf 1.1 };
102
103 template<class T>
|
104 marek 1.16 ArrayRep<T>* ArrayRep<T>::alloc(Uint32 size)
|
105 kumpf 1.1 {
|
106 mike 1.11 // ATTN-MEB: throw out raising to next power of two (put this
107 // logic in reserveCapacity().
108
109 if (!size)
|
110 kumpf 1.17 return (ArrayRep<T>*)&ArrayRepBase::_empty_rep;
|
111 kumpf 1.1
112 // Calculate capacity (size rounded to the next power of two).
113
|
114 keith.petley 1.4 Uint32 initialCapacity = 8;
|
115 kumpf 1.1
|
116 kumpf 1.5 while ((initialCapacity != 0) && (initialCapacity < size))
117 {
|
118 keith.petley 1.4 initialCapacity <<= 1;
|
119 kumpf 1.5 }
120
121 // Test for Uint32 overflow in the capacity
122 if (initialCapacity == 0)
123 {
124 initialCapacity = size;
125 }
126
127 // Test for Uint32 overflow in the memory allocation size
|
128 dave.sudlik 1.15 // throw a bad_alloc exception if overflow would occur.
|
129 kumpf 1.5 if (initialCapacity > (Uint32(0xffffffff)-sizeof(ArrayRep<T>))/sizeof(T))
130 {
|
131 dave.sudlik 1.15 throw PEGASUS_STD(bad_alloc)();
|
132 kumpf 1.5 }
|
133 kumpf 1.1
134 // Create object:
135
|
136 kumpf 1.5 ArrayRep<T>* rep = (ArrayRep<T>*)operator new(
137 sizeof(ArrayRep<T>) + sizeof(T) * initialCapacity);
|
138 david 1.10
|
139 kumpf 1.1 rep->size = size;
|
140 keith.petley 1.4 rep->capacity = initialCapacity;
|
141 mike 1.11 new(&rep->refs) AtomicInt(1);
|
142 kumpf 1.1
143 return rep;
144 }
145
146 template<class T>
|
147 mike 1.11 inline void ArrayRep<T>::ref(const ArrayRep<T>* rep)
|
148 kumpf 1.1 {
|
149 mike 1.12 if ((void*)rep != (void*)&ArrayRepBase::_empty_rep)
|
150 kumpf 1.17 ((ArrayRep<T>*)rep)->refs.inc();
|
151 mike 1.11 }
152
153 template<class T>
154 inline void ArrayRep<T>::unref(const ArrayRep<T>* rep_)
155 {
156 ArrayRep<T>* rep = (ArrayRep<T>*)rep_;
157
158 if (rep != &ArrayRepBase::_empty_rep && rep->refs.decAndTestIfZero())
|
159 kumpf 1.1 {
160 Destroy(rep->data(), rep->size);
|
161 kumpf 1.17 rep->refs.~AtomicInt();
162 ::operator delete(rep);
|
163 mike 1.11 }
164 }
|
165 david 1.10
|
166 mike 1.11 template<class T>
167 ArrayRep<T>* ArrayRep<T>::copy_on_write(ArrayRep<T>* rep)
168 {
169 ArrayRep<T>* new_rep = ArrayRep<T>::alloc(rep->size);
170 new_rep->size = rep->size;
171 CopyToRaw(new_rep->data(), rep->data(), rep->size);
172 unref(rep);
173 return new_rep;
|
174 kumpf 1.1 }
175
176 PEGASUS_NAMESPACE_END
177
178 #endif /* Pegasus_ArrayRep_h */
|