aboutsummaryrefslogtreecommitdiff
path: root/src/array.cpp
diff options
context:
space:
mode:
authorGinger Bill <bill@gingerbill.org>2017-06-08 12:54:52 +0100
committerGinger Bill <bill@gingerbill.org>2017-06-08 12:54:52 +0100
commit2a89d8021cf95f4a4d7dab269a262a1d2237f71b (patch)
treee955f29749310c1be63b43a231d217e584d996f1 /src/array.cpp
parent13deb4706c37acbababc6f60a1b6ec58c630a3f5 (diff)
Use templated `Array` with bounds checking
Diffstat (limited to 'src/array.cpp')
-rw-r--r--src/array.cpp184
1 files changed, 92 insertions, 92 deletions
diff --git a/src/array.cpp b/src/array.cpp
index bb9e789db..a46ccc47f 100644
--- a/src/array.cpp
+++ b/src/array.cpp
@@ -1,98 +1,7 @@
#define ARRAY_GROW_FORMULA(x) (2*(x) + 8)
GB_STATIC_ASSERT(ARRAY_GROW_FORMULA(0) > 0);
-#define Array(Type_) struct { \
- gbAllocator allocator; \
- Type_ * e; \
- isize count; \
- isize capacity; \
-}
-
-typedef Array(void) ArrayVoid;
-
-#define array_init_reserve(x_, allocator_, init_capacity_) do { \
- void **e = cast(void **)&((x_)->e); \
- GB_ASSERT((x_) != NULL); \
- (x_)->allocator = (allocator_); \
- (x_)->count = 0; \
- (x_)->capacity = (init_capacity_); \
- *e = gb_alloc((allocator_), gb_size_of(*(x_)->e)*(init_capacity_)); \
-} while (0)
-
-#define array_init_count(x_, allocator_, init_count_) do { \
- void **e = cast(void **)&((x_)->e); \
- GB_ASSERT((x_) != NULL); \
- (x_)->allocator = (allocator_); \
- (x_)->count = (init_count_); \
- (x_)->capacity = (init_count_); \
- *e = gb_alloc((allocator_), gb_size_of(*(x_)->e)*(init_count_)); \
-} while (0)
-
-#define array_init(x_, allocator_) do { array_init_reserve(x_, allocator_, ARRAY_GROW_FORMULA(0)); } while (0)
-#define array_free(x_) do { gb_free((x_)->allocator, (x_)->e); } while (0)
-#define array_set_capacity(x_, capacity_) do { array__set_capacity((x_), (capacity_), gb_size_of(*(x_)->e)); } while (0)
-
-#define array_grow(x_, min_capacity_) do { \
- isize new_capacity = ARRAY_GROW_FORMULA((x_)->capacity); \
- if (new_capacity < (min_capacity_)) { \
- new_capacity = (min_capacity_); \
- } \
- array_set_capacity(x_, new_capacity); \
-} while (0)
-
-#define array_add(x_, item_) do { \
- if ((x_)->capacity < (x_)->count+1) { \
- array_grow(x_, 0); \
- } \
- (x_)->e[(x_)->count++] = item_; \
-} while (0)
-
-#define array_pop(x_) do { GB_ASSERT((x_)->count > 0); (x_)->count--; } while (0)
-#define array_clear(x_) do { (x_)->count = 0; } while (0)
-
-#define array_resize(x_, new_count_) do { \
- if ((x_)->capacity < (new_count_)) { \
- array_grow((x_), (new_count_)); \
- } \
- (x_)->count = (new_count_); \
-} while (0)
-
-#define array_reserve(x_, new_capacity_) do { \
- if ((x_)->capacity < (new_capacity_)) { \
- array_set_capacity((x_), (new_capacity_)); \
- } \
-} while (0)
-
-
-
-
-void array__set_capacity(void *ptr, isize capacity, isize element_size) {
- ArrayVoid *x = cast(ArrayVoid *)ptr;
- GB_ASSERT(ptr != NULL);
-
- GB_ASSERT(element_size > 0);
-
- if (capacity == x->capacity) {
- return;
- }
-
- if (capacity < x->count) {
- if (x->capacity < capacity) {
- isize new_capacity = ARRAY_GROW_FORMULA(x->capacity);
- if (new_capacity < capacity) {
- new_capacity = capacity;
- }
- array__set_capacity(ptr, new_capacity, element_size);
- }
- x->count = capacity;
- }
-
- x->e = gb_resize(x->allocator, x->e, element_size*x->capacity, element_size*capacity);
- x->capacity = capacity;
-}
-
-
-#if 0
+#if 1
template <typename T>
struct Array {
gbAllocator allocator;
@@ -224,6 +133,97 @@ void array_set_capacity(Array<T> *array, isize capacity) {
array->capacity = capacity;
}
+#endif
+
+#if 0
+#define Array(Type_) struct { \
+ gbAllocator allocator; \
+ Type_ * e; \
+ isize count; \
+ isize capacity; \
+}
+
+typedef Array(void) ArrayVoid;
+
+#define array_init_reserve(x_, allocator_, init_capacity_) do { \
+ void **e = cast(void **)&((x_)->e); \
+ GB_ASSERT((x_) != NULL); \
+ (x_)->allocator = (allocator_); \
+ (x_)->count = 0; \
+ (x_)->capacity = (init_capacity_); \
+ *e = gb_alloc((allocator_), gb_size_of(*(x_)->e)*(init_capacity_)); \
+} while (0)
+
+#define array_init_count(x_, allocator_, init_count_) do { \
+ void **e = cast(void **)&((x_)->e); \
+ GB_ASSERT((x_) != NULL); \
+ (x_)->allocator = (allocator_); \
+ (x_)->count = (init_count_); \
+ (x_)->capacity = (init_count_); \
+ *e = gb_alloc((allocator_), gb_size_of(*(x_)->e)*(init_count_)); \
+} while (0)
+#define array_init(x_, allocator_) do { array_init_reserve(x_, allocator_, ARRAY_GROW_FORMULA(0)); } while (0)
+#define array_free(x_) do { gb_free((x_)->allocator, (x_)->e); } while (0)
+#define array_set_capacity(x_, capacity_) do { array__set_capacity((x_), (capacity_), gb_size_of(*(x_)->e)); } while (0)
+#define array_grow(x_, min_capacity_) do { \
+ isize new_capacity = ARRAY_GROW_FORMULA((x_)->capacity); \
+ if (new_capacity < (min_capacity_)) { \
+ new_capacity = (min_capacity_); \
+ } \
+ array_set_capacity(x_, new_capacity); \
+} while (0)
+
+#define array_add(x_, item_) do { \
+ if ((x_)->capacity < (x_)->count+1) { \
+ array_grow(x_, 0); \
+ } \
+ (x_)->e[(x_)->count++] = item_; \
+} while (0)
+
+#define array_pop(x_) do { GB_ASSERT((x_)->count > 0); (x_)->count--; } while (0)
+#define array_clear(x_) do { (x_)->count = 0; } while (0)
+
+#define array_resize(x_, new_count_) do { \
+ if ((x_)->capacity < (new_count_)) { \
+ array_grow((x_), (new_count_)); \
+ } \
+ (x_)->count = (new_count_); \
+} while (0)
+
+#define array_reserve(x_, new_capacity_) do { \
+ if ((x_)->capacity < (new_capacity_)) { \
+ array_set_capacity((x_), (new_capacity_)); \
+ } \
+} while (0)
+
+
+
+
+void array__set_capacity(void *ptr, isize capacity, isize element_size) {
+ ArrayVoid *x = cast(ArrayVoid *)ptr;
+ GB_ASSERT(ptr != NULL);
+
+ GB_ASSERT(element_size > 0);
+
+ if (capacity == x->capacity) {
+ return;
+ }
+
+ if (capacity < x->count) {
+ if (x->capacity < capacity) {
+ isize new_capacity = ARRAY_GROW_FORMULA(x->capacity);
+ if (new_capacity < capacity) {
+ new_capacity = capacity;
+ }
+ array__set_capacity(ptr, new_capacity, element_size);
+ }
+ x->count = capacity;
+ }
+
+ x->e = gb_resize(x->allocator, x->e, element_size*x->capacity, element_size*capacity);
+ x->capacity = capacity;
+}
#endif
+