gwenhywfar  4.6.0beta
refptr.c
Go to the documentation of this file.
1 /***************************************************************************
2  $RCSfile$
3  -------------------
4  cvs : $Id$
5  begin : Sun Jan 25 2004
6  copyright : (C) 2004 by Martin Preuss
7  email : martin@libchipcard.de
8 
9  ***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24  * MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 
33 #include "refptr_p.h"
34 #include <gwenhywfar/misc.h>
35 #include <gwenhywfar/debug.h>
36 #include <stdlib.h>
37 
38 
39 
40 GWEN_REFPTR_POBJECT *GWEN_RefPtrObject_new(void *dp, GWEN_REFPTR_INFO *rpi){
41  GWEN_REFPTR_POBJECT *rpo;
42 
43  GWEN_NEW_OBJECT(GWEN_REFPTR_POBJECT, rpo);
44  rpo->refCount=1;
45  rpo->ptr=dp;
46  rpo->infoPtr=rpi;
47  if (rpi)
49  return rpo;
50 }
51 
52 
53 
54 void GWEN_RefPtrObject_free(GWEN_REFPTR_POBJECT *rpo){
55  if (rpo) {
56  assert(rpo->refCount);
57  if (--(rpo->refCount)==0) {
58  DBG_VERBOUS(GWEN_LOGDOMAIN, "Freeing GWEN_RefPtrObject");
59  if ((rpo->flags & GWEN_REFPTR_FLAGS_AUTODELETE) &&
60  rpo->infoPtr &&
61  rpo->ptr) {
62  if (rpo->infoPtr->freeFn)
63  rpo->infoPtr->freeFn(rpo->ptr);
64  }
65  GWEN_FREE_OBJECT(rpo);
66  }
67  }
68 }
69 
70 
71 
72 void GWEN_RefPtrObject_Attach(GWEN_REFPTR_POBJECT *rpo){
73  assert(rpo);
74  assert(rpo->refCount);
75  rpo->refCount++;
76 }
77 
78 
79 
81  assert(rpi);
82  return rpi->flags;
83 }
84 
85 
86 
88  assert(rpi);
89  rpi->flags=fl;
90 }
91 
92 
93 
95  assert(rpi);
96  rpi->flags|=fl;
97 }
98 
99 
100 
102  assert(rpi);
103  rpi->flags&=~fl;
104 }
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
116  GWEN_REFPTR_INFO *rpi;
117 
119  rpi->refCount=1;
120 
121  return rpi;
122 }
123 
124 
125 
127  if (rpi) {
128  assert(rpi->refCount);
129  if (--(rpi->refCount)==0) {
130  DBG_VERBOUS(GWEN_LOGDOMAIN, "Freeing GWEN_RefPtrInfo");
131  GWEN_FREE_OBJECT(rpi);
132  }
133  }
134 }
135 
136 
137 
139  assert(rpi);
140  assert(rpi->refCount);
141  rpi->refCount++;
142 }
143 
144 
145 
148  assert(rpi);
149  rpi->freeFn=f;
150 }
151 
152 
153 
156  assert(rpi);
157  rpi->dupFn=f;
158 }
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
173  GWEN_REFPTR *rp;
174 
176  rp->objectPtr=GWEN_RefPtrObject_new(dp, rpi);
177  if (rpi)
178  rp->objectPtr->flags=rpi->flags;
179  return rp;
180 }
181 
182 
183 
185  if (rp) {
186  DBG_VERBOUS(GWEN_LOGDOMAIN, "Freeing GWEN_RefPtr");
187  GWEN_RefPtrObject_free(rp->objectPtr);
188  GWEN_FREE_OBJECT(rp);
189  }
190 }
191 
192 
193 
195  GWEN_REFPTR *nrp;
196 
197  assert(rp);
199  nrp->objectPtr=rp->objectPtr;
200  GWEN_RefPtrObject_Attach(nrp->objectPtr);
201  return nrp;
202 }
203 
204 
205 
207  GWEN_REFPTR *nrp;
208 
209  assert(rp);
210  if (rp->objectPtr) {
211  if (rp->objectPtr->ptr) {
212  if (rp->objectPtr->infoPtr) {
213  if (rp->objectPtr->infoPtr->dupFn) {
214  void *p;
215 
216  p=rp->objectPtr->infoPtr->dupFn(rp->objectPtr->ptr);
217  nrp=GWEN_RefPtr_new(p, rp->objectPtr->infoPtr);
218  return nrp;
219  }
220  }
221  }
222  }
223  return 0;
224 }
225 
226 
227 
229  if (!rp)
230  return 0;
231  if (rp->objectPtr) {
232  return rp->objectPtr->ptr;
233  }
234  return 0;
235 }
236 
237 
238 
240  assert(rp);
241 
242  if (rp->objectPtr) {
243  GWEN_RefPtrObject_free(rp->objectPtr);
244  }
245  rp->objectPtr=GWEN_RefPtrObject_new(dp, rpi);
246 }
247 
248 
249 
250 uint32_t GWEN_RefPtr_GetFlags(const GWEN_REFPTR *rp){
251  assert(rp);
252  if (rp->objectPtr)
253  return rp->objectPtr->flags;
254  DBG_INFO(GWEN_LOGDOMAIN, "No object in RefPtr");
255  return 0;
256 }
257 
258 
259 
260 void GWEN_RefPtr_SetFlags(GWEN_REFPTR *rp, uint32_t fl){
261  assert(rp);
262  if (rp->objectPtr)
263  rp->objectPtr->flags=fl;
264  else {
265  DBG_INFO(GWEN_LOGDOMAIN, "No object in RefPtr");
266  }
267 }
268 
269 
270 
271 void GWEN_RefPtr_AddFlags(GWEN_REFPTR *rp, uint32_t fl){
272  assert(rp);
273  if (rp->objectPtr)
274  rp->objectPtr->flags|=fl;
275  else {
276  DBG_INFO(GWEN_LOGDOMAIN, "No object in RefPtr");
277  }
278 }
279 
280 
281 
282 void GWEN_RefPtr_DelFlags(GWEN_REFPTR *rp, uint32_t fl){
283  assert(rp);
284  if (rp->objectPtr)
285  rp->objectPtr->flags&=~fl;
286  else {
287  DBG_INFO(GWEN_LOGDOMAIN, "No object in RefPtr");
288  }
289 }
290 
291 
292 
293 
294 
295 
296 
297 
298