InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SystemAllocator.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Jesse Jones (jejones)
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __SystemAllocator__
25 #define __SystemAllocator__
26 
27 #include "K2STLUtilities.h"
28 #include "K2Assert.h"
29 
32 template <class T>
34 public:
35  typedef size_t size_type;
36  typedef std::ptrdiff_t difference_type;
37  typedef T* pointer;
38  typedef const T* const_pointer;
39  typedef T& reference;
40  typedef const T& const_reference;
41  typedef T value_type;
42 
44  pointer address(reference x) const {return &x;}
46  const_pointer address(const_reference x) const {return &x;}
47 
49  pointer allocate(size_type n, const void* hint = nil);
51  void deallocate(pointer p, size_type /*n*/);
53  size_type max_size() const {return ULONG_MAX / sizeof(T);}
54 
56  void construct(pointer p, const T& value) {new(p) T(value);}
58  void destroy(pointer p) {p->~T();}
59 
61  bool16 operator==(const SystemAllocator<T>&) const {return true;}
63  bool16 operator!=(const SystemAllocator<T>&) const {return false;}
64 };
65 
66 
67 template <class T>
68 inline typename SystemAllocator<T>::pointer
69 SystemAllocator<T>::allocate(size_type n, const void* /*hint*/)
70 {
71  size_t bytes = n*sizeof(T);
72 
73 #if defined(MACINTOSH) || defined(WASM)
74  // This bypasses our leak detection code! DJB 4/11/2005.
75  void* ptr = malloc(bytes);
76  // But if I replace it, we crash for an unknown reason at static destructor time.
77  //void* ptr = new char[bytes];
78 
79 #elif defined(WINDOWS)
80  void* ptr = VirtualAlloc(nil, bytes, MEM_COMMIT, PAGE_READWRITE);
81 #endif
82 
83  return static_cast<pointer>(ptr);
84 }
85 
86 template <class T>
87 inline void SystemAllocator<T>::deallocate(pointer ptr, size_type /*n*/)
88 {
89  if (ptr != nil) {
90 #if defined(MACINTOSH) || defined(WASM)
91  free(ptr);
92  // delete [] ptr;
93 
94 #elif defined(WINDOWS)
95  BOOL success = VirtualFree(ptr, 0, MEM_RELEASE);
96  ASSERT(success);
97 #endif
98  }
99 }
100 
101 
102 #endif // __SystemAllocator__