| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-2019 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 |
#include "dng_memory.h" |
| 10 |
|
| 11 |
#include "dng_bottlenecks.h" |
| 12 |
#include "dng_exceptions.h" |
| 13 |
#include "dng_safe_arithmetic.h" |
| 14 |
|
| 15 |
#ifdef _MSC_VER |
| 16 |
#include <Windows.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
/*****************************************************************************/ |
| 20 |
|
| 21 |
dng_memory_data::dng_memory_data () |
| 22 |
|
| 23 |
: fBuffer (NULL) |
| 24 |
|
| 25 |
{ |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
/*****************************************************************************/ |
| 30 |
|
| 31 |
dng_memory_data::dng_memory_data (uint32 size) |
| 32 |
|
| 33 |
: fBuffer (NULL) |
| 34 |
|
| 35 |
{ |
| 36 |
|
| 37 |
Allocate (size); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/*****************************************************************************/ |
| 42 |
|
| 43 |
dng_memory_data::dng_memory_data (const dng_safe_uint32 &size) |
| 44 |
|
| 45 |
: fBuffer (NULL) |
| 46 |
|
| 47 |
{ |
| 48 |
|
| 49 |
Allocate (size.Get ()); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/*****************************************************************************/ |
| 54 |
|
| 55 |
dng_memory_data::dng_memory_data (uint32 count, |
| 56 |
std::size_t elementSize) |
| 57 |
|
| 58 |
: fBuffer (NULL) |
| 59 |
|
| 60 |
{ |
| 61 |
|
| 62 |
Allocate (count, elementSize); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/*****************************************************************************/ |
| 67 |
|
| 68 |
dng_memory_data::~dng_memory_data () |
| 69 |
{ |
| 70 |
|
| 71 |
Clear (); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/*****************************************************************************/ |
| 76 |
|
| 77 |
void dng_memory_data::Allocate (uint32 size) |
| 78 |
{ |
| 79 |
|
| 80 |
Clear (); |
| 81 |
|
| 82 |
if (size) |
| 83 |
{ |
| 84 |
//printf("Calling malloc from %s\n", __FUNCTION__); |
| 85 |
fBuffer = (char *) malloc (size); |
| 86 |
|
| 87 |
if (!fBuffer) |
| 88 |
{ |
| 89 |
|
| 90 |
ThrowMemoryFull (); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/*****************************************************************************/ |
| 99 |
|
| 100 |
void dng_memory_data::Allocate (const dng_safe_uint32 &size) |
| 101 |
{ |
| 102 |
|
| 103 |
Allocate (size.Get ()); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/*****************************************************************************/ |
| 108 |
|
| 109 |
void dng_memory_data::Allocate (uint32 count, |
| 110 |
std::size_t elementSize) |
| 111 |
{ |
| 112 |
|
| 113 |
// Convert elementSize to a uint32. |
| 114 |
|
| 115 |
const uint32 elementSizeAsUint32 = static_cast<uint32> (elementSize); |
| 116 |
|
| 117 |
if (static_cast<std::size_t> (elementSizeAsUint32) != elementSize) |
| 118 |
{ |
| 119 |
ThrowOverflow ("elementSize overflow"); |
| 120 |
} |
| 121 |
|
| 122 |
// Compute required number of bytes and allocate memory. |
| 123 |
|
| 124 |
dng_safe_uint32 numBytes = dng_safe_uint32 (count) * elementSizeAsUint32; |
| 125 |
|
| 126 |
Allocate (numBytes.Get ()); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/*****************************************************************************/ |
| 131 |
|
| 132 |
void dng_memory_data::Allocate (const dng_safe_uint32 &count, |
| 133 |
std::size_t elementSize) |
| 134 |
{ |
| 135 |
|
| 136 |
Allocate (count.Get (), elementSize); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/*****************************************************************************/ |
| 141 |
|
| 142 |
void dng_memory_data::Clear () |
| 143 |
{ |
| 144 |
|
| 145 |
if (fBuffer) |
| 146 |
{ |
| 147 |
|
| 148 |
free (fBuffer); |
| 149 |
|
| 150 |
fBuffer = NULL; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/*****************************************************************************/ |
| 157 |
|
| 158 |
dng_memory_block * dng_memory_block::Clone (dng_memory_allocator &allocator) const |
| 159 |
{ |
| 160 |
|
| 161 |
uint32 size = LogicalSize (); |
| 162 |
|
| 163 |
dng_memory_block * result = allocator.Allocate (size); |
| 164 |
|
| 165 |
DoCopyBytes (Buffer (), result->Buffer (), size); |
| 166 |
|
| 167 |
return result; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/*****************************************************************************/ |
| 172 |
|
| 173 |
dng_malloc_block::dng_malloc_block (uint32 logicalSize) |
| 174 |
|
| 175 |
: dng_memory_block (logicalSize) |
| 176 |
|
| 177 |
, fMalloc (NULL) |
| 178 |
|
| 179 |
{ |
| 180 |
|
| 181 |
#if qLinux |
| 182 |
|
| 183 |
#ifdef __MINGW32__ // krazy:exclude=cpp |
| 184 |
|
| 185 |
fMalloc = malloc (PhysicalSize ()); |
| 186 |
|
| 187 |
if (!fMalloc) |
| 188 |
{ |
| 189 |
|
| 190 |
ThrowMemoryFull (); |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
#else |
| 195 |
|
| 196 |
// TO DO: Need to change this alignment for AVX support? |
| 197 |
|
| 198 |
int err = ::posix_memalign ((void **) &fMalloc, |
| 199 |
16, |
| 200 |
(size_t) PhysicalSize ()); |
| 201 |
|
| 202 |
if (err) |
| 203 |
{ |
| 204 |
|
| 205 |
ThrowMemoryFull (); |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
#endif |
| 210 |
|
| 211 |
#elif qAndroid |
| 212 |
|
| 213 |
fMalloc = memalign (16, (size_t) PhysicalSize ()); |
| 214 |
|
| 215 |
if (!fMalloc) |
| 216 |
{ |
| 217 |
|
| 218 |
ThrowMemoryFull (); |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
#else |
| 223 |
|
| 224 |
//fMalloc = (char *) VirtualAlloc (NULL, PhysicalSize (), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 225 |
//printf("Calling malloc from %s\n", __FUNCTION__); |
| 226 |
fMalloc = (char *)malloc(PhysicalSize()); |
| 227 |
|
| 228 |
if (!fMalloc) |
| 229 |
{ |
| 230 |
|
| 231 |
ThrowMemoryFull (); |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
//*(size_t*)(fMalloc) = size_t(PhysicalSize() + 16); |
| 236 |
//fMalloc = (char*)fMalloc+16; |
| 237 |
|
| 238 |
#endif |
| 239 |
|
| 240 |
SetBuffer (fMalloc); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
/*****************************************************************************/ |
| 245 |
|
| 246 |
dng_malloc_block::~dng_malloc_block () |
| 247 |
{ |
| 248 |
|
| 249 |
if (fMalloc) |
| 250 |
{ |
| 251 |
|
| 252 |
//size_t size = *(size_t*)((char*)fMalloc - 16); |
| 253 |
//VirtualFree(fMalloc, 0, MEM_RELEASE); |
| 254 |
free (fMalloc); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
/*****************************************************************************/ |
| 261 |
|
| 262 |
dng_memory_block * dng_memory_allocator::Allocate (uint32 size) |
| 263 |
{ |
| 264 |
|
| 265 |
dng_memory_block *result = new dng_malloc_block (size); |
| 266 |
|
| 267 |
if (!result) |
| 268 |
{ |
| 269 |
|
| 270 |
ThrowMemoryFull (); |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
return result; |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
/*****************************************************************************/ |
| 279 |
|
| 280 |
void * dng_memory_allocator::Malloc (size_t size) |
| 281 |
{ |
| 282 |
|
| 283 |
return malloc (size); |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
/*****************************************************************************/ |
| 288 |
|
| 289 |
void dng_memory_allocator::Free (void *ptr) |
| 290 |
{ |
| 291 |
|
| 292 |
free (ptr); |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
/*****************************************************************************/ |
| 297 |
|
| 298 |
dng_memory_allocator gDefaultDNGMemoryAllocator; |
| 299 |
|
| 300 |
/*****************************************************************************/ |