1 /*
2  * This file is part of the Yices SMT Solver.
3  * Copyright (C) 2017 SRI International.
4  *
5  * Yices is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Yices is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Yices.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /*
20  * STRINGS WITH REFERENCE COUNTERS
21  */
22 
23 #ifndef __REFCOUNT_STRINGS
24 #define __REFCOUNT_STRINGS
25 
26 #include <stdint.h>
27 #include <stddef.h>
28 
29 
30 /*
31  * The reference counter is hidden in a header
32  */
33 typedef struct {
34   uint32_t ref;  // should be plenty; no check for overflow is implemented.
35   char str[0];   // the real size is determined a allocation time.
36 } string_t;
37 
38 
39 /*
40  * Bound on the string size: for a string of length n
41  * we need to allocate (n + 1) + sizeof(string_t) bytes.
42  * Just to be safe, we raise an 'out_of_memory' exception
43  * if n is more than MAX_REFCOUNT_STRING_SIZE.
44  */
45 #define MAX_REFCOUNT_STRING_SIZE (UINT32_MAX - sizeof(string_t) - 1)
46 
47 
48 /*
49  * Make a copy of str with ref count 0.
50  * - str must be terminated by '\0'
51  * - may cause 'out_of_memory' error
52  */
53 extern char *clone_string(const char *str);
54 
55 
56 /*
57  * header of string s
58  */
string_header(const char * s)59 static inline string_t *string_header(const char *s) {
60   return (string_t *) (s - offsetof(string_t, str));
61 }
62 
63 /*
64  * Increment ref counter for string s
65  */
string_incref(char * s)66 static inline void string_incref(char *s) {
67   string_header(s)->ref ++;
68 }
69 
70 /*
71  * Decrement ref counter for s and free the string if the
72  * counter is zero
73  */
74 extern void string_decref(char *s);
75 
76 #endif
77