diff options
Diffstat (limited to 'source/blender/blenlib')
66 files changed, 1023 insertions, 750 deletions
diff --git a/source/blender/blenlib/BLI_array.h b/source/blender/blenlib/BLI_array.h index 3ffca818c0d..8e94b8197ef 100644 --- a/source/blender/blenlib/BLI_array.h +++ b/source/blender/blenlib/BLI_array.h @@ -62,9 +62,10 @@ * Doing the realloc in a macro isn't so simple, * so use a function the macros can use. */ -void _bli_array_grow_func(void **arr_p, const void *arr_static, - const int sizeof_arr_p, const int arr_count, const int num, - const char *alloc_str); +void _bli_array_grow_func( + void **arr_p, const void *arr_static, + const int sizeof_arr_p, const int arr_len, const int num, + const char *alloc_str); /* -------------------------------------------------------------------- */ @@ -74,18 +75,18 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, /** use ``sizeof(*(arr))`` to ensure the array exists and is an array */ #define BLI_array_declare(arr) \ - int _##arr##_count = ((void)(sizeof(*(arr))), 0); \ + int _##arr##_len = ((void)(sizeof(*(arr))), 0); \ void *_##arr##_static = NULL /** * this will use stack space, up to maxstatic array elements, before * switching to dynamic heap allocation */ #define BLI_array_staticdeclare(arr, maxstatic) \ - int _##arr##_count = 0; \ + int _##arr##_len = 0; \ char _##arr##_static[maxstatic * sizeof(*(arr))] /** returns the logical size of the array, not including buffering. */ -#define BLI_array_count(arr) ((void)0, _##arr##_count) +#define BLI_array_len(arr) ((void)0, _##arr##_len) /** * Grow the array by a fixed number of items. @@ -95,23 +96,23 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, #define BLI_array_reserve(arr, num) (void)( \ (((void *)(arr) == NULL) && \ ((void *)(_##arr##_static) != NULL) && \ - /* don't add _##arr##_count below because it must be zero */ \ - (_bli_array_totalsize_static(arr) >= _##arr##_count + (num))) ? \ + /* don't add _##arr##_len below because it must be zero */ \ + (_bli_array_totalsize_static(arr) >= _##arr##_len + (num))) ? \ /* we have an empty array and a static var big enough */ \ (void)(arr = (void *)_##arr##_static) \ : \ /* use existing static array or allocate */ \ - (LIKELY(_bli_array_totalsize(arr) >= _##arr##_count + (num)) ? \ + (LIKELY(_bli_array_totalsize(arr) >= _##arr##_len + (num)) ? \ (void)0 /* do nothing */ : \ _bli_array_grow_func((void **)&(arr), _##arr##_static, \ - sizeof(*(arr)), _##arr##_count, num, \ + sizeof(*(arr)), _##arr##_len, num, \ "BLI_array." #arr)) \ ) /** returns length of array */ #define BLI_array_grow_items(arr, num) \ - (BLI_array_reserve(arr, num), (_##arr##_count += num)) + (BLI_array_reserve(arr, num), (_##arr##_len += num)) #define BLI_array_grow_one(arr) \ BLI_array_grow_items(arr, 1) @@ -119,7 +120,7 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, /** appends an item to the array. */ #define BLI_array_append(arr, item) ( \ (void) BLI_array_grow_one(arr), \ - (void) (arr[_##arr##_count - 1] = item) \ + (void) (arr[_##arr##_len - 1] = item) \ ) /** @@ -127,13 +128,13 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, * item is not a pointer, but actual data value.*/ #define BLI_array_append_r(arr, item) ( \ (void) BLI_array_grow_one(arr), \ - (void) (arr[_##arr##_count - 1] = item), \ - (&arr[_##arr##_count - 1]) \ + (void) (arr[_##arr##_len - 1] = item), \ + (&arr[_##arr##_len - 1]) \ ) /** appends (grows) & returns a pointer to the uninitialized memory */ #define BLI_array_append_ret(arr) \ - (BLI_array_reserve(arr, 1), &arr[(_##arr##_count++)]) + (BLI_array_reserve(arr, 1), &arr[(_##arr##_len++)]) #define BLI_array_free(arr) { \ if (arr && (char *)arr != _##arr##_static) { \ @@ -143,26 +144,26 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, } ((void)0) #define BLI_array_pop(arr) ( \ - (arr && _##arr##_count) ? \ - arr[--_##arr##_count] : \ + (arr && _##arr##_len) ? \ + arr[--_##arr##_len] : \ NULL \ ) /** - * resets the logical size of an array to zero, but doesn't + * Resets the logical size of an array to zero, but doesn't * free the memory. */ -#define BLI_array_empty(arr) \ - { _##arr##_count = 0; } (void)0 +#define BLI_array_clear(arr) \ + { _##arr##_len = 0; } ((void)0) /** - * set the count of the array, doesn't actually increase the allocated array + * Set the length of the array, doesn't actually increase the allocated array * size. don't use this unless you know what you're doing. */ -#define BLI_array_count_set(arr, count) \ - { _##arr##_count = (count); }(void)0 +#define BLI_array_len_set(arr, len) \ + { _##arr##_len = (len); } ((void)0) /** only to prevent unused warnings */ #define BLI_array_fake_user(arr) \ - ((void)_##arr##_count, \ + ((void)_##arr##_len, \ (void)_##arr##_static) /** \} */ @@ -191,7 +192,7 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, #define BLI_array_fixedstack_free(arr) \ if (_##arr##_is_static) { \ MEM_freeN(arr); \ - } (void)0 + } ((void)0) /** \} */ diff --git a/source/blender/blenlib/BLI_array_utils.h b/source/blender/blenlib/BLI_array_utils.h index a46c87cec40..9a510bcfc3b 100644 --- a/source/blender/blenlib/BLI_array_utils.h +++ b/source/blender/blenlib/BLI_array_utils.h @@ -74,10 +74,18 @@ bool _bli_array_iter_span( bool use_wrap, bool use_delimit_bounds, bool (*test_fn)(const void *arr_item, void *user_data), void *user_data, unsigned int span_step[2], unsigned int *r_span_len); -#define BLI_array_iter_span(arr, arr_len, use_wrap, use_delimit_bounds, test_fn, user_data, \ - span_step, r_span_len) \ +#define BLI_array_iter_span( \ + arr, arr_len, use_wrap, use_delimit_bounds, test_fn, user_data, \ + span_step, r_span_len) \ _bli_array_iter_span( \ arr, arr_len, sizeof(*(arr)), use_wrap, use_delimit_bounds, test_fn, user_data, \ span_step, r_span_len) +bool _bli_array_is_zeroed( + const void *arr, + unsigned int arr_len, size_t arr_stride); +#define BLI_array_is_zeroed(arr, arr_len) \ + _bli_array_is_zeroed( \ + arr, arr_len, sizeof(*(arr))) + #endif /* __BLI_ARRAY_UTILS_H__ */ diff --git a/source/blender/blenlib/BLI_assert.h b/source/blender/blenlib/BLI_assert.h new file mode 100644 index 00000000000..b66b95b21fe --- /dev/null +++ b/source/blender/blenlib/BLI_assert.h @@ -0,0 +1,106 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __BLI_ASSERT_H__ +#define __BLI_ASSERT_H__ + +/** \file BLI_assert.h + * \ingroup bli + * + * Defines: + * - #BLI_assert + * - #BLI_STATIC_ASSERT + * - #BLI_STATIC_ASSERT_ALIGN + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef NDEBUG /* for BLI_assert */ +#include <stdio.h> +#endif + +/* BLI_assert(), default only to print + * for aborting need to define WITH_ASSERT_ABORT + */ +/* For 'abort' only. */ +#include <stdlib.h> + +#ifndef NDEBUG +# include "BLI_system.h" + /* _BLI_ASSERT_PRINT_POS */ +# if defined(__GNUC__) +# define _BLI_ASSERT_PRINT_POS(a) \ + fprintf(stderr, "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", __FILE__, __LINE__, __func__, #a) +# elif defined(_MSC_VER) +# define _BLI_ASSERT_PRINT_POS(a) \ + fprintf(stderr, "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", __FILE__, __LINE__, __FUNCTION__, #a) +# else +# define _BLI_ASSERT_PRINT_POS(a) \ + fprintf(stderr, "BLI_assert failed: %s:%d, at \'%s\'\n", __FILE__, __LINE__, #a) +# endif + /* _BLI_ASSERT_ABORT */ +# ifdef WITH_ASSERT_ABORT +# define _BLI_ASSERT_ABORT abort +# else +# define _BLI_ASSERT_ABORT() (void)0 +# endif + /* BLI_assert */ +# define BLI_assert(a) \ + (void)((!(a)) ? ((BLI_system_backtrace(stderr), _BLI_ASSERT_PRINT_POS(a), _BLI_ASSERT_ABORT(), NULL)) : NULL) +#else +# define BLI_assert(a) ((void)0) +#endif + +/* C++ can't use _Static_assert, expects static_assert() but c++0x only, + * Coverity also errors out. */ +#if (!defined(__cplusplus)) && \ + (!defined(__COVERITY__)) && \ + (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */ +# define BLI_STATIC_ASSERT(a, msg) __extension__ _Static_assert(a, msg); +#else +/* Code adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html */ +/* Note we need the two concats below because arguments to ## are not expanded, so we need to + * expand __LINE__ with one indirection before doing the actual concatenation. */ +# define _BLI_ASSERT_CONCAT_(a, b) a##b +# define _BLI_ASSERT_CONCAT(a, b) _BLI_ASSERT_CONCAT_(a, b) + /* These can't be used after statements in c89. */ +# if defined(__COUNTER__) /* MSVC */ +# define BLI_STATIC_ASSERT(a, msg) \ + ; enum { _BLI_ASSERT_CONCAT(static_assert_, __COUNTER__) = 1 / (int)(!!(a)) }; +# else /* older gcc, clang... */ + /* This can't be used twice on the same line so ensure if using in headers + * that the headers are not included twice (by wrapping in #ifndef...#endif) + * Note it doesn't cause an issue when used on same line of separate modules + * compiled with gcc -combine -fwhole-program. */ +# define BLI_STATIC_ASSERT(a, msg) \ + ; enum { _BLI_ASSERT_CONCAT(assert_line_, __LINE__) = 1 / (int)(!!(a)) }; +# endif +#endif + +#define BLI_STATIC_ASSERT_ALIGN(st, align) \ + BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned") + +#ifdef __cplusplus +} +#endif + +#endif /* __BLI_ASSERT_H__ */ diff --git a/source/blender/blenlib/BLI_boxpack2d.h b/source/blender/blenlib/BLI_boxpack_2d.h index b02e3423f88..80e89bdb04f 100644 --- a/source/blender/blenlib/BLI_boxpack2d.h +++ b/source/blender/blenlib/BLI_boxpack_2d.h @@ -25,10 +25,10 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_BOXPACK2D_H__ -#define __BLI_BOXPACK2D_H__ +#ifndef __BLI_BOXPACK_2D_H__ +#define __BLI_BOXPACK_2D_H__ -/** \file BLI_boxpack2d.h +/** \file BLI_boxpack_2d.h * \ingroup bli */ @@ -39,7 +39,7 @@ typedef struct BoxPack { float y; float w; float h; - + /* Verts this box uses * (BL,TR,TL,BR) / 0,1,2,3 */ struct BoxVert *v[4]; @@ -49,5 +49,5 @@ typedef struct BoxPack { void BLI_box_pack_2d(BoxPack *boxarray, const unsigned int len, float *tot_width, float *tot_height); -#endif +#endif /* __BLI_BOXPACK_2D_H__ */ diff --git a/source/blender/blenlib/BLI_buffer.h b/source/blender/blenlib/BLI_buffer.h index 0c0845daf19..0d2ef0dc9b3 100644 --- a/source/blender/blenlib/BLI_buffer.h +++ b/source/blender/blenlib/BLI_buffer.h @@ -77,7 +77,7 @@ enum { (BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_) \ ) -#define BLI_buffer_empty(buffer_) { \ +#define BLI_buffer_clear(buffer_) { \ (buffer_)->count = 0; \ } (void)0 diff --git a/source/blender/blenlib/BLI_console.h b/source/blender/blenlib/BLI_console.h new file mode 100644 index 00000000000..bf433f78f70 --- /dev/null +++ b/source/blender/blenlib/BLI_console.h @@ -0,0 +1,42 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * The Original Code is Copyright (C) 2018 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Srrgey Sharybin. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __BLI_CONSOLE_H__ +#define __BLI_CONSOLE_H__ + +/** \file BLI_console.h + * \ingroup bli + * \brief Set of utility functions and constants to work with consoles. + */ + +/* Format string where one could BLI_snprintf() R, G and B values + * and get proper marker to start colored output in the console. + */ +#define TRUECOLOR_ANSI_COLOR_FORMAT "\x1b[38;2;%d;%d;%dm" + +/* Marker which indicates that colored output is finished. */ +#define TRUECOLOR_ANSI_COLOR_FINISH "\x1b[0m" + +#endif /* __BLI_CONSOLE_H__ */ diff --git a/source/blender/blenlib/BLI_convexhull2d.h b/source/blender/blenlib/BLI_convexhull_2d.h index 4b82071ede8..000d28acdde 100644 --- a/source/blender/blenlib/BLI_convexhull2d.h +++ b/source/blender/blenlib/BLI_convexhull_2d.h @@ -18,10 +18,10 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_CONVEXHULL2D_H__ -#define __BLI_CONVEXHULL2D_H__ +#ifndef __BLI_CONVEXHULL_2D_H__ +#define __BLI_CONVEXHULL_2D_H__ -/** \file BLI_convexhull2d.h +/** \file BLI_convexhull_2d.h * \ingroup bli */ @@ -31,4 +31,4 @@ int BLI_convexhull_2d(const float (*points)[2], const int n, int r_points[]); float BLI_convexhull_aabb_fit_hull_2d(const float (*points_hull)[2], unsigned int n); float BLI_convexhull_aabb_fit_points_2d(const float (*points)[2], unsigned int n); -#endif /* __BLI_CONVEXHULL2D_H__ */ +#endif /* __BLI_CONVEXHULL_2D_H__ */ diff --git a/source/blender/blenlib/BLI_dial.h b/source/blender/blenlib/BLI_dial_2d.h index 71ab57bb61a..dbd71a8ad3f 100644 --- a/source/blender/blenlib/BLI_dial.h +++ b/source/blender/blenlib/BLI_dial_2d.h @@ -18,10 +18,10 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_DIAL_H__ -#define __BLI_DIAL_H__ +#ifndef __BLI_DIAL_2D_H__ +#define __BLI_DIAL_2D_H__ -/** \file BLI_dial.h +/** \file BLI_dial_2d.h * \ingroup bli * * \note dials act similar to old rotation based phones and output an angle. @@ -56,4 +56,4 @@ Dial *BLI_dial_initialize(const float start_position[2], float threshold); float BLI_dial_angle(Dial *dial, const float current_position[2]); -#endif /* __BLI_DIAL_H__ */ +#endif /* __BLI_DIAL_2D_H__ */ diff --git a/source/blender/blenlib/BLI_edgehash.h b/source/blender/blenlib/BLI_edgehash.h index 20272dd97cd..41789201265 100644 --- a/source/blender/blenlib/BLI_edgehash.h +++ b/source/blender/blenlib/BLI_edgehash.h @@ -59,7 +59,7 @@ bool BLI_edgehash_remove(EdgeHash *eh, unsigned int v0, unsigned int void *BLI_edgehash_popkey(EdgeHash *eh, unsigned int v0, unsigned int v1) ATTR_WARN_UNUSED_RESULT; bool BLI_edgehash_haskey(EdgeHash *eh, unsigned int v0, unsigned int v1) ATTR_WARN_UNUSED_RESULT; -int BLI_edgehash_size(EdgeHash *eh) ATTR_WARN_UNUSED_RESULT; +int BLI_edgehash_len(EdgeHash *eh) ATTR_WARN_UNUSED_RESULT; void BLI_edgehash_clear_ex(EdgeHash *eh, EdgeHashFreeFP valfreefp, const unsigned int nentries_reserve); void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP valfreefp); @@ -104,7 +104,7 @@ typedef struct EdgeSetIterator EdgeSetIterator; EdgeSet *BLI_edgeset_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; EdgeSet *BLI_edgeset_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -int BLI_edgeset_size(EdgeSet *es) ATTR_WARN_UNUSED_RESULT; +int BLI_edgeset_len(EdgeSet *es) ATTR_WARN_UNUSED_RESULT; bool BLI_edgeset_add(EdgeSet *es, unsigned int v0, unsigned int v1); void BLI_edgeset_insert(EdgeSet *es, unsigned int v0, unsigned int v1); bool BLI_edgeset_haskey(EdgeSet *eh, unsigned int v0, unsigned int v1) ATTR_WARN_UNUSED_RESULT; diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h index 5c1fa57886a..bb23c63bdb3 100644 --- a/source/blender/blenlib/BLI_fileops.h +++ b/source/blender/blenlib/BLI_fileops.h @@ -35,6 +35,7 @@ #include <stdio.h> #include <sys/stat.h> +#include <stdint.h> #ifdef __cplusplus extern "C" { diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h index b4819ff4e55..7003eb039dd 100644 --- a/source/blender/blenlib/BLI_ghash.h +++ b/source/blender/blenlib/BLI_ghash.h @@ -81,7 +81,10 @@ enum { #endif }; -/* *** */ +/** \name GHash API + * + * Defined in ``BLI_ghash.c`` + * \{ */ GHash *BLI_ghash_new_ex( GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info, @@ -110,11 +113,14 @@ void BLI_ghash_clear_ex( void *BLI_ghash_popkey(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp) ATTR_WARN_UNUSED_RESULT; bool BLI_ghash_haskey(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT; bool BLI_ghash_pop(GHash *gh, GHashIterState *state, void **r_key, void **r_val) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -unsigned int BLI_ghash_size(GHash *gh) ATTR_WARN_UNUSED_RESULT; +unsigned int BLI_ghash_len(GHash *gh) ATTR_WARN_UNUSED_RESULT; void BLI_ghash_flag_set(GHash *gh, unsigned int flag); void BLI_ghash_flag_clear(GHash *gh, unsigned int flag); -/* *** */ +/** \} */ + +/** \name GHash Iterator + * \{ */ GHashIterator *BLI_ghashIterator_new(GHash *gh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; @@ -149,94 +155,15 @@ BLI_INLINE bool BLI_ghashIterator_done(GHashIterator *ghi) { return !ghi BLI_ghashIterator_done(&gh_iter_) == false; \ BLI_ghashIterator_step(&gh_iter_), i_++) -/** \name Callbacks for GHash - * - * \note '_p' suffix denotes void pointer arg, - * so we can have functions that take correctly typed args too. - * \{ */ - -unsigned int BLI_ghashutil_ptrhash(const void *key); -bool BLI_ghashutil_ptrcmp(const void *a, const void *b); - -unsigned int BLI_ghashutil_strhash_n(const char *key, size_t n); -#define BLI_ghashutil_strhash(key) ( \ - CHECK_TYPE_ANY(key, char *, const char *, const char * const), \ - BLI_ghashutil_strhash_p(key)) -unsigned int BLI_ghashutil_strhash_p(const void *key); -unsigned int BLI_ghashutil_strhash_p_murmur(const void *key); -bool BLI_ghashutil_strcmp(const void *a, const void *b); - -#define BLI_ghashutil_inthash(key) ( \ - CHECK_TYPE_ANY(&(key), int *, const int *), \ - BLI_ghashutil_uinthash((unsigned int)key)) -unsigned int BLI_ghashutil_uinthash(unsigned int key); -unsigned int BLI_ghashutil_inthash_p(const void *ptr); -unsigned int BLI_ghashutil_inthash_p_murmur(const void *ptr); -unsigned int BLI_ghashutil_inthash_p_simple(const void *ptr); -bool BLI_ghashutil_intcmp(const void *a, const void *b); - -size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b); - - -unsigned int BLI_ghashutil_uinthash_v4(const unsigned int key[4]); -#define BLI_ghashutil_inthash_v4(key) ( \ - CHECK_TYPE_ANY(key, int *, const int *), \ - BLI_ghashutil_uinthash_v4((const unsigned int *)key)) -#define BLI_ghashutil_inthash_v4_p \ - ((GSetHashFP)BLI_ghashutil_uinthash_v4) -#define BLI_ghashutil_uinthash_v4_p \ - ((GSetHashFP)BLI_ghashutil_uinthash_v4) -unsigned int BLI_ghashutil_uinthash_v4_murmur(const unsigned int key[4]); -#define BLI_ghashutil_inthash_v4_murmur(key) ( \ - CHECK_TYPE_ANY(key, int *, const int *), \ - BLI_ghashutil_uinthash_v4_murmur((const unsigned int *)key)) -#define BLI_ghashutil_inthash_v4_p_murmur \ - ((GSetHashFP)BLI_ghashutil_uinthash_v4_murmur) -#define BLI_ghashutil_uinthash_v4_p_murmur \ - ((GSetHashFP)BLI_ghashutil_uinthash_v4_murmur) -bool BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b); -#define BLI_ghashutil_inthash_v4_cmp \ - BLI_ghashutil_uinthash_v4_cmp - /** \} */ -GHash *BLI_ghash_ptr_new_ex( - const char *info, - const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GHash *BLI_ghash_ptr_new( - const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GHash *BLI_ghash_str_new_ex( - const char *info, - const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GHash *BLI_ghash_str_new( - const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GHash *BLI_ghash_int_new_ex( - const char *info, - const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GHash *BLI_ghash_int_new( - const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GHash *BLI_ghash_pair_new_ex( - const char *info, - const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GHash *BLI_ghash_pair_new( - const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; - -typedef struct GHashPair { - const void *first; - const void *second; -} GHashPair; - -GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second); -unsigned int BLI_ghashutil_pairhash(const void *ptr); -bool BLI_ghashutil_paircmp(const void *a, const void *b); -void BLI_ghashutil_pairfree(void *ptr); - -/** - * GSet is a 'set' implementation (unordered collection of unique elements). +/** \name GSet API + * A 'set' implementation (unordered collection of unique elements). * * Internally this is a 'GHash' without any keys, * which is why this API's are in the same header & source file. - */ + * + * \{ */ typedef struct GSet GSet; @@ -247,21 +174,12 @@ typedef GHashKeyCopyFP GSetKeyCopyFP; typedef GHashIterState GSetIterState; -/* so we can cast but compiler sees as different */ -typedef struct GSetIterator { - GHashIterator _ghi -#ifdef __GNUC__ - __attribute__ ((deprecated)) -#endif - ; -} GSetIterator; - GSet *BLI_gset_new_ex( GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; GSet *BLI_gset_new(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; GSet *BLI_gset_copy(GSet *gs, GSetKeyCopyFP keycopyfp) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -unsigned int BLI_gset_size(GSet *gs) ATTR_WARN_UNUSED_RESULT; +unsigned int BLI_gset_len(GSet *gs) ATTR_WARN_UNUSED_RESULT; void BLI_gset_flag_set(GSet *gs, unsigned int flag); void BLI_gset_flag_clear(GSet *gs, unsigned int flag); void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp); @@ -281,14 +199,22 @@ void BLI_gset_clear(GSet *gs, GSetKeyFreeFP keyfreefp); void *BLI_gset_lookup(GSet *gh, const void *key) ATTR_WARN_UNUSED_RESULT; void *BLI_gset_pop_key(GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT; -GSet *BLI_gset_ptr_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GSet *BLI_gset_ptr_new(const char *info); -GSet *BLI_gset_str_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GSet *BLI_gset_str_new(const char *info); -GSet *BLI_gset_pair_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; -GSet *BLI_gset_pair_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +/** \} */ + +/** \name GSet Iterator + * \{ */ /* rely on inline api for now */ + +/* so we can cast but compiler sees as different */ +typedef struct GSetIterator { + GHashIterator _ghi +#ifdef __GNUC__ + __attribute__ ((deprecated)) +#endif + ; +} GSetIterator; + BLI_INLINE GSetIterator *BLI_gsetIterator_new(GSet *gs) { return (GSetIterator *)BLI_ghashIterator_new((GHash *)gs); } BLI_INLINE void BLI_gsetIterator_init(GSetIterator *gsi, GSet *gs) { BLI_ghashIterator_init((GHashIterator *)gsi, (GHash *)gs); } BLI_INLINE void BLI_gsetIterator_free(GSetIterator *gsi) { BLI_ghashIterator_free((GHashIterator *)gsi); } @@ -306,11 +232,15 @@ BLI_INLINE bool BLI_gsetIterator_done(GSetIterator *gsi) { return BLI_ghashItera BLI_gsetIterator_done(&gs_iter_) == false; \ BLI_gsetIterator_step(&gs_iter_), i_++) +/** \} */ + +/** \name GHash/GSet Debugging API's + * \{ */ /* For testing, debugging only */ #ifdef GHASH_INTERNAL_API -int BLI_ghash_buckets_size(GHash *gh); -int BLI_gset_buckets_size(GSet *gs); +int BLI_ghash_buckets_len(GHash *gh); +int BLI_gset_buckets_len(GSet *gs); double BLI_ghash_calc_quality_ex( GHash *gh, double *r_load, double *r_variance, @@ -321,6 +251,10 @@ double BLI_gset_calc_quality_ex( double BLI_ghash_calc_quality(GHash *gh); double BLI_gset_calc_quality(GSet *gs); #endif /* GHASH_INTERNAL_API */ +/** \} */ + +/** \name GHash/GSet Macros + * \{ */ #define GHASH_FOREACH_BEGIN(type, var, what) \ do { \ @@ -342,6 +276,113 @@ double BLI_gset_calc_quality(GSet *gs); } \ } while(0) +/** \} */ + +/** \name GHash/GSet Utils + * + * Defined in ``BLI_ghash_utils.c`` + * \{ */ + +/** + * Callbacks for GHash (``BLI_ghashutil_``) + * + * \note '_p' suffix denotes void pointer arg, + * so we can have functions that take correctly typed args too. + */ + +unsigned int BLI_ghashutil_ptrhash(const void *key); +bool BLI_ghashutil_ptrcmp(const void *a, const void *b); + +unsigned int BLI_ghashutil_strhash_n(const char *key, size_t n); +#define BLI_ghashutil_strhash(key) ( \ + CHECK_TYPE_ANY(key, char *, const char *, const char * const), \ + BLI_ghashutil_strhash_p(key)) +unsigned int BLI_ghashutil_strhash_p(const void *key); +unsigned int BLI_ghashutil_strhash_p_murmur(const void *key); +bool BLI_ghashutil_strcmp(const void *a, const void *b); + +#define BLI_ghashutil_inthash(key) ( \ + CHECK_TYPE_ANY(&(key), int *, const int *), \ + BLI_ghashutil_uinthash((unsigned int)key)) +unsigned int BLI_ghashutil_uinthash(unsigned int key); +unsigned int BLI_ghashutil_inthash_p(const void *ptr); +unsigned int BLI_ghashutil_inthash_p_murmur(const void *ptr); +unsigned int BLI_ghashutil_inthash_p_simple(const void *ptr); +bool BLI_ghashutil_intcmp(const void *a, const void *b); + +size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b); + + +unsigned int BLI_ghashutil_uinthash_v4(const unsigned int key[4]); +#define BLI_ghashutil_inthash_v4(key) ( \ + CHECK_TYPE_ANY(key, int *, const int *), \ + BLI_ghashutil_uinthash_v4((const unsigned int *)key)) +#define BLI_ghashutil_inthash_v4_p \ + ((GSetHashFP)BLI_ghashutil_uinthash_v4) +#define BLI_ghashutil_uinthash_v4_p \ + ((GSetHashFP)BLI_ghashutil_uinthash_v4) +unsigned int BLI_ghashutil_uinthash_v4_murmur(const unsigned int key[4]); +#define BLI_ghashutil_inthash_v4_murmur(key) ( \ + CHECK_TYPE_ANY(key, int *, const int *), \ + BLI_ghashutil_uinthash_v4_murmur((const unsigned int *)key)) +#define BLI_ghashutil_inthash_v4_p_murmur \ + ((GSetHashFP)BLI_ghashutil_uinthash_v4_murmur) +#define BLI_ghashutil_uinthash_v4_p_murmur \ + ((GSetHashFP)BLI_ghashutil_uinthash_v4_murmur) +bool BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b); +#define BLI_ghashutil_inthash_v4_cmp \ + BLI_ghashutil_uinthash_v4_cmp + +typedef struct GHashPair { + const void *first; + const void *second; +} GHashPair; + +GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second); +unsigned int BLI_ghashutil_pairhash(const void *ptr); +bool BLI_ghashutil_paircmp(const void *a, const void *b); +void BLI_ghashutil_pairfree(void *ptr); + +/** + * Wrapper GHash Creation Functions + */ + +GHash *BLI_ghash_ptr_new_ex( + const char *info, + const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GHash *BLI_ghash_ptr_new( + const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GHash *BLI_ghash_str_new_ex( + const char *info, + const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GHash *BLI_ghash_str_new( + const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GHash *BLI_ghash_int_new_ex( + const char *info, + const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GHash *BLI_ghash_int_new( + const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GHash *BLI_ghash_pair_new_ex( + const char *info, + const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GHash *BLI_ghash_pair_new( + const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; + +GSet *BLI_gset_ptr_new_ex( + const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GSet *BLI_gset_ptr_new( + const char *info); +GSet *BLI_gset_str_new_ex( + const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GSet *BLI_gset_str_new( + const char *info); +GSet *BLI_gset_pair_new_ex( + const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; +GSet *BLI_gset_pair_new( + const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT; + +/** \} */ + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/BLI_gsqueue.h b/source/blender/blenlib/BLI_gsqueue.h index 4600d6f6325..ef0e0f3ab31 100644 --- a/source/blender/blenlib/BLI_gsqueue.h +++ b/source/blender/blenlib/BLI_gsqueue.h @@ -36,11 +36,11 @@ typedef struct _GSQueue GSQueue; GSQueue *BLI_gsqueue_new(size_t elem_size); bool BLI_gsqueue_is_empty(GSQueue *gq); -int BLI_gsqueue_size(GSQueue *gq); +int BLI_gsqueue_len(GSQueue *gq); void BLI_gsqueue_peek(GSQueue *gq, void *r_item); void BLI_gsqueue_pop(GSQueue *gq, void *r_item); void BLI_gsqueue_push(GSQueue *gq, const void *item); -void BLI_gsqueue_pushback(GSQueue *gq, const void *item); +void BLI_gsqueue_push_back(GSQueue *gq, const void *item); void BLI_gsqueue_free(GSQueue *gq); #endif /* __BLI_GSQUEUE_H__ */ diff --git a/source/blender/blenlib/BLI_hash.h b/source/blender/blenlib/BLI_hash.h index e849e5f8f61..f74010479a9 100644 --- a/source/blender/blenlib/BLI_hash.h +++ b/source/blender/blenlib/BLI_hash.h @@ -63,4 +63,21 @@ BLI_INLINE unsigned int BLI_hash_int(unsigned int k) return BLI_hash_int_2d(k, 0); } +BLI_INLINE float BLI_hash_int_01(unsigned int k) +{ + return (float)BLI_hash_int(k) * (1.0f / (float)0xFFFFFFFF); +} + +BLI_INLINE void BLI_hash_pointer_to_color(const void *ptr, int *r, int *g, int *b) +{ + size_t val = (size_t)ptr; + const size_t hash_a = BLI_hash_int(val & 0x0000ffff); + const size_t hash_b = BLI_hash_int((uint)((val & 0xffff0000) >> 32)); + const size_t hash = + hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2)); + *r = (hash & 0xff0000) >> 16; + *g = (hash & 0x00ff00) >> 8; + *b = hash & 0x0000ff; +} + #endif // __BLI_HASH_H__ diff --git a/source/blender/blenlib/BLI_heap.h b/source/blender/blenlib/BLI_heap.h index d3f6d44e164..19e162d777f 100644 --- a/source/blender/blenlib/BLI_heap.h +++ b/source/blender/blenlib/BLI_heap.h @@ -41,9 +41,9 @@ HeapNode *BLI_heap_insert(Heap *heap, float value, void *ptr) ATTR_NONNULL void BLI_heap_insert_or_update(Heap *heap, HeapNode **node_p, float value, void *ptr) ATTR_NONNULL(1, 2); void BLI_heap_remove(Heap *heap, HeapNode *node) ATTR_NONNULL(1, 2); bool BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1); -unsigned int BLI_heap_size(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); +unsigned int BLI_heap_len(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); HeapNode *BLI_heap_top(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); -void *BLI_heap_popmin(Heap *heap) ATTR_NONNULL(1); +void *BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1); void BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value) ATTR_NONNULL(1, 2); void BLI_heap_node_value_update_ptr(Heap *heap, HeapNode *node, float value, void *ptr) ATTR_NONNULL(1, 2); diff --git a/source/blender/blenlib/BLI_iterator.h b/source/blender/blenlib/BLI_iterator.h index 49a3cea58bc..d3d375122a1 100644 --- a/source/blender/blenlib/BLI_iterator.h +++ b/source/blender/blenlib/BLI_iterator.h @@ -57,6 +57,6 @@ typedef void (*IteratorBeginCb)(BLI_Iterator *iter, void *data_in); #define ITER_END \ } \ callback_end_func(&iter_macro); \ -} +} ((void)0) #endif /* __BLI_ITERATOR_H__ */ diff --git a/source/blender/blenlib/BLI_jitter.h b/source/blender/blenlib/BLI_jitter_2d.h index 769fb445678..e2b1f21800c 100644 --- a/source/blender/blenlib/BLI_jitter.h +++ b/source/blender/blenlib/BLI_jitter_2d.h @@ -25,10 +25,10 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_JITTER_H__ -#define __BLI_JITTER_H__ +#ifndef __BLI_JITTER_2D_H__ +#define __BLI_JITTER_2D_H__ -/** \file BLI_jitter.h +/** \file BLI_jitter_2d.h * \ingroup bli */ @@ -36,5 +36,5 @@ void BLI_jitter_init(float (*jitarr)[2], int num); void BLI_jitterate1(float (*jit1)[2], float (*jit2)[2], int num, float radius1); void BLI_jitterate2(float (*jit1)[2], float (*jit2)[2], int num, float radius2); -#endif +#endif /* __BLI_JITTER_2D_H__ */ diff --git a/source/blender/blenlib/BLI_kdopbvh.h b/source/blender/blenlib/BLI_kdopbvh.h index 564659ad21e..db53035dc7b 100644 --- a/source/blender/blenlib/BLI_kdopbvh.h +++ b/source/blender/blenlib/BLI_kdopbvh.h @@ -129,7 +129,7 @@ BVHTreeOverlap *BLI_bvhtree_overlap( const BVHTree *tree1, const BVHTree *tree2, unsigned int *r_overlap_tot, BVHTree_OverlapCallback callback, void *userdata); -int BLI_bvhtree_get_size(const BVHTree *tree); +int BLI_bvhtree_get_len(const BVHTree *tree); float BLI_bvhtree_get_epsilon(const BVHTree *tree); diff --git a/source/blender/blenlib/BLI_lasso.h b/source/blender/blenlib/BLI_lasso_2d.h index 28f21e5bd85..3644224f1d7 100644 --- a/source/blender/blenlib/BLI_lasso.h +++ b/source/blender/blenlib/BLI_lasso_2d.h @@ -25,10 +25,10 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_LASSO_H__ -#define __BLI_LASSO_H__ +#ifndef __BLI_LASSO_2D_H__ +#define __BLI_LASSO_2D_H__ -/** \file BLI_lasso.h +/** \file BLI_lasso_2d.h * \ingroup bli */ @@ -38,4 +38,4 @@ void BLI_lasso_boundbox(struct rcti *rect, const int mcords[][2], const unsigned bool BLI_lasso_is_point_inside(const int mcords[][2], const unsigned int moves, const int sx, const int sy, const int error_value); bool BLI_lasso_is_edge_inside(const int mcords[][2], const unsigned int moves, int x0, int y0, int x1, int y1, const int error_value); -#endif +#endif /* __BLI_LASSO_2D_H__ */ diff --git a/source/blender/blenlib/BLI_link_utils.h b/source/blender/blenlib/BLI_link_utils.h index d469b105f93..5322547ac08 100644 --- a/source/blender/blenlib/BLI_link_utils.h +++ b/source/blender/blenlib/BLI_link_utils.h @@ -35,6 +35,18 @@ list = link; \ } (void)0 +/* Use for append (single linked list, storing the last element). */ +#define BLI_LINKS_APPEND(list, link) { \ + (link)->next = NULL; \ + if ((list)->first) { \ + (list)->last->next = link; \ + } \ + else { \ + (list)->first = link; \ + } \ + (list)->last = link; \ +} (void)0 + #define BLI_LINKS_FREE(list) { \ while (list) { \ void *next = list->next; \ diff --git a/source/blender/blenlib/BLI_linklist_stack.h b/source/blender/blenlib/BLI_linklist_stack.h index dd92dcec936..dd6d737f111 100644 --- a/source/blender/blenlib/BLI_linklist_stack.h +++ b/source/blender/blenlib/BLI_linklist_stack.h @@ -60,7 +60,7 @@ } (void)0 #define BLI_LINKSTACK_SIZE(var) \ - BLI_mempool_count(var##_pool_) + BLI_mempool_len(var##_pool_) /* check for typeof() */ #ifdef __GNUC__ diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h index 2a0f4e6f814..c4ad5acfe4b 100644 --- a/source/blender/blenlib/BLI_listbase.h +++ b/source/blender/blenlib/BLI_listbase.h @@ -74,7 +74,7 @@ void BLI_listbase_sort(struct ListBase *listbase, int (*cmp)(const void *, const void BLI_listbase_sort_r(ListBase *listbase, int (*cmp)(void *, const void *, const void *), void *thunk) ATTR_NONNULL(1, 2); bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL(); void BLI_freelist(struct ListBase *listbase) ATTR_NONNULL(1); -int BLI_listbase_count_ex(const struct ListBase *listbase, const int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); +int BLI_listbase_count_at_most(const struct ListBase *listbase, const int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1); @@ -103,32 +103,33 @@ struct LinkData *BLI_genericNodeN(void *data); * * \code{.c} * - * BLI_LISTBASE_CIRCULAR_FORWARD_BEGIN (listbase, item, item_init) { + * LISTBASE_CIRCULAR_FORWARD_BEGIN(listbase, item, item_init) + * { * ...operate on marker... * } - * BLI_LISTBASE_CIRCULAR_FORWARD_END (listbase, item, item_init); + * LISTBASE_CIRCULAR_FORWARD_END (listbase, item, item_init); * * \endcode */ -#define BLI_LISTBASE_CIRCULAR_FORWARD_BEGIN(lb, lb_iter, lb_init) \ +#define LISTBASE_CIRCULAR_FORWARD_BEGIN(lb, lb_iter, lb_init) \ if ((lb)->first && (lb_init || (lb_init = (lb)->first))) { \ lb_iter = lb_init; \ do { -#define BLI_LISTBASE_CIRCULAR_FORWARD_END(lb, lb_iter, lb_init) \ +#define LISTBASE_CIRCULAR_FORWARD_END(lb, lb_iter, lb_init) \ } while ((lb_iter = (lb_iter)->next ? (lb_iter)->next : (lb)->first), \ (lb_iter != lb_init)); \ } -#define BLI_LISTBASE_CIRCULAR_BACKWARD_BEGIN(lb, lb_iter, lb_init) \ +#define LISTBASE_CIRCULAR_BACKWARD_BEGIN(lb, lb_iter, lb_init) \ if ((lb)->last && (lb_init || (lb_init = (lb)->last))) { \ lb_iter = lb_init; \ do { -#define BLI_LISTBASE_CIRCULAR_BACKWARD_END(lb, lb_iter, lb_init) \ +#define LISTBASE_CIRCULAR_BACKWARD_END(lb, lb_iter, lb_init) \ } while ((lb_iter = (lb_iter)->prev ? (lb_iter)->prev : (lb)->last), \ (lb_iter != lb_init)); \ } -#define BLI_LISTBASE_FOREACH(type, var, list) \ +#define LISTBASE_FOREACH(type, var, list) \ for (type var = (type)((list)->first); \ var != NULL; \ var = (type)(((Link*)(var))->next)) diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 377b9325717..6f8e48d83b2 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -35,6 +35,7 @@ #endif #include <math.h> +#include "BLI_assert.h" #include "BLI_math_inline.h" #ifndef M_PI @@ -132,6 +133,10 @@ MINLINE int max_iiii(int a, int b, int c, int d); MINLINE size_t min_zz(size_t a, size_t b); MINLINE size_t max_zz(size_t a, size_t b); +MINLINE int clamp_i(int value, int min, int max); +MINLINE float clamp_f(float value, float min, float max); +MINLINE size_t clamp_z(size_t value, size_t min, size_t max); + MINLINE int compare_ff(float a, float b, const float max_diff); MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps); diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index 060d2e27740..d58e3022e27 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -85,6 +85,7 @@ bool is_quad_convex_v3(const float v1[3], const float v2[3], const float v3[3], bool is_quad_convex_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2]); bool is_poly_convex_v2(const float verts[][2], unsigned int nr); int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]); +bool is_quad_flip_v3_first_third_fast(const float v0[3], const float v1[3], const float v2[3], const float v3[3]); /********************************* Distance **********************************/ diff --git a/source/blender/blenlib/BLI_mempool.h b/source/blender/blenlib/BLI_mempool.h index b68ca6b1f2b..45efb8d7ef1 100644 --- a/source/blender/blenlib/BLI_mempool.h +++ b/source/blender/blenlib/BLI_mempool.h @@ -53,7 +53,7 @@ void BLI_mempool_clear_ex(BLI_mempool *pool, const int totelem_reserve) ATTR_NONNULL(1); void BLI_mempool_clear(BLI_mempool *pool) ATTR_NONNULL(1); void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1); -int BLI_mempool_count(BLI_mempool *pool) ATTR_NONNULL(1); +int BLI_mempool_len(BLI_mempool *pool) ATTR_NONNULL(1); void *BLI_mempool_findelem(BLI_mempool *pool, unsigned int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); void BLI_mempool_as_table(BLI_mempool *pool, void **data) ATTR_NONNULL(1, 2); diff --git a/source/blender/blenlib/BLI_polyfill2d.h b/source/blender/blenlib/BLI_polyfill_2d.h index 798055f9240..099f08d4663 100644 --- a/source/blender/blenlib/BLI_polyfill2d.h +++ b/source/blender/blenlib/BLI_polyfill_2d.h @@ -18,8 +18,8 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_POLYFILL2D_H__ -#define __BLI_POLYFILL2D_H__ +#ifndef __BLI_POLYFILL_2D_H__ +#define __BLI_POLYFILL_2D_H__ struct MemArena; @@ -40,4 +40,4 @@ void BLI_polyfill_calc( /* default size of polyfill arena */ #define BLI_POLYFILL_ARENA_SIZE MEM_SIZE_OPTIMAL(1 << 14) -#endif /* __BLI_POLYFILL2D_H__ */ +#endif /* __BLI_POLYFILL_2D_H__ */ diff --git a/source/blender/blenlib/BLI_polyfill2d_beautify.h b/source/blender/blenlib/BLI_polyfill_2d_beautify.h index 278771e9611..73b52125904 100644 --- a/source/blender/blenlib/BLI_polyfill2d_beautify.h +++ b/source/blender/blenlib/BLI_polyfill_2d_beautify.h @@ -18,8 +18,8 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_POLYFILL2D_BEAUTIFY_H__ -#define __BLI_POLYFILL2D_BEAUTIFY_H__ +#ifndef __BLI_POLYFILL_2D_BEAUTIFY_H__ +#define __BLI_POLYFILL_2D_BEAUTIFY_H__ struct Heap; struct MemArena; @@ -42,4 +42,4 @@ float BLI_polyfill_beautify_quad_rotate_calc_ex( /* avoid realloc's when creating new structures for polyfill ngons */ #define BLI_POLYFILL_ALLOC_NGON_RESERVE 64 -#endif /* __BLI_POLYFILL2D_BEAUTIFY_H__ */ +#endif /* __BLI_POLYFILL_2D_BEAUTIFY_H__ */ diff --git a/source/blender/blenlib/BLI_smallhash.h b/source/blender/blenlib/BLI_smallhash.h index e096354e5b1..495fc94a53c 100644 --- a/source/blender/blenlib/BLI_smallhash.h +++ b/source/blender/blenlib/BLI_smallhash.h @@ -66,7 +66,7 @@ bool BLI_smallhash_remove(SmallHash *sh, uintptr_t key) ATTR_NONNULL(1); void *BLI_smallhash_lookup(const SmallHash *sh, uintptr_t key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; void **BLI_smallhash_lookup_p(const SmallHash *sh, uintptr_t key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; bool BLI_smallhash_haskey(const SmallHash *sh, uintptr_t key) ATTR_NONNULL(1); -int BLI_smallhash_count(const SmallHash *sh) ATTR_NONNULL(1); +int BLI_smallhash_len(const SmallHash *sh) ATTR_NONNULL(1); void *BLI_smallhash_iternext(SmallHashIter *iter, uintptr_t *key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; void **BLI_smallhash_iternext_p(SmallHashIter *iter, uintptr_t *key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; void *BLI_smallhash_iternew(const SmallHash *sh, SmallHashIter *iter, uintptr_t *key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; diff --git a/source/blender/blenlib/BLI_sort_utils.h b/source/blender/blenlib/BLI_sort_utils.h index e08f4e5ac83..f6bd80b30d3 100644 --- a/source/blender/blenlib/BLI_sort_utils.h +++ b/source/blender/blenlib/BLI_sort_utils.h @@ -32,7 +32,7 @@ * \note keep \a sort_value first, * so cmp functions can be reused. */ -struct SortPointerByFloat { +struct SortPtrByFloat { float sort_value; void *data; }; @@ -42,7 +42,7 @@ struct SortIntByFloat { int data; }; -struct SortPointerByInt { +struct SortPtrByInt { int sort_value; void *data; }; @@ -58,4 +58,7 @@ int BLI_sortutil_cmp_float_reverse(const void *a_, const void *b_); int BLI_sortutil_cmp_int(const void *a_, const void *b_); int BLI_sortutil_cmp_int_reverse(const void *a_, const void *b_); +int BLI_sortutil_cmp_ptr(const void *a_, const void *b_); +int BLI_sortutil_cmp_ptr_reverse(const void *a_, const void *b_); + #endif /* __BLI_SORT_UTILS_H__ */ diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index d137806c575..48be9d1842f 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -103,6 +103,20 @@ int BLI_string_find_split_words( const char delim, int r_words[][2], int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +/** \name String Copy/Format Macros + * Avoid repeating destination with `sizeof(..)`. + * \note `ARRAY_SIZE` allows pointers on some platforms. + * \{ */ +#define STRNCPY(dst, src) \ + BLI_strncpy(dst, src, ARRAY_SIZE(dst)) +#define STRNCPY_RLEN(dst, src) \ + BLI_strncpy_rlen(dst, src, ARRAY_SIZE(dst)) +#define SNPRINTF(dst, format, ...) \ + BLI_snprintf(dst, ARRAY_SIZE(dst), format, __VA_ARGS__) +#define SNPRINTF_RLEN(dst, format, ...) \ + BLI_snprintf_rlen(dst, ARRAY_SIZE(dst), format, __VA_ARGS__) +/** \} */ + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h index 32504a88b48..21542d0d6e1 100644 --- a/source/blender/blenlib/BLI_string_utf8.h +++ b/source/blender/blenlib/BLI_string_utf8.h @@ -77,6 +77,16 @@ size_t BLI_str_partition_ex_utf8( #define BLI_UTF8_WIDTH_MAX 2 /* columns */ #define BLI_UTF8_ERR ((unsigned int)-1) +/** \name String Copy/Format Macros + * Avoid repeating destination with `sizeof(..)`. + * \note `ARRAY_SIZE` allows pointers on some platforms. + * \{ */ +#define STRNCPY_UTF8(dst, src) \ + BLI_strncpy_utf8(dst, src, ARRAY_SIZE(dst)) +#define STRNCPY_UTF8_RLEN(dst, src) \ + BLI_strncpy_utf8_rlen(dst, src, ARRAY_SIZE(dst)) +/** \} */ + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/BLI_threads.h b/source/blender/blenlib/BLI_threads.h index 60da6b39cbe..66eb5cfd22b 100644 --- a/source/blender/blenlib/BLI_threads.h +++ b/source/blender/blenlib/BLI_threads.h @@ -55,19 +55,19 @@ void BLI_threadapi_exit(void); struct TaskScheduler *BLI_task_scheduler_get(void); -void BLI_init_threads(struct ListBase *threadbase, void *(*do_thread)(void *), int tot); +void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot); int BLI_available_threads(struct ListBase *threadbase); -int BLI_available_thread_index(struct ListBase *threadbase); -void BLI_insert_thread(struct ListBase *threadbase, void *callerdata); -void BLI_remove_thread(struct ListBase *threadbase, void *callerdata); -void BLI_remove_thread_index(struct ListBase *threadbase, int index); -void BLI_remove_threads(struct ListBase *threadbase); -void BLI_end_threads(struct ListBase *threadbase); +int BLI_threadpool_available_thread_index(struct ListBase *threadbase); +void BLI_threadpool_insert(struct ListBase *threadbase, void *callerdata); +void BLI_threadpool_remove(struct ListBase *threadbase, void *callerdata); +void BLI_threadpool_remove_index(struct ListBase *threadbase, int index); +void BLI_threadpool_clear(struct ListBase *threadbase); +void BLI_threadpool_end(struct ListBase *threadbase); int BLI_thread_is_main(void); -void BLI_begin_threaded_malloc(void); -void BLI_end_threaded_malloc(void); +void BLI_threaded_malloc_begin(void); +void BLI_threaded_malloc_end(void); /* System Information */ @@ -91,8 +91,8 @@ int BLI_system_num_threads_override_get(void); #define LOCK_FFTW 9 #define LOCK_VIEW3D 10 -void BLI_lock_thread(int type); -void BLI_unlock_thread(int type); +void BLI_thread_lock(int type); +void BLI_thread_unlock(int type); /* Mutex Lock */ @@ -177,7 +177,7 @@ void BLI_thread_queue_free(ThreadQueue *queue); void BLI_thread_queue_push(ThreadQueue *queue, void *work); void *BLI_thread_queue_pop(ThreadQueue *queue); void *BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms); -int BLI_thread_queue_size(ThreadQueue *queue); +int BLI_thread_queue_len(ThreadQueue *queue); bool BLI_thread_queue_is_empty(ThreadQueue *queue); void BLI_thread_queue_wait_finish(ThreadQueue *queue); diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index ee1acc5afdd..46b3748c7ce 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -41,9 +41,8 @@ extern "C" { #include "BLI_compiler_compat.h" #include "BLI_utildefines_variadic.h" -#ifndef NDEBUG /* for BLI_assert */ -#include <stdio.h> -#endif +/* We could remove in future. */ +#include "BLI_assert.h" /* useful for finding bad use of min/max */ #if 0 @@ -412,7 +411,7 @@ extern "C" { } (void)0 /* assuming a static array */ -#if defined(__GNUC__) && !defined(__cplusplus) && !defined(__clang__) +#if defined(__GNUC__) && !defined(__cplusplus) && !defined(__clang__) && !defined(__INTEL_COMPILER) # define ARRAY_SIZE(arr) \ ((sizeof(struct {int isnt_array : ((const void *)&(arr) == &(arr)[0]);}) * 0) + \ (sizeof(arr) / sizeof(*(arr)))) @@ -615,74 +614,6 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size); # define UNUSED_VARS_NDEBUG UNUSED_VARS #endif - -/* BLI_assert(), default only to print - * for aborting need to define WITH_ASSERT_ABORT - */ -/* For 'abort' only. */ -#include <stdlib.h> - -#ifndef NDEBUG -# include "BLI_system.h" -# ifdef WITH_ASSERT_ABORT -# define _BLI_DUMMY_ABORT abort -# else -# define _BLI_DUMMY_ABORT() (void)0 -# endif -# if defined(__GNUC__) || defined(_MSC_VER) /* check __func__ is available */ -# define BLI_assert(a) \ - (void)((!(a)) ? ( \ - ( \ - BLI_system_backtrace(stderr), \ - fprintf(stderr, \ - "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", \ - __FILE__, __LINE__, __func__, STRINGIFY(a)), \ - _BLI_DUMMY_ABORT(), \ - NULL)) : NULL) -# else -# define BLI_assert(a) \ - (void)((!(a)) ? ( \ - ( \ - fprintf(stderr, \ - "BLI_assert failed: %s:%d, at \'%s\'\n", \ - __FILE__, __LINE__, STRINGIFY(a)), \ - _BLI_DUMMY_ABORT(), \ - NULL)) : NULL) -# endif -#else -# define BLI_assert(a) (void)0 -#endif - -/* C++ can't use _Static_assert, expects static_assert() but c++0x only, - * Coverity also errors out. */ -#if (!defined(__cplusplus)) && \ - (!defined(__COVERITY__)) && \ - (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */ -# define BLI_STATIC_ASSERT(a, msg) __extension__ _Static_assert(a, msg); -#else -/* Code adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html */ -/* Note we need the two concats below because arguments to ## are not expanded, so we need to - * expand __LINE__ with one indirection before doing the actual concatenation. */ -# define ASSERT_CONCAT_(a, b) a##b -# define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b) - /* These can't be used after statements in c89. */ -# if defined(__COUNTER__) /* MSVC */ -# define BLI_STATIC_ASSERT(a, msg) \ - ; enum { ASSERT_CONCAT(static_assert_, __COUNTER__) = 1 / (int)(!!(a)) }; -# else /* older gcc, clang... */ - /* This can't be used twice on the same line so ensure if using in headers - * that the headers are not included twice (by wrapping in #ifndef...#endif) - * Note it doesn't cause an issue when used on same line of separate modules - * compiled with gcc -combine -fwhole-program. */ -# define BLI_STATIC_ASSERT(a, msg) \ - ; enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1 / (int)(!!(a)) }; -# endif -#endif - - -#define BLI_STATIC_ASSERT_ALIGN(st, align) \ - BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned") - /* hints for branch prediction, only use in code that runs a _lot_ where */ #ifdef __GNUC__ # define LIKELY(x) __builtin_expect(!!(x), 1) diff --git a/source/blender/blenlib/BLI_voronoi.h b/source/blender/blenlib/BLI_voronoi_2d.h index 9d32061bf97..8d1ff2d4c2b 100644 --- a/source/blender/blenlib/BLI_voronoi.h +++ b/source/blender/blenlib/BLI_voronoi_2d.h @@ -23,12 +23,12 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef __BLI_VORONOI_H__ -#define __BLI_VORONOI_H__ +#ifndef __BLI_VORONOI_2D_H__ +#define __BLI_VORONOI_2D_H__ struct ListBase; -/** \file BLI_voronoi.h +/** \file BLI_voronoi_2d.h * \ingroup bli */ @@ -67,4 +67,4 @@ void BLI_voronoi_triangulate(const VoronoiSite *sites, int sites_total, struct L VoronoiTriangulationPoint **triangulated_points_r, int *triangulated_points_total_r, int (**triangles_r)[3], int *triangles_total_r); -#endif /* __BLI_VORONOI_H__ */ +#endif /* __BLI_VORONOI_2D_H__ */ diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 9e7b4ed8b6f..5bcf4303a84 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -41,10 +41,11 @@ set(INC_SYS set(SRC intern/BLI_args.c intern/BLI_array.c - intern/BLI_dial.c + intern/BLI_dial_2d.c intern/BLI_dynstr.c intern/BLI_filelist.c intern/BLI_ghash.c + intern/BLI_ghash_utils.c intern/BLI_heap.c intern/BLI_kdopbvh.c intern/BLI_kdtree.c @@ -58,10 +59,10 @@ set(SRC intern/array_utils.c intern/astar.c intern/bitmap_draw_2d.c - intern/boxpack2d.c + intern/boxpack_2d.c intern/buffer.c intern/callbacks.c - intern/convexhull2d.c + intern/convexhull_2d.c intern/dynlib.c intern/easing.c intern/edgehash.c @@ -73,8 +74,8 @@ set(SRC intern/gsqueue.c intern/hash_md5.c intern/hash_mm2a.c - intern/jitter.c - intern/lasso.c + intern/jitter_2d.c + intern/lasso_2d.c intern/list_sort_impl.h intern/listbase.c intern/math_base.c @@ -95,8 +96,8 @@ set(SRC intern/memory_utils.c intern/noise.c intern/path_util.c - intern/polyfill2d.c - intern/polyfill2d_beautify.c + intern/polyfill_2d.c + intern/polyfill_2d_beautify.c intern/quadric.c intern/rand.c intern/rct.c @@ -117,7 +118,7 @@ set(SRC intern/time.c intern/timecode.c intern/uvproject.c - intern/voronoi.c + intern/voronoi_2d.c intern/voxel.c intern/winstuff.c intern/winstuff_dir.c @@ -132,14 +133,15 @@ set(SRC BLI_bitmap.h BLI_bitmap_draw_2d.h BLI_blenlib.h - BLI_boxpack2d.h + BLI_boxpack_2d.h BLI_buffer.h BLI_callbacks.h BLI_compiler_attrs.h BLI_compiler_compat.h BLI_compiler_typecheck.h - BLI_convexhull2d.h - BLI_dial.h + BLI_console.h + BLI_convexhull_2d.h + BLI_dial_2d.h BLI_dlrbTree.h BLI_dynlib.h BLI_dynstr.h @@ -158,10 +160,10 @@ set(SRC BLI_hash_mm2a.h BLI_heap.h BLI_iterator.h - BLI_jitter.h + BLI_jitter_2d.h BLI_kdopbvh.h BLI_kdtree.h - BLI_lasso.h + BLI_lasso_2d.h BLI_link_utils.h BLI_linklist.h BLI_linklist_stack.h @@ -185,8 +187,8 @@ set(SRC BLI_mempool.h BLI_noise.h BLI_path_util.h - BLI_polyfill2d.h - BLI_polyfill2d_beautify.h + BLI_polyfill_2d.h + BLI_polyfill_2d_beautify.h BLI_quadric.h BLI_rand.h BLI_rect.h @@ -211,7 +213,7 @@ set(SRC BLI_utildefines_variadic.h BLI_uvproject.h BLI_vfontdata.h - BLI_voronoi.h + BLI_voronoi_2d.h BLI_voxel.h BLI_winstuff.h PIL_time.h diff --git a/source/blender/blenlib/intern/BLI_array.c b/source/blender/blenlib/intern/BLI_array.c index f681d222e69..d16dd36763d 100644 --- a/source/blender/blenlib/intern/BLI_array.c +++ b/source/blender/blenlib/intern/BLI_array.c @@ -66,21 +66,23 @@ /** * This function is only to be called via macros. * - * \note The caller must adjust \a arr_count + * \note The caller must adjust \a arr_len */ -void _bli_array_grow_func(void **arr_p, const void *arr_static, - const int sizeof_arr_p, const int arr_count, const int num, - const char *alloc_str) +void _bli_array_grow_func( + void **arr_p, const void *arr_static, + const int sizeof_arr_p, const int arr_len, const int num, + const char *alloc_str) { void *arr = *arr_p; void *arr_tmp; - arr_tmp = MEM_mallocN(sizeof_arr_p * - ((num < arr_count) ? - (arr_count * 2 + 2) : (arr_count + num)), alloc_str); + arr_tmp = MEM_mallocN( + sizeof_arr_p * + ((num < arr_len) ? + (arr_len * 2 + 2) : (arr_len + num)), alloc_str); if (arr) { - memcpy(arr_tmp, arr, sizeof_arr_p * arr_count); + memcpy(arr_tmp, arr, sizeof_arr_p * arr_len); if (arr != arr_static) { MEM_freeN(arr); @@ -91,6 +93,6 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, /* caller must do */ #if 0 - arr_count += num; + arr_len += num; #endif } diff --git a/source/blender/blenlib/intern/BLI_dial.c b/source/blender/blenlib/intern/BLI_dial_2d.c index 89f18fa10b4..d31367c5e87 100644 --- a/source/blender/blenlib/intern/BLI_dial.c +++ b/source/blender/blenlib/intern/BLI_dial_2d.c @@ -18,7 +18,11 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "BLI_dial.h" +/** \file blender/blenlib/intern/BLI_dial_2d.c + * \ingroup bli + */ + +#include "BLI_dial_2d.h" #include "BLI_math.h" #include "MEM_guardedalloc.h" diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index bf2f25fae69..ec75c140159 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -43,11 +43,12 @@ #include "BLI_sys_types.h" /* for intptr_t support */ #include "BLI_utildefines.h" -#include "BLI_hash_mm2a.h" #include "BLI_mempool.h" #define GHASH_INTERNAL_API -#include "BLI_ghash.h" +#include "BLI_ghash.h" /* own include */ + +/* keep last */ #include "BLI_strict_flags.h" /* -------------------------------------------------------------------- */ @@ -741,7 +742,7 @@ void BLI_ghash_reserve(GHash *gh, const uint nentries_reserve) /** * \return size of the GHash. */ -uint BLI_ghash_size(GHash *gh) +uint BLI_ghash_len(GHash *gh) { return gh->nentries; } @@ -1010,7 +1011,7 @@ void BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfree */ void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp) { - BLI_assert((int)gh->nentries == BLI_mempool_count(gh->entrypool)); + BLI_assert((int)gh->nentries == BLI_mempool_len(gh->entrypool)); if (keyfreefp || valfreefp) ghash_free_cb(gh, keyfreefp, valfreefp); @@ -1044,7 +1045,7 @@ void BLI_ghash_flag_clear(GHash *gh, uint flag) /** * Create a new GHashIterator. The hash table must not be mutated * while the iterator is in use, and the iterator will step exactly - * BLI_ghash_size(gh) times before becoming done. + * BLI_ghash_len(gh) times before becoming done. * * \param gh The GHash to iterate over. * \return Pointer to a new DynStr. @@ -1059,7 +1060,7 @@ GHashIterator *BLI_ghashIterator_new(GHash *gh) /** * Init an already allocated GHashIterator. The hash table must not * be mutated while the iterator is in use, and the iterator will - * step exactly BLI_ghash_size(gh) times before becoming done. + * step exactly BLI_ghash_len(gh) times before becoming done. * * \param ghi The GHashIterator to initialize. * \param gh The GHash to iterate over. @@ -1161,219 +1162,6 @@ bool BLI_ghashIterator_done(GHashIterator *ghi) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Generic Key Hash & Comparison Functions - * \{ */ - -/***/ - -#if 0 -/* works but slower */ -uint BLI_ghashutil_ptrhash(const void *key) -{ - return (uint)(intptr_t)key; -} -#else -/* based python3.3's pointer hashing function */ -uint BLI_ghashutil_ptrhash(const void *key) -{ - size_t y = (size_t)key; - /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid - * excessive hash collisions for dicts and sets */ - y = (y >> 4) | (y << (8 * sizeof(void *) - 4)); - return (uint)y; -} -#endif -bool BLI_ghashutil_ptrcmp(const void *a, const void *b) -{ - return (a != b); -} - -uint BLI_ghashutil_uinthash_v4(const uint key[4]) -{ - uint hash; - hash = key[0]; - hash *= 37; - hash += key[1]; - hash *= 37; - hash += key[2]; - hash *= 37; - hash += key[3]; - return hash; -} -uint BLI_ghashutil_uinthash_v4_murmur(const uint key[4]) -{ - return BLI_hash_mm2((const unsigned char *)key, sizeof(int) * 4 /* sizeof(key) */, 0); -} - -bool BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b) -{ - return (memcmp(a, b, sizeof(uint[4])) != 0); -} - -uint BLI_ghashutil_uinthash(uint key) -{ - key += ~(key << 16); - key ^= (key >> 5); - key += (key << 3); - key ^= (key >> 13); - key += ~(key << 9); - key ^= (key >> 17); - - return key; -} - -uint BLI_ghashutil_inthash_p(const void *ptr) -{ - uintptr_t key = (uintptr_t)ptr; - - key += ~(key << 16); - key ^= (key >> 5); - key += (key << 3); - key ^= (key >> 13); - key += ~(key << 9); - key ^= (key >> 17); - - return (uint)(key & 0xffffffff); -} - -uint BLI_ghashutil_inthash_p_murmur(const void *ptr) -{ - uintptr_t key = (uintptr_t)ptr; - - return BLI_hash_mm2((const unsigned char *)&key, sizeof(key), 0); -} - -uint BLI_ghashutil_inthash_p_simple(const void *ptr) -{ - return GET_UINT_FROM_POINTER(ptr); -} - -bool BLI_ghashutil_intcmp(const void *a, const void *b) -{ - return (a != b); -} - -size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b) -{ - return hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2)); -} - -/** - * This function implements the widely used "djb" hash apparently posted - * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit - * unsigned hash value starts at 5381 and for each byte 'c' in the - * string, is updated: ``hash = hash * 33 + c``. This - * function uses the signed value of each byte. - * - * note: this is the same hash method that glib 2.34.0 uses. - */ -uint BLI_ghashutil_strhash_n(const char *key, size_t n) -{ - const signed char *p; - uint h = 5381; - - for (p = (const signed char *)key; n-- && *p != '\0'; p++) { - h = (uint)((h << 5) + h) + (uint)*p; - } - - return h; -} -uint BLI_ghashutil_strhash_p(const void *ptr) -{ - const signed char *p; - uint h = 5381; - - for (p = ptr; *p != '\0'; p++) { - h = (uint)((h << 5) + h) + (uint)*p; - } - - return h; -} -uint BLI_ghashutil_strhash_p_murmur(const void *ptr) -{ - const unsigned char *key = ptr; - - return BLI_hash_mm2(key, strlen((const char *)key) + 1, 0); -} -bool BLI_ghashutil_strcmp(const void *a, const void *b) -{ - return (a == b) ? false : !STREQ(a, b); -} - -GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second) -{ - GHashPair *pair = MEM_mallocN(sizeof(GHashPair), "GHashPair"); - pair->first = first; - pair->second = second; - return pair; -} - -uint BLI_ghashutil_pairhash(const void *ptr) -{ - const GHashPair *pair = ptr; - uint hash = BLI_ghashutil_ptrhash(pair->first); - return hash ^ BLI_ghashutil_ptrhash(pair->second); -} - -bool BLI_ghashutil_paircmp(const void *a, const void *b) -{ - const GHashPair *A = a; - const GHashPair *B = b; - - return (BLI_ghashutil_ptrcmp(A->first, B->first) || - BLI_ghashutil_ptrcmp(A->second, B->second)); -} - -void BLI_ghashutil_pairfree(void *ptr) -{ - MEM_freeN(ptr); -} - -/** \} */ - -/* -------------------------------------------------------------------- */ -/** \name Convenience GHash Creation Functions - * \{ */ - -GHash *BLI_ghash_ptr_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); -} -GHash *BLI_ghash_ptr_new(const char *info) -{ - return BLI_ghash_ptr_new_ex(info, 0); -} - -GHash *BLI_ghash_str_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); -} -GHash *BLI_ghash_str_new(const char *info) -{ - return BLI_ghash_str_new_ex(info, 0); -} - -GHash *BLI_ghash_int_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, info, nentries_reserve); -} -GHash *BLI_ghash_int_new(const char *info) -{ - return BLI_ghash_int_new_ex(info, 0); -} - -GHash *BLI_ghash_pair_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); -} -GHash *BLI_ghash_pair_new(const char *info) -{ - return BLI_ghash_pair_new_ex(info, 0); -} - -/** \} */ - -/* -------------------------------------------------------------------- */ /** \name GSet Public API * * Use ghash API to give 'set' functionality @@ -1398,7 +1186,7 @@ GSet *BLI_gset_copy(GSet *gs, GHashKeyCopyFP keycopyfp) return (GSet *)ghash_copy((GHash *)gs, keycopyfp, NULL); } -uint BLI_gset_size(GSet *gs) +uint BLI_gset_len(GSet *gs) { return ((GHash *)gs)->nentries; } @@ -1557,7 +1345,7 @@ void *BLI_gset_lookup(GSet *gs, const void *key) /** * Returns the pointer to the key if it's found, removing it from the GSet. - * \node Caller must handle freeing. + * \note Caller must handle freeing. */ void *BLI_gset_pop_key(GSet *gs, const void *key) { @@ -1577,39 +1365,6 @@ void *BLI_gset_pop_key(GSet *gs, const void *key) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Convenience GSet Creation Functions - * \{ */ - -GSet *BLI_gset_ptr_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_gset_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); -} -GSet *BLI_gset_ptr_new(const char *info) -{ - return BLI_gset_ptr_new_ex(info, 0); -} - -GSet *BLI_gset_str_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_gset_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); -} -GSet *BLI_gset_str_new(const char *info) -{ - return BLI_gset_str_new_ex(info, 0); -} - -GSet *BLI_gset_pair_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_gset_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); -} -GSet *BLI_gset_pair_new(const char *info) -{ - return BLI_gset_pair_new_ex(info, 0); -} - -/** \} */ - -/* -------------------------------------------------------------------- */ /** \name Debugging & Introspection * \{ */ @@ -1618,13 +1373,13 @@ GSet *BLI_gset_pair_new(const char *info) /** * \return number of buckets in the GHash. */ -int BLI_ghash_buckets_size(GHash *gh) +int BLI_ghash_buckets_len(GHash *gh) { return (int)gh->nbuckets; } -int BLI_gset_buckets_size(GSet *gs) +int BLI_gset_buckets_len(GSet *gs) { - return BLI_ghash_buckets_size((GHash *)gs); + return BLI_ghash_buckets_len((GHash *)gs); } /** diff --git a/source/blender/blenlib/intern/BLI_ghash_utils.c b/source/blender/blenlib/intern/BLI_ghash_utils.c new file mode 100644 index 00000000000..6554ee7c92f --- /dev/null +++ b/source/blender/blenlib/intern/BLI_ghash_utils.c @@ -0,0 +1,288 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/blenlib/intern/BLI_ghash_utils.c + * \ingroup bli + * + * Helper functions and implementations of standard data types for #GHash + * (not it's implementation). + */ + +#include <string.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_utildefines.h" +#include "BLI_hash_mm2a.h" +#include "BLI_ghash.h" /* own include */ + +/* keep last */ +#include "BLI_strict_flags.h" + +/* -------------------------------------------------------------------- */ +/** \name Generic Key Hash & Comparison Functions + * \{ */ + +#if 0 +/* works but slower */ +uint BLI_ghashutil_ptrhash(const void *key) +{ + return (uint)(intptr_t)key; +} +#else +/* based python3.3's pointer hashing function */ +uint BLI_ghashutil_ptrhash(const void *key) +{ + size_t y = (size_t)key; + /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid + * excessive hash collisions for dicts and sets */ + y = (y >> 4) | (y << (8 * sizeof(void *) - 4)); + return (uint)y; +} +#endif +bool BLI_ghashutil_ptrcmp(const void *a, const void *b) +{ + return (a != b); +} + +uint BLI_ghashutil_uinthash_v4(const uint key[4]) +{ + uint hash; + hash = key[0]; + hash *= 37; + hash += key[1]; + hash *= 37; + hash += key[2]; + hash *= 37; + hash += key[3]; + return hash; +} +uint BLI_ghashutil_uinthash_v4_murmur(const uint key[4]) +{ + return BLI_hash_mm2((const unsigned char *)key, sizeof(int) * 4 /* sizeof(key) */, 0); +} + +bool BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b) +{ + return (memcmp(a, b, sizeof(uint[4])) != 0); +} + +uint BLI_ghashutil_uinthash(uint key) +{ + key += ~(key << 16); + key ^= (key >> 5); + key += (key << 3); + key ^= (key >> 13); + key += ~(key << 9); + key ^= (key >> 17); + + return key; +} + +uint BLI_ghashutil_inthash_p(const void *ptr) +{ + uintptr_t key = (uintptr_t)ptr; + + key += ~(key << 16); + key ^= (key >> 5); + key += (key << 3); + key ^= (key >> 13); + key += ~(key << 9); + key ^= (key >> 17); + + return (uint)(key & 0xffffffff); +} + +uint BLI_ghashutil_inthash_p_murmur(const void *ptr) +{ + uintptr_t key = (uintptr_t)ptr; + + return BLI_hash_mm2((const unsigned char *)&key, sizeof(key), 0); +} + +uint BLI_ghashutil_inthash_p_simple(const void *ptr) +{ + return GET_UINT_FROM_POINTER(ptr); +} + +bool BLI_ghashutil_intcmp(const void *a, const void *b) +{ + return (a != b); +} + +size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b) +{ + return hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2)); +} + +/** + * This function implements the widely used "djb" hash apparently posted + * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit + * unsigned hash value starts at 5381 and for each byte 'c' in the + * string, is updated: ``hash = hash * 33 + c``. This + * function uses the signed value of each byte. + * + * note: this is the same hash method that glib 2.34.0 uses. + */ +uint BLI_ghashutil_strhash_n(const char *key, size_t n) +{ + const signed char *p; + uint h = 5381; + + for (p = (const signed char *)key; n-- && *p != '\0'; p++) { + h = (uint)((h << 5) + h) + (uint)*p; + } + + return h; +} +uint BLI_ghashutil_strhash_p(const void *ptr) +{ + const signed char *p; + uint h = 5381; + + for (p = ptr; *p != '\0'; p++) { + h = (uint)((h << 5) + h) + (uint)*p; + } + + return h; +} +uint BLI_ghashutil_strhash_p_murmur(const void *ptr) +{ + const unsigned char *key = ptr; + + return BLI_hash_mm2(key, strlen((const char *)key) + 1, 0); +} +bool BLI_ghashutil_strcmp(const void *a, const void *b) +{ + return (a == b) ? false : !STREQ(a, b); +} + +GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second) +{ + GHashPair *pair = MEM_mallocN(sizeof(GHashPair), "GHashPair"); + pair->first = first; + pair->second = second; + return pair; +} + +uint BLI_ghashutil_pairhash(const void *ptr) +{ + const GHashPair *pair = ptr; + uint hash = BLI_ghashutil_ptrhash(pair->first); + return hash ^ BLI_ghashutil_ptrhash(pair->second); +} + +bool BLI_ghashutil_paircmp(const void *a, const void *b) +{ + const GHashPair *A = a; + const GHashPair *B = b; + + return (BLI_ghashutil_ptrcmp(A->first, B->first) || + BLI_ghashutil_ptrcmp(A->second, B->second)); +} + +void BLI_ghashutil_pairfree(void *ptr) +{ + MEM_freeN(ptr); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Convenience GHash Creation Functions + * \{ */ + +GHash *BLI_ghash_ptr_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); +} +GHash *BLI_ghash_ptr_new(const char *info) +{ + return BLI_ghash_ptr_new_ex(info, 0); +} + +GHash *BLI_ghash_str_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); +} +GHash *BLI_ghash_str_new(const char *info) +{ + return BLI_ghash_str_new_ex(info, 0); +} + +GHash *BLI_ghash_int_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, info, nentries_reserve); +} +GHash *BLI_ghash_int_new(const char *info) +{ + return BLI_ghash_int_new_ex(info, 0); +} + +GHash *BLI_ghash_pair_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); +} +GHash *BLI_ghash_pair_new(const char *info) +{ + return BLI_ghash_pair_new_ex(info, 0); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Convenience GSet Creation Functions + * \{ */ + +GSet *BLI_gset_ptr_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_gset_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); +} +GSet *BLI_gset_ptr_new(const char *info) +{ + return BLI_gset_ptr_new_ex(info, 0); +} + +GSet *BLI_gset_str_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_gset_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); +} +GSet *BLI_gset_str_new(const char *info) +{ + return BLI_gset_str_new_ex(info, 0); +} + +GSet *BLI_gset_pair_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_gset_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); +} +GSet *BLI_gset_pair_new(const char *info) +{ + return BLI_gset_pair_new_ex(info, 0); +} + +/** \} */ diff --git a/source/blender/blenlib/intern/BLI_heap.c b/source/blender/blenlib/intern/BLI_heap.c index 7c249344541..0c71e75e40f 100644 --- a/source/blender/blenlib/intern/BLI_heap.c +++ b/source/blender/blenlib/intern/BLI_heap.c @@ -303,7 +303,7 @@ bool BLI_heap_is_empty(const Heap *heap) return (heap->size == 0); } -uint BLI_heap_size(const Heap *heap) +uint BLI_heap_len(const Heap *heap) { return heap->size; } @@ -320,7 +320,7 @@ HeapNode *BLI_heap_top(const Heap *heap) /** * Pop the top node off the heap and return it's pointer. */ -void *BLI_heap_popmin(Heap *heap) +void *BLI_heap_pop_min(Heap *heap) { BLI_assert(heap->size != 0); @@ -348,7 +348,7 @@ void BLI_heap_remove(Heap *heap, HeapNode *node) i = p; } - BLI_heap_popmin(heap); + BLI_heap_pop_min(heap); } /** diff --git a/source/blender/blenlib/intern/BLI_kdopbvh.c b/source/blender/blenlib/intern/BLI_kdopbvh.c index 9c055a227a9..06e8ade68a2 100644 --- a/source/blender/blenlib/intern/BLI_kdopbvh.c +++ b/source/blender/blenlib/intern/BLI_kdopbvh.c @@ -856,9 +856,8 @@ static void non_recursive_bvh_div_nodes_task_cb( else { break; } - - parent->totnode = (char)(k + 1); } + parent->totnode = (char)k; } /** @@ -1147,7 +1146,7 @@ void BLI_bvhtree_update_tree(BVHTree *tree) * Number of times #BLI_bvhtree_insert has been called. * mainly useful for asserts functions to check we added the correct number. */ -int BLI_bvhtree_get_size(const BVHTree *tree) +int BLI_bvhtree_get_len(const BVHTree *tree) { return tree->totleaf; } @@ -1924,6 +1923,7 @@ void BLI_bvhtree_ray_cast_all( BLI_bvhtree_ray_cast_all_ex(tree, co, dir, radius, hit_dist, callback, userdata, BVH_RAYCAST_DEFAULT); } +/** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index 5b13f129ad4..3a65e6c42ca 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -474,7 +474,7 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) } } -int BLI_mempool_count(BLI_mempool *pool) +int BLI_mempool_len(BLI_mempool *pool) { return (int)pool->totused; } diff --git a/source/blender/blenlib/intern/array_store.c b/source/blender/blenlib/intern/array_store.c index 5b1715bbb3d..df93dad4c32 100644 --- a/source/blender/blenlib/intern/array_store.c +++ b/source/blender/blenlib/intern/array_store.c @@ -299,7 +299,7 @@ typedef struct BChunk { } BChunk; /** - * Links to store #BChunk data in #BChunkList.chunks. + * Links to store #BChunk data in #BChunkList.chunk_refs. */ typedef struct BChunkRef { struct BChunkRef *next, *prev; @@ -749,6 +749,7 @@ static void bchunk_list_fill_from_array( ASSERT_CHUNKLIST_DATA(chunk_list, data); } +/** \} */ /* --------------------------------------------------------------------------- * Internal Table Lookup Functions @@ -1013,6 +1014,10 @@ static const BChunkRef *table_lookup( /** \} */ +/** \name Main Data De-Duplication Function + * + * \{ */ + /** * \param data: Data to store in the returned value. * \param data_len_original: Length of data in bytes. @@ -1504,6 +1509,8 @@ void BLI_array_store_clear( BLI_mempool_clear(bs->memory.chunk); } +/** \} */ + /** \name BArrayStore Statistics * \{ */ @@ -1759,7 +1766,7 @@ bool BLI_array_store_is_valid( goto user_finally; } } - if (!(BLI_mempool_count(bs->memory.chunk_list) == (int)BLI_ghash_size(chunk_list_map))) { + if (!(BLI_mempool_len(bs->memory.chunk_list) == (int)BLI_ghash_len(chunk_list_map))) { ok = false; goto user_finally; } @@ -1772,11 +1779,11 @@ bool BLI_array_store_is_valid( totrefs += 1; } } - if (!(BLI_mempool_count(bs->memory.chunk) == (int)BLI_ghash_size(chunk_map))) { + if (!(BLI_mempool_len(bs->memory.chunk) == (int)BLI_ghash_len(chunk_map))) { ok = false; goto user_finally; } - if (!(BLI_mempool_count(bs->memory.chunk_ref) == totrefs)) { + if (!(BLI_mempool_len(bs->memory.chunk_ref) == totrefs)) { ok = false; goto user_finally; } diff --git a/source/blender/blenlib/intern/array_utils.c b/source/blender/blenlib/intern/array_utils.c index 32f0111babd..7b2d35a763c 100644 --- a/source/blender/blenlib/intern/array_utils.c +++ b/source/blender/blenlib/intern/array_utils.c @@ -308,3 +308,20 @@ bool _bli_array_iter_span( return false; } + +/** + * Simple utility to check memory is zeroed. + */ +bool _bli_array_is_zeroed( + const void *arr_v, + unsigned int arr_len, size_t arr_stride) +{ + const char *arr_step = (const char *)arr_v; + size_t i = arr_stride * arr_len; + while (i--) { + if (*(arr_step++)) { + return false; + } + } + return true; +} diff --git a/source/blender/blenlib/intern/astar.c b/source/blender/blenlib/intern/astar.c index 0020dbe4612..1b57dc5a683 100644 --- a/source/blender/blenlib/intern/astar.c +++ b/source/blender/blenlib/intern/astar.c @@ -231,7 +231,7 @@ bool BLI_astar_graph_solve( SET_INT_IN_POINTER(node_index_src)); while (!BLI_heap_is_empty(todo_nodes)) { - const int node_curr_idx = GET_INT_FROM_POINTER(BLI_heap_popmin(todo_nodes)); + const int node_curr_idx = GET_INT_FROM_POINTER(BLI_heap_pop_min(todo_nodes)); BLI_AStarGNode *node_curr = &as_graph->nodes[node_curr_idx]; LinkData *ld; diff --git a/source/blender/blenlib/intern/boxpack2d.c b/source/blender/blenlib/intern/boxpack_2d.c index ff17d2ac28b..cf9e29209fb 100644 --- a/source/blender/blenlib/intern/boxpack2d.c +++ b/source/blender/blenlib/intern/boxpack_2d.c @@ -20,7 +20,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/boxpack2d.c +/** \file blender/blenlib/intern/boxpack_2d.c * \ingroup bli */ @@ -30,7 +30,7 @@ #include "MEM_guardedalloc.h" #include "BLI_utildefines.h" -#include "BLI_boxpack2d.h" /* own include */ +#include "BLI_boxpack_2d.h" /* own include */ #include "BLI_sort.h" /* qsort_r */ #define qsort_r BLI_qsort_r diff --git a/source/blender/blenlib/intern/convexhull2d.c b/source/blender/blenlib/intern/convexhull_2d.c index 1f1f214ce32..38928dbbaa0 100644 --- a/source/blender/blenlib/intern/convexhull2d.c +++ b/source/blender/blenlib/intern/convexhull_2d.c @@ -18,7 +18,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/convexhull2d.c +/** \file blender/blenlib/intern/convexhull_2d.c * \ingroup bli */ @@ -28,7 +28,7 @@ #include "MEM_guardedalloc.h" -#include "BLI_convexhull2d.h" +#include "BLI_convexhull_2d.h" #include "BLI_math.h" #include "BLI_strict_flags.h" #include "BLI_utildefines.h" diff --git a/source/blender/blenlib/intern/edgehash.c b/source/blender/blenlib/intern/edgehash.c index 772c8bd6247..7074a776aaf 100644 --- a/source/blender/blenlib/intern/edgehash.c +++ b/source/blender/blenlib/intern/edgehash.c @@ -527,7 +527,7 @@ bool BLI_edgehash_haskey(EdgeHash *eh, uint v0, uint v1) /** * Return number of keys in hash. */ -int BLI_edgehash_size(EdgeHash *eh) +int BLI_edgehash_len(EdgeHash *eh) { return (int)eh->nentries; } @@ -565,7 +565,7 @@ void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP valfreefp) void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp) { - BLI_assert((int)eh->nentries == BLI_mempool_count(eh->epool)); + BLI_assert((int)eh->nentries == BLI_mempool_len(eh->epool)); if (valfreefp) edgehash_free_cb(eh, valfreefp); @@ -599,7 +599,7 @@ void BLI_edgehash_flag_clear(EdgeHash *eh, uint flag) /** * Create a new EdgeHashIterator. The hash table must not be mutated * while the iterator is in use, and the iterator will step exactly - * BLI_edgehash_size(eh) times before becoming done. + * BLI_edgehash_len(eh) times before becoming done. */ EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) { @@ -611,7 +611,7 @@ EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) /** * Init an already allocated EdgeHashIterator. The hash table must not * be mutated while the iterator is in use, and the iterator will - * step exactly BLI_edgehash_size(eh) times before becoming done. + * step exactly BLI_edgehash_len(eh) times before becoming done. * * \param ehi The EdgeHashIterator to initialize. * \param eh The EdgeHash to iterate over. @@ -729,7 +729,7 @@ EdgeSet *BLI_edgeset_new(const char *info) return BLI_edgeset_new_ex(info, 0); } -int BLI_edgeset_size(EdgeSet *es) +int BLI_edgeset_len(EdgeSet *es) { return (int)((EdgeHash *)es)->nentries; } diff --git a/source/blender/blenlib/intern/gsqueue.c b/source/blender/blenlib/intern/gsqueue.c index 25da7701924..5c8c43ab92a 100644 --- a/source/blender/blenlib/intern/gsqueue.c +++ b/source/blender/blenlib/intern/gsqueue.c @@ -81,7 +81,7 @@ bool BLI_gsqueue_is_empty(GSQueue *gq) /** * Query number elements in the queue */ -int BLI_gsqueue_size(GSQueue *gq) +int BLI_gsqueue_len(GSQueue *gq) { GSQueueElem *elem; int size = 0; @@ -162,7 +162,7 @@ void BLI_gsqueue_push(GSQueue *gq, const void *item) * \param item A pointer to an appropriately * sized structure (the size passed to BLI_gsqueue_new). */ -void BLI_gsqueue_pushback(GSQueue *gq, const void *item) +void BLI_gsqueue_push_back(GSQueue *gq, const void *item) { GSQueueElem *elem = MEM_mallocN(sizeof(*elem) + gq->elem_size, "gqueue_push"); memcpy(elem->data, item, gq->elem_size); diff --git a/source/blender/blenlib/intern/jitter.c b/source/blender/blenlib/intern/jitter_2d.c index bc2b5677fa5..26351f52c7f 100644 --- a/source/blender/blenlib/intern/jitter.c +++ b/source/blender/blenlib/intern/jitter_2d.c @@ -25,7 +25,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/jitter.c +/** \file blender/blenlib/intern/jitter_2d.c * \ingroup bli * \brief Jitter offset table */ @@ -35,7 +35,7 @@ #include "MEM_guardedalloc.h" #include "BLI_rand.h" -#include "BLI_jitter.h" +#include "BLI_jitter_2d.h" #include "BLI_strict_flags.h" diff --git a/source/blender/blenlib/intern/lasso.c b/source/blender/blenlib/intern/lasso_2d.c index 710da09521a..663f3ffea22 100644 --- a/source/blender/blenlib/intern/lasso.c +++ b/source/blender/blenlib/intern/lasso_2d.c @@ -26,7 +26,7 @@ * */ -/** \file blender/blenlib/intern/lasso.c +/** \file blender/blenlib/intern/lasso_2d.c * \ingroup bli */ @@ -35,7 +35,7 @@ #include "BLI_math.h" #include "BLI_strict_flags.h" -#include "BLI_lasso.h" /* own include */ +#include "BLI_lasso_2d.h" /* own include */ void BLI_lasso_boundbox(rcti *rect, const int mcords[][2], const unsigned int moves) { diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index 96c3972d802..b87fed571b7 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -484,7 +484,7 @@ void BLI_freelistN(ListBase *listbase) * * \note Use to avoid redundant looping. */ -int BLI_listbase_count_ex(const ListBase *listbase, const int count_max) +int BLI_listbase_count_at_most(const ListBase *listbase, const int count_max) { Link *link; int count = 0; diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 2f5b0f420b1..4bedcbdf5bf 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -324,6 +324,27 @@ MINLINE size_t max_zz(size_t a, size_t b) return (b < a) ? a : b; } +MINLINE int clamp_i(int value, int min, int max) +{ + return min_ii(max_ii(value, min), max); +} + +MINLINE float clamp_f(float value, float min, float max) +{ + if (value > max) { + return max; + } + else if (value < min) { + return min; + } + return value; +} + +MINLINE size_t clamp_z(size_t value, size_t min, size_t max) +{ + return min_zz(max_zz(value, min), max); +} + /** * Almost-equal for IEEE floats, using absolute difference method. * @@ -347,10 +368,8 @@ MINLINE int compare_ff_relative(float a, float b, const float max_diff, const in { union {float f; int i;} ua, ub; -#if 0 /* No BLI_assert in INLINE :/ */ BLI_assert(sizeof(float) == sizeof(int)); BLI_assert(max_ulps < (1 << 22)); -#endif if (fabsf(a - b) <= max_diff) { return 1; @@ -454,7 +473,7 @@ MALWAYS_INLINE __m128 _bli_math_fastpow24(const __m128 arg) */ /* 0x3F4CCCCD = 4/5 */ /* 0x4F55A7FB = 2^(127/(4/5) - 127) * 0.994^(1/(4/5)) */ - /* error max = 0.17 avg = 0.0018 |avg| = 0.05 */ + /* error max = 0.17, avg = 0.0018, |avg| = 0.05 */ __m128 x = _bli_math_fastpow(0x3F4CCCCD, 0x4F55A7FB, arg); __m128 arg2 = _mm_mul_ps(arg, arg); __m128 arg4 = _mm_mul_ps(arg2, arg2); diff --git a/source/blender/blenlib/intern/math_bits_inline.c b/source/blender/blenlib/intern/math_bits_inline.c index 37fdcd7878a..9b16756134e 100644 --- a/source/blender/blenlib/intern/math_bits_inline.c +++ b/source/blender/blenlib/intern/math_bits_inline.c @@ -34,7 +34,7 @@ MINLINE int bitscan_forward_i(int a) { BLI_assert(a != 0); -# ifdef _MSC_VER +#ifdef _MSC_VER unsigned long ctz; _BitScanForward(&ctz, a); return ctz; @@ -63,7 +63,7 @@ MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a) MINLINE int bitscan_reverse_i(int a) { BLI_assert(a != 0); -# ifdef _MSC_VER +#ifdef _MSC_VER unsigned long clz; _BitScanReverse(&clz, a); return clz; diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index cb39134af45..534abeb913d 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -656,135 +656,80 @@ void rgb_to_lab(float r, float g, float b, float *ll, float *la, float *lb) xyz_to_lab(x, y, z, ll, la, lb); } -static void xyz_to_lms(float x, float y, float z, float *l, float *m, float *s) -{ - *l = 0.3897f * x + 0.6890f * y - 0.0787f * z; - *m = -0.2298f * x + 1.1834f * y + 0.0464f * z; - *s = z; -} - -static void lms_to_xyz(float l, float m, float s, float *x, float *y, float *z) -{ - *x = 1.9102f * l - 1.1121f * m + 0.2019f * s; - *y = 0.3709f * l + 0.6290f * m + 0.0000f * s; - *z = s; -} - -static void normalize_rgb(float rgb[3]) -{ - const float max = max_fff(rgb[0], rgb[1], rgb[2]); +/* ****************************** blackbody ******************************** */ - if (max > 0.0f) { - mul_v3_fl(rgb, 1.0f / max); - } -} - -/* Color rendering of spectra, adapted from public domain code by John Walker, - * http://www.fourmilab.ch/ +/* Calculate color in range 800..12000 using an approximation + * a/x+bx+c for R and G and ((at + b)t + c)t + d) for B + * Max absolute error for RGB is (0.00095, 0.00077, 0.00057), + * which is enough to get the same 8 bit/channel color. */ -static void spectrum_to_xyz(float temperature, float xyz[3]) -{ - int i; - float lambda, x = 0.0f, y = 0.0f, z = 0.0f, xyz_sum; - - /* CIE colour matching functions xBar, yBar, and zBar for wavelengths from - * 380 through 780 nanometers, every 5 nanometers. - * For a wavelength lambda in this range: - * - * cie_colour_match[(lambda - 380) / 5][0] = xBar - * cie_colour_match[(lambda - 380) / 5][1] = yBar - * cie_colour_match[(lambda - 380) / 5][2] = zBar - */ - - const float cie_colour_match[81][3] = { - {0.0014f, 0.0000f, 0.0065f}, {0.0022f, 0.0001f, 0.0105f}, {0.0042f, 0.0001f, 0.0201f}, - {0.0076f, 0.0002f, 0.0362f}, {0.0143f, 0.0004f, 0.0679f}, {0.0232f, 0.0006f, 0.1102f}, - {0.0435f, 0.0012f, 0.2074f}, {0.0776f, 0.0022f, 0.3713f}, {0.1344f, 0.0040f, 0.6456f}, - {0.2148f, 0.0073f, 1.0391f}, {0.2839f, 0.0116f, 1.3856f}, {0.3285f, 0.0168f, 1.6230f}, - {0.3483f, 0.0230f, 1.7471f}, {0.3481f, 0.0298f, 1.7826f}, {0.3362f, 0.0380f, 1.7721f}, - {0.3187f, 0.0480f, 1.7441f}, {0.2908f, 0.0600f, 1.6692f}, {0.2511f, 0.0739f, 1.5281f}, - {0.1954f, 0.0910f, 1.2876f}, {0.1421f, 0.1126f, 1.0419f}, {0.0956f, 0.1390f, 0.8130f}, - {0.0580f, 0.1693f, 0.6162f}, {0.0320f, 0.2080f, 0.4652f}, {0.0147f, 0.2586f, 0.3533f}, - {0.0049f, 0.3230f, 0.2720f}, {0.0024f, 0.4073f, 0.2123f}, {0.0093f, 0.5030f, 0.1582f}, - {0.0291f, 0.6082f, 0.1117f}, {0.0633f, 0.7100f, 0.0782f}, {0.1096f, 0.7932f, 0.0573f}, - {0.1655f, 0.8620f, 0.0422f}, {0.2257f, 0.9149f, 0.0298f}, {0.2904f, 0.9540f, 0.0203f}, - {0.3597f, 0.9803f, 0.0134f}, {0.4334f, 0.9950f, 0.0087f}, {0.5121f, 1.0000f, 0.0057f}, - {0.5945f, 0.9950f, 0.0039f}, {0.6784f, 0.9786f, 0.0027f}, {0.7621f, 0.9520f, 0.0021f}, - {0.8425f, 0.9154f, 0.0018f}, {0.9163f, 0.8700f, 0.0017f}, {0.9786f, 0.8163f, 0.0014f}, - {1.0263f, 0.7570f, 0.0011f}, {1.0567f, 0.6949f, 0.0010f}, {1.0622f, 0.6310f, 0.0008f}, - {1.0456f, 0.5668f, 0.0006f}, {1.0026f, 0.5030f, 0.0003f}, {0.9384f, 0.4412f, 0.0002f}, - {0.8544f, 0.3810f, 0.0002f}, {0.7514f, 0.3210f, 0.0001f}, {0.6424f, 0.2650f, 0.0000f}, - {0.5419f, 0.2170f, 0.0000f}, {0.4479f, 0.1750f, 0.0000f}, {0.3608f, 0.1382f, 0.0000f}, - {0.2835f, 0.1070f, 0.0000f}, {0.2187f, 0.0816f, 0.0000f}, {0.1649f, 0.0610f, 0.0000f}, - {0.1212f, 0.0446f, 0.0000f}, {0.0874f, 0.0320f, 0.0000f}, {0.0636f, 0.0232f, 0.0000f}, - {0.0468f, 0.0170f, 0.0000f}, {0.0329f, 0.0119f, 0.0000f}, {0.0227f, 0.0082f, 0.0000f}, - {0.0158f, 0.0057f, 0.0000f}, {0.0114f, 0.0041f, 0.0000f}, {0.0081f, 0.0029f, 0.0000f}, - {0.0058f, 0.0021f, 0.0000f}, {0.0041f, 0.0015f, 0.0000f}, {0.0029f, 0.0010f, 0.0000f}, - {0.0020f, 0.0007f, 0.0000f}, {0.0014f, 0.0005f, 0.0000f}, {0.0010f, 0.0004f, 0.0000f}, - {0.0007f, 0.0002f, 0.0000f}, {0.0005f, 0.0002f, 0.0000f}, {0.0003f, 0.0001f, 0.0000f}, - {0.0002f, 0.0001f, 0.0000f}, {0.0002f, 0.0001f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f}, - {0.0001f, 0.0000f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f}, {0.0000f, 0.0000f, 0.0000f} - }; - - for (i = 0, lambda = 380.0f; lambda < 780.1f; i++, lambda += 5.0f) { - /* wavelength in meter */ - const float wlm = lambda * 1e-9f; - const float Me = (3.74183e-16f * powf(wlm, -5.0f)) / (expf(1.4388e-2f / (wlm * temperature)) - 1.0f); - - x += Me * cie_colour_match[i][0]; - y += Me * cie_colour_match[i][1]; - z += Me * cie_colour_match[i][2]; +static const float blackbody_table_r[6][3] = { + { 2.52432244e+03f, -1.06185848e-03f, 3.11067539e+00f }, + { 3.37763626e+03f, -4.34581697e-04f, 1.64843306e+00f }, + { 4.10671449e+03f, -8.61949938e-05f, 6.41423749e-01f }, + { 4.66849800e+03f, 2.85655028e-05f, 1.29075375e-01f }, + { 4.60124770e+03f, 2.89727618e-05f, 1.48001316e-01f }, + { 3.78765709e+03f, 9.36026367e-06f, 3.98995841e-01f }, +}; + +static const float blackbody_table_g[6][3] = { + { -7.50343014e+02f, 3.15679613e-04f, 4.73464526e-01f }, + { -1.00402363e+03f, 1.29189794e-04f, 9.08181524e-01f }, + { -1.22075471e+03f, 2.56245413e-05f, 1.20753416e+00f }, + { -1.42546105e+03f, -4.01730887e-05f, 1.44002695e+00f }, + { -1.18134453e+03f, -2.18913373e-05f, 1.30656109e+00f }, + { -5.00279505e+02f, -4.59745390e-06f, 1.09090465e+00f }, +}; + +static const float blackbody_table_b[6][4] = { + { 0.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, 0.0f, 0.0f }, + { -2.02524603e-11f, 1.79435860e-07f, -2.60561875e-04f, -1.41761141e-02f }, + { -2.22463426e-13f, -1.55078698e-08f, 3.81675160e-04f, -7.30646033e-01f }, + { 6.72595954e-13f, -2.73059993e-08f, 4.24068546e-04f, -7.52204323e-01f }, +}; + +static void blackbody_temperature_to_rgb(float rgb[3], float t) +{ + if(t >= 12000.0f) { + rgb[0] = 0.826270103f; + rgb[1] = 0.994478524f; + rgb[2] = 1.56626022f; + } + else if(t < 965.0f) { + rgb[0] = 4.70366907f; + rgb[1] = 0.0f; + rgb[2] = 0.0f; + } + else { + int i = (t >= 6365.0f)? 5: + (t >= 3315.0f)? 4: + (t >= 1902.0f)? 3: + (t >= 1449.0f)? 2: + (t >= 1167.0f)? 1: 0; + + const float *r = blackbody_table_r[i]; + const float *g = blackbody_table_g[i]; + const float *b = blackbody_table_b[i]; + + const float t_inv = 1.0f / t; + rgb[0] = r[0] * t_inv + r[1] * t + r[2]; + rgb[1] = g[0] * t_inv + g[1] * t + g[2]; + rgb[2] = ((b[0] * t + b[1]) * t + b[2]) * t + b[3]; } - - xyz_sum = (x + y + z); - - xyz[0] = x / xyz_sum; - xyz[1] = y / xyz_sum; - xyz[2] = z / xyz_sum; } void blackbody_temperature_to_rgb_table(float *r_table, int width, float min, float max) { - int i, j = 0, dj = 1; - float rgb[3], xyz[3], lms[3], lms_w[3]; - float bb_temp; - - if (min < max) { - SWAP(float, min, max); - j = width - 1; - dj = -1; - } + for (int i = 0; i < width; i++) { + float temperature = min + (max - min) / (float)width * (float)i; - for (i = 0; i < width; i++, j += dj) { - bb_temp = min + (max - min) / (float)width * (float)i; + float rgb[3]; + blackbody_temperature_to_rgb(rgb, temperature); - /* integrate blackbody radiation spectrum to XYZ */ - spectrum_to_xyz(bb_temp, xyz); - - /* normalize highest temperature to white (in LMS system) */ - xyz_to_lms(xyz[0], xyz[1], xyz[2], &lms[0], &lms[1], &lms[2]); - - if (i == 0) { - lms_w[0] = 1.0f / lms[0]; - lms_w[1] = 1.0f / lms[1]; - lms_w[2] = 1.0f / lms[2]; - } - - mul_v3_v3(lms, lms_w); - - lms_to_xyz(lms[0], lms[1], lms[2], &xyz[0], &xyz[1], &xyz[2]); - - /* convert to RGB */ - xyz_to_rgb(xyz[0], xyz[1], xyz[2], &rgb[0], &rgb[1], &rgb[2], BLI_XYZ_CIE); - constrain_rgb(&rgb[0], &rgb[1], &rgb[2]); - normalize_rgb(rgb); - - copy_v3_v3(&r_table[(j << 2)], rgb); - - if (rgb[2] > 0.1f) - r_table[(j << 2) + 3] = rgb[2]; - else - r_table[(j << 2) + 3] = 0.0f; + copy_v3_v3(&r_table[i * 4], rgb); + r_table[i * 4 + 3] = 0.0f; } } diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 78797e603be..171af1c8264 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -3813,7 +3813,7 @@ void orthographic_m4(float matrix[4][4], const float left, const float right, co matrix[3][0] = -(right + left) / Xdelta; matrix[1][1] = 2.0f / Ydelta; matrix[3][1] = -(top + bottom) / Ydelta; - matrix[2][2] = -2.0f / Zdelta; /* note: negate Z */ + matrix[2][2] = -2.0f / Zdelta; /* note: negate Z */ matrix[3][2] = -(farClip + nearClip) / Zdelta; } @@ -3832,7 +3832,7 @@ void perspective_m4(float mat[4][4], const float left, const float right, const } mat[0][0] = nearClip * 2.0f / Xdelta; mat[1][1] = nearClip * 2.0f / Ydelta; - mat[2][0] = (right + left) / Xdelta; /* note: negate Z */ + mat[2][0] = (right + left) / Xdelta; /* note: negate Z */ mat[2][1] = (top + bottom) / Ydelta; mat[2][2] = -(farClip + nearClip) / Zdelta; mat[2][3] = -1.0f; @@ -4968,6 +4968,18 @@ int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], con return ret; } +bool is_quad_flip_v3_first_third_fast(const float v1[3], const float v2[3], const float v3[3], const float v4[3]) +{ + float d_12[3], d_13[3], d_14[3]; + float cross_a[3], cross_b[3]; + sub_v3_v3v3(d_12, v2, v1); + sub_v3_v3v3(d_13, v3, v1); + sub_v3_v3v3(d_14, v4, v1); + cross_v3_v3v3(cross_a, d_12, d_13); + cross_v3_v3v3(cross_b, d_14, d_13); + return dot_v3v3(cross_a, cross_b) > 0.0f; +} + /** * Return the value which the distance between points will need to be scaled by, * to define a handle, given both points are on a perfect circle. diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c index 05562502278..d6e48fa59e7 100644 --- a/source/blender/blenlib/intern/math_vector.c +++ b/source/blender/blenlib/intern/math_vector.c @@ -890,6 +890,8 @@ void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axi void rotate_v3_v3v3fl(float r[3], const float p[3], const float axis[3], const float angle) { + BLI_assert(r != p); + float axis_n[3]; normalize_v3_v3(axis_n, axis); diff --git a/source/blender/blenlib/intern/polyfill2d.c b/source/blender/blenlib/intern/polyfill_2d.c index 018e2f9be5a..8c0870f0c07 100644 --- a/source/blender/blenlib/intern/polyfill2d.c +++ b/source/blender/blenlib/intern/polyfill_2d.c @@ -18,7 +18,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/polyfill2d.c +/** \file blender/blenlib/intern/polyfill_2d.c * \ingroup bli * * An ear clipping algorithm to triangulate single boundary polygons. @@ -53,7 +53,7 @@ #include "BLI_memarena.h" #include "BLI_alloca.h" -#include "BLI_polyfill2d.h" /* own include */ +#include "BLI_polyfill_2d.h" /* own include */ #include "BLI_strict_flags.h" diff --git a/source/blender/blenlib/intern/polyfill2d_beautify.c b/source/blender/blenlib/intern/polyfill_2d_beautify.c index c0c95da5c63..17f3205aaff 100644 --- a/source/blender/blenlib/intern/polyfill2d_beautify.c +++ b/source/blender/blenlib/intern/polyfill_2d_beautify.c @@ -18,7 +18,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/polyfill2d_beautify.c +/** \file blender/blenlib/intern/polyfill_2d_beautify.c * \ingroup bli * * This function is to improve the tessellation resulting from polyfill2d, @@ -44,7 +44,7 @@ #include "BLI_memarena.h" #include "BLI_heap.h" -#include "BLI_polyfill2d_beautify.h" /* own include */ +#include "BLI_polyfill_2d_beautify.h" /* own include */ #include "BLI_strict_flags.h" @@ -265,7 +265,8 @@ static void polyedge_rotate( struct HalfEdge *e) { /** CCW winding, rotate internal edge to new vertical state. - * <pre> + * + * \code{.unparsed} * Before After * X X * / \ /|\ @@ -276,7 +277,7 @@ static void polyedge_rotate( * e2\ /e1 e2\ | /e1 * \ / \|/ * X X - * </pre> + * \endcode */ struct HalfEdge *ed[6]; uint ed_index[6]; @@ -401,7 +402,7 @@ void BLI_polyfill_beautify( } while (BLI_heap_is_empty(eheap) == false) { - struct HalfEdge *e = BLI_heap_popmin(eheap); + struct HalfEdge *e = BLI_heap_pop_min(eheap); eheap_table[e->base_index] = NULL; polyedge_rotate(half_edges, e); diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index 1a178db1413..700524965f0 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -386,6 +386,8 @@ void BLI_halton_1D(unsigned int prime, double offset, int n, double *r) { const double invprime = 1.0 / (double)prime; + *r = 0.0; + for (int s = 0; s < n; s++) { *r = halton_ex(invprime, &offset); } @@ -395,6 +397,8 @@ void BLI_halton_2D(unsigned int prime[2], double offset[2], int n, double *r) { const double invprimes[2] = {1.0 / (double)prime[0], 1.0 / (double)prime[1]}; + r[0] = r[1] = 0.0; + for (int s = 0; s < n; s++) { for (int i = 0; i < 2; i++) { r[i] = halton_ex(invprimes[i], &offset[i]); @@ -406,6 +410,8 @@ void BLI_halton_3D(unsigned int prime[3], double offset[3], int n, double *r) { const double invprimes[3] = {1.0 / (double)prime[0], 1.0 / (double)prime[1], 1.0 / (double)prime[2]}; + r[0] = r[1] = r[2] = 0.0; + for (int s = 0; s < n; s++) { for (int i = 0; i < 3; i++) { r[i] = halton_ex(invprimes[i], &offset[i]); diff --git a/source/blender/blenlib/intern/smallhash.c b/source/blender/blenlib/intern/smallhash.c index 1b862ffe005..56d77abed24 100644 --- a/source/blender/blenlib/intern/smallhash.c +++ b/source/blender/blenlib/intern/smallhash.c @@ -310,7 +310,7 @@ bool BLI_smallhash_haskey(const SmallHash *sh, uintptr_t key) return (e != NULL); } -int BLI_smallhash_count(const SmallHash *sh) +int BLI_smallhash_len(const SmallHash *sh) { return (int)sh->nentries; } diff --git a/source/blender/blenlib/intern/sort_utils.c b/source/blender/blenlib/intern/sort_utils.c index c75e8e7455f..2d55e77b98b 100644 --- a/source/blender/blenlib/intern/sort_utils.c +++ b/source/blender/blenlib/intern/sort_utils.c @@ -37,6 +37,10 @@ struct SortAnyByInt { int sort_value; }; +struct SortAnyByPtr { + const void *sort_value; +}; + int BLI_sortutil_cmp_float(const void *a_, const void *b_) { const struct SortAnyByFloat *a = a_; @@ -72,3 +76,21 @@ int BLI_sortutil_cmp_int_reverse(const void *a_, const void *b_) else if (a->sort_value > b->sort_value) return -1; else return 0; } + +int BLI_sortutil_cmp_ptr(const void *a_, const void *b_) +{ + const struct SortAnyByPtr *a = a_; + const struct SortAnyByPtr *b = b_; + if (a->sort_value > b->sort_value) return 1; + else if (a->sort_value < b->sort_value) return -1; + else return 0; +} + +int BLI_sortutil_cmp_ptr_reverse(const void *a_, const void *b_) +{ + const struct SortAnyByPtr *a = a_; + const struct SortAnyByPtr *b = b_; + if (a->sort_value < b->sort_value) return 1; + else if (a->sort_value > b->sort_value) return -1; + else return 0; +} diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index c08329ef34f..f69b35ce5a9 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -185,7 +185,7 @@ size_t BLI_file_size(const char *path) } /** - * Returns the st_mode from statting the specified path name, or 0 if it couldn't be statted + * Returns the st_mode from stat-ing the specified path name, or 0 if stat fails * (most likely doesn't exist or no access). */ int BLI_exists(const char *name) diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 6022732025b..1a6fd082e95 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -208,7 +208,7 @@ size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) } /** - * Portable replacement for #vsnprintf + * Portable replacement for `vsnprintf`. */ size_t BLI_vsnprintf(char *__restrict buffer, size_t maxncpy, const char *__restrict format, va_list arg) { @@ -503,7 +503,7 @@ int BLI_strcaseeq(const char *a, const char *b) } /** - * Portable replacement for #strcasestr (not available in MSVC) + * Portable replacement for `strcasestr` (not available in MSVC) */ char *BLI_strcasestr(const char *s, const char *find) { @@ -931,13 +931,13 @@ size_t BLI_str_partition_ex( if (end) { if (from_right) { for (tmp = end - 1; (tmp >= str) && (*tmp != *d); tmp--); - if (tmp < str) { + if (tmp < str) { tmp = NULL; } } else { tmp = func(str, *d); - if (tmp >= end) { + if (tmp >= end) { tmp = NULL; } } diff --git a/source/blender/blenlib/intern/task.c b/source/blender/blenlib/intern/task.c index 05cfe4f3972..5d3c6b35ac1 100644 --- a/source/blender/blenlib/intern/task.c +++ b/source/blender/blenlib/intern/task.c @@ -696,7 +696,7 @@ static TaskPool *task_pool_create_ex(TaskScheduler *scheduler, * and malloc could be non-thread safe at this point because * no other jobs are running. */ - BLI_begin_threaded_malloc(); + BLI_threaded_malloc_begin(); return pool; } @@ -763,7 +763,7 @@ void BLI_task_pool_free(TaskPool *pool) MEM_freeN(pool); - BLI_end_threaded_malloc(); + BLI_threaded_malloc_end(); } BLI_INLINE bool task_can_use_local_queues(TaskPool *pool, int thread_id) @@ -1310,11 +1310,11 @@ static void parallel_mempool_func( /** * This function allows to parallelize for loops over Mempool items. * - * \param pool The iterable BLI_mempool to loop over. - * \param userdata Common userdata passed to all instances of \a func. - * \param func Callback function. - * \param use_threading If \a true, actually split-execute loop in threads, else just do a sequential forloop - * (allows caller to use any kind of test to switch on parallelization or not). + * \param mempool: The iterable BLI_mempool to loop over. + * \param userdata: Common userdata passed to all instances of \a func. + * \param func: Callback function. + * \param use_threading: If \a true, actually split-execute loop in threads, else just do a sequential for loop + * (allows caller to use any kind of test to switch on parallelization or not). * * \note There is no static scheduling here. */ @@ -1329,7 +1329,7 @@ void BLI_task_parallel_mempool( ParallelMempoolState state; int i, num_threads, num_tasks; - if (BLI_mempool_count(mempool) == 0) { + if (BLI_mempool_len(mempool) == 0) { return; } diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index 2f961701801..761f3982e28 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -83,13 +83,13 @@ static TaskScheduler *task_scheduler = NULL; * int maxthreads = 2; * int cont = 1; * - * BLI_init_threads(&lb, do_something_func, maxthreads); + * BLI_threadpool_init(&lb, do_something_func, maxthreads); * * while (cont) { * if (BLI_available_threads(&lb) && !(escape loop event)) { * // get new job (data pointer) * // tag job 'processed - * BLI_insert_thread(&lb, job); + * BLI_threadpool_insert(&lb, job); * } * else PIL_sleep_ms(50); * @@ -98,7 +98,7 @@ static TaskScheduler *task_scheduler = NULL; * for (go over all jobs) * if (job is ready) { * if (job was not removed) { - * BLI_remove_thread(&lb, job); + * BLI_threadpool_remove(&lb, job); * } * } * else cont = 1; @@ -110,7 +110,7 @@ static TaskScheduler *task_scheduler = NULL; * } * } * - * BLI_end_threads(&lb); + * BLI_threadpool_end(&lb); * ************************************************ */ static SpinLock _malloc_lock; @@ -183,7 +183,7 @@ TaskScheduler *BLI_task_scheduler_get(void) * problem otherwise: scene render will kill of the mutex! */ -void BLI_init_threads(ListBase *threadbase, void *(*do_thread)(void *), int tot) +void BLI_threadpool_init(ListBase *threadbase, void *(*do_thread)(void *), int tot) { int a; @@ -228,7 +228,7 @@ int BLI_available_threads(ListBase *threadbase) } /* returns thread number, for sample patterns or threadsafe tables */ -int BLI_available_thread_index(ListBase *threadbase) +int BLI_threadpool_available_thread_index(ListBase *threadbase) { ThreadSlot *tslot; int counter = 0; @@ -258,7 +258,7 @@ int BLI_thread_is_main(void) return pthread_equal(pthread_self(), mainid); } -void BLI_insert_thread(ListBase *threadbase, void *callerdata) +void BLI_threadpool_insert(ListBase *threadbase, void *callerdata) { ThreadSlot *tslot; @@ -273,7 +273,7 @@ void BLI_insert_thread(ListBase *threadbase, void *callerdata) printf("ERROR: could not insert thread slot\n"); } -void BLI_remove_thread(ListBase *threadbase, void *callerdata) +void BLI_threadpool_remove(ListBase *threadbase, void *callerdata) { ThreadSlot *tslot; @@ -286,7 +286,7 @@ void BLI_remove_thread(ListBase *threadbase, void *callerdata) } } -void BLI_remove_thread_index(ListBase *threadbase, int index) +void BLI_threadpool_remove_index(ListBase *threadbase, int index) { ThreadSlot *tslot; int counter = 0; @@ -301,7 +301,7 @@ void BLI_remove_thread_index(ListBase *threadbase, int index) } } -void BLI_remove_threads(ListBase *threadbase) +void BLI_threadpool_clear(ListBase *threadbase) { ThreadSlot *tslot; @@ -314,7 +314,7 @@ void BLI_remove_threads(ListBase *threadbase) } } -void BLI_end_threads(ListBase *threadbase) +void BLI_threadpool_end(ListBase *threadbase) { ThreadSlot *tslot; @@ -418,12 +418,12 @@ static ThreadMutex *global_mutex_from_type(const int type) } } -void BLI_lock_thread(int type) +void BLI_thread_lock(int type) { pthread_mutex_lock(global_mutex_from_type(type)); } -void BLI_unlock_thread(int type) +void BLI_thread_unlock(int type) { pthread_mutex_unlock(global_mutex_from_type(type)); } @@ -773,12 +773,12 @@ void *BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms) return work; } -int BLI_thread_queue_size(ThreadQueue *queue) +int BLI_thread_queue_len(ThreadQueue *queue) { int size; pthread_mutex_lock(&queue->mutex); - size = BLI_gsqueue_size(queue->queue); + size = BLI_gsqueue_len(queue->queue); pthread_mutex_unlock(&queue->mutex); return size; @@ -819,7 +819,7 @@ void BLI_thread_queue_wait_finish(ThreadQueue *queue) /* ************************************************ */ -void BLI_begin_threaded_malloc(void) +void BLI_threaded_malloc_begin(void) { unsigned int level = atomic_fetch_and_add_u(&thread_levels, 1); if (level == 0) { @@ -832,7 +832,7 @@ void BLI_begin_threaded_malloc(void) } } -void BLI_end_threaded_malloc(void) +void BLI_threaded_malloc_end(void) { unsigned int level = atomic_sub_and_fetch_u(&thread_levels, 1); if (level == 0) { diff --git a/source/blender/blenlib/intern/voronoi.c b/source/blender/blenlib/intern/voronoi_2d.c index e0cbe278ffb..40e98d5914c 100644 --- a/source/blender/blenlib/intern/voronoi.c +++ b/source/blender/blenlib/intern/voronoi_2d.c @@ -23,20 +23,18 @@ * ***** END GPL LICENSE BLOCK ***** */ -/* +/** \file blender/blenlib/intern/voronoi_2d.c + * \ingroup bli + * * Fortune's algorithm implemented using explanation and some code snippets from * http://blog.ivank.net/fortunes-algorithm-and-implementation.html */ -/** \file blender/blenlib/intern/voronoi.c - * \ingroup bli - */ - #include "MEM_guardedalloc.h" #include "BLI_listbase.h" #include "BLI_math.h" -#include "BLI_voronoi.h" +#include "BLI_voronoi_2d.h" #include "BLI_utildefines.h" #define VORONOI_EPS 1e-2f |