~ubuntu-branches/ubuntu/trusty/nilfs-tools/trusty

« back to all changes in this revision

Viewing changes to lib/vector.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2011-07-10 17:10:21 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110710171021-xkbtr81jprb97iec
Tags: upstream-2.1.0~rc1
ImportĀ upstreamĀ versionĀ 2.1.0~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * vector.c - resizable array (should be replaced with STL vector).
 
3
 *
 
4
 * Licensed under LGPLv2: the complete text of the GNU Lesser General
 
5
 * Public License can be found in COPYING file of the nilfs-utils
 
6
 * package.
 
7
 *
 
8
 * Copyright (C) 2008-2011 Nippon Telegraph and Telephone Corporation.
 
9
 */
 
10
 
 
11
#ifdef HAVE_CONFIG_H
 
12
#include "config.h"
 
13
#endif  /* HAVE_CONFIG_H */
 
14
 
 
15
#if HAVE_STDLIB_H
 
16
#include <stdlib.h>
 
17
#endif  /* HAVE_STDLIB_H */
 
18
 
 
19
#if HAVE_STRING_H
 
20
#include <string.h>
 
21
#endif  /* HAVE_STRING_H */
 
22
 
 
23
#include <errno.h>
 
24
#include "vector.h"
 
25
 
 
26
 
 
27
/**
 
28
 * nilfs_vector_create - create a vector
 
29
 * @elemsize: element size
 
30
 *
 
31
 * Description: nilfs_vector_create() creates a new vector, which is a
 
32
 * resizable array of data. The size of each element is specified by
 
33
 * @elemsize.
 
34
 *
 
35
 * Return Value: On success, the pointer to the newly-created vector is
 
36
 * returned. On error, NULL is returned.
 
37
 */
 
38
struct nilfs_vector *nilfs_vector_create(size_t elemsize)
 
39
{
 
40
        struct nilfs_vector *vector;
 
41
 
 
42
        if ((vector = malloc(sizeof(struct nilfs_vector))) == NULL)
 
43
                return NULL;
 
44
 
 
45
        if ((vector->v_data = malloc(elemsize * NILFS_VECTOR_INIT_MAXELEMS)) == NULL) {
 
46
                free(vector);
 
47
                return NULL;
 
48
        }
 
49
 
 
50
        vector->v_elemsize = elemsize;
 
51
        vector->v_maxelems = NILFS_VECTOR_INIT_MAXELEMS;
 
52
        vector->v_nelems = 0;
 
53
 
 
54
        return vector;
 
55
}
 
56
 
 
57
/**
 
58
 * nilfs_vector_destroy - destroy a vector
 
59
 * @vector: vector
 
60
 *
 
61
 * Description: nilfs_vector_destroy() destroys the vector specified by
 
62
 * @vector, which must have been returned by a previous call to
 
63
 * nilfs_vector_create().
 
64
 */
 
65
void nilfs_vector_destroy(struct nilfs_vector *vector)
 
66
{
 
67
        if (vector != NULL) {
 
68
                free(vector->v_data);
 
69
                free(vector);
 
70
        }
 
71
}
 
72
 
 
73
/**
 
74
 * nilfs_vector_get_new_element - add a new element
 
75
 * @vector: vector
 
76
 *
 
77
 * Description: nilfs_vector_get_new_element() adds a new element at the end
 
78
 * of the vector @vector.
 
79
 *
 
80
 * Return Value: on success, the pointer to the new element is returned. On
 
81
 * error, NULL is returned.
 
82
 */
 
83
void *nilfs_vector_get_new_element(struct nilfs_vector *vector)
 
84
{
 
85
        void *data;
 
86
        size_t maxelems;
 
87
 
 
88
        /* resize array if necessary */
 
89
        if (vector->v_nelems >= vector->v_maxelems) {
 
90
                maxelems = vector->v_maxelems * NILFS_VECTOR_FACTOR;
 
91
                if ((data = realloc(vector->v_data,
 
92
                                    vector->v_elemsize * maxelems)) == NULL)
 
93
                        return NULL;
 
94
                vector->v_data = data;
 
95
                vector->v_maxelems = maxelems;
 
96
        }
 
97
        return vector->v_data + vector->v_elemsize * vector->v_nelems++;
 
98
}
 
99
 
 
100
/**
 
101
 * nilfs_vector_delete_elements - delete elements
 
102
 * @vector: vector
 
103
 * @index: index
 
104
 * @nelems: number of elements to be deleted
 
105
 *
 
106
 * Description: nilfs_vector_delete_elements() deletes @nelems elements from
 
107
 * @index.
 
108
 *
 
109
 * Return Value: On success, 0 is returned. On error, -1 is returned.
 
110
 */
 
111
int nilfs_vector_delete_elements(struct nilfs_vector *vector,
 
112
                                 unsigned int index,
 
113
                                 size_t nelems)
 
114
{
 
115
        if ((index >= vector->v_nelems) ||
 
116
            (index + nelems - 1 >= vector->v_nelems)) {
 
117
                errno = EINVAL;
 
118
                return -1;
 
119
        }
 
120
 
 
121
        if (index + nelems - 1 < vector->v_nelems - 1)
 
122
                memmove(vector->v_data + vector->v_elemsize * index,
 
123
                        vector->v_data + vector->v_elemsize * (index + nelems),
 
124
                        (vector->v_nelems - (index + nelems)) *
 
125
                        vector->v_elemsize);
 
126
        vector->v_nelems -= nelems;
 
127
        return 0;
 
128
}