1 martin 1.25 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.26 //
|
3 martin 1.25 // 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.26 //
|
10 martin 1.25 // 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.26 //
|
17 martin 1.25 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.26 //
|
20 martin 1.25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.26 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.25 // 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.26 //
|
28 martin 1.25 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.8 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 ////////////////////////////////////////////////////////////////////////////////
33 //
34 // Memory.h
35 //
|
36 kumpf 1.13 // This file contains assorted memory-oriented routines:
|
37 mike 1.8 //
|
38 kumpf 1.13 // Zeros(): fills memory with zeros.
39 // Destroy(): destructs multiple objects in contiguous memory.
40 // CopyToRaw(): copies multiple objects to raw memory.
41 // InitializeRaw(): default constructs mulitple object over raw memory.
|
42 mike 1.8 //
|
43 kumpf 1.13 // Each of these is a template but specializations are provide for
44 // efficiency (which in some cases removes uncessary loops).
|
45 mike 1.8 //
46 ////////////////////////////////////////////////////////////////////////////////
47
48 #ifndef Pegasus_Memory_h
49 #define Pegasus_Memory_h
50
|
51 kumpf 1.19 #include <cstring>
|
52 david.dillard 1.20 #include <memory.h>
|
53 mike 1.8 #include <Pegasus/Common/Config.h>
54 #include <Pegasus/Common/CIMType.h>
55 #include <Pegasus/Common/Char16.h>
56
57 PEGASUS_NAMESPACE_BEGIN
58
59 template<class T>
60 inline void Zeros(T* items, Uint32 size)
61 {
62 memset(items, 0, sizeof(T) * size);
63 }
64
65 template<class T>
66 inline void Destroy(T* items, Uint32 size)
67 {
|
68 kumpf 1.23 #ifdef PEGASUS_OS_HPUX
69 // Empirical evidence shows that the memory allocator on HP-UX performs
70 // better when deallocating more recently allocated memory first (LIFO).
71 // Since most arrays are built up by appending elements to the end, it is
72 // advantageous to destruct the elements in reverse order.
73 items += size-1;
74 while (size--)
75 (items--)->~T();
76 #else
|
77 mike 1.8 while (size--)
|
78 kumpf 1.13 items++->~T();
|
79 kumpf 1.23 #endif
|
80 mike 1.8 }
81
|
82 kumpf 1.13 template<class T>
83 inline void CopyToRaw(T* to, const T* from, Uint32 size)
|
84 mike 1.8 {
85 while (size--)
|
86 mike 1.9 {
|
87 kumpf 1.13 // The following fails on TRU64:
88 // new(to++) T(*from++);
89 // Probably a compiler error so I changed it to this:
90 // Mike Brasher
91
92 new(to) T(*from);
93 to++;
94 from++;
|
95 mike 1.9 }
|
96 mike 1.8 }
97
|
98 marek 1.21 inline void CopyToRaw(char* to, const char* from, Uint32 size)
99 {
100 memcpy(to, from, sizeof(char) * size);
101 }
102
|
103 mike 1.8 inline void CopyToRaw(Boolean* to, const Boolean* from, Uint32 size)
104 {
105 memcpy(to, from, sizeof(Boolean) * size);
106 }
107
108 inline void CopyToRaw(Uint8* to, const Uint8* from, Uint32 size)
109 {
110 memcpy(to, from, sizeof(Uint8) * size);
111 }
112
113 inline void CopyToRaw(Sint8* to, const Sint8* from, Uint32 size)
114 {
115 memcpy(to, from, sizeof(Sint8) * size);
116 }
117
118 inline void CopyToRaw(Uint16* to, const Uint16* from, Uint32 size)
119 {
120 memcpy(to, from, sizeof(Uint16) * size);
121 }
122
123 inline void CopyToRaw(Sint16* to, const Sint16* from, Uint32 size)
124 mike 1.8 {
125 memcpy(to, from, sizeof(Sint16) * size);
126 }
127
128 inline void CopyToRaw(Uint32* to, const Uint32* from, Uint32 size)
129 {
130 memcpy(to, from, sizeof(Uint32) * size);
131 }
132
133 inline void CopyToRaw(Sint32* to, const Sint32* from, Uint32 size)
134 {
135 memcpy(to, from, sizeof(Sint32) * size);
136 }
137
138 inline void CopyToRaw(Uint64* to, const Uint64* from, Uint32 size)
139 {
140 memcpy(to, from, sizeof(Uint64) * size);
141 }
142
143 inline void CopyToRaw(Sint64* to, const Sint64* from, Uint32 size)
144 {
145 mike 1.8 memcpy(to, from, sizeof(Sint64) * size);
146 }
147
148 inline void CopyToRaw(Real32* to, const Real32* from, Uint32 size)
149 {
150 memcpy(to, from, sizeof(Real32) * size);
151 }
152
153 inline void CopyToRaw(Real64* to, const Real64* from, Uint32 size)
154 {
155 memcpy(to, from, sizeof(Real64) * size);
156 }
157
158 inline void CopyToRaw(Char16* to, const Char16* from, Uint32 size)
159 {
160 memcpy(to, from, sizeof(Char16) * size);
161 }
162
163 template<class T>
164 inline void InitializeRaw(T* items, Uint32 size)
165 {
166 mike 1.8 while (size--)
|
167 kumpf 1.11 {
|
168 kumpf 1.13 new(items) T();
169 items++;
|
170 kumpf 1.11 }
|
171 mike 1.8 }
172
173 #define PEGASUS_MEMORY_FUNCTIONS(T) \
174 inline void Destroy(T*, Uint32) { } \
175 inline void InitializeRaw(T* items, Uint32 size) { Zeros(items, size); }
176
177 PEGASUS_MEMORY_FUNCTIONS(char*)
178 PEGASUS_MEMORY_FUNCTIONS(const char*)
179 PEGASUS_MEMORY_FUNCTIONS(Boolean)
180 PEGASUS_MEMORY_FUNCTIONS(Uint8)
181 PEGASUS_MEMORY_FUNCTIONS(Sint8)
182 PEGASUS_MEMORY_FUNCTIONS(Uint16)
183 PEGASUS_MEMORY_FUNCTIONS(Sint16)
184 PEGASUS_MEMORY_FUNCTIONS(Uint32)
185 PEGASUS_MEMORY_FUNCTIONS(Sint32)
186 PEGASUS_MEMORY_FUNCTIONS(Uint64)
187 PEGASUS_MEMORY_FUNCTIONS(Sint64)
188 PEGASUS_MEMORY_FUNCTIONS(Real32)
189 PEGASUS_MEMORY_FUNCTIONS(Real64)
190 PEGASUS_MEMORY_FUNCTIONS(Char16)
191
192 mike 1.8 PEGASUS_NAMESPACE_END
193
194 #endif /* Pegasus_Memory_h */
|