Browse Source

code clean up for vector

git-svn-id: svn://svn.lighttpd.net/xcache/trunk@1566 c26eb9a1-5813-0410-bd6c-c2e55f420ca7
master
Xuefer 5 years ago
parent
commit
30d56bf2d7
4 changed files with 54 additions and 52 deletions
  1. +2
    -2
      mod_cacher/xc_cacher.c
  2. +1
    -1
      processor/foot.m4
  3. +2
    -2
      processor/var-helper.h
  4. +49
    -47
      util/xc_vector.h

+ 2
- 2
mod_cacher/xc_cacher.c View File

@@ -2827,7 +2827,7 @@ static void xc_holds_init(TSRMLS_D) /* {{{ */
XG(php_holds_size) = xc_php_hcache.size;
XG(php_holds) = calloc(XG(php_holds_size), sizeof(xc_vector_t));
for (i = 0; i < xc_php_hcache.size; i ++) {
xc_vector_init(xc_entry_php_t *, &XG(php_holds[i]), 1);
xc_vector_pinit(xc_entry_php_t *, &XG(php_holds[i]));
}
}

@@ -2835,7 +2835,7 @@ static void xc_holds_init(TSRMLS_D) /* {{{ */
XG(var_holds_size) = xc_var_hcache.size;
XG(var_holds) = calloc(XG(var_holds_size), sizeof(xc_vector_t));
for (i = 0; i < xc_var_hcache.size; i ++) {
xc_vector_init(xc_entry_php_t *, &XG(var_holds[i]), 1);
xc_vector_pinit(xc_entry_php_t *, &XG(var_holds[i]));
}
}
}


+ 1
- 1
processor/foot.m4 View File

@@ -12,7 +12,7 @@ EXPORTED_FUNCTION(`$1 *xc_processor_store_$1(const xc_processor_storage_t *stora
processor.handle_reference = 1;
processor.relocatediff = storage->relocatediff;

IFAUTOCHECK(`xc_vector_init(unsigned long, &processor.allocsizes, 0);')
IFAUTOCHECK(`xc_vector_init(unsigned long, &processor.allocsizes);')

/* calc size */ {
zend_hash_init(&processor.strings, 0, NULL, NULL, 0);


+ 2
- 2
processor/var-helper.h View File

@@ -4,7 +4,7 @@ static void xc_var_collect_object(xc_processor_t *processor, zend_object_handle
size_t next_index;

if (!xc_vector_initialized(&processor->objects)) {
xc_vector_init(zend_object, &processor->objects, 0);
xc_vector_init(zend_object, &processor->objects);
zend_hash_init(&processor->handle_to_index, 0, NULL, NULL, 0);
}

@@ -40,7 +40,7 @@ static void xc_var_collect_class(xc_processor_t *processor, zend_class_entry *ce
size_t next_index;

if (!xc_vector_initialized(&processor->class_names)) {
xc_vector_init(xc_constant_string_t, &processor->class_names, 0);
xc_vector_init(xc_constant_string_t, &processor->class_names);
zend_hash_init(&processor->class_name_to_index, 0, NULL, NULL, 0);
}



+ 49
- 47
util/xc_vector.h View File

@@ -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_);
}
}



Loading…
Cancel
Save