1 karl 1.6 //%2003////////////////////////////////////////////////////////////////////////
|
2 kumpf 1.1 //
|
3 karl 1.6 // 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 // IBM Corp.; EMC Corporation, The Open Group.
|
7 kumpf 1.1 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 kumpf 1.2 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
12 kumpf 1.1 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
|
15 kumpf 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
16 kumpf 1.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
18 kumpf 1.2 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
21 kumpf 1.1 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Mike Brasher (mbrasher@bmc.com)
27 //
28 // Modified By: Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_ArrayRep_h
33 #define Pegasus_ArrayRep_h
34
|
35 humberto 1.7 #ifdef PEGASUS_OS_OS400
36 #include <stdlib.h>
37 #endif
38
|
39 kumpf 1.1 #include <new>
40 #include <Pegasus/Common/Config.h>
41 #include <Pegasus/Common/Memory.h>
42
43 PEGASUS_NAMESPACE_BEGIN
44
45 /* ArrayRep<T>
46 The ArrayRep object represents the array size, capacity,
47 and elements in one contiguous chunk of memory. The elements
48 follow immediately after the end of the ArrayRep structure in memory.
49 The union is used to force 64 bit alignment of these elements. This is
50 a private class and should not be accessed directly by the user.
51 */
52 template<class T>
|
53 chuck 1.3 #ifdef PEGASUS_OS_OS400
54 /* For OS/400, this forces the alignment mentioned above. */
55 __align(16)
56 #endif
|
57 kumpf 1.1 struct ArrayRep
58 {
59 Uint32 size;
60
61 /* This union forces the first element (which follows this structure
62 in memory) to be aligned on a 64 bit boundary. It is a requirement
63 that even an array of characters be aligned for any purpose (as malloc()
64 does). That way, arrays of characters can be used for alignement
65 sensitive data.
66 */
67 union
68 {
69 Uint32 capacity;
70 Uint64 alignment;
71 };
72
73 // Obtains a pointer to the first element in the array.
74 T* data() { return (T*)(void*)(this + 1); }
75
76 // Same as method above but returns a constant pointer.
77 const T* data() const { return (const T*)(void*)(this + 1); }
78 kumpf 1.1
79 /* Creates a clone of the current */
80 ArrayRep<T>* clone() const;
81
82 /* Create and initialize a ArrayRep instance. Note that the
83 memory for the elements is unitialized and must be initialized by
84 the caller.
85 */
86 static ArrayRep<T>* PEGASUS_STATIC_CDECL create(Uint32 size);
87
88 /* Disposes of the object.
89 */
90 static void PEGASUS_STATIC_CDECL destroy(ArrayRep<T>* rep);
91 };
92
93 template<class T>
94 ArrayRep<T>* ArrayRep<T>::clone() const
95 {
96 ArrayRep<T>* rep = ArrayRep<T>::create(capacity);
97 rep->size = size;
98 CopyToRaw(rep->data(), data(), size);
99 kumpf 1.1 return rep;
100 }
101
102 template<class T>
103 ArrayRep<T>* PEGASUS_STATIC_CDECL ArrayRep<T>::create(Uint32 size)
104 {
105 // Calculate capacity (size rounded to the next power of two).
106
|
107 keith.petley 1.4 Uint32 initialCapacity = 8;
|
108 kumpf 1.1
|
109 kumpf 1.5 while ((initialCapacity != 0) && (initialCapacity < size))
110 {
|
111 keith.petley 1.4 initialCapacity <<= 1;
|
112 kumpf 1.5 }
113
114 // Test for Uint32 overflow in the capacity
115 if (initialCapacity == 0)
116 {
117 initialCapacity = size;
118 }
119
120 // Test for Uint32 overflow in the memory allocation size
121 if (initialCapacity > (Uint32(0xffffffff)-sizeof(ArrayRep<T>))/sizeof(T))
122 {
123 return 0;
124 }
|
125 kumpf 1.1
126 // Create object:
127
|
128 humberto 1.7 #ifdef PEGASUS_OS_OS400
129 // use Terraspace for allocation,
130 // bypass 16mb single allocation limit imposed by single store memory model on OS400
131 ArrayRep<T>* rep = (ArrayRep<T>*)_C_TS_malloc(sizeof(ArrayRep<T>) + sizeof(T) * initialCapacity);
132 #else
|
133 kumpf 1.5 ArrayRep<T>* rep = (ArrayRep<T>*)operator new(
134 sizeof(ArrayRep<T>) + sizeof(T) * initialCapacity);
|
135 humberto 1.7 #endif
|
136 kumpf 1.1 rep->size = size;
|
137 keith.petley 1.4 rep->capacity = initialCapacity;
|
138 kumpf 1.1
139 return rep;
140 }
141
142 template<class T>
143 void PEGASUS_STATIC_CDECL ArrayRep<T>::destroy(ArrayRep<T>* rep)
144 {
145 if (rep)
146 {
147 Destroy(rep->data(), rep->size);
|
148 humberto 1.7 #ifdef PEGASUS_OS_OS400
149 _C_TS_free(rep); // Terraspace deallocation
150 #else
|
151 kumpf 1.1 operator delete(rep);
|
152 humberto 1.7 #endif
|
153 kumpf 1.1 return;
154 }
155 }
156
157 PEGASUS_NAMESPACE_END
158
159 #endif /* Pegasus_ArrayRep_h */
|