summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorXuefer <xuefer@gmail.com>2014-11-11 16:09:55 +0000
committerXuefer <xuefer@gmail.com>2014-11-11 16:09:55 +0000
commit30d56bf2d775fa30aa116778bb342a45b654e0a7 (patch)
treedfd091ef31286582252c587a02f0494895e00d5c /util
parent4bd347e02b7a47b9ba876eaee2f46e4d9f3b614b (diff)
downloadxcache-30d56bf2d775fa30aa116778bb342a45b654e0a7.tar.gz
xcache-30d56bf2d775fa30aa116778bb342a45b654e0a7.zip
code clean up for vector
git-svn-id: svn://svn.lighttpd.net/xcache/trunk@1566 c26eb9a1-5813-0410-bd6c-c2e55f420ca7
Diffstat (limited to 'util')
-rw-r--r--util/xc_vector.h96
1 files changed, 49 insertions, 47 deletions
diff --git a/util/xc_vector.h b/util/xc_vector.h
index 5792eb2..aab8a0c 100644
--- a/util/xc_vector.h
+++ b/util/xc_vector.h
@@ -10,88 +10,89 @@
#include <assert.h>
typedef struct {
- size_t size;
- void *data;
+ void *data_;
+ size_t size_;
+ size_t capacity_;
- size_t capacity;
- size_t data_size;
- int persistent;
+ size_t data_size_;
+ int persistent_;
} xc_vector_t;
-#define xc_vector_initializer(type, persistent_) { \
- 0, \
+#define xc_vector_initializer(type, persistent) { \
NULL, \
-\
0, \
+ 0, \
+\
sizeof(type), \
- persistent_, \
+ persistent, \
}
-#define xc_vector_init(type, vector, persistent_) do { \
- (vector)->size = 0; \
- (vector)->data = NULL; \
-\
- (vector)->capacity = 0; \
- (vector)->data_size = sizeof(type); \
- (vector)->persistent = persistent_; \
+#define xc_vector_init_ex(type, vector, data, size, persistent) do { \
+ (vector)->data_ = data; \
+ (vector)->size_ = (vector)->capacity_ = size; \
+ (vector)->data_size_ = sizeof(type); \
+ (vector)->persistent_ = persistent; \
} while (0)
+#define xc_vector_init(type, vector) xc_vector_init_ex(type, vector, NULL, 0, 0)
+#define xc_vector_pinit(type, vector) xc_vector_init_ex(type, vector, NULL, 0, 1)
+
static inline void xc_vector_destroy_impl(xc_vector_t *vector TSRMLS_DC)
{
- vector->size = 0;
- if (vector->data) {
- pefree(vector->data, vector->persistent);
- vector->data = NULL;
+ vector->size_ = 0;
+ if (vector->data_) {
+ pefree(vector->data_, vector->persistent_);
+ vector->data_ = NULL;
}
- vector->capacity = 0;
- vector->data_size = 0;
+ vector->capacity_ = 0;
+ vector->data_size_ = 0;
}
#define xc_vector_destroy(vector) xc_vector_destroy_impl(vector TSRMLS_CC)
-#define xc_vector_clear(vector) do { (vector)->size = 0; } while (0)
+#define xc_vector_clear(vector) do { (vector)->size_ = 0; } while (0)
-#define xc_vector_size(vector) ((vector)->size)
-#define xc_vector_initialized(vector) ((vector)->data_size != 0)
+#define xc_vector_size(vector) ((vector)->size_)
+#define xc_vector_initialized(vector) ((vector)->data_size_ != 0)
#define xc_vector_element_ptr_(vector, index) ( \
(void *) ( \
- ((char *) (vector)->data) + (index) * (vector)->data_size \
+ ((char *) (vector)->data_) + (index) * (vector)->data_size_ \
) \
)
static inline xc_vector_t *xc_vector_check_type_(xc_vector_t *vector, size_t data_size)
{
- assert(vector->data_size = data_size);
+ assert(vector->data_size_ = data_size);
return vector;
}
-#define xc_vector_data(type, vector) ((type *) xc_vector_check_type_(vector, sizeof(type))->data)
+#define xc_vector_data(type, vector) ((type *) xc_vector_check_type_(vector, sizeof(type))->data_)
static inline void xc_vector_check_reserve_(xc_vector_t *vector TSRMLS_DC)
{
- if (vector->size == vector->capacity) {
- if (vector->capacity) {
- vector->capacity <<= 1;
- vector->data = perealloc(vector->data, vector->data_size * vector->capacity, vector->persistent);
+ if (vector->size_ == vector->capacity_) {
+ if (vector->capacity_) {
+ vector->capacity_ <<= 1;
+ vector->data_ = perealloc(vector->data_, vector->data_size_ * vector->capacity_, vector->persistent_);
}
else {
- vector->capacity = 8;
- vector->data = pemalloc(vector->data_size * vector->capacity, vector->persistent);
+ vector->capacity_ = 8;
+ vector->data_ = pemalloc(vector->data_size_ * vector->capacity_, vector->persistent_);
}
}
}
#define xc_vector_push_back(vector, value_ptr) do { \
xc_vector_check_reserve_(vector TSRMLS_CC); \
- memcpy(xc_vector_element_ptr_(vector, (vector)->size++), value_ptr, (vector)->data_size); \
+ memcpy(xc_vector_element_ptr_(vector, (vector)->size_++), value_ptr, (vector)->data_size_); \
} while (0)
static inline void *xc_vector_detach_impl(xc_vector_t *vector)
{
- void *data = vector->data;
+ void *data = vector->data_;
- vector->data = NULL;
- vector->capacity = 0;
- vector->size = 0;
+ vector->data_ = NULL;
+ vector->capacity_ = 0;
+ vector->size_ = 0;
return data;
}
@@ -100,14 +101,14 @@ static inline void *xc_vector_detach_impl(xc_vector_t *vector)
static inline xc_vector_t *xc_vector_pop_back_check_(xc_vector_t *vector, size_t data_size)
{
assert(vector);
- assert(vector->data_size == data_size);
- assert(vector->capacity > 0);
+ assert(vector->data_size_ == data_size);
+ assert(vector->capacity_ > 0);
return vector;
}
#define xc_vector_pop_back(type, vector) xc_vector_data(type, \
xc_vector_pop_back_check_(vector, sizeof(type)) \
-)[--(vector)->size]
+)[--(vector)->size_]
static inline void xc_vector_reverse(xc_vector_t *vector)
{
@@ -115,11 +116,12 @@ static inline void xc_vector_reverse(xc_vector_t *vector)
void *tmp;
assert(vector);
- tmp = alloca(vector->data_size);
- for (left = vector->data, right = xc_vector_element_ptr_(vector, vector->size - 1); left < right; left += vector->data_size, right -= vector->data_size) {
- memcpy(tmp, left, vector->data_size);
- memcpy(left, right, vector->data_size);
- memcpy(right, tmp, vector->data_size);
+ assert(vector->data_size_);
+ tmp = alloca(vector->data_size_);
+ for (left = vector->data_, right = xc_vector_element_ptr_(vector, vector->size_ - 1); left < right; left += vector->data_size_, right -= vector->data_size_) {
+ memcpy(tmp, left, vector->data_size_);
+ memcpy(left, right, vector->data_size_);
+ memcpy(right, tmp, vector->data_size_);
}
}