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/BLI_temporary_allocator.h
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

65 lines
2.4 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.
*/
/** \file
* \ingroup bli
*
* This allocation method assumes
* 1. The allocations are short-lived.
* 2. The total number of allocations is bound by a constant per thread.
*
* These two assumptions make it possible to cache and reuse relatively large buffers. They allow
* to hand out buffers that are much larger than the requested size, without the fear of running
* out of memory.
*
* The assumptions might feel a bit limiting at first, but hold true in many cases. For example,
* many algorithms need to store temporary data. With this allocator, the allocation can become
* very cheap for common cases.
*
* Many cpu-bound algorithms can benefit from being split up into several stages, whereby the
* output of one stage is written into an array that is read by the next stage. This makes them
* easier to debug, profile and optimize. Often a reason this is not done is that the memory
* allocation might be expensive. The goal of this allocator is to make this a non-issue, by
* reusing the same long buffers over and over again.
*
* All allocated buffers are 64 byte aligned, to make them as reusable as possible.
* If the requested size is too large, there is a fallback to normal allocation. The allocation
* overhead is probably very small in these cases anyway.
*
* The best way to use this allocator is to use one of the prepared containers like TemporaryVector
* and TemporaryArray.
*/
#ifndef __BLI_TEMPORARY_ALLOCATOR_H__
#define __BLI_TEMPORARY_ALLOCATOR_H__
#include "BLI_utildefines.h"
#ifdef __cplusplus
extern "C" {
#endif
#define BLI_TEMPORARY_BUFFER_ALIGNMENT 64
void *BLI_temporary_allocate(uint size);
void BLI_temporary_deallocate(void *buffer);
#ifdef __cplusplus
}
#endif
#endif /* __BLI_TEMPORARY_ALLOCATOR_H__ */