ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/vector.h
Revision: 1.2
Committed: Sun Apr 4 21:33:08 2004 UTC (20 years, 1 month ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.1: +14 -7 lines
Log Message:
Add a macro for declaring static vectors too.

File Contents

# Content
1 #include <stdlib.h>
2
3 typedef void (*vector_init_function)(void *item);
4 typedef void (*vector_destroy_function)(void *item);
5
6 typedef struct {
7 size_t item_size;
8 int used_count;
9 int alloc_count;
10 int block_size;
11 vector_init_function init_fn;
12 vector_destroy_function destroy_fn;
13 } vector_header;
14
15 #define VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn) \
16 vector_header name##_header = { \
17 sizeof(item_type), \
18 0, \
19 0, \
20 block_size, \
21 (vector_init_function) init_fn, \
22 (vector_destroy_function) destroy_fn \
23 }
24
25 int statgrab_vector_resize(void **vector, vector_header *h, int count);
26
27 /* Declare a vector. Specify the init/destroy functions as NULL if you don't
28 * need them. The block size is how many items to allocate at once. */
29 #define VECTOR_DECLARE(name, item_type, block_size, init_fn, destroy_fn) \
30 item_type *name = NULL; \
31 VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn)
32
33 /* As VECTOR_DECLARE, but for a static vector. */
34 #define VECTOR_DECLARE_STATIC(name, item_type, block_size, init_fn, destroy_fn) \
35 static item_type *name = NULL; \
36 static VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn)
37
38 /* Return the current size of a vector. */
39 #define VECTOR_SIZE(name) \
40 name##_header.used_count
41
42 /* Resize a vector. Returns 0 on success, -1 on out-of-memory. On
43 * out-of-memory, the old contents of the vector will be destroyed and the old
44 * vector will be freed. */
45 #define VECTOR_RESIZE(name, num_items) \
46 statgrab_vector_resize((void **) &name, &name##_header, num_items)
47
48 /* Free a vector, destroying its contents. */
49 #define VECTOR_FREE(name) \
50 VECTOR_RESIZE(name, 0)
51