ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/vector.c
Revision: 1.9
Committed: Wed Apr 7 14:53:40 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.8: +3 -3 lines
Log Message:
Whitespace tidyup - change spaces to tabs.

File Contents

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