tsort: replace with openbsd version
[unleashed.git] / contrib / libjeffpc / val.c
blobd2884bd17094efb5f6c3f4cd49ebb3c2e524362c
1 /*
2 * Copyright (c) 2014-2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
23 #include <string.h>
24 #include <errno.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <inttypes.h>
29 #include <umem.h>
31 #include <jeffpc/val.h>
32 #include <jeffpc/jeffpc.h>
33 #include <jeffpc/error.h>
35 static umem_cache_t *val_cache;
37 void init_val_subsys(void)
39 val_cache = umem_cache_create("val-cache", sizeof(struct val),
40 0, NULL, NULL, NULL, NULL, NULL, 0);
41 ASSERT(val_cache);
44 static void __val_init(struct val *val, enum val_type type)
46 val->type = type;
48 switch (type) {
49 case VT_BOOL:
50 val->b = false;
51 break;
52 case VT_INT:
53 val->i = 0;
54 break;
55 case VT_STR:
56 case VT_SYM:
57 val->str = NULL;
58 break;
59 case VT_CONS:
60 val->cons.head = NULL;
61 val->cons.tail = NULL;
62 break;
66 static void __val_cleanup(struct val *val)
68 switch (val->type) {
69 case VT_INT:
70 case VT_BOOL:
71 break;
72 case VT_STR:
73 case VT_SYM:
74 str_putref(val->str);
75 break;
76 case VT_CONS:
77 val_putref(val->cons.head);
78 val_putref(val->cons.tail);
79 break;
83 struct val *val_alloc(enum val_type type)
85 struct val *val;
87 val = umem_cache_alloc(val_cache, 0);
88 if (!val)
89 return val;
91 refcnt_init(&val->refcnt, 1);
93 __val_init(val, type);
95 return val;
98 void val_free(struct val *val)
100 ASSERT(val);
101 ASSERT3U(refcnt_read(&val->refcnt), ==, 0);
103 __val_cleanup(val);
105 umem_cache_free(val_cache, val);
108 #define DEF_VAL_SET(fxn, vttype, valelem, ctype) \
109 int val_set_##fxn(struct val *val, ctype v) \
111 __val_cleanup(val); \
113 val->type = vttype; \
114 val->valelem = v; \
116 return 0; \
119 DEF_VAL_SET(int, VT_INT, i, uint64_t)
120 DEF_VAL_SET(str, VT_STR, str, struct str *)
121 DEF_VAL_SET(sym, VT_SYM, str, struct str *)
122 DEF_VAL_SET(bool, VT_BOOL, b, bool)
124 int val_set_cons(struct val *val, struct val *head, struct val *tail)
126 __val_cleanup(val);
128 val->type = VT_CONS;
129 val->cons.head = head;
130 val->cons.tail = tail;
132 return 0;
135 void val_dump(struct val *val, int indent)
137 if (!val)
138 return;
140 switch (val->type) {
141 case VT_STR:
142 case VT_SYM:
143 fprintf(stderr, "%*s'%s'\n", indent, "", str_cstr(val->str));
144 break;
145 case VT_INT:
146 fprintf(stderr, "%*s%"PRIu64"\n", indent, "", val->i);
147 break;
148 case VT_BOOL:
149 fprintf(stderr, "%*s%s\n", indent, "",
150 val->b ? "true" : "false");
151 break;
152 case VT_CONS:
153 fprintf(stderr, "%*scons head:\n", indent, "");
154 val_dump(val->cons.head, indent + 2);
155 fprintf(stderr, "%*scons tail:\n", indent, "");
156 val_dump(val->cons.tail, indent + 2);
157 break;
158 default:
159 fprintf(stderr, "Unknown type %d\n", val->type);
160 break;