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
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);
44 static void __val_init(struct val
*val
, enum val_type type
)
60 val
->cons
.head
= NULL
;
61 val
->cons
.tail
= NULL
;
66 static void __val_cleanup(struct val
*val
)
77 val_putref(val
->cons
.head
);
78 val_putref(val
->cons
.tail
);
83 struct val
*val_alloc(enum val_type type
)
87 val
= umem_cache_alloc(val_cache
, 0);
91 refcnt_init(&val
->refcnt
, 1);
93 __val_init(val
, type
);
98 void val_free(struct val
*val
)
101 ASSERT3U(refcnt_read(&val
->refcnt
), ==, 0);
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; \
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
)
129 val
->cons
.head
= head
;
130 val
->cons
.tail
= tail
;
135 void val_dump(struct val
*val
, int indent
)
143 fprintf(stderr
, "%*s'%s'\n", indent
, "", str_cstr(val
->str
));
146 fprintf(stderr
, "%*s%"PRIu64
"\n", indent
, "", val
->i
);
149 fprintf(stderr
, "%*s%s\n", indent
, "",
150 val
->b
? "true" : "false");
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);
159 fprintf(stderr
, "Unknown type %d\n", val
->type
);