1 /*****************************************************************************/
2 // Copyright 2006 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 /* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_memory.cpp#1 $ */
10 /* $DateTime: 2012/05/30 13:28:51 $ */
11 /* $Change: 832332 $ */
12 /* $Author: tknoll $ */
13
14 /*****************************************************************************/
15
16 #include "dng_memory.h"
17
18 #include "dng_bottlenecks.h"
19 #include "dng_exceptions.h"
20 #include "dng_safe_arithmetic.h"
21
22 /*****************************************************************************/
23
dng_memory_data()24 dng_memory_data::dng_memory_data ()
25
26 : fBuffer (NULL)
27
28 {
29
30 }
31
32 /*****************************************************************************/
33
dng_memory_data(uint32 size)34 dng_memory_data::dng_memory_data (uint32 size)
35
36 : fBuffer (NULL)
37
38 {
39
40 Allocate (size);
41
42 }
43
44 /*****************************************************************************/
45
dng_memory_data(uint32 count,std::size_t elementSize)46 dng_memory_data::dng_memory_data (uint32 count, std::size_t elementSize)
47
48 : fBuffer (NULL)
49
50 {
51
52 Allocate (count, elementSize);
53
54 }
55
56 /*****************************************************************************/
57
~dng_memory_data()58 dng_memory_data::~dng_memory_data ()
59 {
60
61 Clear ();
62
63 }
64
65 /*****************************************************************************/
66
Allocate(uint32 size)67 void dng_memory_data::Allocate (uint32 size)
68 {
69
70 Clear ();
71
72 if (size)
73 {
74
75 fBuffer = (char*)malloc (size);
76
77 if (!fBuffer)
78 {
79
80 ThrowMemoryFull ();
81
82 }
83
84 }
85
86 }
87
88 /*****************************************************************************/
89
Allocate(uint32 count,std::size_t elementSize)90 void dng_memory_data::Allocate (uint32 count, std::size_t elementSize)
91 {
92
93 // Convert elementSize to a uint32.
94 const uint32 elementSizeAsUint32 = static_cast<uint32> (elementSize);
95 if (static_cast<std::size_t> (elementSizeAsUint32) != elementSize)
96 {
97 ThrowMemoryFull();
98 }
99
100 // Compute required number of bytes and allocate memory.
101 uint32 numBytes;
102 if (!SafeUint32Mult(count, elementSizeAsUint32, &numBytes))
103 {
104 ThrowMemoryFull();
105 }
106 Allocate(numBytes);
107
108 }
109
Clear()110 void dng_memory_data::Clear ()
111 {
112
113 if (fBuffer)
114 {
115
116 free (fBuffer);
117
118 fBuffer = NULL;
119
120 }
121
122 }
123
124 /*****************************************************************************/
125
Clone(dng_memory_allocator & allocator) const126 dng_memory_block * dng_memory_block::Clone (dng_memory_allocator &allocator) const
127 {
128
129 uint32 size = LogicalSize ();
130
131 dng_memory_block * result = allocator.Allocate (size);
132
133 DoCopyBytes (Buffer (), result->Buffer (), size);
134
135 return result;
136
137 }
138
139 /*****************************************************************************/
140
141 class dng_malloc_block : public dng_memory_block
142 {
143
144 private:
145
146 void *fMalloc;
147
148 public:
149
150 dng_malloc_block (uint32 logicalSize);
151
152 virtual ~dng_malloc_block ();
153
154 private:
155
156 // Hidden copy constructor and assignment operator.
157
158 dng_malloc_block (const dng_malloc_block &block);
159
160 dng_malloc_block & operator= (const dng_malloc_block &block);
161
162 };
163
164 /*****************************************************************************/
165
dng_malloc_block(uint32 logicalSize)166 dng_malloc_block::dng_malloc_block (uint32 logicalSize)
167
168 : dng_memory_block (logicalSize)
169
170 , fMalloc (NULL)
171
172 {
173
174 #if (qLinux && !defined(__ANDROID_API__)) || (defined(__ANDROID_API__) && __ANDROID_API__ >= 17)
175
176 int err = ::posix_memalign( (void **) &fMalloc, 16, (size_t) PhysicalSize() );
177
178 if (err)
179 {
180
181 ThrowMemoryFull ();
182
183 }
184
185 #else
186
187 fMalloc = (char*)malloc (PhysicalSize ());
188
189 if (!fMalloc)
190 {
191
192 ThrowMemoryFull ();
193
194 }
195
196 #endif
197
198 SetBuffer (fMalloc);
199
200 }
201
202 /*****************************************************************************/
203
~dng_malloc_block()204 dng_malloc_block::~dng_malloc_block ()
205 {
206
207 if (fMalloc)
208 {
209
210 free (fMalloc);
211
212 }
213
214 }
215
216 /*****************************************************************************/
217
Allocate(uint32 size)218 dng_memory_block * dng_memory_allocator::Allocate (uint32 size)
219 {
220
221 dng_memory_block *result = new dng_malloc_block (size);
222
223 if (!result)
224 {
225
226 ThrowMemoryFull ();
227
228 }
229
230 return result;
231
232 }
233
234 /*****************************************************************************/
235
236 dng_memory_allocator gDefaultDNGMemoryAllocator;
237
238 /*****************************************************************************/
239