From 3a08153d7a842b7ab1e40a9048730e1a3ddab5f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Fri, 31 May 2019 01:45:41 +0200 Subject: DRW: Refactor to support draw call batching Reviewers: brecht Differential Revision: D4997 --- source/blender/blenlib/BLI_memblock.h | 11 +++++++++-- source/blender/blenlib/intern/BLI_memblock.c | 23 ++++++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_memblock.h b/source/blender/blenlib/BLI_memblock.h index c5ef26ffb91..8bd8642a4e8 100644 --- a/source/blender/blenlib/BLI_memblock.h +++ b/source/blender/blenlib/BLI_memblock.h @@ -30,16 +30,20 @@ extern "C" { #include "BLI_compiler_attrs.h" +#define BLI_MEM_BLOCK_CHUNK_SIZE (1 << 15) /* 32KiB */ + struct BLI_memblock; typedef struct BLI_memblock BLI_memblock; typedef void (*MemblockValFreeFP)(void *val); -BLI_memblock *BLI_memblock_create(uint elem_size) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -void *BLI_memblock_alloc(BLI_memblock *mblk) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); +BLI_memblock *BLI_memblock_create_ex(uint elem_size, uint chunk_size) ATTR_WARN_UNUSED_RESULT; +void *BLI_memblock_alloc(BLI_memblock *mblk) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); void BLI_memblock_clear(BLI_memblock *mblk, MemblockValFreeFP valfreefp) ATTR_NONNULL(1); void BLI_memblock_destroy(BLI_memblock *mblk, MemblockValFreeFP free_callback) ATTR_NONNULL(1); +#define BLI_memblock_create(elem_size) BLI_memblock_create_ex(elem_size, BLI_MEM_BLOCK_CHUNK_SIZE) + typedef struct BLI_memblock_iter { void **chunk_list; int cur_index; @@ -53,6 +57,9 @@ typedef struct BLI_memblock_iter { void BLI_memblock_iternew(BLI_memblock *pool, BLI_memblock_iter *iter) ATTR_NONNULL(); void *BLI_memblock_iterstep(BLI_memblock_iter *iter) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +void *BLI_memblock_elem_get(BLI_memblock *mblk, int chunk, int elem) ATTR_WARN_UNUSED_RESULT + ATTR_NONNULL(); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/intern/BLI_memblock.c b/source/blender/blenlib/intern/BLI_memblock.c index f26860afe77..f7239f1b9d1 100644 --- a/source/blender/blenlib/intern/BLI_memblock.c +++ b/source/blender/blenlib/intern/BLI_memblock.c @@ -37,7 +37,6 @@ #include "BLI_strict_flags.h" /* keep last */ -#define BLI_MEM_BLOCK_CHUNK_SIZE (1 << 15) /* 32KiB */ #define CHUNK_LIST_SIZE 16 struct BLI_memblock { @@ -61,18 +60,19 @@ struct BLI_memblock { int chunk_len; }; -BLI_memblock *BLI_memblock_create(uint elem_size) +BLI_memblock *BLI_memblock_create_ex(uint elem_size, uint chunk_size) { - BLI_assert(elem_size < BLI_MEM_BLOCK_CHUNK_SIZE); + BLI_assert(elem_size < chunk_size); BLI_memblock *mblk = MEM_mallocN(sizeof(BLI_memblock), "BLI_memblock"); mblk->elem_size = (int)elem_size; mblk->elem_next = 0; mblk->elem_last = -1; - mblk->chunk_size = BLI_MEM_BLOCK_CHUNK_SIZE; + mblk->chunk_size = (int)chunk_size; mblk->chunk_len = CHUNK_LIST_SIZE; mblk->chunk_list = MEM_callocN(sizeof(void *) * (uint)mblk->chunk_len, "chunk list"); - mblk->chunk_list[0] = MEM_callocN((uint)mblk->chunk_size, "BLI_memblock chunk"); + mblk->chunk_list[0] = MEM_mallocN_aligned((uint)mblk->chunk_size, 32, "BLI_memblock chunk"); + memset(mblk->chunk_list[0], 0x0, (uint)mblk->chunk_size); mblk->chunk_max_ofs = (mblk->chunk_size / mblk->elem_size) * mblk->elem_size; mblk->elem_next_ofs = 0; mblk->chunk_next = 0; @@ -143,8 +143,9 @@ void *BLI_memblock_alloc(BLI_memblock *mblk) } if (UNLIKELY(mblk->chunk_list[mblk->chunk_next] == NULL)) { - mblk->chunk_list[mblk->chunk_next] = MEM_callocN((uint)mblk->chunk_size, - "BLI_memblock chunk"); + mblk->chunk_list[mblk->chunk_next] = MEM_mallocN_aligned( + (uint)mblk->chunk_size, 32, "BLI_memblock chunk"); + memset(mblk->chunk_list[mblk->chunk_next], 0x0, (uint)mblk->chunk_size); } } return ptr; @@ -180,3 +181,11 @@ void *BLI_memblock_iterstep(BLI_memblock_iter *iter) } return ptr; } + +/* Direct access. elem is element index inside the chosen chunk. */ +void *BLI_memblock_elem_get(BLI_memblock *mblk, int chunk, int elem) +{ + BLI_assert(chunk < mblk->chunk_len); + BLI_assert(elem < (mblk->chunk_size / mblk->elem_size)); + return (char *)(mblk->chunk_list[chunk]) + mblk->elem_size * elem; +} -- cgit v1.2.3