1 /***************************************************************************
2 shmap_value.c - description
3 -------------------
4 begin : Wed Feb 27 2002
5 copyright : (C) 2002 by Margus Laak
6 email : margus@mail.ee
7 ***************************************************************************/
8
9 /***************************************************************************
10 * This library is free software; you can redistribute it and/or *
11 * modify it under the terms of the GNU Lesser General Public *
12 * License as published by the Free Software Foundation; either *
13 * version 2.1 of the License, or (at your option) any later version. *
14 * *
15 * This library is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18 * Lesser General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU Lesser General Public *
21 * License along with this library; if not, write to the Free Software *
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
23 ***************************************************************************/
24
25 #include "shmap_value.h"
26
27
shmap_value_init(void * p_mem,void * key,int keyhash,void * value,int opts)28 _shmap_value * shmap_value_init ( void * p_mem, void * key, int keyhash, void * value, int opts )
29 {
30 _shmap_value * ret_value;
31
32 CHECK_NULL(p_mem)
33
34 ret_value = (_shmap_value *) sh_mem_malloc(p_mem, sizeof(_shmap_value));
35 if ( ret_value != NULL )
36 {
37 ret_value->key = key;
38 ret_value->value = value;
39 ret_value->options = opts;
40 ret_value->keyhash = keyhash;
41 }
42
43 return ret_value;
44 }
45
shmap_pool_value_init(_shpool * pool,void * key,int keyhash,void * value,int opts)46 _shmap_value * shmap_pool_value_init ( _shpool * pool, void * key, int keyhash, void * value, int opts )
47 {
48 _shmap_value * ret_value;
49
50 CHECK_NULL(pool)
51
52 ret_value = (_shmap_value *) sh_pool_malloc(pool, sizeof(_shmap_value));
53 if ( ret_value != NULL )
54 {
55 ret_value->key = key;
56 ret_value->value = value;
57 ret_value->options = opts;
58 ret_value->keyhash = keyhash;
59 }
60
61 return ret_value;
62 }
63
64 /*
65 * Cleans the map item value
66 *
67 */
shmap_value_dispose(void * p_mem,_shmap_value * value,shmap_cleanup cleanup)68 int shmap_value_dispose ( void * p_mem, _shmap_value * value, shmap_cleanup cleanup )
69 {
70 int opts;
71
72 if ( cleanup != NULL )
73 {
74 cleanup(value, &opts);
75 }
76 else
77 {
78 opts = value->options;
79 }
80
81 if ( opts & __SHMAP_FREE_KEY )
82 sh_mem_free(p_mem, value->key);
83 if ( opts & __SHMAP_FREE_VALUE )
84 sh_mem_free(p_mem, value->value);
85 sh_mem_free(p_mem, value);
86
87 return 1;
88 }
89
shmap_pool_value_dispose(_shpool * pool,_shmap_value * value,shmap_cleanup cleanup)90 int shmap_pool_value_dispose ( _shpool * pool, _shmap_value * value, shmap_cleanup cleanup )
91 {
92 int opts;
93
94 if ( cleanup != NULL )
95 {
96 cleanup(value, &opts);
97 }
98 else
99 {
100 opts = value->options;
101 }
102
103 if ( opts & __SHMAP_FREE_KEY )
104 sh_pool_free(pool, value->key);
105 if ( opts & __SHMAP_FREE_VALUE )
106 sh_pool_free(pool, value->value);
107 sh_pool_free(pool, value);
108
109 return 1;
110 }
111
112 /*
113 only rewrites the value pointer for another one
114 if necessary also free-s the old pointer
115 */
shmap_value_replace_value(void * p_mem,_shmap_value * p_val,void * val,shmap_cleanup cleanup)116 int shmap_value_replace_value ( void * p_mem, _shmap_value * p_val, void * val, shmap_cleanup cleanup )
117 {
118 int opts;
119
120 if ( cleanup != NULL )
121 {
122 cleanup(p_val, &opts);
123 }
124 else
125 {
126 opts = p_val->options;
127 }
128
129 if ( opts & __SHMAP_FREE_VALUE )
130 sh_mem_free(p_mem, p_val->value);
131 p_val->value = val;
132
133 return 1;
134 }
135
shmap_pool_replace_value(_shpool * pool,_shmap_value * p_val,void * val,shmap_cleanup cleanup)136 int shmap_pool_replace_value ( _shpool * pool, _shmap_value * p_val, void * val, shmap_cleanup cleanup )
137 {
138 int opts;
139
140 if ( cleanup != NULL )
141 {
142 cleanup(p_val, &opts);
143 }
144 else
145 {
146 opts = p_val->options;
147 }
148
149 if ( opts & __SHMAP_FREE_VALUE )
150 sh_pool_free(pool, p_val->value);
151 p_val->value = val;
152
153 return 1;
154 }
155
shmap_value_replace(void * p_mem,_shmap_value * p_val,_shmap_value * value,shmap_cleanup cleanup)156 int shmap_value_replace ( void * p_mem, _shmap_value * p_val, _shmap_value * value, shmap_cleanup cleanup )
157 {
158 int opts;
159
160 if ( cleanup != NULL )
161 {
162 cleanup(p_val, &opts);
163 }
164 else
165 {
166 opts = p_val->options;
167 }
168
169 if ( opts & __SHMAP_FREE_VALUE )
170 sh_mem_free(p_mem, p_val->value);
171 p_val->value = value->value;
172 if ( opts & __SHMAP_FREE_KEY )
173 sh_mem_free(p_mem, p_val->key);
174 p_val->key = value->key;
175
176 p_val->keyhash = value->keyhash;
177
178 return 1;
179 }
180
shmap_pool_replace(_shpool * pool,_shmap_value * p_val,_shmap_value * value,shmap_cleanup cleanup)181 int shmap_pool_replace ( _shpool * pool, _shmap_value * p_val, _shmap_value * value, shmap_cleanup cleanup )
182 {
183 int opts;
184
185 if ( cleanup != NULL )
186 {
187 cleanup(p_val, &opts);
188 }
189 else
190 {
191 opts = p_val->options;
192 }
193
194 if ( opts & __SHMAP_FREE_VALUE )
195 sh_pool_free(pool, p_val->value);
196 p_val->value = value->value;
197 if ( opts & __SHMAP_FREE_KEY )
198 sh_pool_free(pool, p_val->key);
199 p_val->key = value->key;
200
201 p_val->keyhash = value->keyhash;
202
203 return 1;
204 }
205
206