BLI_Buffer: add BLI_buffer_reinit

Useful for re-using a buffer when the existing data can be thrown away.
This commit is contained in:
Campbell Barton 2015-10-04 14:14:28 +11:00
parent 9f046e95c0
commit 74a2fa3095
7 changed files with 54 additions and 17 deletions

View File

@ -97,7 +97,7 @@ UvVertMap *BKE_mesh_uv_vert_map_create(
float (*tf_uv)[2] = NULL;
if (use_winding) {
tf_uv = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, (size_t)mp->totloop);
tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, (size_t)mp->totloop);
}
nverts = mp->totloop;

View File

@ -575,7 +575,7 @@ static void pbvh_bmesh_edge_loops(BLI_Buffer *buf, BMEdge *e)
buf->count = 2;
}
else {
BLI_buffer_resize(buf, BM_edge_face_count(e));
BLI_buffer_reinit(buf, BM_edge_face_count(e));
BM_iter_as_array(NULL, BM_LOOPS_OF_EDGE, e, buf->data, buf->count);
}
}

View File

@ -64,7 +64,7 @@ enum {
#define BLI_buffer_at(buffer_, type_, index_) ( \
(((type_ *)(buffer_)->data)[ \
(BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
(BLI_assert(index_ >= 0 && index_ < (buffer_)->count)), \
(BLI_assert((int)index_ >= 0 && (size_t)index_ < (buffer_)->count)), \
index_]))
#define BLI_buffer_array(buffer_, type_) ( \
@ -73,6 +73,9 @@ enum {
#define BLI_buffer_resize_data(buffer_, type_, new_count_) ( \
(BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
#define BLI_buffer_reinit_data(buffer_, type_, new_count_) ( \
(BLI_buffer_reinit(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
#define BLI_buffer_append(buffer_, type_, val_) ( \
BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
(BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_) \
@ -85,6 +88,9 @@ enum {
/* Never decreases the amount of memory allocated */
void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count);
/* Ensure size, throwing away old data, respecting BLI_BUFFER_USE_CALLOC */
void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count);
/* Does not free the buffer structure itself */
void _bli_buffer_free(BLI_Buffer *buffer);
#define BLI_buffer_free(name_) { \

View File

@ -102,6 +102,37 @@ void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
buffer->count = new_count;
}
/**
* Similar to #BLI_buffer_resize, but use when the existing data can be:
* - Ignored (malloc'd)
* - Cleared (when BLI_BUFFER_USE_CALLOC is set)
*/
void BLI_buffer_reinit(BLI_Buffer *buffer, size_t new_count)
{
if (UNLIKELY(new_count > buffer->alloc_count)) {
if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
MEM_freeN(buffer->data);
}
if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
buffer->alloc_count *= 2;
}
else {
buffer->alloc_count = new_count;
}
buffer->data = buffer_alloc(buffer, new_count);
}
else {
if (buffer->flag & BLI_BUFFER_USE_CALLOC) {
memset(buffer->data, 0,
buffer->elem_size * new_count);
}
}
buffer->count = new_count;
}
/* callers use BLI_buffer_free */
void _bli_buffer_free(BLI_Buffer *buffer)
{

View File

@ -728,9 +728,9 @@ static void solidify_add_thickness(BMesh *bm, const float dist)
if (BMO_elem_flag_test(bm, f, FACE_MARK)) {
/* array for passing verts to angle_poly_v3 */
float *face_angles = BLI_buffer_resize_data(&face_angles_buf, float, f->len);
float *face_angles = BLI_buffer_reinit_data(&face_angles_buf, float, f->len);
/* array for receiving angles from angle_poly_v3 */
float **verts = BLI_buffer_resize_data(&verts_buf, float *, f->len);
float **verts = BLI_buffer_reinit_data(&verts_buf, float *, f->len);
BM_ITER_ELEM_INDEX (l, &loopIter, f, BM_LOOPS_OF_FACE, i) {
verts[i] = l->v->co;

View File

@ -671,7 +671,7 @@ UvVertMap *BM_uv_vert_map_create(
float (*tf_uv)[2];
if (use_winding) {
tf_uv = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, efa->len);
tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa->len);
}
BM_ITER_ELEM_INDEX(l, &liter, efa, BM_LOOPS_OF_FACE, i) {
@ -823,7 +823,7 @@ UvElementMap *BM_uv_element_map_create(
float (*tf_uv)[2];
if (use_winding) {
tf_uv = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, efa->len);
tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa->len);
}
BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {

View File

@ -187,8 +187,8 @@ static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, BMEditMesh *em, MTe
BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
const int efa_len = efa->len;
float (*tf_uv)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, efa_len);
float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uvorig_buf, vec2f, efa_len);
float (*tf_uv)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa_len);
float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
tf = BM_ELEM_CD_GET_VOID_P(efa, cd_poly_tex_offset);
BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
@ -230,8 +230,8 @@ static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, BMEditMesh *em, MTe
BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
const int efa_len = efa->len;
float (*tf_uv)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, efa_len);
float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uvorig_buf, vec2f, efa_len);
float (*tf_uv)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa_len);
float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
area = BM_face_calc_area(efa) / totarea;
@ -284,12 +284,12 @@ static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, BMEditMesh *em, MTe
if (uvedit_face_visible_test(scene, ima, efa, tf)) {
const int efa_len = efa->len;
float (*tf_uv)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uv_buf, vec2f, efa_len);
float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_resize_data(&tf_uvorig_buf, vec2f, efa_len);
float *uvang = BLI_buffer_resize_data(&uvang_buf, float, efa_len);
float *ang = BLI_buffer_resize_data(&ang_buf, float, efa_len);
float (*av)[3] = (float (*)[3])BLI_buffer_resize_data(&av_buf, vec3f, efa_len);
float (*auv)[2] = (float (*)[2])BLI_buffer_resize_data(&auv_buf, vec2f, efa_len);
float (*tf_uv)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa_len);
float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
float *uvang = BLI_buffer_reinit_data(&uvang_buf, float, efa_len);
float *ang = BLI_buffer_reinit_data(&ang_buf, float, efa_len);
float (*av)[3] = (float (*)[3])BLI_buffer_reinit_data(&av_buf, vec3f, efa_len);
float (*auv)[2] = (float (*)[2])BLI_buffer_reinit_data(&auv_buf, vec2f, efa_len);
int j;
BM_elem_flag_enable(efa, BM_ELEM_TAG);