Skip to content

Commit 167dba6

Browse files
committed
btrfs-progs: add an extensible pointer array struct
API for extensible array of pointers for covenience. A simple wrapper around a (void *) array with length. Signed-off-by: David Sterba <[email protected]>
1 parent 7ed070e commit 167dba6

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ objects = \
194194
kernel-shared/uuid-tree.o \
195195
kernel-shared/volumes.o \
196196
kernel-shared/zoned.o \
197+
common/array.o \
197198
common/cpu-utils.o \
198199
common/device-scan.o \
199200
common/device-utils.o \

common/array.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or
3+
* modify it under the terms of the GNU General Public
4+
* License v2 as published by the Free Software Foundation.
5+
*
6+
* This program is distributed in the hope that it will be useful,
7+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9+
* General Public License for more details.
10+
*
11+
* You should have received a copy of the GNU General Public
12+
* License along with this program; if not, write to the
13+
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14+
* Boston, MA 021110-1307, USA.
15+
*/
16+
17+
#define _GNU_SOURCE 1
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <string.h>
21+
#include "common/array.h"
22+
23+
/*
24+
* Extensible array of pointers. Length is the number of user defined pointers,
25+
* capacity is the whole allocated size. Pointers are potentially unstable after
26+
* an append operation. Array initialized to all zeros is valid and can be extended.
27+
*/
28+
29+
static const int alloc_increment = 32;
30+
31+
/* Initialize new array, preallocate @capacity elemennts. */
32+
int array_init(struct array *arr, unsigned int capacity)
33+
{
34+
void *tmp;
35+
36+
arr->data = NULL;
37+
arr->length = 0;
38+
arr->capacity = capacity;
39+
if (arr->capacity == 0)
40+
arr->capacity = alloc_increment;
41+
42+
tmp = calloc(arr->capacity, sizeof(void *));
43+
if (!tmp)
44+
return -1;
45+
arr->data = tmp;
46+
return 0;
47+
}
48+
49+
/* Free internal data array. */
50+
void array_free(struct array *arr)
51+
{
52+
free(arr->data);
53+
arr->length = 0;
54+
arr->capacity = 0;
55+
arr->data = NULL;
56+
}
57+
58+
/* Free all elements up to length. */
59+
void array_free_elements(struct array *arr)
60+
{
61+
unsigned int i;
62+
63+
for (i = 0; i < arr->length; i++) {
64+
free(arr->data[i]);
65+
arr->data[i] = NULL;
66+
}
67+
arr->length = 0;
68+
}
69+
70+
/* Reset all elements to NULL up to capacity. */
71+
void array_clear(struct array *arr)
72+
{
73+
for (unsigned int i = 0; i < arr->capacity; i++)
74+
arr->data[i] = NULL;
75+
}
76+
77+
/* Use the whole capacity for elements. */
78+
void array_use_capacity(struct array *arr)
79+
{
80+
arr->length = arr->capacity;
81+
}
82+
83+
/* Append a new element (increas length), extend the array if needed. */
84+
int array_append(struct array *arr, void *element)
85+
{
86+
if (arr->length == arr->capacity) {
87+
void **tmp;
88+
89+
tmp = realloc(arr->data, (arr->capacity + alloc_increment) * sizeof(void *));
90+
if (!tmp)
91+
return -1;
92+
arr->data = tmp;
93+
memset(&arr->data[arr->capacity], 0, alloc_increment * sizeof(void *));
94+
arr->capacity += alloc_increment;
95+
}
96+
arr->data[arr->length] = element;
97+
arr->length++;
98+
return 0;
99+
}

common/array.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or
3+
* modify it under the terms of the GNU General Public
4+
* License v2 as published by the Free Software Foundation.
5+
*
6+
* This program is distributed in the hope that it will be useful,
7+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9+
* General Public License for more details.
10+
*
11+
* You should have received a copy of the GNU General Public
12+
* License along with this program; if not, write to the
13+
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14+
* Boston, MA 021110-1307, USA.
15+
*/
16+
17+
#ifndef __COMMON_ARRAY_H__
18+
#define __COMMON_ARRAY_H__
19+
20+
/*
21+
* Extensible array of pointers.
22+
*/
23+
struct array {
24+
void **data;
25+
unsigned int length;
26+
unsigned int capacity;
27+
};
28+
29+
int array_init(struct array *arr, unsigned int capacity);
30+
void array_free(struct array *arr);
31+
void array_free_elements(struct array *arr);
32+
void array_clear(struct array *arr);
33+
void array_use_capacity(struct array *arr);
34+
int array_append(struct array *arr, void *element);
35+
36+
#endif

0 commit comments

Comments
 (0)