Nugget
allocator_malloc.h
1 // Copyright (c) Electronic Arts Inc. All rights reserved.
4 
5 
6 #ifndef EASTL_ALLOCATOR_MALLOC_H
7 #define EASTL_ALLOCATOR_MALLOC_H
8 
9 
10 #include <EABase/eahave.h>
11 #include <EASTL/allocator.h>
12 #include <stddef.h>
13 
14 
15 // EASTL_ALIGNED_MALLOC_AVAILABLE
16 //
17 // Identifies if the standard library provides a built-in aligned version of malloc.
18 // Defined as 0 or 1, depending on the standard library or platform availability.
19 // None of the viable C functions provides for an aligned malloc with offset, so we
20 // don't consider that supported in any case.
21 //
22 // Options for aligned allocations:
23 // C11 aligned_alloc http://linux.die.net/man/3/aligned_alloc
24 // glibc memalign http://linux.die.net/man/3/posix_memalign
25 // Posix posix_memalign http://pubs.opengroup.org/onlinepubs/000095399/functions/posix_memalign.html
26 // VC++ _aligned_malloc http://msdn.microsoft.com/en-us/library/8z34s9c6%28VS.80%29.aspx This is not suitable, since it has a limitation that you need to free via _aligned_free.
27 //
28 #if !defined EASTL_ALIGNED_MALLOC_AVAILABLE
29  #if defined(EA_PLATFORM_POSIX) && !defined(EA_PLATFORM_APPLE)
30  // memalign is more consistently available than posix_memalign, though its location isn't consistent across
31  // platforms and compiler libraries. Typically it's declared in one of three headers: stdlib.h, malloc.h, or malloc/malloc.h
32  #include <stdlib.h> // memalign, posix_memalign.
33  #define EASTL_ALIGNED_MALLOC_AVAILABLE 1
34 
35  #if EA_HAS_INCLUDE_AVAILABLE
36  #if EA_HAS_INCLUDE(<malloc/malloc.h>)
37  #include <malloc/malloc.h>
38  #elif EA_HAS_INCLUDE(<malloc.h>)
39  #include <malloc.h>
40  #endif
41  #elif defined(EA_PLATFORM_BSD)
42  #include <malloc/malloc.h>
43  #elif defined(__clang__)
44  #if __has_include(<malloc/malloc.h>)
45  #include <malloc/malloc.h>
46  #elif __has_include(<malloc.h>)
47  #include <malloc.h>
48  #endif
49  #else
50  #include <malloc.h>
51  #endif
52  #else
53  #define EASTL_ALIGNED_MALLOC_AVAILABLE 0
54  #endif
55 #endif
56 
57 
58 namespace eastl
59 {
60 
62  // allocator_malloc
63  //
64  // Implements an EASTL allocator that uses malloc/free as opposed to
65  // new/delete or PPMalloc Malloc/Free.
66  //
67  // Example usage:
68  // vector<int, allocator_malloc> intVector;
69  //
71  {
72  public:
73  allocator_malloc(const char* = NULL)
74  { }
75 
77  { }
78 
79  allocator_malloc(const allocator_malloc&, const char*)
80  { }
81 
82  allocator_malloc& operator=(const allocator_malloc&)
83  { return *this; }
84 
85  bool operator==(const allocator_malloc&)
86  { return true; }
87 
88  bool operator!=(const allocator_malloc&)
89  { return false; }
90 
91  void* allocate(size_t n, int /*flags*/ = 0)
92  { return malloc(n); }
93 
94  void* allocate(size_t n, size_t alignment, size_t alignmentOffset, int /*flags*/ = 0)
95  {
96  #if EASTL_ALIGNED_MALLOC_AVAILABLE
97  if((alignmentOffset % alignment) == 0) // We check for (offset % alignmnent == 0) instead of (offset == 0) because any block which is aligned on e.g. 64 also is aligned at an offset of 64 by definition.
98  return memalign(alignment, n); // memalign is more consistently available than posix_memalign.
99  #else
100  if((alignment <= EASTL_SYSTEM_ALLOCATOR_MIN_ALIGNMENT) && ((alignmentOffset % alignment) == 0))
101  return malloc(n);
102  #endif
103  return NULL;
104  }
105 
106  void deallocate(void* p, size_t /*n*/)
107  { free(p); }
108 
109  const char* get_name() const
110  { return "allocator_malloc"; }
111 
112  void set_name(const char*)
113  { }
114  };
115 
116 
117 } // namespace eastl
118 
119 
120 
121 #endif // Header include guard
122 
123 
124 
125 
126 
127 
128 
129 
130 
Definition: allocator_malloc.h:71
EA Standard Template Library.
Definition: algorithm.h:288