-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathda.c
More file actions
38 lines (30 loc) · 665 Bytes
/
da.c
File metadata and controls
38 lines (30 loc) · 665 Bytes
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
/*
* Colibri Dynamic Array
*
* Copyright (c) 2017-2023 Alexei A. Smekalkine
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <errno.h>
#include <stdlib.h>
#include <data/da.h>
static bool da_resize (struct da *o, size_t next)
{
void **data;
if (sizeof (o->data[0]) * next < sizeof (o->data[0]) * o->avail) {
errno = ENOMEM;
return false;
}
if ((data = realloc (o->data, sizeof (o->data[0]) * next)) == NULL)
return false;
o->avail = next;
o->data = data;
return true;
}
bool da_append (struct da *o, void *e)
{
if (o->count >= o->avail && !da_resize (o, o->avail * 2 | 1))
return false;
o->data[o->count++] = e;
return true;
}