ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/vector.c
Revision: 1.2
Committed: Mon Apr 5 00:20:05 2004 UTC (20 years, 1 month ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.1: +5 -5 lines
Log Message:
Deal with vectors as char ** rather than void **, so we can do pointer
arithmetic without Sun cc complaining.

File Contents

# Content
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 * $Id: vector.c,v 1.1 2004/04/04 21:17:58 ats Exp $
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29
30 #include "vector.h"
31
32 int statgrab_vector_resize(char **vector, vector_header *h, int count) {
33 int new_count, i;
34
35 /* Destroy any now-unused items. */
36 if (count < h->used_count && h->destroy_fn != NULL) {
37 for (i = count; i < h->used_count; i++) {
38 h->destroy_fn((void *) ((*vector) + i * h->item_size));
39 }
40 }
41
42 /* Round up the desired size to the next multiple of the block size. */
43 new_count = ((count - 1 + h->block_size) / h->block_size)
44 * h->block_size;
45
46 /* Resize the vector if necessary. */
47 if (new_count != h->alloc_count) {
48 char *new_vector;
49
50 new_vector = realloc(*vector, new_count * h->item_size);
51 if (new_vector == NULL && new_count != 0) {
52 /* Out of memory -- free the contents of the vector. */
53 statgrab_vector_resize(vector, h, 0);
54 return -1;
55 }
56
57 *vector = new_vector;
58 h->alloc_count = new_count;
59 }
60
61 /* Initialise any new items. */
62 if (count > h->used_count && h->init_fn != NULL) {
63 for (i = h->used_count; i < count; i++) {
64 h->init_fn((void *) ((*vector) + i * h->item_size));
65 }
66 }
67
68 h->used_count = count;
69
70 return 0;
71 }
72