diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-09-10 15:04:20 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-09-10 15:04:20 +0400 |
commit | 73be170fb56d4e1415ee9891b5488370f9fbd01d (patch) | |
tree | 88b44a89461b0e781cbe97e639cab8a87c7fdd60 /source/blender/blenlib/BLI_linklist_stack.h | |
parent | 561e82e927fddefe65e6367821ce30b18e45f6f4 (diff) |
add BLI_SMALLSTACK_***, macros: stack doesn't have a fixed size, it uses stack memory and never allocates.
it can be added and removed many times per function but will always remain the size of the maximum number of items.
not used yet, added for normal calculation function from mont29.
Diffstat (limited to 'source/blender/blenlib/BLI_linklist_stack.h')
-rw-r--r-- | source/blender/blenlib/BLI_linklist_stack.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_linklist_stack.h b/source/blender/blenlib/BLI_linklist_stack.h index e9cf820c6c8..4562ce2487e 100644 --- a/source/blender/blenlib/BLI_linklist_stack.h +++ b/source/blender/blenlib/BLI_linklist_stack.h @@ -40,6 +40,15 @@ * \note _##var##_type is a dummy var only used for typechecks. */ +/* -------------------------------------------------------------------- */ +/* Linked Stack using BLI_mempool + * + * Uses mempool for storage. + */ + +/** \name Linked Stack (mempool) + * \{ */ + #define BLI_LINKSTACK_DECLARE(var, type) \ LinkNode *var; \ BLI_mempool *_##var##_pool; \ @@ -87,4 +96,64 @@ #include "BLI_linklist.h" #include "BLI_mempool.h" +/** \} */ + + +/* -------------------------------------------------------------------- */ +/* Linked Stack, using stack memory (alloca) + * + * alloca never frees, pop'd items are stored in a free-list for reuse. + * only use for lists small enough to fit on the stack. + */ + + +/** \name Linked Stack (alloca) + * \{ */ + +#ifdef __GNUC__ +# define _BLI_SMALLSTACK_CAST(var) typeof(_##var##_type) +#else +# define _BLI_SMALLSTACK_CAST(var) +#endif + +#define BLI_SMALLSTACK_DECLARE(var, type) \ + LinkNode *_##var##_stack = NULL, *_##var##_free = NULL, *_##var##_temp = NULL; \ + type _##var##_type + +#define BLI_SMALLSTACK_PUSH(var, data) \ +{ \ + CHECK_TYPE_PAIR(data, _##var##_type); \ + if (_##var##_free) { \ + _##var##_temp = _##var##_free; \ + _##var##_free = _##var##_free->next; \ + } \ + else { \ + _##var##_temp = alloca(sizeof(LinkNode)); \ + } \ + _##var##_temp->next = _##var##_stack; \ + _##var##_temp->link = data; \ + _##var##_stack = _##var##_temp; \ +} (void)0 + +/* internal use, no null check */ +#define _BLI_SMALLSTACK_DEL(var) \ + (void)((_##var##_temp = _##var##_stack->next), \ + (_##var##_stack->next = _##var##_free), \ + (_##var##_free = _##var##_stack), \ + (_##var##_stack = _##var##_temp)) \ + +/* check for typeof() */ +#define BLI_SMALLSTACK_POP(var) \ + (_BLI_SMALLSTACK_CAST(var)) ((_##var##_stack) ? \ + (_BLI_SMALLSTACK_DEL(var), (_##var##_free->link)) : NULL) + +#define BLI_SMALLSTACK_FREE(var) { \ + (void)&(_##var##_type); \ +} (void)0 + +#include "BLI_alloca.h" + +/** \} */ + #endif /* __BLI_LINKLIST_STACK_H__ */ + |