blob: f6e22547423a0fb9e6842242ed4670daec6d48a2 (
plain)
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
|
/*
* Copyright (c) 2026, Apollo Telephone Laboratories.
* Provided under the BSD-3 clause.
*/
#include <stdlib.h>
#include <string.h>
#include "defconf/ptrbox.h"
static inline void
ptrbox_insert(struct ptrbox *ptrbox, struct ptrbox_entry *entry)
{
if (ptrbox == NULL || entry == NULL) {
return;
}
TAILQ_INSERT_TAIL(&ptrbox->entries, entry, link);
++ptrbox->entry_count;
}
/*
* Allocate a new pointer box entry
*
* @data: Data to be associated with pointer box entry
*/
static struct ptrbox_entry *
ptrbox_alloc_entry(void *data)
{
struct ptrbox_entry *entry;
if (data == NULL) {
return NULL;
}
entry = malloc(sizeof(*entry));
if (entry == NULL) {
return NULL;
}
entry->data = data;
return entry;
}
char *
ptrbox_strdup(struct ptrbox *ptrbox, const char *s)
{
struct ptrbox_entry *entry;
char *dup;
if (ptrbox == NULL || s == NULL) {
return NULL;
}
if ((dup = strdup(s)) == NULL) {
return NULL;
}
if ((entry = ptrbox_alloc_entry(dup)) == NULL) {
free(dup);
return NULL;
}
ptrbox_insert(ptrbox, entry);
return dup;
}
int
ptrbox_init(struct ptrbox *ptrbox)
{
if (ptrbox == NULL) {
return -1;
}
TAILQ_INIT(&ptrbox->entries);
ptrbox->entry_count = 0;
return 0;
}
void
ptrbox_destroy(struct ptrbox *ptrbox)
{
struct ptrbox_entry *entry, *tmp;
if (ptrbox == NULL) {
return;
}
entry = TAILQ_FIRST(&ptrbox->entries);
while (entry != NULL) {
if (entry->data != NULL) {
free(entry->data);
}
tmp = entry;
entry = TAILQ_NEXT(entry, link);
free(tmp);
}
}
|