ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
Revision: 1.3
Committed: Thu Jan 11 23:06:51 2007 UTC (17 years, 4 months ago) by root
Branch: MAIN
CVS Tags: rel-0_1
Changes since 1.2: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5     #define res_pair(text) \
6     { \
7     AV *av = newAV (); \
8     av_push (av, newSVpv (text, 0)); \
9     av_push (av, newRV_inc (sv)); \
10     av_push (about, newRV_noinc ((SV *)av)); \
11     }
12    
13     #define res_gv(sigil) \
14     { \
15     AV *av = newAV (); \
16     av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \
17     HvNAME (GvSTASH (sv)), \
18     GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \
19     0)); \
20     av_push (about, newRV_noinc ((SV *)av)); \
21     }
22    
23     MODULE = Devel::FindRef PACKAGE = Devel::FindRef
24    
25     PROTOTYPES: ENABLE
26    
27     SV *
28     ptr2ref (IV ptr)
29     CODE:
30     RETVAL = newRV_inc (INT2PTR (SV *, ptr));
31     OUTPUT:
32     RETVAL
33    
34     void
35     find_ (SV *target)
36     PPCODE:
37     {
38     SV *arena, *targ;
39     int rmagical, i;
40     AV *about = newAV ();
41     AV *excl = newAV ();
42    
43     if (!SvROK (target))
44 root 1.3 croak ("find expects a reference to a perl value");
45 root 1.1
46     targ = SvRV (target);
47    
48     for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena))
49     {
50     UV idx = SvREFCNT (arena);
51    
52     /* Remember that the zeroth slot is used as the pointer onwards, so don't
53     include it. */
54     while (--idx > 0)
55     {
56     SV *sv = &arena [idx];
57 root 1.2
58     if (SvTYPE (sv) >= SVt_LAST)
59     continue;
60    
61     /* temporarily disable RMAGICAL, it can easily interfere with us */
62 root 1.1 if ((rmagical = SvRMAGICAL (sv)))
63     SvRMAGICAL_off (sv);
64    
65 root 1.2 if (SvTYPE (sv) >= SVt_PVMG)
66     {
67     MAGIC *mg = SvMAGIC (sv);
68     while (mg)
69     {
70     if (mg->mg_obj == targ)
71     res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
72     if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
73     res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type));
74    
75     mg = mg->mg_moremagic;
76     }
77     }
78 root 1.1
79     switch (SvTYPE (sv))
80     {
81     case SVt_RV:
82     if (sv != target && SvRV (sv) == targ)
83     res_pair ("referenced by");
84     break;
85    
86     case SVt_PVAV:
87     if (AvREAL (sv))
88     for (i = AvFILLp (sv) + 1; i--; )
89     if (AvARRAY (sv)[i] == targ)
90     res_pair (form ("in array element %d of", i));
91     break;
92    
93     case SVt_PVHV:
94     if (hv_iterinit ((HV *)sv))
95     {
96     HE *he;
97     while ((he = hv_iternext ((HV *)sv)))
98     if (HeVAL (he) == targ)
99     res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he)));
100     }
101     break;
102    
103     case SVt_PVCV:
104     {
105     int depth = CvDEPTH (sv);
106     if (depth)
107     {
108     AV *padlist = CvPADLIST (sv);
109     while (depth)
110     {
111     AV *pad = (AV *)AvARRAY (padlist)[depth];
112     av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads from being found */
113     for (i = AvFILLp (pad); i--; )
114     if (AvARRAY (pad)[i] == targ)
115     res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i])));
116    
117     --depth;
118     }
119     }
120     }
121     break;
122 root 1.2
123     case SVt_PVGV:
124     if (GvGP (sv))
125     {
126     if (GvSV (sv) == targ)
127     res_gv ('$');
128     if (GvAV (sv) == (AV *)targ)
129     res_gv ('@');
130     if (GvHV (sv) == (HV *)targ)
131     res_gv ('%');
132     if (GvCV (sv) == (CV *)targ)
133     res_gv ('&');
134     }
135     break;
136 root 1.1 }
137    
138     if (rmagical)
139     SvRMAGICAL_off (sv);
140     }
141     }
142    
143     EXTEND (SP, 2);
144     PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));
145     PUSHs (sv_2mortal (newRV_noinc ((SV *)excl)));
146     }
147