1 karl 1.14 //%2006////////////////////////////////////////////////////////////////////////
|
2 kumpf 1.1 //
|
3 karl 1.8 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.6 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.8 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.14 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 kumpf 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
15 kumpf 1.2 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
18 kumpf 1.1 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
|
21 kumpf 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
22 kumpf 1.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
24 kumpf 1.2 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
27 kumpf 1.1 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Mike Brasher (mbrasher@bmc.com)
33 //
34 // Modified By: Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
|
35 mike 1.11 // Mike Brasher, Inova Europe (mike-brasher@austin.rr.com)
|
36 kumpf 1.1 //
37 //%/////////////////////////////////////////////////////////////////////////////
38
39 #ifndef Pegasus_ArrayRep_h
40 #define Pegasus_ArrayRep_h
41
|
42 humberto 1.7 #ifdef PEGASUS_OS_OS400
43 #include <stdlib.h>
44 #endif
45
|
46 kumpf 1.1 #include <new>
47 #include <Pegasus/Common/Config.h>
48 #include <Pegasus/Common/Memory.h>
|
49 mike 1.11 #include <Pegasus/Common/AtomicInt.h>
50 #include <Pegasus/Common/Linkage.h>
51
52 #define Array_rep (static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep))
53 #define Array_size (Array_rep)->size
54 #define Array_data (Array_rep)->data()
55 #define Array_capacity (Array_rep)->capacity
56 #define Array_refs (Array_rep)->refs
|
57 kumpf 1.1
|
58 mike 1.13 // The OS400 platform uses this aligment directive to ensure that
59 // ArrayRepBase is aligned an a 16 byte boundary.
60 #ifdef PEGASUS_OS_OS400
61 # define PEGASUS_ALIGN16 __align(16)
62 #else
63 # define PEGASUS_ALIGN16 /* not necessary on other platforms */
64 #endif
65
|
66 kumpf 1.1 PEGASUS_NAMESPACE_BEGIN
67
|
68 mike 1.13 PEGASUS_ALIGN16
|
69 mike 1.11 struct PEGASUS_COMMON_LINKAGE ArrayRepBase
70 {
71 // We put this first to avoid gaps in this structure. Some compilers may
72 // align it on a large boundary.
73 AtomicInt refs;
74
75 Uint32 size;
76
77 union
78 {
79 Uint32 capacity;
80 Uint64 alignment;
81 };
82
83 // Called only only _empty_rep object. We set the reference count to
84 // two and it will remain two for the lifetime of the process. Anything
85 // other than one will do. If the _empty_rep.refs were one, an Array
86 // object may think it is the sole owner and attempt to modify it.
87
88 ArrayRepBase() : refs(2), size(0), capacity(0) { }
89
90 mike 1.11 static ArrayRepBase _empty_rep;
91 };
92
|
93 kumpf 1.1 /* ArrayRep<T>
94 The ArrayRep object represents the array size, capacity,
95 and elements in one contiguous chunk of memory. The elements
96 follow immediately after the end of the ArrayRep structure in memory.
97 The union is used to force 64 bit alignment of these elements. This is
98 a private class and should not be accessed directly by the user.
99 */
100 template<class T>
|
101 mike 1.11 struct ArrayRep : public ArrayRepBase
|
102 kumpf 1.1 {
103 // Obtains a pointer to the first element in the array.
104 T* data() { return (T*)(void*)(this + 1); }
105
106 // Same as method above but returns a constant pointer.
107 const T* data() const { return (const T*)(void*)(this + 1); }
108
|
109 mike 1.11 /* Make a new copy */
110 static ArrayRep<T>* copy_on_write(ArrayRep<T>* rep);
|
111 kumpf 1.1
112 /* Create and initialize a ArrayRep instance. Note that the
113 memory for the elements is unitialized and must be initialized by
114 the caller.
115 */
|
116 marek 1.16 static ArrayRep<T>* alloc(Uint32 size);
|
117 mike 1.11
118 static void ref(const ArrayRep<T>* rep);
|
119 kumpf 1.1
|
120 mike 1.11 static void unref(const ArrayRep<T>* rep);
|
121 kumpf 1.1 };
122
123 template<class T>
|
124 marek 1.16 ArrayRep<T>* ArrayRep<T>::alloc(Uint32 size)
|
125 kumpf 1.1 {
|
126 mike 1.11 // ATTN-MEB: throw out raising to next power of two (put this
127 // logic in reserveCapacity().
128
129 if (!size)
130 return (ArrayRep<T>*)&ArrayRepBase::_empty_rep;
|
131 kumpf 1.1
132 // Calculate capacity (size rounded to the next power of two).
133
|
134 keith.petley 1.4 Uint32 initialCapacity = 8;
|
135 kumpf 1.1
|
136 kumpf 1.5 while ((initialCapacity != 0) && (initialCapacity < size))
137 {
|
138 keith.petley 1.4 initialCapacity <<= 1;
|
139 kumpf 1.5 }
140
141 // Test for Uint32 overflow in the capacity
142 if (initialCapacity == 0)
143 {
144 initialCapacity = size;
145 }
146
147 // Test for Uint32 overflow in the memory allocation size
|
148 dave.sudlik 1.15 // throw a bad_alloc exception if overflow would occur.
|
149 kumpf 1.5 if (initialCapacity > (Uint32(0xffffffff)-sizeof(ArrayRep<T>))/sizeof(T))
150 {
|
151 dave.sudlik 1.15 throw PEGASUS_STD(bad_alloc)();
|
152 kumpf 1.5 }
|
153 kumpf 1.1
154 // Create object:
155
|
156 kumpf 1.5 ArrayRep<T>* rep = (ArrayRep<T>*)operator new(
157 sizeof(ArrayRep<T>) + sizeof(T) * initialCapacity);
|
158 david 1.10
|
159 kumpf 1.1 rep->size = size;
|
160 keith.petley 1.4 rep->capacity = initialCapacity;
|
161 mike 1.11 new(&rep->refs) AtomicInt(1);
|
162 kumpf 1.1
163 return rep;
164 }
165
166 template<class T>
|
167 mike 1.11 inline void ArrayRep<T>::ref(const ArrayRep<T>* rep)
|
168 kumpf 1.1 {
|
169 mike 1.12 if ((void*)rep != (void*)&ArrayRepBase::_empty_rep)
|
170 mike 1.11 ((ArrayRep<T>*)rep)->refs.inc();
171 }
172
173 template<class T>
174 inline void ArrayRep<T>::unref(const ArrayRep<T>* rep_)
175 {
176 ArrayRep<T>* rep = (ArrayRep<T>*)rep_;
177
178 if (rep != &ArrayRepBase::_empty_rep && rep->refs.decAndTestIfZero())
|
179 kumpf 1.1 {
180 Destroy(rep->data(), rep->size);
|
181 mike 1.11 rep->refs.~AtomicInt();
182 ::operator delete(rep);
183 }
184 }
|
185 david 1.10
|
186 mike 1.11 template<class T>
187 ArrayRep<T>* ArrayRep<T>::copy_on_write(ArrayRep<T>* rep)
188 {
189 ArrayRep<T>* new_rep = ArrayRep<T>::alloc(rep->size);
190 new_rep->size = rep->size;
191 CopyToRaw(new_rep->data(), rep->data(), rep->size);
192 unref(rep);
193 return new_rep;
|
194 kumpf 1.1 }
195
196 PEGASUS_NAMESPACE_END
197
198 #endif /* Pegasus_ArrayRep_h */
|