1 |
+ |
/* |
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 |
|
#include <stdlib.h> |
25 |
|
|
26 |
|
typedef void (*vector_init_function)(void *item); |
30 |
|
size_t item_size; |
31 |
|
int used_count; |
32 |
|
int alloc_count; |
33 |
+ |
int error; |
34 |
|
int block_size; |
35 |
|
vector_init_function init_fn; |
36 |
|
vector_destroy_function destroy_fn; |
37 |
|
} vector_header; |
38 |
|
|
39 |
< |
int statgrab_vector_resize(void **vector, vector_header *h, int count); |
16 |
< |
|
17 |
< |
/* Declare a vector. Specify the init/destroy functions as NULL if you don't |
18 |
< |
* need them. The block size is how many items to allocate at once. */ |
19 |
< |
#define VECTOR_DECLARE(name, item_type, block_size, init_fn, destroy_fn) \ |
20 |
< |
item_type * name = NULL; \ |
39 |
> |
#define VECTOR_HEADER(name, item_type, block_size, init_fn, destroy_fn) \ |
40 |
|
vector_header name##_header = { \ |
41 |
|
sizeof(item_type), \ |
42 |
|
0, \ |
43 |
|
0, \ |
44 |
+ |
0, \ |
45 |
|
block_size, \ |
46 |
|
(vector_init_function) init_fn, \ |
47 |
|
(vector_destroy_function) destroy_fn \ |
48 |
|
} |
49 |
|
|
50 |
+ |
/* Internal function to resize the vector. */ |
51 |
+ |
void *sg_vector_resize(void *vector, vector_header *h, int count); |
52 |
+ |
|
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 |
|
/* Return the current size of a vector. */ |
65 |
|
#define VECTOR_SIZE(name) \ |
66 |
|
name##_header.used_count |
69 |
|
* out-of-memory, the old contents of the vector will be destroyed and the old |
70 |
|
* vector will be freed. */ |
71 |
|
#define VECTOR_RESIZE(name, num_items) \ |
72 |
< |
statgrab_vector_resize((void **) &name, &name##_header, num_items) |
72 |
> |
(name = sg_vector_resize((char *) name, &name##_header, num_items), \ |
73 |
> |
name##_header.error) |
74 |
|
|
75 |
|
/* Free a vector, destroying its contents. */ |
76 |
|
#define VECTOR_FREE(name) \ |
77 |
|
VECTOR_RESIZE(name, 0) |
43 |
– |
|
78 |
|
|