diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-10-04 06:14:28 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-10-04 06:29:27 +0300 |
commit | 74a2fa309555da9b76abdc205cec43909872fc20 (patch) | |
tree | 2bb8a658c2282b7af1b8738278530822152047c3 | |
parent | 9f046e95c0a5c4b842d7405111ebab90e1ba63b6 (diff) |
BLI_Buffer: add BLI_buffer_reinit
Useful for re-using a buffer when the existing data can be thrown away.
-rw-r--r-- | source/blender/blenkernel/intern/mesh_mapping.c | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/pbvh_bmesh.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_buffer.h | 8 | ||||
-rw-r--r-- | source/blender/blenlib/intern/buffer.c | 31 | ||||
-rw-r--r-- | source/blender/bmesh/operators/bmo_extrude.c | 4 | ||||
-rw-r--r-- | source/blender/editors/mesh/editmesh_utils.c | 4 | ||||
-rw-r--r-- | source/blender/editors/uvedit/uvedit_draw.c | 20 |
7 files changed, 54 insertions, 17 deletions
diff --git a/source/blender/blenkernel/intern/mesh_mapping.c b/source/blender/blenkernel/intern/mesh_mapping.c index cef570a54f5..8dce3c372f6 100644 --- a/source/blender/blenkernel/intern/mesh_mapping.c +++ b/source/blender/blenkernel/intern/mesh_mapping.c @@ -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; diff --git a/source/blender/blenkernel/intern/pbvh_bmesh.c b/source/blender/blenkernel/intern/pbvh_bmesh.c index 3e236079a66..ff4cae09437 100644 --- a/source/blender/blenkernel/intern/pbvh_bmesh.c +++ b/source/blender/blenkernel/intern/pbvh_bmesh.c @@ -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); } } diff --git a/source/blender/blenlib/BLI_buffer.h b/source/blender/blenlib/BLI_buffer.h index 941c1c93ae9..a9ecd3a4627 100644 --- a/source/blender/blenlib/BLI_buffer.h +++ b/source/blender/blenlib/BLI_buffer.h @@ -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_) { \ diff --git a/source/blender/blenlib/intern/buffer.c b/source/blender/blenlib/intern/buffer.c index 7588a6ec14d..6ab57221a63 100644 --- a/source/blender/blenlib/intern/buffer.c +++ b/source/blender/blenlib/intern/buffer.c @@ -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) { diff --git a/source/blender/bmesh/operators/bmo_extrude.c b/source/blender/bmesh/operators/bmo_extrude.c index 4449f223f35..3eae98b3c46 100644 --- a/source/blender/bmesh/operators/bmo_extrude.c +++ b/source/blender/bmesh/operators/bmo_extrude.c @@ -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; diff --git a/source/blender/editors/mesh/editmesh_utils.c b/source/blender/editors/mesh/editmesh_utils.c index d521b2c01e5..c101b416169 100644 --- a/source/blender/editors/mesh/editmesh_utils.c +++ b/source/blender/editors/mesh/editmesh_utils.c @@ -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) { diff --git a/source/blender/editors/uvedit/uvedit_draw.c b/source/blender/editors/uvedit/uvedit_draw.c index 4e9ab680dc6..f4eed3e1fda 100644 --- a/source/blender/editors/uvedit/uvedit_draw.c +++ b/source/blender/editors/uvedit/uvedit_draw.c @@ -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); |