diff options
author | Lars Hjemli <hjemli@gmail.com> | 2011-02-19 14:00:56 +0100 |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2011-02-19 14:00:59 +0100 |
commit | e66a16cebcdac53b63e77876acef1ca9e4877038 (patch) | |
tree | 839123e66853c4a80c305a3a1c13295fe5e6acc9 /vector.c | |
parent | Use GIT-1.7.4 (diff) | |
parent | html.c: use '+' to escape spaces in urls (diff) | |
download | cgit-pink-e66a16cebcdac53b63e77876acef1ca9e4877038.tar.gz cgit-pink-e66a16cebcdac53b63e77876acef1ca9e4877038.zip |
Merge branch 'lh/improve-range-search'
* lh/improve-range-search: html.c: use '+' to escape spaces in urls ui-log.c: improve handling of range-search argument Add vector utility functions
Diffstat (limited to 'vector.c')
-rw-r--r-- | vector.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/vector.c b/vector.c new file mode 100644 index 0000000..0863908 --- /dev/null +++ b/vector.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include "vector.h" + +static int grow(struct vector *vec, int gently) +{ + size_t new_alloc; + void *new_data; + + new_alloc = vec->alloc * 3 / 2; + if (!new_alloc) + new_alloc = 8; + new_data = realloc(vec->data, new_alloc * vec->size); + if (!new_data) { + if (gently) + return ENOMEM; + perror("vector.c:grow()"); + exit(1); + } + vec->data = new_data; + vec->alloc = new_alloc; + return 0; +} + +int vector_push(struct vector *vec, const void *data, int gently) +{ + int rc; + + if (vec->count == vec->alloc && (rc = grow(vec, gently))) + return rc; + if (data) + memmove(vec->data + vec->count * vec->size, data, vec->size); + else + memset(vec->data + vec->count * vec->size, 0, vec->size); + vec->count++; + return 0; +} |