-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
120 lines (93 loc) · 3.07 KB
/
array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (c) 2017-2018 Josef 'Jeff' Sipek <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include <jeffpc/array.h>
#include <jeffpc/error.h>
struct array {
size_t elem_size; /* size of each element */
size_t elem_count; /* number of visible elements */
size_t preallocated; /* number of allocated elements */
uint8_t raw[];
};
static inline size_t total_size(size_t elem_size, size_t count)
{
return sizeof(struct array) + elem_size * count;
}
static inline void *get_ptr(struct array *array, size_t idx)
{
return &array->raw[array->elem_size * idx];
}
void *array_alloc(size_t elem_size, size_t prealloc_count)
{
struct array *array;
array = malloc(total_size(elem_size, prealloc_count));
if (!array)
return NULL;
array->elem_size = elem_size;
array->elem_count = 0;
array->preallocated = prealloc_count;
return array->raw;
}
void array_free(void *raw)
{
if (!raw)
return;
free(container_of(raw, struct array, raw));
}
static struct array *__array_truncate(struct array *array,
size_t new_elem_count)
{
struct array *newarray;
if (new_elem_count <= array->elem_count)
goto done; /* shrinking or no change */
if (new_elem_count <= array->preallocated)
goto clear; /* growing into preallocated space */
/* grow the allocation */
newarray = realloc(array, total_size(array->elem_size, new_elem_count));
if (!newarray)
return ERR_PTR(-ENOMEM);
array = newarray;
array->preallocated = new_elem_count;
clear:
memset(get_ptr(array, array->elem_count), 0,
total_size(array->elem_size, new_elem_count) -
total_size(array->elem_size, array->elem_count));
done:
array->elem_count = new_elem_count;
return array;
}
#undef array_truncate
int array_truncate(void **raw, size_t idx)
{
struct array *array = container_of(*raw, struct array, raw);
array = __array_truncate(array, idx);
if (IS_ERR(array))
return PTR_ERR(array);
*raw = array->raw;
return 0;
}
size_t array_size(void *raw)
{
if (!raw)
return 0;
return container_of(raw, struct array, raw)->elem_count;
}