changes to BLI_buffer
authorCampbell Barton <ideasman42@gmail.com>
Sat, 16 Mar 2013 00:12:14 +0000 (00:12 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 16 Mar 2013 00:12:14 +0000 (00:12 +0000)
- assert if BLI_buffer_at() is called with an out of bounds value.
- add BLI_buffer_resize_data() macro which resizes and returns a pointer to the new array.
- warn if missing call to BLI_buffer_free().

source/blender/blenlib/BLI_buffer.h
source/blender/blenlib/intern/buffer.c

index 880a97acb809a2cdf9af309c453bc006e008a16a..fe835e7cadc85c602a2312f92397d0b0f8e72b38 100644 (file)
@@ -52,6 +52,7 @@ enum {
 };
 
 #define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
+       char name_ ## user;  /* warn for free only */ \
        type_ *name_ ## _static_[static_count_]; \
        BLI_Buffer name_ = { \
        /* clear the static memory if this is a calloc'd array */ \
@@ -66,15 +67,26 @@ enum {
 
 /* never use static*/
 #define BLI_buffer_declare(type_, name_, flag_) \
+       bool name_ ## user;  /* warn for free only */ \
        BLI_Buffer name_ = {NULL, \
                            sizeof(type_), \
                            0, \
                            0, \
                            flag_}
 
-
 #define BLI_buffer_at(buffer_, type_, index_) ( \
-       (((type_ *)(buffer_)->data)[(BLI_assert(sizeof(type_) == (buffer_)->elem_size)), index_]))
+       (((type_ *)(buffer_)->data)[ \
+               (BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
+               (BLI_assert(index_ >= 0 && index_ < (buffer_)->count)), \
+               index_]))
+
+#define BLI_buffer_array(buffer_, type_) ( \
+       &(BLI_buffer_at(buffer_, type_, 0)))
+
+#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_append(buffer_, type_, val_)  ( \
        BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
@@ -85,6 +97,10 @@ enum {
 void BLI_buffer_resize(BLI_Buffer *buffer, int new_count);
 
 /* Does not free the buffer structure itself */
-void BLI_buffer_free(BLI_Buffer *buffer);
+void _bli_buffer_free(BLI_Buffer *buffer);
+#define BLI_buffer_free(name_) { \
+       _bli_buffer_free(name_); \
+       (void)name_ ## user;  /* ensure we free */ \
+} (void)0
 
 #endif  /* __BLI_BUFFER_H__ */
index b2280b9a0d19f4bc88c9b64572820d436b1a7a39..aac3a3bc3f357ff8bcb180b40a4e97b116ed57b9 100644 (file)
@@ -70,7 +70,8 @@ void BLI_buffer_resize(BLI_Buffer *buffer, int new_count)
        buffer->count = new_count;
 }
 
-void BLI_buffer_free(BLI_Buffer *buffer)
+/* callers use BLI_buffer_free */
+void _bli_buffer_free(BLI_Buffer *buffer)
 {
        if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
                if (buffer->data) {