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; |
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 |
< |
/* A pointer value that won't be returned by malloc, and isn't NULL, so can be |
51 |
< |
* used as a sentinel by allocation routines that need to return NULL |
27 |
< |
* sometimes. */ |
28 |
< |
extern char statgrab_vector_sentinel_value; |
29 |
< |
static void *statgrab_vector_sentinel = &statgrab_vector_sentinel_value; |
50 |
> |
/* Internal function to resize the vector. */ |
51 |
> |
void *sg_vector_resize(void *vector, vector_header *h, int count); |
52 |
|
|
31 |
– |
/* Internal function to resize the vector. Ideally it would take void ** as the |
32 |
– |
* first parameter, but ANSI strict-aliasing rules would then prevent it from |
33 |
– |
* doing anything with it, so we return a sentinel value as above instead if |
34 |
– |
* allocation fails. */ |
35 |
– |
void *statgrab_vector_resize(void *vector, vector_header *h, int count); |
36 |
– |
|
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) \ |
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 |
< |
* vector will be freed. |
55 |
< |
* |
56 |
< |
* This is ugly because it needs to check for the sentinel value. |
57 |
< |
*/ |
70 |
> |
* vector will be freed. */ |
71 |
|
#define VECTOR_RESIZE(name, num_items) \ |
72 |
< |
(((name = statgrab_vector_resize((char *) name, &name##_header, num_items)) \ |
73 |
< |
== statgrab_vector_sentinel) ? -1 : 0) |
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) \ |