hreg.h

filter: Statementstmt 100% 8 / 8 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 12 / 12 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« genxs.pl ↑ Summary
1 #ifndef HREG_H_
2 #define HREG_H_
3  
4 #include "EXTERN.h"
5 #include "perl.h"
6 #include "XSUB.h"
7 #include <stdint.h>
8  
9 /*#define HR_DEBUG*/
10  
11 //#define HR_DEBUG
12  
13 #ifndef HR_DEBUG
14 static int _hr_enable_debug = -1;
15 #define _hr_can_debug \
16 (_hr_enable_debug >= 0 ? _hr_enable_debug : \
17 (_hr_enable_debug = getenv("HR_DEBUG") ? 1 : 0))
18  
19 #define HR_DEBUG(fmt, ...) if(_hr_can_debug) { \
20 fprintf(stderr, "[%s:%d (%s)] " fmt "\n", \
21 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
22 }
23 #endif
24  
25 #define _hashref_eq(r1, r2) \
26 (SvROK(r1) && SvROK(r2) && \
27 SvRV(r1) == SvRV(r2))
28  
29  
30 #define _mg_action_list(mg) (HR_Action*)mg->mg_ptr
31  
32 static inline void
33 20 _mk_ptr_string(char *str, size_t value)
34 {
35 /*Pirated from:
36 http://code.google.com/p/stringencoders/
37 */
38 20 char* wstr=str;
39 char aux;
40 // Conversion. Number is reversed.
41
42 296 ▶ do *wstr++ = (char)(48 + (value % 10)); while (value /= 10);
Branch: 1 branch
BranchTrueFalseTotal
do *wstr++ = (char)(48 + (value % 10)); while (value /= 10); 276 20 296
43 20 *wstr='\0';
44
45 // Reverse string
46 20 wstr--;
47 160 ▶ while (wstr > str)
Branch: 1 branch
BranchTrueFalseTotal
while (wstr > str) 140 20 160
48 140 aux = *wstr, *wstr-- = *str, *str++ = aux;
49 20 }
50  
51 //#define mk_ptr_string(vname, ptr) \
52 // char vname[128] = { '\0' }; \
53 // sprintf(vname, "%lu", ptr);
54  
55 #define mk_ptr_string(vname, ptr) \
56 char vname[128]; \
57 _mk_ptr_string(vname, (size_t)ptr);
58  
59 #ifdef __GNUC__
60 #define inline __inline__
61 #endif
62  
63 #if __STDC__ && __STDC_VERSION__ <= 199409L && !defined __GNUC__
64 #warning "c89 mode and GCC not detected. Not using inline"
65 #define HR_INLINE static
66 #else
67 #define HR_INLINE static inline
68 #endif
69  
70 #define HR_TABLE_ARRAY
71  
72 /*Memory Stuff*/
73  
74 //#define HR_PERL_MALLOC
75  
76 #ifdef HR_PERL_MALLOC
77 #warning "Using Perl_malloc"
78 #undef Perl_malloc
79 #undef Perl_mfree
80  
81 #define Newxz_Action(ptr) \
82 ptr = Perl_malloc(sizeof(HR_Action)); \
83 Zero(ptr, 1, HR_Action);
84  
85 #define Newxz_Action_len(ptr, extra_len) \
86 ptr = Perl_malloc(sizeof(HR_Action) + extra_len); \
87 Zero(ptr, 1, HR_Action);
88  
89 #define Resize_Action_tail(ptr, tail_len) \
90 ptr = Perl_realloc(ptr, sizeof(HR_Action)+tail_len);
91  
92 #define Free_Action(ptr) \
93 Perl_mfree(ptr);
94  
95 #else /*!HR_PERL_MALLOC*/
96
97 #define Newxz_Action(ptr) \
98 Newxz(ptr, 1, HR_Action)
99  
100 #define Free_Action(ptr) \
101 Safefree(ptr);
102  
103 #endif
104  
105 /*action tail functions*/
106  
107 #define action_is_tailed_nocheck(actionp) \
108 ((actionp->flags & HR_FLAG_STR_NO_ALLOC) == 0)
109  
110 #define action_is_tailed(actionp) \
111 (actionp->ktype == HR_KEY_TYPE_STR && action_is_tailed_nocheck(actionp))
112  
113 #define action_keystr(actionp) \
114 (action_is_tailed_nocheck(actionp)) ? \
115 (char*)(((char*)actionp)+sizeof(HR_Action)) : \
116 actionp->key
117  
118 #define action_keylen(actionp) \
119 (action_is_tailed_nocheck(actionp)) ? \
120 (size_t)(actionp->key) : \
121 strlen(actionp->key)
122  
123 #define HREG_API_INTERNAL
124  
125 typedef enum {
126 HR_ACTION_TYPE_NULL = 0,
127 HR_ACTION_TYPE_DEL_AV = 1,
128 HR_ACTION_TYPE_DEL_HV = 2,
129 HR_ACTION_TYPE_CALL_CV = 3,
130 HR_ACTION_TYPE_CALL_CFUNC = 4
131 } HR_ActionType_t;
132  
133 typedef enum {
134 HR_KEY_TYPE_NULL = 0,
135 HR_KEY_TYPE_PTR = 1,
136 HR_KEY_TYPE_STR = 2,
137
138
139 /*Extended options for searching*/
140 /*RV implies we should:
141 1) check the flags to see if the stored key is an RV,
142 2) compare the keys performing SvRV on the stored key,
143 assume current search spec is already dereferenced
144 */
145 HR_KEY_STYPE_PTR_RV = 100,
146 /*Sometimes a container is opaque (like a C callback)*/
147 HR_KEY_SFLAG_HASHREF_OPAQUE = 0x2000
148
149 } HR_KeyType_t;
150  
151 typedef enum {
152 HR_ACTION_NOT_FOUND, /*Action was not found in the list*/
153 HR_ACTION_DELETED, /*Deletion OK*/
154 HR_ACTION_EMPTY /*No more actions left in the table. Hint for unmagic*/
155 } HR_DeletionStatus_t;
156  
157  
158 enum {
159 HR_FLAG_STR_NO_ALLOC = 1 << 0, /*Do not copy/allocate/free string*/
160 HR_FLAG_HASHREF_WEAKEN = 1 << 1, /*Weaken hashref, assumes HASHREF_RV*/
161 HR_FLAG_SV_REFCNT_DEC = 1 << 2, /*Key is an SV whose REFCNT we should decrease*/
162 HR_FLAG_PTR_NO_STRINGIFY = 1 << 3, /*Do not stringify the pointer*/
163 HR_FLAG_HASHREF_RV = 1 << 4, /*hashref is a reference, not a plain SV*/
164 };
165  
166 /*We re-use the STR_NO_ALLOC field for an SV flag, which is obviously a TYPE_PTR*/
167 #define HR_FLAG_SV_KEY (1<<0)
168  
169 #define action_key_is_rv(aptr) ((aptr)->flags & HR_FLAG_SV_REFCNT_DEC)
170 #define action_container_is_sv(aptr) ((aptr->atype != HR_ACTION_TYPE_CALL_CFUNC))
171 #define action_container_is_rv(aptr) ((aptr->flags & (HR_FLAG_HASHREF_RV)))
172 typedef struct HR_Action HR_Action;
173 typedef void(*HR_ActionCallback)(void*,SV*,HR_Action*);
174  
175 struct
176 __attribute__((__packed__))
177 HR_Action {
178 HR_Action *next;
179 void *key; /*Key*/
180 unsigned int atype : 3; /*Action type*/
181 unsigned int ktype : 2; /*Key type*/
182 SV *hashref; /*Container*/
183 unsigned int flags : 5; /*Flags*/
184 };
185  
186 /*This will clear an action's data fields, while keeping the next pointer
187 for the linked list*/
188  
189 #define action_clear(actionp) \
190 Zero(((char**)actionp)+1, sizeof(HR_Action)-sizeof(HR_Action*), char);
191  
192  
193 #define HR_ACTION_LIST_TERMINATOR \
194 { NULL, NULL, HR_KEY_TYPE_NULL, HR_ACTION_TYPE_NULL, 0, 0 }
195  
196 /*Helper macros for common HR_Action specifications*/
197 #define HR_DREF_FLDS_ptr_from_hv(ptr, container) \
198 { .ktype = HR_KEY_TYPE_PTR, .atype = HR_ACTION_TYPE_DEL_HV, \
199 .key = (char*)(ptr), .hashref = container }
200  
201 #define HR_DREF_FLDS_Nstr_from_hv(newstr, container) \
202 { .ktype = HR_KEY_TYPE_STR, .atype = HR_ACTION_TYPE_DEL_HV, \
203 .key = newstr, .hashref = container }
204  
205 #define HR_DREF_FLDS_Estr_from_hv(estr, container) \
206 { .ktype = HR_KEY_TYPE_STR, .atype = HR_ACTION_TYPE_DEL_HV, \
207 .key = estr, .hashref = container, .flags = HR_FLAG_STR_NO_ALLOC }
208  
209 #define HR_DREF_FLDS_arg_for_cfunc(arg, fptr) \
210 { .ktype = HR_KEY_TYPE_PTR, .atype = HR_ACTION_TYPE_CALL_CFUNC, \
211 .key = arg, .hashref = (SV*)fptr }
212  
213 HREG_API_INTERNAL
214 void HR_add_action(HR_Action *action_list, HR_Action *new_action, int want_unique);
215  
216 HREG_API_INTERNAL
217 void HR_trigger_and_free_actions(HR_Action *action_list, SV *object);
218  
219 HREG_API_INTERNAL
220 HR_DeletionStatus_t
221 HR_del_action(HR_Action *action_list, SV *hashref, void *key, HR_KeyType_t ktype);
222  
223 HREG_API_INTERNAL
224 HR_DeletionStatus_t
225 HR_nullify_action(HR_Action *action_list, SV *hashref, void *key, HR_KeyType_t ktype);
226  
227 HREG_API_INTERNAL
228 HR_Action*
229 HR_free_action(HR_Action *action);
230 /*
231 ////////////////////////////////////////////////////////////////////////////////
232 ////////////////////////////////////////////////////////////////////////////////
233 ////////////////////////////////////////////////////////////////////////////////
234 /// Perl Functions ///
235 ////////////////////////////////////////////////////////////////////////////////
236 ////////////////////////////////////////////////////////////////////////////////
237 ////////////////////////////////////////////////////////////////////////////////
238 */
239 HREG_API_INTERNAL
240 void HR_add_actions_real(SV *objref, HR_Action *actions);
241  
242 /*Perl API*/
243  
244 void HR_PL_add_action_ptr(SV *objref, SV *hashref);
245 void HR_PL_add_action_str(SV *objref, SV *hashref, char *key);
246  
247 void HR_PL_del_action_ptr(SV *object, SV *hashref, UV addr);
248 void HR_PL_del_action_str(SV *object, SV *hashref, char *str);
249 void HR_PL_del_action_container(SV *object, SV *hashref);
250 void HR_PL_del_action_sv(SV *object, SV *hashref, SV *keysv);
251  
252  
253 /*More extended deletion type..*/
254 void HR_XS_del_action_ext(SV *object, void *container,
255 void *arg, HR_KeyType_t ktype);
256  
257  
258 /*This is mainly for Ref::Destructor, and allows a more versatile, possibly
259 slower, but safer specification of actions. Specifically, the target object
260 will always be a reference (though it can be weakened)
261 */
262  
263 void HR_PL_add_action_ext(
264 SV *objref, UV key, unsigned int atype, unsigned int ktype, SV *hashref,
265 unsigned int flags);
266  
267  
268 /* H::R implementation */
269  
270 SV* HRXSK_new(char *package, char *key, SV *forward, SV *scalar_lookup);
271 char* HRXSK_kstring(SV* self);
272 UV HRXSK_prefix_len(SV *self);
273 void HRXSK_ithread_postdup(SV *newself, SV *newtable, HV *ptr_map, UV old_table);
274  
275 SV* HRXSK_encap_new(char *package, SV *encapsulated_object,
276 SV *table, SV *forward, SV *scalar_lookup);
277 UV HRXSK_encap_kstring(SV *ksv_ref);
278 void HRXSK_encap_weaken(SV *ksv_ref);
279 void HRXSK_encap_link_value(SV *self, SV *value);
280 SV* HRXSK_encap_getencap(SV *self);
281  
282  
283 void HRXSATTR_unlink_value(SV *aobj, SV *value);
284 SV* HRXSATTR_get_hash(SV *aobj);
285 char* HRXSATTR_kstring(SV *aobj);
286 UV HRXSATTR_prefix_len(SV *aobj);
287 SV* HRXSATTR_encap_ukey(SV *aobj);
288  
289 void HRXSK_encap_ithread_predup(SV *self, SV *table, HV *ptr_map, SV *value);
290 void HRXSK_encap_ithread_postdup(SV *newself, SV *newtable, HV *ptr_map, UV old_table);
291  
292 void HRXSATTR_ithread_predup(SV *self, SV *table, HV *ptr_map);
293 void HRXSATTR_ithread_postdup(SV *newself, SV *newtable, HV *ptr_map);
294  
295 /*H::R API*/
296 void HRA_table_init(SV *self);
297 void HRA_store_sk(SV *hr, SV *ukey, SV *value, ...);
298 void HRA_store_kt(SV *hr, SV *ukey, SV *t, SV *value, ...);
299 SV* HRA_fetch_sk(SV *hr, SV *ukey); /*we manipulate perl's stack in this one*/
300  
301 void HRA_store_a(SV *hr, SV *attr, char *t, SV *value, ...);
302 void HRA_fetch_a(SV *hr, SV *attr, char *t);
303 void HRA_dissoc_a(SV *hr, SV *attr, char *t, SV *value);
304 void HRA_unlink_a(SV *hr, SV *attr, char *t);
305 SV* HRA_attr_get(SV *hr, SV *attr, char *t); //Do we really need this?
306 void HRA_ithread_store_lookup_info(SV *self, HV *ptr_map);
307  
308 #endif /*HREG_H_*/
« genxs.pl ↑ Summary