Changeset 2028

Show
Ignore:
Timestamp:
02/24/08 00:38:44 (3 months ago)
Author:
karpet
Message:

refactor the ref counting. TODO more tests

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/bindings/perl/3.xs

    r2019 r2028  
    1616PROTOTYPES: enable 
    1717 
    18 # TODO more from libswish3.h                
    19 # constants            
    20 BOOT: 
    21         { 
    22         HV *stash; 
    23    
    24         stash = gv_stashpv("SWISH::3",       TRUE); 
    25         newCONSTSUB(stash, "SWISH_PROP",           newSVpv(SWISH_PROP, 0)); 
    26         newCONSTSUB(stash, "SWISH_PROP_MAX",       newSVpv(SWISH_PROP_MAX, 0)); 
    27         newCONSTSUB(stash, "SWISH_PROP_SORT",      newSVpv(SWISH_PROP_SORT, 0)); 
    28         newCONSTSUB(stash, "SWISH_META",           newSVpv(SWISH_META, 0)); 
    29         newCONSTSUB(stash, "SWISH_MIME",           newSVpv(SWISH_MIME, 0)); 
    30         newCONSTSUB(stash, "SWISH_PARSERS",        newSVpv(SWISH_PARSERS, 0)); 
    31         newCONSTSUB(stash, "SWISH_INDEX",          newSVpv(SWISH_INDEX, 0)); 
    32         newCONSTSUB(stash, "SWISH_ALIAS",          newSVpv(SWISH_ALIAS, 0)); 
    33         newCONSTSUB(stash, "SWISH_WORDS",          newSVpv(SWISH_WORDS, 0)); 
    34         newCONSTSUB(stash, "SWISH_PROP_RECCNT",    newSVpv(SWISH_PROP_RECCNT, 0)); 
    35         newCONSTSUB(stash, "SWISH_PROP_RANK",      newSVpv(SWISH_PROP_RANK, 0)); 
    36         newCONSTSUB(stash, "SWISH_PROP_DOCID",     newSVpv(SWISH_PROP_DOCID, 0)); 
    37         newCONSTSUB(stash, "SWISH_PROP_DOCPATH",   newSVpv(SWISH_PROP_DOCPATH, 0)); 
    38         newCONSTSUB(stash, "SWISH_PROP_DBFILE",    newSVpv(SWISH_PROP_DBFILE, 0)); 
    39         newCONSTSUB(stash, "SWISH_PROP_TITLE",     newSVpv(SWISH_PROP_TITLE, 0)); 
    40         newCONSTSUB(stash, "SWISH_PROP_SIZE",      newSVpv(SWISH_PROP_SIZE, 0)); 
    41         newCONSTSUB(stash, "SWISH_PROP_MTIME",     newSVpv(SWISH_PROP_MTIME, 0)); 
    42         newCONSTSUB(stash, "SWISH_PROP_DESCRIPTION",newSVpv(SWISH_PROP_DESCRIPTION, 0)); 
    43         newCONSTSUB(stash, "SWISH_PROP_CONNECTOR", newSVpv(SWISH_PROP_CONNECTOR, 0)); 
    44         newCONSTSUB(stash, "SWISH_PROP_STRING",    newSViv(SWISH_PROP_STRING)); 
    45         newCONSTSUB(stash, "SWISH_PROP_DATE",      newSViv(SWISH_PROP_DATE)); 
    46         newCONSTSUB(stash, "SWISH_PROP_INT",       newSViv(SWISH_PROP_INT)); 
    47         } 
    48  
     18INCLUDE: XS/Constants.xs 
    4919 
    5020swish_3* 
     
    6030        stash   = newHV(); 
    6131        s3      = swish_init_swish3( &sp_handler, newRV_inc((SV*)stash) ); 
    62         s3->ref_cnt = 1
     32        s3->ref_cnt++
    6333         
    6434        sp_hv_store(stash, DATA_CLASS_KEY,      newSVpv(DATA_CLASS, 0)); 
     
    7040        //sp_describe_object(newRV_noinc((SV*)s3->stash)); 
    7141 
    72         s3->analyzer->ref_cnt = 1; 
    7342        s3->analyzer->tokenizer = &sp_tokenize; 
    7443        analyzer_stash  = newHV(); 
    7544        s3->analyzer->stash = newRV_inc((SV*)analyzer_stash); 
    76          
    77         s3->config->ref_cnt = 1; 
    78         s3->parser->ref_cnt = 1; 
    79          
     45                 
    8046        RETVAL = s3; 
    8147                 
     
    202168 
    203169    // set_config 
    204     case 1:  swish_free_config(self->config); 
    205              self->config = (swish_Config*)sp_ptr_from_object(ST(1)); 
     170    case 1:  self->config->ref_cnt--; 
     171             //warn("set_config ref_cnt of old config = %d", self->config->ref_cnt); 
     172             if (self->config->ref_cnt < 1) { 
     173                if (SWISH_DEBUG) { 
     174                    warn("freeing config on set_config"); 
     175                } 
     176                swish_free_config(self->config); 
     177             } 
     178             self->config = (swish_Config*)sp_extract_ptr(ST(1)); 
     179             self->config->ref_cnt++; 
     180             //warn("set_config ref_cnt of new config = %d", self->config->ref_cnt); 
    206181             break; 
    207182              
    208183    // get_config    
    209184    case 2:  self->config->ref_cnt++; 
    210              RETVAL = sp_ptr_to_object(sp_hvref_fetch_as_char(stash, CONFIG_CLASS_KEY), (IV)self->config); 
     185             RETVAL = sp_bless_ptr(sp_hvref_fetch_as_char(stash, CONFIG_CLASS_KEY), (IV)self->config); 
    211186             break; 
    212187 
    213188    // set_analyzer 
    214     case 3:  swish_free_analyzer(self->analyzer); 
    215              self->analyzer = (swish_Analyzer*)sp_ptr_from_object(ST(1)); 
     189    case 3:  self->analyzer->ref_cnt--; 
     190             //warn("set_analyzer ref_cnt of old analyzer: %d", self->analyzer->ref_cnt); 
     191             if (self->analyzer->ref_cnt < 1) { 
     192                if (SWISH_DEBUG) { 
     193                    warn("freeing analyzer on set_analyzer"); 
     194                } 
     195                swish_free_analyzer(self->analyzer); 
     196             } 
     197             self->analyzer = (swish_Analyzer*)sp_extract_ptr(ST(1)); 
     198             self->analyzer->ref_cnt++; 
     199             //warn("set_analyzer ref_cnt of new analyzer: %d", self->analyzer->ref_cnt); 
    216200             break; 
    217201 
    218202    // get_analyzer 
    219203    case 4:  self->analyzer->ref_cnt++; 
    220              RETVAL = sp_ptr_to_object(sp_hvref_fetch_as_char(stash, ANALYZER_CLASS_KEY), (IV)self->analyzer); 
     204             RETVAL = sp_bless_ptr(sp_hvref_fetch_as_char(stash, ANALYZER_CLASS_KEY), (IV)self->analyzer); 
    221205             break; 
    222206 
    223207    // set_parser 
    224     case 5:  swish_free_parser(self->parser); 
    225              self->parser = (swish_Parser*)sp_ptr_from_object(ST(1)); 
     208    case 5:  self->parser->ref_cnt--; 
     209             if (self->parser->ref_cnt < 1) { 
     210                if (SWISH_DEBUG) { 
     211                    warn("freeing parser on set_parser"); 
     212                } 
     213                swish_free_parser(self->parser); 
     214             } 
     215             self->parser = (swish_Parser*)sp_extract_ptr(ST(1)); 
     216             self->parser->ref_cnt++; 
    226217             break; 
    227218            
    228219    // get_parser   
    229220    case 6:  self->parser->ref_cnt++; 
    230              RETVAL = sp_ptr_to_object(sp_hvref_fetch_as_char(stash, PARSER_CLASS_KEY), (IV)self->parser); 
     221             RETVAL = sp_bless_ptr(sp_hvref_fetch_as_char(stash, PARSER_CLASS_KEY), (IV)self->parser); 
    231222             break; 
    232223 
     
    300291         
    301292    CODE: 
    302         s3 = (swish_3*)sp_ptr_from_object(self); 
     293        s3 = (swish_3*)sp_extract_ptr(self); 
     294        s3->ref_cnt--; 
    303295         
    304296        if (SWISH_DEBUG) { 
     
    307299        } 
    308300         
    309         // TODO free pointers when we figure out ref cnt 
    310         swish_free_swish3( s3 ); 
     301        if (s3->ref_cnt < 1) { 
     302            swish_free_swish3( s3 ); 
     303        } 
    311304         
    312305 
     
    314307refcount(obj) 
    315308    SV* obj; 
    316      
     309         
    317310    CODE: 
    318311        RETVAL = SvREFCNT((SV*)SvRV(obj)); 
     
    322315 
    323316 
     317    
    324318 
    325319# include the other .xs files 
  • libswish3/trunk/bindings/perl/XS/Analyzer.xs

    r2019 r2028  
    1313 
    1414    CODE: 
    15         //RETVAL = swish_init_analyzer((swish_Config*)sp_ptr_from_object( (SV*)config )); 
     15        //RETVAL = swish_init_analyzer((swish_Config*)sp_extract_ptr( (SV*)config )); 
    1616        RETVAL = swish_init_analyzer(config); 
    17         RETVAL->ref_cnt = 1
     17        RETVAL->ref_cnt++
    1818        stash = newHV(); 
    1919        RETVAL->stash = newRV_inc((SV*)stash); 
     
    6262     
    6363    CODE: 
     64        self->ref_cnt--; 
     65         
    6466        if (SWISH_DEBUG) { 
    6567            warn("DESTROYing swish_Analyzer object %s  [%d] [ref_cnt = %d]",  
    6668                SvPV(ST(0), PL_na), self, self->ref_cnt); 
    6769        } 
     70         
     71        if (self->ref_cnt < 1) { 
     72            swish_free_analyzer(self); 
     73        } 
     74         
    6875 
    6976 
     
    120127                         
    121128        RETVAL = sp_tokenize( 
    122                         (swish_Analyzer*)sp_ptr_from_object(self), 
     129                        (swish_Analyzer*)sp_extract_ptr(self), 
    123130                        buf, 
    124131                        word_pos, 
     
    179186        swish_init_words(); /* in case it wasn't initialized elsewhere... */ 
    180187        RETVAL = swish_tokenize( 
    181                         (swish_Analyzer*)sp_ptr_from_object(self), 
     188                        (swish_Analyzer*)sp_extract_ptr(self), 
    182189                        buf, 
    183190                        word_pos, 
     
    194201        RETVAL 
    195202 
    196 int 
    197 refcount(obj) 
    198     SV* obj; 
    199      
    200     CODE: 
    201         RETVAL = SvREFCNT((SV*)SvRV(obj)); 
    202      
    203     OUTPUT: 
    204         RETVAL 
    205          
    206          
  • libswish3/trunk/bindings/perl/XS/Config.xs

    r2019 r2028  
    22 
    33PROTOTYPES: enable 
     4 
     5swish_Config * 
     6new(CLASS) 
     7    char* CLASS; 
     8     
     9    CODE: 
     10        RETVAL = sp_new_config(); 
     11         
     12    OUTPUT: 
     13        RETVAL 
     14 
    415 
    516AV* 
     
    6980 
    7081 
    71 swish_Config * 
    72 new(CLASS) 
    73     char* CLASS; 
    74      
    75     CODE: 
    76         RETVAL = sp_new_config(); 
    77          
    78     OUTPUT: 
    79         RETVAL 
    80  
    81  
    8282 
    8383void 
     
    112112     
    113113    CODE: 
     114        self->ref_cnt--; 
     115         
     116        if (self->stash != NULL) { 
     117            SvREFCNT_dec(self->stash); 
     118        } 
     119         
    114120        if (SWISH_DEBUG) { 
    115121            warn("DESTROYing swish_Config object %s  [%d] [ref_cnt = %d]",  
     
    117123        } 
    118124 
    119         if (self->stash != NULL) { 
     125        if (self->ref_cnt < 1) { 
     126             
     127          if (self->stash != NULL) { 
    120128            if (SWISH_DEBUG) { 
    121129                warn("decreasing refcnt on config->stash [currently %d]", 
     
    123131                ); 
    124132            } 
    125             SvREFCNT_dec(self->stash); 
     133            while ( SvREFCNT((SV*)SvRV((SV*)self->stash)) > 0 ) { 
     134                SvREFCNT_dec(self->stash); 
     135            } 
    126136            self->stash = NULL; 
     137          } 
     138           
     139          swish_free_config( self ); 
     140           
    127141        } 
     142         
    128143 
    129144         
    130 int 
    131 refcount(obj) 
    132     SV* obj; 
    133      
    134     CODE: 
    135         RETVAL = SvREFCNT((SV*)SvRV(obj)); 
    136      
    137     OUTPUT: 
    138         RETVAL 
    139          
    140          
  • libswish3/trunk/bindings/perl/XS/Data.xs

    r2019 r2028  
    44 
    55 
    6 swish_3
     6SV
    77s3(self) 
    8     swish_ParseData* self 
     8    swish_ParserData *self; 
    99     
    1010    PREINIT: 
    11         char* CLASS; 
     11        char    *class; 
     12        swish_3 *s3; 
    1213 
    1314    CODE: 
    14         CLASS  = sp_get_objects_class((SV*)self->s3); 
    15         RETVAL = self->s3; 
     15        self->s3->ref_cnt++; 
     16        class  = sp_hvref_fetch_as_char((SV*)self->s3->stash, SWISH3_CLASS_KEY); 
     17        warn("s3 class = %s\n", class); 
     18        RETVAL = sp_bless_ptr( class, (IV)self->s3 ); 
    1619         
    1720    OUTPUT: 
     
    1922         
    2023    CLEANUP: 
    21         SvREFCNT_inc(RETVAL); 
     24        SvREFCNT_inc( RETVAL ); 
    2225 
    2326 
    2427swish_Config* 
    2528config(self) 
    26     swish_ParseData* self 
     29    swish_ParserData* self 
    2730     
    2831        PREINIT: 
     
    3134    CODE: 
    3235        CLASS  = sp_hvref_fetch_as_char(self->s3->stash, CONFIG_CLASS_KEY); 
     36        self->s3->config->ref_cnt++; 
    3337        RETVAL = self->s3->config; 
    34         RETVAL->ref_cnt++; 
    3538         
    3639    OUTPUT: 
     
    4043SV* 
    4144property(self, p) 
    42     swish_ParseData* self; 
     45    swish_ParserData* self; 
    4346    xmlChar* p; 
    4447     
     
    5558SV* 
    5659metaname(self, m) 
    57     swish_ParseData* self; 
     60    swish_ParserData* self; 
    5861    xmlChar* m; 
    5962     
     
    7174HV* 
    7275properties(self) 
    73     swish_ParseData* self 
     76    swish_ParserData* self 
    7477     
    7578    CODE: 
     
    8285HV* 
    8386metanames(self) 
    84     swish_ParseData* self 
     87    swish_ParserData* self 
    8588     
    8689    CODE: 
     
    9497swish_DocInfo * 
    9598doc(self) 
    96     swish_ParseData* self 
     99    swish_ParserData* self 
    97100     
    98101    PREINIT: 
     
    109112swish_WordList * 
    110113wordlist(self) 
    111     swish_ParseData* self 
     114    swish_ParserData* self 
    112115     
    113116    PREINIT: 
     
    125128        RETVAL 
    126129         
    127      
    128 # must decrement refcount for stashed SWISH::3::Parser object 
    129 # since we increment it in parse_buf() and parse_file() 
    130 # TODO: this way of doing it doesn't work. 
    131 # but isn't it a potential mem leak to just _inc in parse_*() without 
    132 # _dec somewhere else? just means that the SWISH::3::Parser object 
    133 # may never get garbage collected. 
    134 #void 
    135 #DESTROY(self) 
    136 #    swish_ParseData* self; 
    137 #     
    138 #    CODE: 
    139 #        SvREFCNT_dec( self->user_data ); 
    140          
    141 int 
    142 refcount(obj) 
    143     SV* obj; 
    144      
    145     CODE: 
    146         RETVAL = SvREFCNT((SV*)SvRV(obj)); 
    147      
    148     OUTPUT: 
    149         RETVAL 
    150          
    151  
  • libswish3/trunk/bindings/perl/XS/Doc.xs

    r2019 r2028  
    7979    OUTPUT: 
    8080        RETVAL 
    81  
    82 int 
    83 refcount(obj) 
    84     SV* obj; 
    85      
    86     CODE: 
    87         RETVAL = SvREFCNT((SV*)SvRV(obj)); 
    88      
    89     OUTPUT: 
    90         RETVAL 
    91          
    92  
  • libswish3/trunk/bindings/perl/XS/Word.xs

    r2019 r2028  
    5959        RETVAL 
    6060 
    61  
    62 int 
    63 refcount(obj) 
    64     SV* obj; 
    65      
    66     CODE: 
    67         RETVAL = SvREFCNT((SV*)SvRV(obj)); 
    68      
    69     OUTPUT: 
    70         RETVAL 
  • libswish3/trunk/bindings/perl/XS/WordList.xs

    r2019 r2028  
    5151    CODE: 
    5252        self->ref_cnt--; 
     53         
     54        if (SWISH_DEBUG) { 
     55            warn("DESTROYing swish_WordList object %s  [%d] [ref_cnt = %d]",  
     56                SvPV(ST(0), PL_na), self, self->ref_cnt); 
     57        } 
     58         
    5359        if (self->ref_cnt < 1) { 
    5460            swish_free_wordlist(self); 
    5561        } 
    56          
    57 int 
    58 refcount(obj) 
    59     SV* obj; 
    60      
    61     CODE: 
    62         RETVAL = SvREFCNT((SV*)SvRV(obj)); 
    63      
    64     OUTPUT: 
    65         RETVAL 
    66          
    67  
  • libswish3/trunk/bindings/perl/lib/SWISH/3.pm

    r2019 r2028  
    3838*regex    = \&get_regex; 
    3939*parser   = \&get_parser; 
     40 
     41# alias debugging methods for all classes 
     42*SWISH::3::Config::refcount   = \&refcount; 
     43*SWISH::3::Analyzer::refcount = \&refcount; 
     44*SWISH::3::WordList::refcount = \&refcount; 
     45*SWISH::3::Word::refcount     = \&refcount; 
     46*SWISH::3::Doc::refcount      = \&refcount; 
     47*SWISH::3::Data::refcount     = \&refcount; 
    4048 
    4149sub new { 
  • libswish3/trunk/bindings/perl/macros.h

    r2019 r2028  
    2121#define PARSER_KEY          "sp_parser" 
    2222#define TOKEN_HANDLER_KEY   "sp_token_handler" 
     23#define SWISH3_CLASS_KEY    "sp_self_class" 
    2324 
    2425 
  • libswish3/trunk/bindings/perl/t/07-refcnt.t

    r2015 r2028  
    99is( $analyzer->refcount, 1, "analyzer refcount == 1" ); 
    1010 
     11 
    1112#undef $s3; 
    1213is( $analyzer->refcount, 1, "analyzer refcount == 1" ); 
     
    1718#$s3->dump($analyzer); 
    1819is( $s3->config->refcount, 1, "config refcount == 1" ); 
     20 
     21# avoid spurious mem error from libswish3 
     22# just because of order of Perl ref cleanup 
     23undef $analyzer; 
     24 
  • libswish3/trunk/bindings/perl/t/08-handler.t

    r2019 r2028  
    33use_ok('SWISH::3'); 
    44 
    5 ok( my $s3 = SWISH::3->new( handler => sub { diag("got data: $_[0]") } ), 
     5ok( my $s3 = SWISH::3->new( handler => sub { diag("got data: $_[0] ") } ), 
     6#ok( my $s3 = SWISH::3->new(  ), 
    67    "new parser" ); 
    78ok( $s3->parse("t/test.html"), "parse HTML" ); 
  • libswish3/trunk/bindings/perl/t/10tokenize.t

    r2019 r2028  
    3535    } 
    3636} 
     37 
     38undef $analyzer; 
     39undef $wlist; 
     40undef $s3; 
  • libswish3/trunk/bindings/perl/t/11get_set_parser.t

    r2019 r2028  
    3535); 
    3636ok( my $ana2 = $s3->get_analyzer, "get ana2" ); 
     37 
     38# avoid spurious libswish3 mem error due to "random" order of Perl 
     39# SV cleanup 
     40undef $analyzer; 
     41undef $ana2; 
     42undef $ana1; 
     43undef $conf2; 
     44undef $conf1; 
     45undef $config; 
     46undef $s3; 
     47 
  • libswish3/trunk/bindings/perl/typemap

    r2014 r2028  
    11TYPEMAP 
    2 const char *               T_PV 
    3 xmlChar *                  T_PV 
    4 swish_Config *             O_OBJECT 
    5 xmlHashTablePtr            O_OBJECT 
    6 swish_ParseData *          O_OBJECT 
    7 xmlBufferPtr              T_IV 
    8 swish_WordList *           O_OBJECT 
    9 swish_DocInfo *            O_OBJECT 
    10 swish_Word *               O_OBJECT 
    11 swish_Analyzer *           O_OBJECT 
    12 swish_Parser *             O_OBJECT 
    13 swish_NamedBuffer *        O_OBJECT 
    14 swish_3 *                  O_OBJECT 
     2const char*               T_PV 
     3xmlChar*                  T_PV 
     4swish_Config*             O_OBJECT 
     5xmlHashTablePtr           O_OBJECT 
     6swish_ParserData*         O_OBJECT 
     7xmlBufferPtr              T_IV 
     8swish_WordList*           O_OBJECT 
     9swish_DocInfo*            O_OBJECT 
     10swish_Word*               O_OBJECT 
     11swish_Analyzer*           O_OBJECT 
     12swish_Parser*             O_OBJECT 
     13swish_NamedBuffer*        O_OBJECT 
     14swish_3*                  O_OBJECT 
    1515 
    1616INPUT 
     
    2828# char* having the name of the package for the blessing. 
    2929O_OBJECT 
    30         sv_setref_pv( $arg, (char *)CLASS, (void*)$var ); 
     30        sv_setref_pv( $arg, (char*)CLASS, (void*)$var ); 
  • libswish3/trunk/bindings/perl/xs_helpers.c

    r2019 r2028  
    66/* C code to make writing XS easier */ 
    77 
    8 /* a SWISH::3 object is a blessed C struct */ 
     8/* sp_Obj is a struct that makes refcounting easier */ 
     9 
     10typedef struct sp_Obj sp_Obj; 
     11struct sp_Obj  
     12
     13    void    *c_ptr; 
     14    SV      *perl_obj; 
     15}; 
    916 
    1017static SV*      sp_hv_store( HV* h, const char* key, SV* val); 
     
    1926static SV*      sp_hvref_delete( SV* h, const char* key ); 
    2027static void     sp_hvref_replace( SV * hashref, char* key, SV* value ); 
    21 static SV*      sp_ptr_to_object( char* CLASS, IV c_ptr ); 
     28static SV*      sp_bless_ptr( char* CLASS, IV c_ptr ); 
    2229static char*    sp_get_objects_class( SV* object ); 
    2330static HV*      sp_extract_hash_from_object( SV* object ); 
    24 static void     sp_dump_hash(SV* hash_ref); 
     31static void     sp_dump_hash( SV* hash_ref ); 
    2532static void     sp_describe_object( SV* object ); 
    26 static IV       sp_ptr_from_object( SV* object ); 
     33static IV       sp_extract_ptr( SV* object ); 
    2734static SV*      sp_accessor( SV* object, char* name ); 
    28 static void     sp_store_xml2_pair_in_perl_hash(xmlChar* val, HV* perl_hash, xmlChar* key); 
     35static void     sp_store_xml2_pair_in_perl_hash( xmlChar* val, HV* perl_hash, xmlChar* key ); 
    2936static HV*      sp_xml2_hash_to_perl_hash( xmlHashTablePtr xml2_hash ); 
    3037static void     sp_add_key_to_array(xmlChar* val, AV* mykeys, xmlChar* key); 
     
    3239static void     sp_nb_hash_to_phash(xmlBufferPtr buf, HV *phash, xmlChar *key); 
    3340static HV*      sp_nb_to_hash( swish_NamedBuffer* nb ); 
    34 static void     sp_test_handler( swish_ParseData* parse_data ); 
    35 static void     sp_handler( swish_ParseData* parse_data ); 
     41static void     sp_test_handler( swish_ParserData* parse_data ); 
     42static void     sp_handler( swish_ParserData* parse_data ); 
    3643static swish_WordList* sp_tokenize( swish_Analyzer* analyzer, xmlChar* str, ... ); 
    3744static void     sp_token_handler( swish_Token *token ); 
     
    4249static swish_Analyzer*  sp_new_analyzer(); 
    4350 
     51static sp_Obj*  sp_new_Obj( char* class, void* c_ptr); 
     52static SV*      sp_Obj_to_perl(sp_Obj* self); 
     53static void     sp_Obj_inc_refcount(sp_Obj *self); 
     54static void     sp_Obj_dec_refcount(sp_Obj *self); 
     55static int      sp_Obj_get_refcount(sp_Obj *self); 
     56 
     57static int 
     58sp_Obj_get_refcount(sp_Obj *self) 
     59{ 
     60    return SvREFCNT(self->perl_obj); 
     61} 
     62 
     63static void 
     64sp_Obj_inc_refcount(sp_Obj *self) 
     65{ 
     66    SvREFCNT_inc(self->perl_obj); 
     67} 
     68 
     69static void 
     70sp_Obj_dec_refcount(sp_Obj *self) 
     71{ 
     72    /* If the SV's refcount falls to 0, DESTROY will be invoked from 
     73     * Perl-space. 
     74     */ 
     75    SvREFCNT_dec(self->perl_obj); 
     76} 
     77 
     78 
     79static sp_Obj*   
     80sp_new_Obj( char *class, void *c_ptr ) 
     81{ 
     82    sp_Obj* self; 
     83    SV*     perl_obj; 
     84     
     85    self        = swish_xmalloc(sizeof(sp_Obj*)); 
     86    self->c_ptr = c_ptr; 
     87     
     88    /* init a new perl object wrapping the sp_Obj ptr */ 
     89    perl_obj = newSV(0); 
     90    sv_setref_pv(perl_obj, class, self); 
     91     
     92    /* make a reference to the object and save the reference */ 
     93    self->perl_obj = SvRV(perl_obj); 
     94     
     95    /* increment ref count of the inner object (perl_obj) */ 
     96    sp_Obj_inc_refcount(self); 
     97     
     98    /* decrement the outer object (RV) so that it is DESTROYed 
     99       by Perl when we return self 
     100    */ 
     101    SvREFCNT_dec(perl_obj); 
     102     
     103    /* self contains the original c_ptr and a perl SV 
     104       which is a wrapper around self. This means we use Perl's 
     105       reference counting feature to control when our sp_Obj ptr 
     106       is DESTROYed. 
     107    */ 
     108     
     109    return self; 
     110} 
     111 
     112static SV* 
     113sp_Obj_to_perl( sp_Obj* self ) 
     114{ 
     115    return newRV_inc(self->perl_obj); 
     116} 
     117 
    44118static void 
    45119sp_SV_is_qr( SV *qr ) 
     
    170244/* make a Perl blessed object from a C pointer */ 
    171245static SV*  
    172 sp_ptr_to_object( char* CLASS, IV c_ptr ) 
     246sp_bless_ptr( char* CLASS, IV c_ptr ) 
    173247{ 
    174248    dTHX; 
     
    267341/* return the C pointer from a Perl blessed O_OBJECT */ 
    268342static IV  
    269 sp_ptr_from_object( SV* object ) 
     343sp_extract_ptr( SV* object ) 
    270344{ 
    271345    dTHX; 
     
    282356    //warn("looking for %s in %s\n", name, class); 
    283357    HV* hash = sp_extract_hash_from_object( object ); 
    284     SV** sv  = hv_fetch(hash, name, strlen(name), 0); 
     358    SV* sv   = sp_hv_fetch( hash, (const char*)name ); 
    285359     
    286360    if (!sv) 
    287361        croak("no %s in %s object!", name, class); 
    288362         
    289     return *sv; 
     363    return sv; 
    290364} 
    291365 
     
    386460 
    387461void  
    388 sp_test_handler( swish_ParseData* parse_data ) 
     462sp_test_handler( swish_ParserData* parse_data ) 
    389463{ 
    390464    dTHX; 
     
    403477*/ 
    404478static void  
    405 sp_handler( swish_ParseData* parse_data ) 
     479sp_handler( swish_ParserData* parse_data ) 
    406480{ 
    407481    dTHX; 
    408482    dSP; 
    409483 
    410     swish_3*    s3; 
    411     SV*         handler;  
    412     SV*         obj; 
    413     char*       data_class; 
     484    swish_3    *s3; 
     485    SV         *handler;  
     486    SV         *obj; 
     487    char       *data_class; 
    414488     
    415489    //warn("sp_handler called"); 
     
    420494    //sp_describe_object(s3->stash); 
    421495     
    422     handler     = sp_hvref_fetch(s3->stash, HANDLER_KEY); 
     496    handler     = sp_hvref_fetch((SV*)s3->stash, HANDLER_KEY); 
    423497     
    424498    //warn("got handler and s3"); 
    425499     
    426     data_class  = sp_hvref_fetch_as_char(s3->stash, DATA_CLASS_KEY); 
     500    data_class  = sp_hvref_fetch_as_char((SV*)s3->stash, DATA_CLASS_KEY); 
    427501     
    428502    //warn("data_class = %s", data_class); 
    429503     
    430     obj         = sp_ptr_to_object( data_class, (IV)parse_data ); 
     504    obj         = sp_bless_ptr( data_class, (IV)parse_data ); 
    431505     
    432506    //sp_describe_object(obj); 
     
    446520     
    447521    SV* obj; 
    448     obj = sp_ptr_to_object( TOKEN_CLASS, (IV)token ); 
     522    obj = sp_bless_ptr( TOKEN_CLASS, (IV)token ); 
    449523     
    450524    PUSHMARK(SP); 
     
    646720    config = swish_init_config(); 
    647721    config->ref_cnt++; 
    648     config->stash = newRV_inc((SV*)stash); 
    649     sp_hvref_store( config->stash, SELF_KEY, sp_ptr_to_object(CONFIG_CLASS, (IV)config) ); 
     722    //config->stash = newRV_inc((SV*)stash); 
     723    //sp_hvref_store( config->stash, SELF_KEY, sp_bless_ptr(CONFIG_CLASS, (IV)config) ); 
    650724     
    651725    return config; 
  • libswish3/trunk/src/libswish3/analyzer.c

    r2027 r2028  
    3737    a->minwordlen = SWISH_MIN_WORD_LEN; 
    3838    a->lc         = 1; 
    39     a->ref_cnt    = 1
     39    a->ref_cnt    = 0
    4040         
    4141    if (xmlStrEqual((xmlChar*)SWISH_DEFAULT_VALUE, 
     
    8080swish_free_analyzer( swish_Analyzer * a ) 
    8181{ 
    82     a->ref_cnt--; 
    83     if (a->ref_cnt > 0) { 
    84         SWISH_WARN("analyzer ref_cnt > 0: %d\n", a->ref_cnt); 
     82    if (a->ref_cnt != 0) { 
     83        SWISH_WARN("analyzer ref_cnt != 0: %d\n", a->ref_cnt); 
     84    } 
     85    if (SWISH_DEBUG >= SWISH_DEBUG_MEMORY) { 
     86        SWISH_DEBUG_MSG("free analyzer"); 
    8587    } 
    8688    swish_xfree(a);    
  • libswish3/trunk/src/libswish3/config.c

    r2027 r2028  
    9999    swish_xfree(config->flags); 
    100100 
    101     if (config->stash != NULL) 
    102     { 
     101    if (config->ref_cnt != 0) { 
     102        SWISH_WARN("config ref_cnt != 0: %d", config->ref_cnt); 
     103    } 
     104 
     105    if (config->stash != NULL) { 
    103106        SWISH_WARN("possible memory leak: config->stash was not freed"); 
    104107    } 
     
    217220 
    218221    config->conf = c; 
    219     config->ref_cnt = 1
     222    config->ref_cnt = 0
    220223    config->stash = NULL; 
    221224    config->flags = flags; 
  • libswish3/trunk/src/libswish3/namedbuffer.c

    r2027 r2028  
    6969    swish_NamedBuffer * nb = swish_xmalloc(sizeof(swish_NamedBuffer)); 
    7070    nb->stash   = NULL; 
    71     nb->ref_cnt = 1
     71    nb->ref_cnt = 0
    7272    nb->hash    = xmlHashCreate(8);    /* will grow as needed */ 
    7373     
     
    8585    xmlHashFree(nb->hash, (xmlHashDeallocator)free_name_from_hash); 
    8686    
    87     nb->ref_cnt--;  
    88     if (nb->ref_cnt > 0) 
    89         SWISH_WARN("freeing NamedBuffer with ref_cnt > 0 (%d)", nb->ref_cnt); 
     87    if (nb->ref_cnt != 0) { 
     88        SWISH_WARN("freeing NamedBuffer with ref_cnt != 0 (%d)", nb->ref_cnt); 
     89    } 
    9090         
    9191    if (nb->stash != NULL) 
  • libswish3/trunk/src/libswish3/parser.c

    r2027 r2028  
    155155    swish_Parser * p = (swish_Parser*) swish_xmalloc(sizeof(swish_Parser)); 
    156156    p->handler  = handler; 
    157     p->ref_cnt  = 1
     157    p->ref_cnt  = 0
    158158     
    159159    /* libxml2 stuff */ 
     
    170170swish_free_parser( swish_Parser * p ) 
    171171{         
    172     p->ref_cnt--; 
    173     if (p->ref_cnt > 0) { 
    174         SWISH_WARN("parser ref_cnt > 0: %d\n", p->ref_cnt); 
     172    if (p->ref_cnt != 0) { 
     173        SWISH_WARN("parser ref_cnt != 0: %d\n", p->ref_cnt); 
    175174    } 
    176175    xmlCleanupParser(); 
  • libswish3/trunk/src/libswish3/swish.c

    r2027 r2028  
    3131    swish_init(); 
    3232    s3              = swish_xmalloc(sizeof(swish_3)); 
    33     s3->ref_cnt     = 1
     33    s3->ref_cnt     = 0
    3434    s3->config      = swish_init_config(); 
     35    s3->config->ref_cnt++; 
    3536    s3->analyzer    = swish_init_analyzer(s3->config); 
     37    s3->analyzer->ref_cnt++; 
    3638    s3->parser      = swish_init_parser(handler); 
     39    s3->parser->ref_cnt++; 
    3740    s3->stash       = stash; 
    3841    return s3; 
     
    4245swish_free_swish3(swish_3* s3) 
    4346{ 
    44     //SWISH_DEBUG_MSG("freeing parser"); 
    45     swish_free_parser(s3->parser); 
    46     //SWISH_DEBUG_MSG("freeing analyzer"); 
    47     swish_free_analyzer(s3->analyzer); 
    48     //SWISH_DEBUG_MSG("freeing config"); 
    49     swish_free_config(s3->config); 
     47    s3->parser->ref_cnt--; 
     48    if (s3->parser->ref_cnt < 1) { 
     49        //SWISH_DEBUG_MSG("freeing parser"); 
     50        swish_free_parser(s3->parser); 
     51    } 
     52 
     53    s3->analyzer->ref_cnt--; 
     54    if (s3->analyzer->ref_cnt < 1) { 
     55        //SWISH_DEBUG_MSG("freeing analyzer"); 
     56        swish_free_analyzer(s3->analyzer); 
     57    } 
     58 
     59    s3->config->ref_cnt--; 
     60    if (s3->config->ref_cnt < 1) { 
     61        //SWISH_DEBUG_MSG("freeing config"); 
     62        swish_free_config(s3->config); 
     63    } 
     64 
    5065    //SWISH_DEBUG_MSG("freeing s3"); 
    51     s3->ref_cnt--; 
    52     if (s3->ref_cnt > 0) { 
    53         SWISH_WARN("s3 ref_cnt > 0: %d\n", s3->ref_cnt); 
     66    if (s3->ref_cnt != 0) { 
     67        SWISH_WARN("s3 ref_cnt != 0: %d\n", s3->ref_cnt); 
    5468    } 
    5569    swish_xfree(s3); 
     
    7791    if (!SWISH_DEBUG) { 
    7892        SWISH_DEBUG += (int)strtol(getenv("SWISH_DEBUG"), (char**)NULL, 10); 
    79         SWISH_DEBUG += (int)strtol(getenv("SWISH_DEBUG_MEMORY"), (char**)NULL, 10); 
     93        if ((int)strtol(getenv("SWISH_DEBUG_MEMORY"), (char**)NULL, 10)) { 
     94            SWISH_DEBUG += SWISH_DEBUG_MEMORY; 
     95        } 
    8096        SWISH_DEBUG += (int)strtol(getenv("SWISH_DEBUG_CONFIG"), (char**)NULL, 10); 
    8197        SWISH_DEBUG += (int)strtol(getenv("SWISH_DEBUG_DOCINFO"), (char**)NULL, 10); 
  • libswish3/trunk/src/libs