-
Notifications
You must be signed in to change notification settings - Fork 7
/
allocator_malloc.h
130 lines (96 loc) · 3.65 KB
/
allocator_malloc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_ALLOCATOR_MALLOC_H
#define EASTL_ALLOCATOR_MALLOC_H
#include <eastl/EABase/eahave.h>
#include <eastl/allocator.h>
#include <stddef.h>
// EASTL_ALIGNED_MALLOC_AVAILABLE
//
// Identifies if the standard library provides a built-in aligned version of malloc.
// Defined as 0 or 1, depending on the standard library or platform availability.
// None of the viable C functions provides for an aligned malloc with offset, so we
// don't consider that supported in any case.
//
// Options for aligned allocations:
// C11 aligned_alloc http://linux.die.net/man/3/aligned_alloc
// glibc memalign http://linux.die.net/man/3/posix_memalign
// Posix posix_memalign http://pubs.opengroup.org/onlinepubs/000095399/functions/posix_memalign.html
// 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.
//
#if !defined EASTL_ALIGNED_MALLOC_AVAILABLE
#if defined(EA_PLATFORM_POSIX) && !defined(EA_PLATFORM_APPLE)
// memalign is more consistently available than posix_memalign, though its location isn't consistent across
// platforms and compiler libraries. Typically it's declared in one of three headers: stdlib.h, malloc.h, or malloc/malloc.h
#include <stdlib.h> // memalign, posix_memalign.
#define EASTL_ALIGNED_MALLOC_AVAILABLE 1
#if EA_HAS_INCLUDE_AVAILABLE
#if EA_HAS_INCLUDE(<malloc/malloc.h>)
#include <malloc/malloc.h>
#elif EA_HAS_INCLUDE(<malloc.h>)
#include <malloc.h>
#endif
#elif defined(EA_PLATFORM_BSD)
#include <malloc/malloc.h>
#elif defined(__clang__)
#if __has_include(<malloc/malloc.h>)
#include <malloc/malloc.h>
#elif __has_include(<malloc.h>)
#include <malloc.h>
#endif
#else
#include <malloc.h>
#endif
#else
#define EASTL_ALIGNED_MALLOC_AVAILABLE 0
#endif
#endif
namespace eastl
{
///////////////////////////////////////////////////////////////////////////////
// allocator_malloc
//
// Implements an EASTL allocator that uses malloc/free as opposed to
// new/delete or PPMalloc Malloc/Free.
//
// Example usage:
// vector<int, allocator_malloc> intVector;
//
class allocator_malloc
{
public:
allocator_malloc(const char* = NULL)
{ }
allocator_malloc(const allocator_malloc&)
{ }
allocator_malloc(const allocator_malloc&, const char*)
{ }
allocator_malloc& operator=(const allocator_malloc&)
{ return *this; }
bool operator==(const allocator_malloc&)
{ return true; }
bool operator!=(const allocator_malloc&)
{ return false; }
void* allocate(size_t n, int /*flags*/ = 0)
{ return malloc(n); }
void* allocate(size_t n, size_t alignment, size_t alignmentOffset, int /*flags*/ = 0)
{
#if EASTL_ALIGNED_MALLOC_AVAILABLE
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.
return memalign(alignment, n); // memalign is more consistently available than posix_memalign.
#else
if((alignment <= EASTL_SYSTEM_ALLOCATOR_MIN_ALIGNMENT) && ((alignmentOffset % alignment) == 0))
return malloc(n);
#endif
return NULL;
}
void deallocate(void* p, size_t /*n*/)
{ free(p); }
const char* getName() const
{ return "allocator_malloc"; }
void setName(const char*)
{ }
};
} // namespace eastl
#endif // Header include guard