Debian Codegraph @

ulib/util.c

raw
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4
5 #include "ulib/util.h"
6
7 #ifdef __cplusplus
8 }
9 #endif
10
11 /******************************************************************************
12 * all for croak_caller before 5.13.4.
13 */
14 #define PERL_ARGS_ASSERT_DOPOPTOSUB_AT assert(cxstk)
15
16 #ifdef PERL_GLOBAL_STRUCT
17 # define dVAR pVAR = (struct perl_vars*)PERL_GET_VARS()
18 #else
19 # define dVAR dNOOP
20 #endif
21
22 #ifndef dopoptosub
23 #define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
24 #endif
25
26 #ifndef dopoptosub_at
27 #if !defined(PERL_IMPLICIT_CONTEXT)
28 # define dopoptosub_at my_dopoptosub_at
29 #else
30 # define dopoptosub_at(a,b) my_dopoptosub_at(aTHX_ a,b)
31 #endif
32 #endif
33
34 STATIC I32
35 my_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
36 {
37 dVAR;
38 I32 i;
39
40 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
41
42 for (i = startingblock; i >= 0; i--) {
43 register const PERL_CONTEXT * const cx = &cxstk[i];
44 switch (CxTYPE(cx)) {
45 default:
46 continue;
47 case CXt_EVAL:
48 case CXt_SUB:
49 case CXt_FORMAT:
50 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
51 return i;
52 }
53 }
54 return i;
55 }
56
57 const PERL_CONTEXT *
58 my_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
59 {
60 register I32 cxix = dopoptosub(cxstack_ix);
61 register const PERL_CONTEXT *cx;
62 register const PERL_CONTEXT *ccstack = cxstack;
63 const PERL_SI *top_si = PL_curstackinfo;
64
65 for (;;) {
66 /* we may be in a higher stacklevel, so dig down deeper */
67 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
68 top_si = top_si->si_prev;
69 ccstack = top_si->si_cxstack;
70 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
71 }
72 if (cxix < 0)
73 return NULL;
74 /* caller() should not report the automatic calls to &DB::sub */
75 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
76 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
77 count++;
78 if (!count--)
79 break;
80 cxix = dopoptosub_at(ccstack, cxix - 1);
81 }
82
83 cx = &ccstack[cxix];
84 if (dbcxp) *dbcxp = cx;
85
86 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
87 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
88 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
89 field below is defined for any cx. */
90 /* caller() should not report the automatic calls to &DB::sub */
91 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
92 cx = &ccstack[dbcxix];
93 }
94
95 return cx;
96 }
97
98 void my_croak_caller(const char *pat, ...) {
99 dTHX;
100 va_list args;
101 const PERL_CONTEXT *cx = my_caller_cx(aTHX_ 0, NULL);
102
103 /* make error appear at call site */
104 assert(cx);
105 PL_curcop = cx->blk_oldcop;
106
107 va_start(args, pat);
108 vcroak(pat, &args);
109 NOT_REACHED; /* NOTREACHED */
110 va_end(args);
111 }
112
113 /******************************************************************************/
114
115 void uu_clear(struct_uu_t *io) {
116 io->v1.time_low = 0;
117 io->v1.time_mid = 0;
118 io->v1.time_high_and_version = 0;
119 io->v1.clock_seq_and_variant = 0;
120 io->v1.node[0] = 0;
121 io->v1.node[1] = 0;
122 io->v1.node[2] = 0;
123 io->v1.node[3] = 0;
124 io->v1.node[4] = 0;
125 io->v1.node[5] = 0;
126 }
127
128 void uu_copy_binary(pUCXT, const uu_t in, uu_t out) {
129 /* XXX use Copy */
130 U8 *cp1 = (U8*)&in;
131 U8 *cp2 = out;
132 UV i;
133
134 for (i=0; i < 16; i++)
135 *cp1++ = *cp2++;
136 }
137
138 void uu_copy_struct(pUCXT, const struct_uu_t *in, struct_uu_t *out) {
139 /* XXX use Copy */
140 out->v1.time_low = in->v1.time_low;
141 out->v1.time_mid = in->v1.time_mid;
142 out->v1.time_high_and_version = in->v1.time_high_and_version;
143 out->v1.clock_seq_and_variant = in->v1.clock_seq_and_variant;
144 out->v1.node[0] = in->v1.node[0];
145 out->v1.node[1] = in->v1.node[1];
146 out->v1.node[2] = in->v1.node[2];
147 out->v1.node[3] = in->v1.node[3];
148 out->v1.node[4] = in->v1.node[4];
149 out->v1.node[5] = in->v1.node[5];
150 }
151
152 static NV uu_time_v1(const struct_uu_t *in) {
153 U64 sum;
154 NV rv;
155
156 sum = ((U64)in->v1.time_high_and_version & 0x0fff) << 48
157 | ((U64)in->v1.time_mid) << 32
158 | (U64)in->v1.time_low;
159 sum -= 122192928000000000ULL;
160 rv = (NV)sum / 10000000.0;
161
162 return rv;
163 }
164
165 static NV uu_time_v4(const struct_uu_t *in) {
166 return 0.0;
167 }
168
169 static NV uu_time_v6(const struct_uu_t *in) {
170 U64 sum;
171 NV rv;
172
173 sum = ((U64)in->v6.time_high) << 28
174 | ((U64)in->v6.time_mid) << 12
175 | ((U64)in->v6.time_low_and_version & 0x0fff);
176 sum -= 122192928000000000ULL;
177 rv = (NV)sum / 10000000.0;
178
179 return rv;
180 }
181
182 static NV uu_time_v7(const struct_uu_t *in) {
183 U64 sum;
184 NV rv;
185
186 sum = ((U64)in->v7.time_high) << 16
187 | (U64)in->v7.time_low;
188 rv = (NV)sum / 1000.0;
189
190 return rv;
191 }
192
193 NV uu_time(const struct_uu_t *in) {
194 int version;
195
196 version = in->v1.time_high_and_version >> 12;
197
198 switch(version) {
199 case 1: return uu_time_v1(in);
200 case 4: return uu_time_v4(in);
201 case 6: return uu_time_v6(in);
202 case 7: return uu_time_v7(in);
203 }
204 return 0;
205 }
206
207 /* a.k.a. version */
208 UV uu_type(const struct_uu_t *in) {
209 UV type;
210
211 type = in->v1.time_high_and_version >> 12;
212
213 if (type <= 8)
214 return type;
215 return 0;
216 }
217
218 UV uu_variant(const struct_uu_t *in) {
219 U16 variant;
220
221 variant = in->v1.clock_seq_and_variant;
222
223 if ((variant & 0x8000) == 0) return 0;
224 if ((variant & 0x4000) == 0) return 1;
225 if ((variant & 0x2000) == 0) return 2;
226 return 3;
227 }
228
229 /* ex:set ts=2 sw=2 itab=spaces: */