ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/vector.h
Revision: 1.7
Committed: Tue Apr 6 14:54:48 2004 UTC (20 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_17, LIBSTATGRAB_0_16, LIBSTATGRAB_0_15, LIBSTATGRAB_0_14, LIBSTATGRAB_0_13, LIBSTATGRAB_0_12, LIBSTATGRAB_0_11_1, LIBSTATGRAB_0_11, LIBSTATGRAB_0_10_3, LIBSTATGRAB_0_10_2, LIBSTATGRAB_0_10_1, LIBSTATGRAB_0_10, HEAD
Changes since 1.6: +23 -0 lines
Log Message:
Add header block.

File Contents

# User Rev Content
1 tdb 1.7 /*
2     * i-scream libstatgrab
3     * http://www.i-scream.org
4     * Copyright (C) 2000-2004 i-scream
5     *
6     * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation; either
9     * version 2.1 of the License, or (at your option) any later version.
10     *
11     * This library is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15     *
16     * You should have received a copy of the GNU Lesser General Public
17     * License along with this library; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19     * 02111-1307 USA
20     *
21     * $Id$
22     */
23    
24 ats 1.1 #include <stdlib.h>
25    
26     typedef void (*vector_init_function)(void *item);
27     typedef void (*vector_destroy_function)(void *item);
28    
29     typedef struct {
30     size_t item_size;
31     int used_count;
32     int alloc_count;
33 ats 1.5 int error;
34 ats 1.1 int block_size;
35     vector_init_function init_fn;
36     vector_destroy_function destroy_fn;
37     } vector_header;
38    
39 ats 1.2 #define VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn) \
40 ats 1.1 vector_header name##_header = { \
41     sizeof(item_type), \
42     0, \
43     0, \
44 ats 1.5 0, \
45 ats 1.1 block_size, \
46     (vector_init_function) init_fn, \
47     (vector_destroy_function) destroy_fn \
48     }
49    
50 ats 1.5 /* Internal function to resize the vector. */
51 ats 1.6 void *sg_vector_resize(void *vector, vector_header *h, int count);
52 ats 1.2
53     /* Declare a vector. Specify the init/destroy functions as NULL if you don't
54     * need them. The block size is how many items to allocate at once. */
55     #define VECTOR_DECLARE(name, item_type, block_size, init_fn, destroy_fn) \
56     item_type *name = NULL; \
57     VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn)
58    
59     /* As VECTOR_DECLARE, but for a static vector. */
60     #define VECTOR_DECLARE_STATIC(name, item_type, block_size, init_fn, destroy_fn) \
61     static item_type *name = NULL; \
62     static VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn)
63    
64 ats 1.1 /* Return the current size of a vector. */
65     #define VECTOR_SIZE(name) \
66     name##_header.used_count
67    
68     /* Resize a vector. Returns 0 on success, -1 on out-of-memory. On
69     * out-of-memory, the old contents of the vector will be destroyed and the old
70 ats 1.5 * vector will be freed. */
71 ats 1.1 #define VECTOR_RESIZE(name, num_items) \
72 ats 1.6 (name = sg_vector_resize((char *) name, &name##_header, num_items), \
73 ats 1.5 name##_header.error)
74 ats 1.1
75     /* Free a vector, destroying its contents. */
76     #define VECTOR_FREE(name) \
77     VECTOR_RESIZE(name, 0)
78