use --WORLD as root of all actions (GC just deletes --WORLD)
[mala.git] / engine / stringbucket.c
blob6070283e757b93bef37efe2198da5d60ac89e33e
1 /*
2 stringbucket.c - MaLa stringbucket handling
4 Copyright (C) 2004, 2005, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program 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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
20 #define _GNU_SOURCE
22 #include <stdlib.h>
23 #include <search.h>
24 #include "stringbucket.h"
25 #include "strings.h"
28 void mala_stringbucket_init (MalaStringBucket bucket,
29 mala_string_cmp_fn cmpfn,
30 void (*freefn)(void*))
32 bucket->bucket = NULL;
33 bucket->cmpfn = cmpfn;
34 bucket->freefn = freefn;
35 bucket->gc_trace = MALA_NOTRACE;
38 void
39 mala_stringbucket_erase (MalaStringBucket bucket)
41 tdestroy (bucket->bucket, (void(*)(void*)) mala_string_free_forced);
42 bucket->bucket = NULL;
46 int
47 mala_stringbucket_exists_cstr (MalaStringBucket bucket, const char * cstr)
49 mala_string tmp = MALA_STRING_AUTO (cstr);
50 return !!tfind (&tmp, &bucket->bucket, bucket->cmpfn);
54 * remove string from bucket
56 MalaString
57 mala_stringbucket_string_remove (MalaString_ref self)
59 MALA_ASSERT(self);
61 if (!(*self)->bucket)
62 return *self;
64 /* self is the last reference in the bucket */
65 if ((*self)->refcnt <= 8)
67 if ((*self)->user && (*self)->bucket->freefn)
68 (*self)->bucket->freefn ((*self)->user);
69 (*self)->user = NULL;
71 tdelete (*self, &(*self)->bucket->bucket, (*self)->bucket->cmpfn);
72 (*self)->bucket = NULL;
74 /* buckets counted as reference too */
75 (*self)->refcnt -= 4;
77 return *self;
79 else
81 /* make a new string */
82 (*self)->refcnt -= 4;
83 return *self = mala_string_new ((*self)->str, NULL);
88 MalaString
89 mala_stringbucket_insert (MalaStringBucket bucket, MalaString_ref s)
91 MalaString_ref loc;
93 /* no bucket */
94 if (!bucket)
95 return *s;
97 /* already in this bucket */
98 if (bucket && (*s)->bucket == bucket)
99 return *s;
101 /* in another bucket, remove it */
102 if ((*s)->bucket)
104 mala_stringbucket_string_remove (s);
107 /*create in bucket*/
108 mala_string tmp = MALA_STRING_AUTO ((*s)->str);
110 /*create in bucket*/
111 loc = (MalaString_ref) tsearch (&tmp, &bucket->bucket, bucket->cmpfn);
112 if (!loc)
113 return NULL;
115 if (*loc == &tmp)
117 /*not in bucket*/
118 (*s)->bucket = bucket;
119 *loc = *s;
121 else
123 /*string already in bucket*/
124 mala_string_free (*s);
125 *s = *loc;
127 (*s)->refcnt += 4;
128 return *s;
134 // Local Variables:
135 // mode: C
136 // c-file-style: "gnu"
137 // End:
138 // arch-tag: 84fbd8ab-4e90-4d04-ab70-d9b955dde41c
139 // end_of_file