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
122
123
124
125
126
127
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup bli
*
* This file offers a couple of memory allocators that can be used with containers such as Vector
* and Map. Note that these allocators are not designed to work with standard containers like
* std::vector.
*
* Also see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html for why the standard
* allocators are not a good fit applications like Blender. The current implementations in this
* file are fairly simple still, more complexity can be added when necessary. For now they do their
* job good enough.
*/
#pragma once
#include <stdlib.h>
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BLI_math_base.h"
#include "BLI_temporary_allocator.h"
namespace BLI {
/**
* Use Blenders guarded allocator (aka MEM_malloc). This should always be used except there is a
* good reason not to use it.
*/
class GuardedAllocator {
public:
void *allocate(uint size, const char *name)
{
return MEM_mallocN(size, name);
}
void *allocate_aligned(uint size, uint alignment, const char *name)
{
alignment = std::max<uint>(alignment, 8);
return MEM_mallocN_aligned(size, alignment, name);
}
void deallocate(void *ptr)
{
MEM_freeN(ptr);
}
};
/**
* This is a simple wrapper around malloc/free. Only use this when the GuardedAllocator cannot be
* used. This can be the case when the allocated element might live longer than Blenders Allocator.
*/
class RawAllocator {
private:
struct MemHead {
int offset;
};
public:
void *allocate(uint size, const char *UNUSED(name))
{
void *ptr = malloc(size + sizeof(MemHead));
((MemHead *)ptr)->offset = sizeof(MemHead);
return POINTER_OFFSET(ptr, sizeof(MemHead));
}
void *allocate_aligned(uint size, uint alignment, const char *UNUSED(name))
{
BLI_assert(is_power_of_2_i((int)alignment));
void *ptr = malloc(size + alignment + sizeof(MemHead));
void *used_ptr = (void *)((uintptr_t)POINTER_OFFSET(ptr, alignment + sizeof(MemHead)) &
~((uintptr_t)alignment - 1));
uint offset = (uint)((uintptr_t)used_ptr - (uintptr_t)ptr);
BLI_assert(offset >= sizeof(MemHead));
((MemHead *)used_ptr - 1)->offset = (int)offset;
return used_ptr;
}
void deallocate(void *ptr)
{
MemHead *head = (MemHead *)ptr - 1;
int offset = -head->offset;
void *actual_pointer = POINTER_OFFSET(ptr, offset);
free(actual_pointer);
}
};
/**
* Use this only under specific circumstances as described in BLI_temporary_allocator.h.
*/
class TemporaryAllocator {
public:
void *allocate(uint size, const char *UNUSED(name))
{
return BLI_temporary_allocate(size);
}
void *allocate_aligned(uint size, uint alignment, const char *UNUSED(name))
{
BLI_assert(alignment <= 64);
UNUSED_VARS_NDEBUG(alignment);
return BLI_temporary_allocate(size);
}
void deallocate(void *ptr)
{
BLI_temporary_deallocate(ptr);
}
};
} // namespace BLI
|