ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/vector.h
Revision: 1.6
Committed: Mon Apr 5 15:40:15 2004 UTC (20 years, 1 month ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.5: +2 -2 lines
Log Message:
Rename all the functions, types and enums in the API to be consistent.
Types are now of the form "sg_mem_stats"; functions of the form
"sg_get_mem_stats"; enums of the form "SG_PROCESS_STATE_RUNNING".
(The old get_proc_snapshot follows the usual calling convention now.)

Make internal functions static where possible.

Rename non-static internal functions to have an sg_ prefix so they don't
collide with those from other libraries. In particular, strlcpy and
strlcat are now called sg_strlcpy and sg_strlcat and are always
included.

Fix saidar and statgrab to use the new API.

File Contents

# User Rev Content
1 ats 1.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 ats 1.5 int error;
11 ats 1.1 int block_size;
12     vector_init_function init_fn;
13     vector_destroy_function destroy_fn;
14     } vector_header;
15    
16 ats 1.2 #define VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn) \
17 ats 1.1 vector_header name##_header = { \
18     sizeof(item_type), \
19     0, \
20     0, \
21 ats 1.5 0, \
22 ats 1.1 block_size, \
23     (vector_init_function) init_fn, \
24     (vector_destroy_function) destroy_fn \
25     }
26    
27 ats 1.5 /* Internal function to resize the vector. */
28 ats 1.6 void *sg_vector_resize(void *vector, vector_header *h, int count);
29 ats 1.2
30     /* Declare a vector. Specify the init/destroy functions as NULL if you don't
31     * need them. The block size is how many items to allocate at once. */
32     #define VECTOR_DECLARE(name, item_type, block_size, init_fn, destroy_fn) \
33     item_type *name = NULL; \
34     VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn)
35    
36     /* As VECTOR_DECLARE, but for a static vector. */
37     #define VECTOR_DECLARE_STATIC(name, item_type, block_size, init_fn, destroy_fn) \
38     static item_type *name = NULL; \
39     static VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn)
40    
41 ats 1.1 /* Return the current size of a vector. */
42     #define VECTOR_SIZE(name) \
43     name##_header.used_count
44    
45     /* Resize a vector. Returns 0 on success, -1 on out-of-memory. On
46     * out-of-memory, the old contents of the vector will be destroyed and the old
47 ats 1.5 * vector will be freed. */
48 ats 1.1 #define VECTOR_RESIZE(name, num_items) \
49 ats 1.6 (name = sg_vector_resize((char *) name, &name##_header, num_items), \
50 ats 1.5 name##_header.error)
51 ats 1.1
52     /* Free a vector, destroying its contents. */
53     #define VECTOR_FREE(name) \
54     VECTOR_RESIZE(name, 0)
55