Blender V5.3
BLI_mempool.cc File Reference
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include "atomic_ops.h"
#include "BLI_utildefines.hh"
#include "BLI_asan.hh"
#include "BLI_math_base_c.hh"
#include "BLI_mempool.hh"
#include "BLI_mempool_private.h"
#include "MEM_guardedalloc.h"
#include "BLI_strict_flags.hh"

Go to the source code of this file.

Classes

struct  blender::BLI_freenode
struct  blender::BLI_mempool_chunk
struct  blender::BLI_mempool

Namespaces

namespace  blender
 BMesh Iterators.

Macros

#define POISON_REDZONE_SIZE   0
#define MAKE_ID(a, b, c, d)
#define MAKE_ID_8(a, b, c, d, e, f, g, h)
#define FREEWORD
#define USEDWORD   MAKE_ID('u', 's', 'e', 'd')
#define USE_CHUNK_POW2
#define MEMPOOL_ELEM_SIZE_MIN   (sizeof(void *) * 2)
#define CHUNK_DATA(chunk)
#define NODE_STEP_NEXT(node)
#define NODE_STEP_PREV(node)
#define CHUNK_OVERHEAD   uint(MEM_SIZE_OVERHEAD + sizeof(BLI_mempool_chunk))

Functions

static void blender::mempool_asan_unlock (BLI_mempool *pool)
static void blender::mempool_asan_lock (BLI_mempool *pool)
BLI_INLINE BLI_mempool_chunk * blender::mempool_chunk_find (BLI_mempool_chunk *head, uint index)
BLI_INLINE uint blender::mempool_maxchunks (const uint elem_num, const uint pchunk)
static BLI_mempool_chunk * blender::mempool_chunk_alloc (const BLI_mempool *pool)
static BLI_freenode * blender::mempool_chunk_add (BLI_mempool *pool, BLI_mempool_chunk *mpchunk, BLI_freenode *last_tail)
static void blender::mempool_chunk_free (BLI_mempool_chunk *mpchunk, BLI_mempool *pool)
static void blender::mempool_chunk_free_all (BLI_mempool_chunk *mpchunk, BLI_mempool *pool)
BLI_mempool * blender::BLI_mempool_create (unsigned int esize, unsigned int elem_num, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
void * blender::BLI_mempool_alloc (BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
void * blender::BLI_mempool_calloc (BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
void blender::BLI_mempool_free (BLI_mempool *pool, void *addr) ATTR_NONNULL(1
int blender::BLI_mempool_len (const BLI_mempool *pool) ATTR_NONNULL(1)
void * blender::BLI_mempool_findelem (BLI_mempool *pool, unsigned int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void blender::BLI_mempool_as_array (BLI_mempool *pool, void *data) ATTR_NONNULL(1
void void * blender::BLI_mempool_as_arrayN (BLI_mempool *pool, const char *allocstr) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void blender::BLI_mempool_iternew (BLI_mempool *pool, BLI_mempool_iter *iter) ATTR_NONNULL()
static void blender::mempool_threadsafe_iternew (BLI_mempool *pool, BLI_mempool_threadsafe_iter *ts_iter)
ParallelMempoolTaskData * blender::mempool_iter_threadsafe_create (BLI_mempool *pool, const size_t iter_num)
void blender::mempool_iter_threadsafe_destroy (ParallelMempoolTaskData *iter_arr)
void * blender::BLI_mempool_iterstep (BLI_mempool_iter *iter) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void * blender::mempool_iter_threadsafe_step (BLI_mempool_threadsafe_iter *ts_iter)
void void blender::BLI_mempool_clear_ex (BLI_mempool *pool, int elem_num_reserve) ATTR_NONNULL(1)
void blender::BLI_mempool_clear (BLI_mempool *pool) ATTR_NONNULL(1)
void blender::BLI_mempool_destroy (BLI_mempool *pool) ATTR_NONNULL(1)
void void void blender::BLI_mempool_set_memory_debug ()

Variables

static bool blender::mempool_debug_memset = false

Detailed Description

Simple, fast memory allocator for allocating many elements of the same size.

Supports:

  • Freeing chunks.
  • Iterating over allocated chunks (optionally when using the #BLI_MEMPOOL_ALLOW_ITER flag).

Definition in file BLI_mempool.cc.

Macro Definition Documentation

◆ CHUNK_DATA

#define CHUNK_DATA ( chunk)
Value:
((BLI_freenode *)(CHECK_TYPE_INLINE(chunk, BLI_mempool_chunk *), (void *)((chunk) + 1)))
#define CHECK_TYPE_INLINE(val, type)

Definition at line 133 of file BLI_mempool.cc.

Referenced by blender::BLI_mempool_free(), blender::BLI_mempool_iterstep(), blender::mempool_chunk_add(), and blender::mempool_iter_threadsafe_step().

◆ CHUNK_OVERHEAD

#define CHUNK_OVERHEAD   uint(MEM_SIZE_OVERHEAD + sizeof(BLI_mempool_chunk))

Extra bytes implicitly used for every chunk alloc.

Definition at line 140 of file BLI_mempool.cc.

Referenced by blender::BLI_mempool_create().

◆ FREEWORD

#define FREEWORD
Value:
((sizeof(void *) > sizeof(int32_t)) ? MAKE_ID_8('e', 'e', 'r', 'f', 'f', 'r', 'e', 'e') : \
MAKE_ID('e', 'f', 'f', 'e'))
#define MAKE_ID_8(a, b, c, d, e, f, g, h)
#define MAKE_ID(a, b, c, d)
Definition blendthumb.hh:50

Important that this value is not aligned with sizeof(void *). So having a pointer to 2/4/8... aligned memory is enough to ensure the freeword will never be used. To be safe, use a word that's the same in both directions.

Definition at line 63 of file BLI_mempool.cc.

Referenced by blender::BLI_mempool_free(), blender::BLI_mempool_iterstep(), blender::mempool_chunk_add(), and blender::mempool_iter_threadsafe_step().

◆ MAKE_ID

#define MAKE_ID ( a,
b,
c,
d )
Value:
(int(d) << 24 | int(c) << 16 | (b) << 8 | (a))

Definition at line 52 of file BLI_mempool.cc.

◆ MAKE_ID_8

#define MAKE_ID_8 ( a,
b,
c,
d,
e,
f,
g,
h )
Value:
(int64_t(h) << 56 | int64_t(g) << 48 | int64_t(f) << 40 | int64_t(e) << 32 | int64_t(d) << 24 | \
int64_t(c) << 16 | int64_t(b) << 8 | (a))
long long int int64_t

Definition at line 53 of file BLI_mempool.cc.

◆ MEMPOOL_ELEM_SIZE_MIN

#define MEMPOOL_ELEM_SIZE_MIN   (sizeof(void *) * 2)

Definition at line 131 of file BLI_mempool.cc.

Referenced by blender::BLI_mempool_create().

◆ NODE_STEP_NEXT

#define NODE_STEP_NEXT ( node)
Value:
((BLI_freenode *)((char *)(node) + esize))

Definition at line 136 of file BLI_mempool.cc.

Referenced by blender::BLI_mempool_as_array(), blender::BLI_mempool_free(), and blender::mempool_chunk_add().

◆ NODE_STEP_PREV

#define NODE_STEP_PREV ( node)
Value:
((BLI_freenode *)((char *)(node) - esize))

Definition at line 137 of file BLI_mempool.cc.

Referenced by blender::BLI_mempool_free(), and blender::mempool_chunk_add().

◆ POISON_REDZONE_SIZE

◆ USE_CHUNK_POW2

#define USE_CHUNK_POW2

Definition at line 73 of file BLI_mempool.cc.

◆ USEDWORD

#define USEDWORD   MAKE_ID('u', 's', 'e', 'd')

The 'used' word just needs to be set to something besides FREEWORD.

Definition at line 70 of file BLI_mempool.cc.

Referenced by blender::BLI_mempool_alloc().