-
Notifications
You must be signed in to change notification settings - Fork 0
/
se-node.c
56 lines (43 loc) · 927 Bytes
/
se-node.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
/*
* S-Expression Node helpers
*
* Copyright (c) 2016-2021 Alexei A. Smekalkine <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <stdarg.h>
#include <stdlib.h>
#include "se-node.h"
/*
* NOTE: Ownership of arguments transferred, and in case of errors all
* arguments will be freed.
*/
struct se_node *se_node_alloc (int type, int rank, ...)
{
struct se_node *o;
va_list ap;
size_t i;
va_start (ap, rank);
if ((o = malloc (sizeof (*o) + sizeof (o->arg[0]) * rank)) == NULL)
goto error;
o->type = type;
o->rank = rank;
for (i = 0; i < rank; ++i)
o->arg[i] = va_arg (ap, struct se_node *);
va_end (ap);
return o;
error:
for (i = 0; i < rank; ++i)
se_node_free (va_arg (ap, struct se_node *));
va_end (ap);
return NULL;
}
void se_node_free (struct se_node *o)
{
size_t i;
if (o == NULL)
return;
for (i = 0; i < o->rank; ++i)
se_node_free (o->arg[i]);
free (o);
}