1 karl 1.22 //%2006////////////////////////////////////////////////////////////////////////
|
2 mike 1.8 //
|
3 karl 1.17 // 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.16 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.17 // 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.18 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.22 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 mike 1.8 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
15 kumpf 1.14 // 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 mike 1.8 // 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.14 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
22 mike 1.8 // 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.14 // 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 mike 1.8 // 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 //%/////////////////////////////////////////////////////////////////////////////
33
34 ////////////////////////////////////////////////////////////////////////////////
35 //
36 // Memory.h
37 //
|
38 kumpf 1.13 // This file contains assorted memory-oriented routines:
|
39 mike 1.8 //
|
40 kumpf 1.13 // Zeros(): fills memory with zeros.
41 // Destroy(): destructs multiple objects in contiguous memory.
42 // CopyToRaw(): copies multiple objects to raw memory.
43 // InitializeRaw(): default constructs mulitple object over raw memory.
|
44 mike 1.8 //
|
45 kumpf 1.13 // Each of these is a template but specializations are provide for
46 // efficiency (which in some cases removes uncessary loops).
|
47 mike 1.8 //
48 ////////////////////////////////////////////////////////////////////////////////
49
50 #ifndef Pegasus_Memory_h
51 #define Pegasus_Memory_h
52
|
53 kumpf 1.19 #include <cstring>
|
54 david.dillard 1.20 #include <memory.h>
|
55 mike 1.8 #include <Pegasus/Common/Config.h>
56 #include <Pegasus/Common/CIMType.h>
57 #include <Pegasus/Common/Char16.h>
58
59 PEGASUS_NAMESPACE_BEGIN
60
61 template<class T>
62 inline void Zeros(T* items, Uint32 size)
63 {
64 memset(items, 0, sizeof(T) * size);
65 }
66
67 template<class T>
68 inline void Destroy(T* items, Uint32 size)
69 {
|
70 kumpf 1.23 #ifdef PEGASUS_OS_HPUX
71 // Empirical evidence shows that the memory allocator on HP-UX performs
72 // better when deallocating more recently allocated memory first (LIFO).
73 // Since most arrays are built up by appending elements to the end, it is
74 // advantageous to destruct the elements in reverse order.
75 items += size-1;
76 while (size--)
77 (items--)->~T();
78 #else
|
79 mike 1.8 while (size--)
|
80 kumpf 1.13 items++->~T();
|
81 kumpf 1.23 #endif
|
82 mike 1.8 }
83
|
84 kumpf 1.13 template<class T>
85 inline void CopyToRaw(T* to, const T* from, Uint32 size)
|
86 mike 1.8 {
87 while (size--)
|
88 mike 1.9 {
|
89 kumpf 1.13 // The following fails on TRU64:
90 // new(to++) T(*from++);
91 // Probably a compiler error so I changed it to this:
92 // Mike Brasher
93
94 new(to) T(*from);
95 to++;
96 from++;
|
97 mike 1.9 }
|
98 mike 1.8 }
99
|
100 marek 1.21 inline void CopyToRaw(char* to, const char* from, Uint32 size)
101 {
102 memcpy(to, from, sizeof(char) * size);
103 }
104
|
105 mike 1.8 inline void CopyToRaw(Boolean* to, const Boolean* from, Uint32 size)
106 {
107 memcpy(to, from, sizeof(Boolean) * size);
108 }
109
110 inline void CopyToRaw(Uint8* to, const Uint8* from, Uint32 size)
111 {
112 memcpy(to, from, sizeof(Uint8) * size);
113 }
114
115 inline void CopyToRaw(Sint8* to, const Sint8* from, Uint32 size)
116 {
117 memcpy(to, from, sizeof(Sint8) * size);
118 }
119
120 inline void CopyToRaw(Uint16* to, const Uint16* from, Uint32 size)
121 {
122 memcpy(to, from, sizeof(Uint16) * size);
123 }
124
125 inline void CopyToRaw(Sint16* to, const Sint16* from, Uint32 size)
126 mike 1.8 {
127 memcpy(to, from, sizeof(Sint16) * size);
128 }
129
130 inline void CopyToRaw(Uint32* to, const Uint32* from, Uint32 size)
131 {
132 memcpy(to, from, sizeof(Uint32) * size);
133 }
134
135 inline void CopyToRaw(Sint32* to, const Sint32* from, Uint32 size)
136 {
137 memcpy(to, from, sizeof(Sint32) * size);
138 }
139
140 inline void CopyToRaw(Uint64* to, const Uint64* from, Uint32 size)
141 {
142 memcpy(to, from, sizeof(Uint64) * size);
143 }
144
145 inline void CopyToRaw(Sint64* to, const Sint64* from, Uint32 size)
146 {
147 mike 1.8 memcpy(to, from, sizeof(Sint64) * size);
148 }
149
150 inline void CopyToRaw(Real32* to, const Real32* from, Uint32 size)
151 {
152 memcpy(to, from, sizeof(Real32) * size);
153 }
154
155 inline void CopyToRaw(Real64* to, const Real64* from, Uint32 size)
156 {
157 memcpy(to, from, sizeof(Real64) * size);
158 }
159
160 inline void CopyToRaw(Char16* to, const Char16* from, Uint32 size)
161 {
162 memcpy(to, from, sizeof(Char16) * size);
163 }
164
165 template<class T>
166 inline void InitializeRaw(T* items, Uint32 size)
167 {
168 mike 1.8 while (size--)
|
169 kumpf 1.11 {
|
170 kumpf 1.13 new(items) T();
171 items++;
|
172 kumpf 1.11 }
|
173 mike 1.8 }
174
175 #define PEGASUS_MEMORY_FUNCTIONS(T) \
176 inline void Destroy(T*, Uint32) { } \
177 inline void InitializeRaw(T* items, Uint32 size) { Zeros(items, size); }
178
179 PEGASUS_MEMORY_FUNCTIONS(char*)
180 PEGASUS_MEMORY_FUNCTIONS(const char*)
181 PEGASUS_MEMORY_FUNCTIONS(Boolean)
182 PEGASUS_MEMORY_FUNCTIONS(Uint8)
183 PEGASUS_MEMORY_FUNCTIONS(Sint8)
184 PEGASUS_MEMORY_FUNCTIONS(Uint16)
185 PEGASUS_MEMORY_FUNCTIONS(Sint16)
186 PEGASUS_MEMORY_FUNCTIONS(Uint32)
187 PEGASUS_MEMORY_FUNCTIONS(Sint32)
188 PEGASUS_MEMORY_FUNCTIONS(Uint64)
189 PEGASUS_MEMORY_FUNCTIONS(Sint64)
190 PEGASUS_MEMORY_FUNCTIONS(Real32)
191 PEGASUS_MEMORY_FUNCTIONS(Real64)
192 PEGASUS_MEMORY_FUNCTIONS(Char16)
193
194 mike 1.8 PEGASUS_NAMESPACE_END
195
196 #endif /* Pegasus_Memory_h */
|