| 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 |
/** Support for memory allocation. |
| 10 |
*/ |
| 11 |
|
| 12 |
/*****************************************************************************/ |
| 13 |
|
| 14 |
#ifndef __dng_memory__ |
| 15 |
#define __dng_memory__ |
| 16 |
|
| 17 |
/*****************************************************************************/ |
| 18 |
|
| 19 |
#include "dng_classes.h" |
| 20 |
#include "dng_exceptions.h" |
| 21 |
#include "dng_flags.h" |
| 22 |
#include "dng_safe_arithmetic.h" |
| 23 |
#include "dng_types.h" |
| 24 |
#include "dng_uncopyable.h" |
| 25 |
|
| 26 |
#include <cstdlib> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
/*****************************************************************************/ |
| 30 |
|
| 31 |
#if qDNGAVXSupport |
| 32 |
#define DNG_ALIGN_SIMD(x) ((((uintptr) (x)) + 31) & ~((uintptr) 31)) |
| 33 |
#else |
| 34 |
#define DNG_ALIGN_SIMD(x) ((((uintptr) (x)) + 15) & ~((uintptr) 15)) |
| 35 |
#endif |
| 36 |
|
| 37 |
/*****************************************************************************/ |
| 38 |
|
| 39 |
/// @brief Class to provide resource acquisition is instantiation discipline |
| 40 |
/// for small memory allocations. |
| 41 |
/// |
| 42 |
/// This class does not use dng_memory_allocator for memory allocation. |
| 43 |
|
| 44 |
class dng_memory_data: private dng_uncopyable |
| 45 |
{ |
| 46 |
|
| 47 |
private: |
| 48 |
|
| 49 |
char *fBuffer; |
| 50 |
|
| 51 |
public: |
| 52 |
|
| 53 |
/// Construct an empty memory buffer using malloc. |
| 54 |
/// \exception dng_memory_full with fErrorCode equal to dng_error_memory. |
| 55 |
|
| 56 |
dng_memory_data (); |
| 57 |
|
| 58 |
/// Construct memory buffer of size bytes using malloc. |
| 59 |
/// \param size Number of bytes of memory needed. |
| 60 |
/// \exception dng_memory_full with fErrorCode equal to dng_error_memory. |
| 61 |
|
| 62 |
dng_memory_data (uint32 size); |
| 63 |
|
| 64 |
dng_memory_data (const dng_safe_uint32 &size); |
| 65 |
|
| 66 |
/// Note: This constructor is for internal use only and should not be |
| 67 |
/// considered part of the DNG SDK API. |
| 68 |
/// |
| 69 |
/// Construct memory buffer of count elements of elementSize bytes each. |
| 70 |
/// \param count Number of elements. |
| 71 |
/// \param elementSize Size of each element. |
| 72 |
/// \exception dng_memory_full with fErrorCode equal to dng_error_memory. |
| 73 |
|
| 74 |
dng_memory_data (uint32 count, |
| 75 |
std::size_t elementSize); |
| 76 |
|
| 77 |
/// Release memory buffer using free. |
| 78 |
|
| 79 |
~dng_memory_data (); |
| 80 |
|
| 81 |
/// Clear existing memory buffer and allocate new memory of size bytes. |
| 82 |
/// \param size Number of bytes of memory needed. |
| 83 |
/// \exception dng_memory_full with fErrorCode equal to dng_error_memory. |
| 84 |
|
| 85 |
void Allocate (uint32 size); |
| 86 |
|
| 87 |
void Allocate (const dng_safe_uint32 &size); |
| 88 |
|
| 89 |
/// Note: This method is for internal use only and should not be |
| 90 |
/// considered part of the DNG SDK API. |
| 91 |
/// |
| 92 |
/// Clear existing memory buffer and allocate new memory of count |
| 93 |
/// elements of elementSize bytes each. |
| 94 |
/// \param count Number of elements. |
| 95 |
/// \param elementSize Size of each element. |
| 96 |
/// \exception dng_memory_full with fErrorCode equal to dng_error_memory. |
| 97 |
|
| 98 |
void Allocate (uint32 count, |
| 99 |
std::size_t elementSize); |
| 100 |
|
| 101 |
void Allocate (const dng_safe_uint32 &count, |
| 102 |
std::size_t elementSize); |
| 103 |
|
| 104 |
/// Release any allocated memory using free. Object is still valid and |
| 105 |
/// Allocate can be called again. |
| 106 |
|
| 107 |
void Clear (); |
| 108 |
|
| 109 |
/// Return pointer to allocated memory as a void *.. |
| 110 |
/// \retval void * valid for as many bytes as were allocated. |
| 111 |
|
| 112 |
void * Buffer () |
| 113 |
{ |
| 114 |
return fBuffer; |
| 115 |
} |
| 116 |
|
| 117 |
/// Return pointer to allocated memory as a const void *. |
| 118 |
/// \retval const void * valid for as many bytes as were allocated. |
| 119 |
|
| 120 |
const void * Buffer () const |
| 121 |
{ |
| 122 |
return fBuffer; |
| 123 |
} |
| 124 |
|
| 125 |
/// Return pointer to allocated memory as a char *. |
| 126 |
/// \retval char * valid for as many bytes as were allocated. |
| 127 |
|
| 128 |
char * Buffer_char () |
| 129 |
{ |
| 130 |
return (char *) Buffer (); |
| 131 |
} |
| 132 |
|
| 133 |
/// Return pointer to allocated memory as a const char *. |
| 134 |
/// \retval const char * valid for as many bytes as were allocated. |
| 135 |
|
| 136 |
const char * Buffer_char () const |
| 137 |
{ |
| 138 |
return (const char *) Buffer (); |
| 139 |
} |
| 140 |
|
| 141 |
/// Return pointer to allocated memory as a uint8 *. |
| 142 |
/// \retval uint8 * valid for as many bytes as were allocated. |
| 143 |
|
| 144 |
uint8 * Buffer_uint8 () |
| 145 |
{ |
| 146 |
return (uint8 *) Buffer (); |
| 147 |
} |
| 148 |
|
| 149 |
/// Return pointer to allocated memory as a const uint8 *. |
| 150 |
/// \retval const uint8 * valid for as many bytes as were allocated. |
| 151 |
|
| 152 |
const uint8 * Buffer_uint8 () const |
| 153 |
{ |
| 154 |
return (const uint8 *) Buffer (); |
| 155 |
} |
| 156 |
|
| 157 |
/// Return pointer to allocated memory as a uint16 *. |
| 158 |
/// \retval uint16 * valid for as many bytes as were allocated. |
| 159 |
|
| 160 |
uint16 * Buffer_uint16 () |
| 161 |
{ |
| 162 |
return (uint16 *) Buffer (); |
| 163 |
} |
| 164 |
|
| 165 |
/// Return pointer to allocated memory as a const uint16 *. |
| 166 |
/// \retval const uint16 * valid for as many bytes as were allocated. |
| 167 |
|
| 168 |
const uint16 * Buffer_uint16 () const |
| 169 |
{ |
| 170 |
return (const uint16 *) Buffer (); |
| 171 |
} |
| 172 |
|
| 173 |
/// Return pointer to allocated memory as a int16 *. |
| 174 |
/// \retval int16 * valid for as many bytes as were allocated. |
| 175 |
|
| 176 |
int16 * Buffer_int16 () |
| 177 |
{ |
| 178 |
return (int16 *) Buffer (); |
| 179 |
} |
| 180 |
|
| 181 |
/// Return pointer to allocated memory as a const int16 *. |
| 182 |
/// \retval const int16 * valid for as many bytes as were allocated. |
| 183 |
|
| 184 |
const int16 * Buffer_int16 () const |
| 185 |
{ |
| 186 |
return (const int16 *) Buffer (); |
| 187 |
} |
| 188 |
|
| 189 |
/// Return pointer to allocated memory as a uint32 *. |
| 190 |
/// \retval uint32 * valid for as many bytes as were allocated. |
| 191 |
|
| 192 |
uint32 * Buffer_uint32 () |
| 193 |
{ |
| 194 |
return (uint32 *) Buffer (); |
| 195 |
} |
| 196 |
|
| 197 |
/// Return pointer to allocated memory as a uint32 *. |
| 198 |
/// \retval uint32 * valid for as many bytes as were allocated. |
| 199 |
|
| 200 |
const uint32 * Buffer_uint32 () const |
| 201 |
{ |
| 202 |
return (const uint32 *) Buffer (); |
| 203 |
} |
| 204 |
|
| 205 |
/// Return pointer to allocated memory as a const int32 *. |
| 206 |
/// \retval const int32 * valid for as many bytes as were allocated. |
| 207 |
|
| 208 |
int32 * Buffer_int32 () |
| 209 |
{ |
| 210 |
return (int32 *) Buffer (); |
| 211 |
} |
| 212 |
|
| 213 |
/// Return pointer to allocated memory as a const int32 *. |
| 214 |
/// \retval const int32 * valid for as many bytes as were allocated. |
| 215 |
|
| 216 |
const int32 * Buffer_int32 () const |
| 217 |
{ |
| 218 |
return (const int32 *) Buffer (); |
| 219 |
} |
| 220 |
|
| 221 |
/// Return pointer to allocated memory as a uint64 *. |
| 222 |
/// \retval uint64 * valid for as many bytes as were allocated. |
| 223 |
|
| 224 |
uint64 * Buffer_uint64 () |
| 225 |
{ |
| 226 |
return (uint64 *) Buffer (); |
| 227 |
} |
| 228 |
|
| 229 |
/// Return pointer to allocated memory as a uint64 *. |
| 230 |
/// \retval uint64 * valid for as many bytes as were allocated. |
| 231 |
|
| 232 |
const uint64 * Buffer_uint64 () const |
| 233 |
{ |
| 234 |
return (const uint64 *) Buffer (); |
| 235 |
} |
| 236 |
|
| 237 |
/// Return pointer to allocated memory as a const int64 *. |
| 238 |
/// \retval const int64 * valid for as many bytes as were allocated. |
| 239 |
|
| 240 |
int64 * Buffer_int64 () |
| 241 |
{ |
| 242 |
return (int64 *) Buffer (); |
| 243 |
} |
| 244 |
|
| 245 |
/// Return pointer to allocated memory as a const int64 *. |
| 246 |
/// \retval const int64 * valid for as many bytes as were allocated. |
| 247 |
|
| 248 |
const int64 * Buffer_int64 () const |
| 249 |
{ |
| 250 |
return (const int64 *) Buffer (); |
| 251 |
} |
| 252 |
|
| 253 |
/// Return pointer to allocated memory as a real32 *. |
| 254 |
/// \retval real32 * valid for as many bytes as were allocated. |
| 255 |
|
| 256 |
real32 * Buffer_real32 () |
| 257 |
{ |
| 258 |
return (real32 *) Buffer (); |
| 259 |
} |
| 260 |
|
| 261 |
/// Return pointer to allocated memory as a const real32 *. |
| 262 |
/// \retval const real32 * valid for as many bytes as were allocated. |
| 263 |
|
| 264 |
const real32 * Buffer_real32 () const |
| 265 |
{ |
| 266 |
return (const real32 *) Buffer (); |
| 267 |
} |
| 268 |
|
| 269 |
/// Return pointer to allocated memory as a real64 *. |
| 270 |
/// \retval real64 * valid for as many bytes as were allocated. |
| 271 |
|
| 272 |
real64 * Buffer_real64 () |
| 273 |
{ |
| 274 |
return (real64 *) Buffer (); |
| 275 |
} |
| 276 |
|
| 277 |
/// Return pointer to allocated memory as a const real64 *. |
| 278 |
/// \retval const real64 * valid for as many bytes as were allocated. |
| 279 |
|
| 280 |
const real64 * Buffer_real64 () const |
| 281 |
{ |
| 282 |
return (const real64 *) Buffer (); |
| 283 |
} |
| 284 |
|
| 285 |
}; |
| 286 |
|
| 287 |
/*****************************************************************************/ |
| 288 |
|
| 289 |
/// @brief Class to provide resource acquisition is instantiation discipline for |
| 290 |
/// image buffers and other larger memory allocations. |
| 291 |
/// |
| 292 |
/// This class requires a dng_memory_allocator for allocation. |
| 293 |
|
| 294 |
class dng_memory_block: private dng_uncopyable |
| 295 |
{ |
| 296 |
|
| 297 |
private: |
| 298 |
|
| 299 |
uint32 fLogicalSize; |
| 300 |
|
| 301 |
char *fBuffer; |
| 302 |
|
| 303 |
protected: |
| 304 |
|
| 305 |
dng_memory_block (uint32 logicalSize) |
| 306 |
: fLogicalSize (logicalSize) |
| 307 |
, fBuffer (NULL) |
| 308 |
{ |
| 309 |
} |
| 310 |
|
| 311 |
uint32 PhysicalSize () |
| 312 |
{ |
| 313 |
|
| 314 |
// This size is padded for TWO reasons! The first is allow |
| 315 |
// alignment to 16-byte boundaries if the allocator does not do |
| 316 |
// that already. The second, which is very important, so to |
| 317 |
// provide safe overread areas for SSE2-type bottlenecks, which |
| 318 |
// can often be written faster by allowing them to reading |
| 319 |
// slightly block. Someone on the image core them did not |
| 320 |
// understand this and removed this padding. I'm undoing this |
| 321 |
// removal and restoring this padding, since removing it might |
| 322 |
// lead to memory access crashes in some cases. |
| 323 |
// |
| 324 |
// Please do NOT change the following padding unless you are very |
| 325 |
// sure what you are doing. |
| 326 |
|
| 327 |
dng_safe_uint32 safeLogicalSize (fLogicalSize); |
| 328 |
|
| 329 |
#if qDNGAVXSupport |
| 330 |
|
| 331 |
// Allow 32-byte alignment + 64-byte overread in both directions: |
| 332 |
// 32 + 64 + 64 = 160. |
| 333 |
|
| 334 |
return (safeLogicalSize + 160u).Get (); |
| 335 |
|
| 336 |
#else |
| 337 |
|
| 338 |
// Allow 16-byte alignment + overread. |
| 339 |
|
| 340 |
return (safeLogicalSize + 64u).Get (); |
| 341 |
|
| 342 |
#endif // qDNGAVXSupport |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
void SetBuffer (void *p) |
| 347 |
{ |
| 348 |
fBuffer = (char *) DNG_ALIGN_SIMD (p); |
| 349 |
} |
| 350 |
|
| 351 |
public: |
| 352 |
|
| 353 |
virtual ~dng_memory_block () |
| 354 |
{ |
| 355 |
} |
| 356 |
|
| 357 |
dng_memory_block * Clone (dng_memory_allocator &allocator) const; |
| 358 |
|
| 359 |
/// Getter for available size, in bytes, of memory block. |
| 360 |
/// \retval size in bytes of available memory in memory block. |
| 361 |
|
| 362 |
uint32 LogicalSize () const |
| 363 |
{ |
| 364 |
return fLogicalSize; |
| 365 |
} |
| 366 |
|
| 367 |
/// Return pointer to allocated memory as a void *.. |
| 368 |
/// \retval void * valid for as many bytes as were allocated. |
| 369 |
|
| 370 |
void * Buffer () |
| 371 |
{ |
| 372 |
return fBuffer; |
| 373 |
} |
| 374 |
|
| 375 |
/// Return pointer to allocated memory as a const void *. |
| 376 |
/// \retval const void * valid for as many bytes as were allocated. |
| 377 |
|
| 378 |
const void * Buffer () const |
| 379 |
{ |
| 380 |
return fBuffer; |
| 381 |
} |
| 382 |
|
| 383 |
/// Return pointer to allocated memory as a char *. |
| 384 |
/// \retval char * valid for as many bytes as were allocated. |
| 385 |
|
| 386 |
char * Buffer_char () |
| 387 |
{ |
| 388 |
return (char *) Buffer (); |
| 389 |
} |
| 390 |
|
| 391 |
/// Return pointer to allocated memory as a const char *. |
| 392 |
/// \retval const char * valid for as many bytes as were allocated. |
| 393 |
|
| 394 |
const char * Buffer_char () const |
| 395 |
{ |
| 396 |
return (const char *) Buffer (); |
| 397 |
} |
| 398 |
|
| 399 |
/// Return pointer to allocated memory as a uint8 *. |
| 400 |
/// \retval uint8 * valid for as many bytes as were allocated. |
| 401 |
|
| 402 |
uint8 * Buffer_uint8 () |
| 403 |
{ |
| 404 |
return (uint8 *) Buffer (); |
| 405 |
} |
| 406 |
|
| 407 |
/// Return pointer to allocated memory as a const uint8 *. |
| 408 |
/// \retval const uint8 * valid for as many bytes as were allocated. |
| 409 |
|
| 410 |
const uint8 * Buffer_uint8 () const |
| 411 |
{ |
| 412 |
return (const uint8 *) Buffer (); |
| 413 |
} |
| 414 |
|
| 415 |
/// Return pointer to allocated memory as a uint16 *. |
| 416 |
/// \retval uint16 * valid for as many bytes as were allocated. |
| 417 |
|
| 418 |
uint16 * Buffer_uint16 () |
| 419 |
{ |
| 420 |
return (uint16 *) Buffer (); |
| 421 |
} |
| 422 |
|
| 423 |
/// Return pointer to allocated memory as a const uint16 *. |
| 424 |
/// \retval const uint16 * valid for as many bytes as were allocated. |
| 425 |
|
| 426 |
const uint16 * Buffer_uint16 () const |
| 427 |
{ |
| 428 |
return (const uint16 *) Buffer (); |
| 429 |
} |
| 430 |
|
| 431 |
/// Return pointer to allocated memory as a int16 *. |
| 432 |
/// \retval int16 * valid for as many bytes as were allocated. |
| 433 |
|
| 434 |
int16 * Buffer_int16 () |
| 435 |
{ |
| 436 |
return (int16 *) Buffer (); |
| 437 |
} |
| 438 |
|
| 439 |
/// Return pointer to allocated memory as a const int16 *. |
| 440 |
/// \retval const int16 * valid for as many bytes as were allocated. |
| 441 |
|
| 442 |
const int16 * Buffer_int16 () const |
| 443 |
{ |
| 444 |
return (const int16 *) Buffer (); |
| 445 |
} |
| 446 |
|
| 447 |
/// Return pointer to allocated memory as a uint32 *. |
| 448 |
/// \retval uint32 * valid for as many bytes as were allocated. |
| 449 |
|
| 450 |
uint32 * Buffer_uint32 () |
| 451 |
{ |
| 452 |
return (uint32 *) Buffer (); |
| 453 |
} |
| 454 |
|
| 455 |
/// Return pointer to allocated memory as a const uint32 *. |
| 456 |
/// \retval const uint32 * valid for as many bytes as were allocated. |
| 457 |
|
| 458 |
const uint32 * Buffer_uint32 () const |
| 459 |
{ |
| 460 |
return (const uint32 *) Buffer (); |
| 461 |
} |
| 462 |
|
| 463 |
/// Return pointer to allocated memory as a int32 *. |
| 464 |
/// \retval int32 * valid for as many bytes as were allocated. |
| 465 |
|
| 466 |
int32 * Buffer_int32 () |
| 467 |
{ |
| 468 |
return (int32 *) Buffer (); |
| 469 |
} |
| 470 |
|
| 471 |
/// Return pointer to allocated memory as a const int32 *. |
| 472 |
/// \retval const int32 * valid for as many bytes as were allocated. |
| 473 |
|
| 474 |
const int32 * Buffer_int32 () const |
| 475 |
{ |
| 476 |
return (const int32 *) Buffer (); |
| 477 |
} |
| 478 |
|
| 479 |
/// Return pointer to allocated memory as a real32 *. |
| 480 |
/// \retval real32 * valid for as many bytes as were allocated. |
| 481 |
|
| 482 |
real32 * Buffer_real32 () |
| 483 |
{ |
| 484 |
return (real32 *) Buffer (); |
| 485 |
} |
| 486 |
|
| 487 |
/// Return pointer to allocated memory as a const real32 *. |
| 488 |
/// \retval const real32 * valid for as many bytes as were allocated. |
| 489 |
|
| 490 |
const real32 * Buffer_real32 () const |
| 491 |
{ |
| 492 |
return (const real32 *) Buffer (); |
| 493 |
} |
| 494 |
|
| 495 |
/// Return pointer to allocated memory as a real64 *. |
| 496 |
/// \retval real64 * valid for as many bytes as were allocated. |
| 497 |
|
| 498 |
real64 * Buffer_real64 () |
| 499 |
{ |
| 500 |
return (real64 *) Buffer (); |
| 501 |
} |
| 502 |
|
| 503 |
/// Return pointer to allocated memory as a const real64 *. |
| 504 |
/// \retval const real64 * valid for as many bytes as were allocated. |
| 505 |
|
| 506 |
const real64 * Buffer_real64 () const |
| 507 |
{ |
| 508 |
return (const real64 *) Buffer (); |
| 509 |
} |
| 510 |
|
| 511 |
}; |
| 512 |
|
| 513 |
/*****************************************************************************/ |
| 514 |
|
| 515 |
/// @brief Interface for dng_memory_block allocator. |
| 516 |
|
| 517 |
class dng_memory_allocator |
| 518 |
{ |
| 519 |
|
| 520 |
public: |
| 521 |
|
| 522 |
virtual ~dng_memory_allocator () |
| 523 |
{ |
| 524 |
} |
| 525 |
|
| 526 |
/// Allocate a dng_memory block. |
| 527 |
/// \param size Number of bytes in memory block. |
| 528 |
/// \retval A dng_memory_block with at least size bytes of valid storage. |
| 529 |
/// \exception dng_exception with fErrorCode equal to dng_error_memory. |
| 530 |
|
| 531 |
virtual dng_memory_block * Allocate (uint32 size); |
| 532 |
|
| 533 |
/// Directly allocate a block of at least 'size' bytes. |
| 534 |
/// \param size Number of bytes in memory block. |
| 535 |
/// \retval A pointer to a contiguous block of memory with at least |
| 536 |
/// size bytes of valid storage. |
| 537 |
/// Caller is responsible for freeing the memory with Free. |
| 538 |
/// Default implementation uses standard library 'malloc' routine. |
| 539 |
|
| 540 |
virtual void * Malloc (size_t size); |
| 541 |
|
| 542 |
/// Free the specified block of memory previously allocated with Malloc. |
| 543 |
/// Default implementation uses standard library 'free' routine. |
| 544 |
|
| 545 |
virtual void Free (void *ptr); |
| 546 |
|
| 547 |
}; |
| 548 |
|
| 549 |
/*****************************************************************************/ |
| 550 |
|
| 551 |
class dng_malloc_block : public dng_memory_block |
| 552 |
{ |
| 553 |
|
| 554 |
private: |
| 555 |
|
| 556 |
void *fMalloc; |
| 557 |
|
| 558 |
public: |
| 559 |
|
| 560 |
dng_malloc_block (uint32 logicalSize); |
| 561 |
|
| 562 |
virtual ~dng_malloc_block (); |
| 563 |
|
| 564 |
}; |
| 565 |
|
| 566 |
/*****************************************************************************/ |
| 567 |
|
| 568 |
/// @brief Default memory allocator used if NULL is passed in for allocator |
| 569 |
/// when constructing a dng_host. |
| 570 |
/// |
| 571 |
/// Uses new and delete for memory block object and malloc/free for underlying |
| 572 |
/// buffer. |
| 573 |
|
| 574 |
extern dng_memory_allocator gDefaultDNGMemoryAllocator; |
| 575 |
|
| 576 |
/*****************************************************************************/ |
| 577 |
|
| 578 |
/// @brief C++ allocator (i.e. an implementation of the Allocator concept) |
| 579 |
/// that throws a dng_exception with error code dng_error_memory if it cannot |
| 580 |
/// allocate memory. |
| 581 |
|
| 582 |
template <typename T> |
| 583 |
class dng_std_allocator |
| 584 |
{ |
| 585 |
|
| 586 |
public: |
| 587 |
|
| 588 |
typedef T value_type; |
| 589 |
|
| 590 |
#if defined(_MSC_VER) && _MSC_VER >= 1900 |
| 591 |
|
| 592 |
// Default implementations of default constructor and copy |
| 593 |
// constructor. |
| 594 |
|
| 595 |
dng_std_allocator () = default; |
| 596 |
|
| 597 |
// dng_std_allocator (const dng_std_allocator &) = default; |
| 598 |
|
| 599 |
template<class U> dng_std_allocator (const dng_std_allocator<U> &) {} |
| 600 |
|
| 601 |
#endif |
| 602 |
|
| 603 |
T * allocate (size_t n) |
| 604 |
{ |
| 605 |
const size_t size = SafeSizetMult (n, sizeof (T)); |
| 606 |
T *retval = static_cast<T *> (malloc (size)); |
| 607 |
if (!retval) |
| 608 |
{ |
| 609 |
ThrowMemoryFull (); |
| 610 |
} |
| 611 |
return retval; |
| 612 |
} |
| 613 |
|
| 614 |
void deallocate (T *ptr, |
| 615 |
size_t /* n */) |
| 616 |
{ |
| 617 |
free (ptr); |
| 618 |
} |
| 619 |
|
| 620 |
}; |
| 621 |
|
| 622 |
template <class T> |
| 623 |
bool operator== (const dng_std_allocator<T> & /* a1 */, |
| 624 |
const dng_std_allocator<T> & /* a2 */) |
| 625 |
{ |
| 626 |
return true; |
| 627 |
} |
| 628 |
|
| 629 |
template <class T> |
| 630 |
bool operator!= (const dng_std_allocator<T> & /* a1 */, |
| 631 |
const dng_std_allocator<T> & /* a2 */) |
| 632 |
{ |
| 633 |
return false; |
| 634 |
} |
| 635 |
|
| 636 |
/*****************************************************************************/ |
| 637 |
|
| 638 |
// std::vector specialized to use dng_std_allocator for allocation. |
| 639 |
|
| 640 |
#if 0 |
| 641 |
// original implementation without using custom allocator |
| 642 |
#define dng_std_vector std::vector |
| 643 |
#else |
| 644 |
// preferred implementation using custom allocator, requires C++11 |
| 645 |
template <class T> using dng_std_vector = std::vector<T, dng_std_allocator<T> >; |
| 646 |
#endif |
| 647 |
|
| 648 |
/*****************************************************************************/ |
| 649 |
|
| 650 |
#endif |
| 651 |
|
| 652 |
/*****************************************************************************/ |