diff options
author | Lars Hjemli <hjemli@gmail.com> | 2011-02-19 22:00:56 (JST) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2011-02-19 22:00:59 (JST) |
commit | e66a16cebcdac53b63e77876acef1ca9e4877038 (patch) | |
tree | 839123e66853c4a80c305a3a1c13295fe5e6acc9 /vector.c | |
parent | 286c4c0a1d7085afdc8d9ddba86da4ed9f2f7400 (diff) | |
parent | c2680325f68192368d32f26458fea9cfb50df6e5 (diff) | |
download | cgit-e66a16cebcdac53b63e77876acef1ca9e4877038.zip cgit-e66a16cebcdac53b63e77876acef1ca9e4877038.tar.gz |
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 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <string.h> | ||
3 | #include <errno.h> | ||
4 | #include "vector.h" | ||
5 | |||
6 | static int grow(struct vector *vec, int gently) | ||
7 | { | ||
8 | size_t new_alloc; | ||
9 | void *new_data; | ||
10 | |||
11 | new_alloc = vec->alloc * 3 / 2; | ||
12 | if (!new_alloc) | ||
13 | new_alloc = 8; | ||
14 | new_data = realloc(vec->data, new_alloc * vec->size); | ||
15 | if (!new_data) { | ||
16 | if (gently) | ||
17 | return ENOMEM; | ||
18 | perror("vector.c:grow()"); | ||
19 | exit(1); | ||
20 | } | ||
21 | vec->data = new_data; | ||
22 | vec->alloc = new_alloc; | ||
23 | return 0; | ||
24 | } | ||
25 | |||
26 | int vector_push(struct vector *vec, const void *data, int gently) | ||
27 | { | ||
28 | int rc; | ||
29 | |||
30 | if (vec->count == vec->alloc && (rc = grow(vec, gently))) | ||
31 | return rc; | ||
32 | if (data) | ||
33 | memmove(vec->data + vec->count * vec->size, data, vec->size); | ||
34 | else | ||
35 | memset(vec->data + vec->count * vec->size, 0, vec->size); | ||
36 | vec->count++; | ||
37 | return 0; | ||
38 | } | ||