This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenlib/intern/BLI_temporary_allocator.cc
Jacques Lucke 369d5e8ad2 BLI: new C++ ArrayRef, Vector, Stack, ... data structures
Many generic C++ data structures have been developed in the
functions branch. This commit merges a first chunk of them into
master. The following new data structures are included:

Array: Owns a memory buffer with a fixed size. It is different
  from std::array in that the size is not part of the type.

ArrayRef: References an array owned by someone else. All elements
  in the referenced array are considered to be const. This should
  be the preferred parameter type for functions that take arrays
  as input.

MutableArrayRef: References an array owned by someone else. The
  elements in the referenced array can be changed.

IndexRange: Specifies a continuous range of integers with a start
  and end index.

IntrusiveListBaseWrapper: A utility class that allows iterating
  over ListBase instances where the prev and next pointer are
  stored in the objects directly.

Stack: A stack implemented on top of a vector.

Vector: An array that can grow dynamically.

Allocators: Three allocator types are included that can be used
  by the container types to support different use cases.

The Stack and Vector support small object optimization. So when
the amount of elements in them is below a certain threshold, no
memory allocation is performed.

Additionally, most methods have unit tests.

I'm merging this without normal code review, after I checked the
code roughly with Sergey, and after we talked about it with Brecht.
2019-09-12 14:23:21 +02:00

116 lines
3.0 KiB
C++

/*
* 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.
*/
#include <mutex>
#include <stack>
#include "BLI_temporary_allocator.h"
#include "BLI_stack_cxx.h"
using namespace BLI;
constexpr uint ALIGNMENT = BLI_TEMPORARY_BUFFER_ALIGNMENT;
constexpr uint SMALL_BUFFER_SIZE = 64 * 1024;
constexpr uintptr_t ALIGNMENT_MASK = ~(uintptr_t)(ALIGNMENT - 1);
enum TemporaryBufferType {
Small,
Large,
};
struct MemHead {
void *raw_ptr;
TemporaryBufferType type;
};
static MemHead &get_memhead(void *aligned_ptr)
{
return *((MemHead *)aligned_ptr - 1);
}
static void *raw_allocate(uint size)
{
uint total_allocation_size = size + ALIGNMENT + sizeof(MemHead);
uintptr_t raw_ptr = (uintptr_t)malloc(total_allocation_size);
uintptr_t aligned_ptr = (raw_ptr + ALIGNMENT + sizeof(MemHead)) & ALIGNMENT_MASK;
MemHead &memhead = get_memhead((void *)aligned_ptr);
memhead.raw_ptr = (void *)raw_ptr;
return (void *)aligned_ptr;
}
static void raw_deallocate(void *ptr)
{
BLI_assert(((uintptr_t)ptr & ~ALIGNMENT_MASK) == 0);
MemHead &memhead = get_memhead(ptr);
void *raw_ptr = memhead.raw_ptr;
free(raw_ptr);
}
struct ThreadLocalBuffers {
uint allocated_amount = 0;
Stack<void *, 32, RawAllocator> buffers;
~ThreadLocalBuffers()
{
for (void *ptr : buffers) {
raw_deallocate(ptr);
}
}
};
thread_local ThreadLocalBuffers local_storage;
void *BLI_temporary_allocate(uint size)
{
/* The total amount of allocated buffers using this allocator should be limited by a constant. If
* it grows unbounded, there is likely a memory leak somewhere. */
BLI_assert(local_storage.allocated_amount < 100);
if (size <= SMALL_BUFFER_SIZE) {
auto &buffers = local_storage.buffers;
if (buffers.empty()) {
void *ptr = raw_allocate(SMALL_BUFFER_SIZE);
MemHead &memhead = get_memhead(ptr);
memhead.type = TemporaryBufferType::Small;
local_storage.allocated_amount++;
return ptr;
}
else {
return buffers.pop();
}
}
else {
void *ptr = raw_allocate(size);
MemHead &memhead = get_memhead(ptr);
memhead.type = TemporaryBufferType::Large;
return ptr;
}
}
void BLI_temporary_deallocate(void *buffer)
{
MemHead &memhead = get_memhead(buffer);
if (memhead.type == TemporaryBufferType::Small) {
auto &buffers = local_storage.buffers;
buffers.push(buffer);
}
else {
raw_deallocate(buffer);
}
}