1 |
ats |
1.1 |
/* |
2 |
|
|
* i-scream central monitoring system |
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 |
ats |
1.5 |
* $Id: vector.c,v 1.4 2004/04/05 00:58:05 ats Exp $ |
22 |
ats |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
#ifdef HAVE_CONFIG_H |
25 |
|
|
#include "config.h" |
26 |
|
|
#endif |
27 |
|
|
|
28 |
|
|
#include <stdlib.h> |
29 |
|
|
|
30 |
|
|
#include "vector.h" |
31 |
|
|
|
32 |
ats |
1.5 |
void *sg_vector_resize(void *vector, vector_header *h, int count) { |
33 |
ats |
1.1 |
int new_count, i; |
34 |
|
|
|
35 |
ats |
1.4 |
/* Destroy any now-unused items. |
36 |
|
|
* |
37 |
|
|
* Note that there's an assumption here that making the vector smaller |
38 |
|
|
* will never fail; if it did, then we would have destroyed items here |
39 |
|
|
* but not actually got rid of the vector pointing to them before the |
40 |
|
|
* error return.) */ |
41 |
ats |
1.1 |
if (count < h->used_count && h->destroy_fn != NULL) { |
42 |
|
|
for (i = count; i < h->used_count; i++) { |
43 |
ats |
1.3 |
h->destroy_fn((void *) (vector + i * h->item_size)); |
44 |
ats |
1.1 |
} |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/* Round up the desired size to the next multiple of the block size. */ |
48 |
|
|
new_count = ((count - 1 + h->block_size) / h->block_size) |
49 |
|
|
* h->block_size; |
50 |
|
|
|
51 |
|
|
/* Resize the vector if necessary. */ |
52 |
|
|
if (new_count != h->alloc_count) { |
53 |
ats |
1.2 |
char *new_vector; |
54 |
ats |
1.1 |
|
55 |
ats |
1.3 |
new_vector = realloc(vector, new_count * h->item_size); |
56 |
ats |
1.1 |
if (new_vector == NULL && new_count != 0) { |
57 |
|
|
/* Out of memory -- free the contents of the vector. */ |
58 |
ats |
1.5 |
sg_vector_resize(vector, h, 0); |
59 |
ats |
1.4 |
h->error = -1; |
60 |
|
|
return vector; |
61 |
ats |
1.1 |
} |
62 |
|
|
|
63 |
ats |
1.3 |
vector = new_vector; |
64 |
ats |
1.1 |
h->alloc_count = new_count; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
/* Initialise any new items. */ |
68 |
|
|
if (count > h->used_count && h->init_fn != NULL) { |
69 |
|
|
for (i = h->used_count; i < count; i++) { |
70 |
ats |
1.3 |
h->init_fn((void *) (vector + i * h->item_size)); |
71 |
ats |
1.1 |
} |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
h->used_count = count; |
75 |
ats |
1.4 |
h->error = 0; |
76 |
ats |
1.3 |
return vector; |
77 |
ats |
1.1 |
} |
78 |
|
|
|