extend BLI_buffer
- add option to calloc or not, existing code wasnt consistent here, would calloc on first alloc but not on realloc, also - the static memory was never zero'd. use flag BLI_BUFFER_USE_CALLOC to ensure all new memory is zero'd (static/alloc/realloc's). - add BLI_buffer_declare_static / BLI_buffer_declare so its possible to have a buffer that never uses static memory.
This commit is contained in:
@@ -1040,8 +1040,9 @@ void BKE_pbvh_build_bmesh(PBVH *bvh, BMesh *bm, int smooth_shading,
|
|||||||
int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
|
int BKE_pbvh_bmesh_update_topology(PBVH *bvh, PBVHTopologyUpdateMode mode,
|
||||||
const float center[3], float radius)
|
const float center[3], float radius)
|
||||||
{
|
{
|
||||||
BLI_buffer_declare(BMFace*, edge_faces, 8);
|
BLI_buffer_declare_static(BMFace *, edge_faces, BLI_BUFFER_NOP, 8);
|
||||||
BLI_buffer_declare(BMFace*, deleted_faces, 32);
|
BLI_buffer_declare_static(BMFace *, deleted_faces, BLI_BUFFER_NOP, 32);
|
||||||
|
|
||||||
int modified = FALSE;
|
int modified = FALSE;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@
|
|||||||
/* Usage examples:
|
/* Usage examples:
|
||||||
*
|
*
|
||||||
* {
|
* {
|
||||||
* BLI_buffer_declare(int, my_int_array, 32);
|
* BLI_buffer_declare_static(int, my_int_array, BLI_BUFFER_NOP, 32);
|
||||||
*
|
*
|
||||||
* BLI_buffer_append(my_int_array, int, 42);
|
* BLI_buffer_append(my_int_array, int, 42);
|
||||||
* assert(my_int_array.count == 1);
|
* assert(my_int_array.count == 1);
|
||||||
@@ -42,23 +42,43 @@ typedef struct {
|
|||||||
void *data;
|
void *data;
|
||||||
const int elem_size;
|
const int elem_size;
|
||||||
int count, alloc_count;
|
int count, alloc_count;
|
||||||
int using_static;
|
int flag;
|
||||||
} BLI_Buffer;
|
} BLI_Buffer;
|
||||||
|
|
||||||
#define BLI_buffer_declare(type_, name_, static_count_) \
|
enum {
|
||||||
|
BLI_BUFFER_NOP = 0,
|
||||||
|
BLI_BUFFER_USE_STATIC = (1 << 0),
|
||||||
|
BLI_BUFFER_USE_CALLOC = (1 << 1), /* ensure the array is always calloc'd */
|
||||||
|
};
|
||||||
|
|
||||||
|
#define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
|
||||||
type_ *name_ ## _static_[static_count_]; \
|
type_ *name_ ## _static_[static_count_]; \
|
||||||
BLI_Buffer name_ = {name_ ## _static_, \
|
BLI_Buffer name_ = { \
|
||||||
|
/* clear the static memory if this is a calloc'd array */ \
|
||||||
|
((void)((flag_ & BLI_BUFFER_USE_CALLOC) ? \
|
||||||
|
memset(name_ ## _static_, 0, sizeof(name_ ## _static_)) : 0\
|
||||||
|
), /* memset-end */ \
|
||||||
|
name_ ## _static_), \
|
||||||
sizeof(type_), \
|
sizeof(type_), \
|
||||||
0, \
|
0, \
|
||||||
static_count_, \
|
static_count_, \
|
||||||
TRUE}
|
BLI_BUFFER_USE_STATIC | flag_}
|
||||||
|
|
||||||
|
/* never use static*/
|
||||||
|
#define BLI_buffer_declare(type_, name_, flag_) \
|
||||||
|
BLI_Buffer name_ = {NULL, \
|
||||||
|
sizeof(type_), \
|
||||||
|
0, \
|
||||||
|
0, \
|
||||||
|
flag_}
|
||||||
|
|
||||||
#define BLI_buffer_at(buffer_, type_, index_) \
|
#define BLI_buffer_at(buffer_, type_, index_) \
|
||||||
(((type_*)(buffer_)->data)[index_])
|
(((type_*)(buffer_)->data)[index_])
|
||||||
|
|
||||||
#define BLI_buffer_append(buffer_, type_, val_) \
|
#define BLI_buffer_append(buffer_, type_, val_) { \
|
||||||
BLI_buffer_resize(buffer_, (buffer_)->count + 1); \
|
BLI_buffer_resize(buffer_, (buffer_)->count + 1); \
|
||||||
BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_
|
BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_; \
|
||||||
|
} (void)0
|
||||||
|
|
||||||
/* Never decreases the amount of memory allocated */
|
/* Never decreases the amount of memory allocated */
|
||||||
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count);
|
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count);
|
||||||
@@ -66,4 +86,4 @@ void BLI_buffer_resize(BLI_Buffer *buffer, int new_count);
|
|||||||
/* Does not free the buffer structure itself */
|
/* Does not free the buffer structure itself */
|
||||||
void BLI_buffer_free(BLI_Buffer *buffer);
|
void BLI_buffer_free(BLI_Buffer *buffer);
|
||||||
|
|
||||||
#endif
|
#endif /* __BLI_BUFFER_H__ */
|
||||||
|
|||||||
@@ -25,25 +25,45 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
static void *buffer_alloc(BLI_Buffer *buffer, int len)
|
||||||
|
{
|
||||||
|
return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
|
||||||
|
MEM_callocN : MEM_mallocN)
|
||||||
|
(buffer->elem_size * len, "BLI_Buffer.data");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *buffer_realloc(BLI_Buffer *buffer, int len)
|
||||||
|
{
|
||||||
|
return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
|
||||||
|
MEM_recallocN : MEM_reallocN)
|
||||||
|
(buffer->data, (buffer->elem_size * len));
|
||||||
|
}
|
||||||
|
|
||||||
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count)
|
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count)
|
||||||
{
|
{
|
||||||
if (new_count > buffer->alloc_count) {
|
if (new_count > buffer->alloc_count) {
|
||||||
if (buffer->using_static) {
|
if (buffer->flag & BLI_BUFFER_USE_STATIC) {
|
||||||
void *orig = buffer->data;
|
void *orig = buffer->data;
|
||||||
buffer->data = MEM_callocN(buffer->elem_size * new_count,
|
|
||||||
"BLI_Buffer.data");
|
buffer->data = buffer_alloc(buffer, new_count);
|
||||||
memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
|
memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
|
||||||
buffer->alloc_count = new_count;
|
buffer->alloc_count = new_count;
|
||||||
buffer->using_static = FALSE;
|
buffer->flag &= ~BLI_BUFFER_USE_STATIC;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (new_count < buffer->alloc_count * 2)
|
if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
|
||||||
buffer->alloc_count *= 2;
|
buffer->alloc_count *= 2;
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
buffer->alloc_count = new_count;
|
buffer->alloc_count = new_count;
|
||||||
buffer->data = MEM_reallocN(buffer->data,
|
}
|
||||||
(buffer->elem_size *
|
|
||||||
buffer->alloc_count));
|
if (buffer->data) {
|
||||||
|
buffer->data = buffer_realloc(buffer, buffer->alloc_count);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
buffer->data = buffer_alloc(buffer, buffer->alloc_count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +72,10 @@ void BLI_buffer_resize(BLI_Buffer *buffer, int new_count)
|
|||||||
|
|
||||||
void BLI_buffer_free(BLI_Buffer *buffer)
|
void BLI_buffer_free(BLI_Buffer *buffer)
|
||||||
{
|
{
|
||||||
if (!buffer->using_static)
|
if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
|
||||||
|
if (buffer->data) {
|
||||||
MEM_freeN(buffer->data);
|
MEM_freeN(buffer->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
memset(buffer, 0, sizeof(*buffer));
|
memset(buffer, 0, sizeof(*buffer));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user