Changeset 2028
- Timestamp:
- 02/24/08 00:38:44 (3 months ago)
- Files:
-
- libswish3/trunk/bindings/perl/3.xs (modified) (8 diffs)
- libswish3/trunk/bindings/perl/XS/Analyzer.xs (modified) (5 diffs)
- libswish3/trunk/bindings/perl/XS/Config.xs (modified) (5 diffs)
- libswish3/trunk/bindings/perl/XS/Constants.xs (added)
- libswish3/trunk/bindings/perl/XS/Data.xs (modified) (10 diffs)
- libswish3/trunk/bindings/perl/XS/Doc.xs (modified) (1 diff)
- libswish3/trunk/bindings/perl/XS/Word.xs (modified) (1 diff)
- libswish3/trunk/bindings/perl/XS/WordList.xs (modified) (1 diff)
- libswish3/trunk/bindings/perl/lib/SWISH/3.pm (modified) (1 diff)
- libswish3/trunk/bindings/perl/macros.h (modified) (1 diff)
- libswish3/trunk/bindings/perl/t/07-refcnt.t (modified) (2 diffs)
- libswish3/trunk/bindings/perl/t/08-handler.t (modified) (1 diff)
- libswish3/trunk/bindings/perl/t/10tokenize.t (modified) (1 diff)
- libswish3/trunk/bindings/perl/t/11get_set_parser.t (modified) (1 diff)
- libswish3/trunk/bindings/perl/typemap (modified) (2 diffs)
- libswish3/trunk/bindings/perl/xs_helpers.c (modified) (12 diffs)
- libswish3/trunk/src/libswish3/analyzer.c (modified) (2 diffs)
- libswish3/trunk/src/libswish3/config.c (modified) (2 diffs)
- libswish3/trunk/src/libswish3/namedbuffer.c (modified) (2 diffs)
- libswish3/trunk/src/libswish3/parser.c (modified) (2 diffs)
- libswish3/trunk/src/libswish3/swish.c (modified) (3 diffs)
- libswish3/trunk/src/libswish3/words.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libswish3/trunk/bindings/perl/3.xs
r2019 r2028 16 16 PROTOTYPES: enable 17 17 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 18 INCLUDE: XS/Constants.xs 49 19 50 20 swish_3* … … 60 30 stash = newHV(); 61 31 s3 = swish_init_swish3( &sp_handler, newRV_inc((SV*)stash) ); 62 s3->ref_cnt = 1;32 s3->ref_cnt++; 63 33 64 34 sp_hv_store(stash, DATA_CLASS_KEY, newSVpv(DATA_CLASS, 0)); … … 70 40 //sp_describe_object(newRV_noinc((SV*)s3->stash)); 71 41 72 s3->analyzer->ref_cnt = 1;73 42 s3->analyzer->tokenizer = &sp_tokenize; 74 43 analyzer_stash = newHV(); 75 44 s3->analyzer->stash = newRV_inc((SV*)analyzer_stash); 76 77 s3->config->ref_cnt = 1; 78 s3->parser->ref_cnt = 1; 79 45 80 46 RETVAL = s3; 81 47 … … 202 168 203 169 // 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); 206 181 break; 207 182 208 183 // get_config 209 184 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); 211 186 break; 212 187 213 188 // 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); 216 200 break; 217 201 218 202 // get_analyzer 219 203 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); 221 205 break; 222 206 223 207 // 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++; 226 217 break; 227 218 228 219 // get_parser 229 220 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); 231 222 break; 232 223 … … 300 291 301 292 CODE: 302 s3 = (swish_3*)sp_ptr_from_object(self); 293 s3 = (swish_3*)sp_extract_ptr(self); 294 s3->ref_cnt--; 303 295 304 296 if (SWISH_DEBUG) { … … 307 299 } 308 300 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 } 311 304 312 305 … … 314 307 refcount(obj) 315 308 SV* obj; 316 309 317 310 CODE: 318 311 RETVAL = SvREFCNT((SV*)SvRV(obj)); … … 322 315 323 316 317 324 318 325 319 # include the other .xs files libswish3/trunk/bindings/perl/XS/Analyzer.xs
r2019 r2028 13 13 14 14 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 )); 16 16 RETVAL = swish_init_analyzer(config); 17 RETVAL->ref_cnt = 1;17 RETVAL->ref_cnt++; 18 18 stash = newHV(); 19 19 RETVAL->stash = newRV_inc((SV*)stash); … … 62 62 63 63 CODE: 64 self->ref_cnt--; 65 64 66 if (SWISH_DEBUG) { 65 67 warn("DESTROYing swish_Analyzer object %s [%d] [ref_cnt = %d]", 66 68 SvPV(ST(0), PL_na), self, self->ref_cnt); 67 69 } 70 71 if (self->ref_cnt < 1) { 72 swish_free_analyzer(self); 73 } 74 68 75 69 76 … … 120 127 121 128 RETVAL = sp_tokenize( 122 (swish_Analyzer*)sp_ ptr_from_object(self),129 (swish_Analyzer*)sp_extract_ptr(self), 123 130 buf, 124 131 word_pos, … … 179 186 swish_init_words(); /* in case it wasn't initialized elsewhere... */ 180 187 RETVAL = swish_tokenize( 181 (swish_Analyzer*)sp_ ptr_from_object(self),188 (swish_Analyzer*)sp_extract_ptr(self), 182 189 buf, 183 190 word_pos, … … 194 201 RETVAL 195 202 196 int197 refcount(obj)198 SV* obj;199 200 CODE:201 RETVAL = SvREFCNT((SV*)SvRV(obj));202 203 OUTPUT:204 RETVAL205 206 libswish3/trunk/bindings/perl/XS/Config.xs
r2019 r2028 2 2 3 3 PROTOTYPES: enable 4 5 swish_Config * 6 new(CLASS) 7 char* CLASS; 8 9 CODE: 10 RETVAL = sp_new_config(); 11 12 OUTPUT: 13 RETVAL 14 4 15 5 16 AV* … … 69 80 70 81 71 swish_Config *72 new(CLASS)73 char* CLASS;74 75 CODE:76 RETVAL = sp_new_config();77 78 OUTPUT:79 RETVAL80 81 82 82 83 83 void … … 112 112 113 113 CODE: 114 self->ref_cnt--; 115 116 if (self->stash != NULL) { 117 SvREFCNT_dec(self->stash); 118 } 119 114 120 if (SWISH_DEBUG) { 115 121 warn("DESTROYing swish_Config object %s [%d] [ref_cnt = %d]", … … 117 123 } 118 124 119 if (self->stash != NULL) { 125 if (self->ref_cnt < 1) { 126 127 if (self->stash != NULL) { 120 128 if (SWISH_DEBUG) { 121 129 warn("decreasing refcnt on config->stash [currently %d]", … … 123 131 ); 124 132 } 125 SvREFCNT_dec(self->stash); 133 while ( SvREFCNT((SV*)SvRV((SV*)self->stash)) > 0 ) { 134 SvREFCNT_dec(self->stash); 135 } 126 136 self->stash = NULL; 137 } 138 139 swish_free_config( self ); 140 127 141 } 142 128 143 129 144 130 int131 refcount(obj)132 SV* obj;133 134 CODE:135 RETVAL = SvREFCNT((SV*)SvRV(obj));136 137 OUTPUT:138 RETVAL139 140 libswish3/trunk/bindings/perl/XS/Data.xs
r2019 r2028 4 4 5 5 6 swish_3*6 SV* 7 7 s3(self) 8 swish_Parse Data* self8 swish_ParserData *self; 9 9 10 10 PREINIT: 11 char* CLASS; 11 char *class; 12 swish_3 *s3; 12 13 13 14 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 ); 16 19 17 20 OUTPUT: … … 19 22 20 23 CLEANUP: 21 SvREFCNT_inc( RETVAL);24 SvREFCNT_inc( RETVAL ); 22 25 23 26 24 27 swish_Config* 25 28 config(self) 26 swish_Parse Data* self29 swish_ParserData* self 27 30 28 31 PREINIT: … … 31 34 CODE: 32 35 CLASS = sp_hvref_fetch_as_char(self->s3->stash, CONFIG_CLASS_KEY); 36 self->s3->config->ref_cnt++; 33 37 RETVAL = self->s3->config; 34 RETVAL->ref_cnt++;35 38 36 39 OUTPUT: … … 40 43 SV* 41 44 property(self, p) 42 swish_Parse Data* self;45 swish_ParserData* self; 43 46 xmlChar* p; 44 47 … … 55 58 SV* 56 59 metaname(self, m) 57 swish_Parse Data* self;60 swish_ParserData* self; 58 61 xmlChar* m; 59 62 … … 71 74 HV* 72 75 properties(self) 73 swish_Parse Data* self76 swish_ParserData* self 74 77 75 78 CODE: … … 82 85 HV* 83 86 metanames(self) 84 swish_Parse Data* self87 swish_ParserData* self 85 88 86 89 CODE: … … 94 97 swish_DocInfo * 95 98 doc(self) 96 swish_Parse Data* self99 swish_ParserData* self 97 100 98 101 PREINIT: … … 109 112 swish_WordList * 110 113 wordlist(self) 111 swish_Parse Data* self114 swish_ParserData* self 112 115 113 116 PREINIT: … … 125 128 RETVAL 126 129 127 128 # must decrement refcount for stashed SWISH::3::Parser object129 # 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_*() without132 # _dec somewhere else? just means that the SWISH::3::Parser object133 # may never get garbage collected.134 #void135 #DESTROY(self)136 # swish_ParseData* self;137 #138 # CODE:139 # SvREFCNT_dec( self->user_data );140 141 int142 refcount(obj)143 SV* obj;144 145 CODE:146 RETVAL = SvREFCNT((SV*)SvRV(obj));147 148 OUTPUT:149 RETVAL150 151 libswish3/trunk/bindings/perl/XS/Doc.xs
r2019 r2028 79 79 OUTPUT: 80 80 RETVAL 81 82 int83 refcount(obj)84 SV* obj;85 86 CODE:87 RETVAL = SvREFCNT((SV*)SvRV(obj));88 89 OUTPUT:90 RETVAL91 92 libswish3/trunk/bindings/perl/XS/Word.xs
r2019 r2028 59 59 RETVAL 60 60 61 62 int63 refcount(obj)64 SV* obj;65 66 CODE:67 RETVAL = SvREFCNT((SV*)SvRV(obj));68 69 OUTPUT:70 RETVALlibswish3/trunk/bindings/perl/XS/WordList.xs
r2019 r2028 51 51 CODE: 52 52 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 53 59 if (self->ref_cnt < 1) { 54 60 swish_free_wordlist(self); 55 61 } 56 57 int58 refcount(obj)59 SV* obj;60 61 CODE:62 RETVAL = SvREFCNT((SV*)SvRV(obj));63 64 OUTPUT:65 RETVAL66 67 libswish3/trunk/bindings/perl/lib/SWISH/3.pm
r2019 r2028 38 38 *regex = \&get_regex; 39 39 *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; 40 48 41 49 sub new { libswish3/trunk/bindings/perl/macros.h
r2019 r2028 21 21 #define PARSER_KEY "sp_parser" 22 22 #define TOKEN_HANDLER_KEY "sp_token_handler" 23 #define SWISH3_CLASS_KEY "sp_self_class" 23 24 24 25 libswish3/trunk/bindings/perl/t/07-refcnt.t
r2015 r2028 9 9 is( $analyzer->refcount, 1, "analyzer refcount == 1" ); 10 10 11 11 12 #undef $s3; 12 13 is( $analyzer->refcount, 1, "analyzer refcount == 1" ); … … 17 18 #$s3->dump($analyzer); 18 19 is( $s3->config->refcount, 1, "config refcount == 1" ); 20 21 # avoid spurious mem error from libswish3 22 # just because of order of Perl ref cleanup 23 undef $analyzer; 24 libswish3/trunk/bindings/perl/t/08-handler.t
r2019 r2028 3 3 use_ok('SWISH::3'); 4 4 5 ok( my $s3 = SWISH::3->new( handler => sub { diag("got data: $_[0]") } ), 5 ok( my $s3 = SWISH::3->new( handler => sub { diag("got data: $_[0] ") } ), 6 #ok( my $s3 = SWISH::3->new( ), 6 7 "new parser" ); 7 8 ok( $s3->parse("t/test.html"), "parse HTML" ); libswish3/trunk/bindings/perl/t/10tokenize.t
r2019 r2028 35 35 } 36 36 } 37 38 undef $analyzer; 39 undef $wlist; 40 undef $s3; libswish3/trunk/bindings/perl/t/11get_set_parser.t
r2019 r2028 35 35 ); 36 36 ok( my $ana2 = $s3->get_analyzer, "get ana2" ); 37 38 # avoid spurious libswish3 mem error due to "random" order of Perl 39 # SV cleanup 40 undef $analyzer; 41 undef $ana2; 42 undef $ana1; 43 undef $conf2; 44 undef $conf1; 45 undef $config; 46 undef $s3; 47 libswish3/trunk/bindings/perl/typemap
r2014 r2028 1 1 TYPEMAP 2 const char * T_PV3 xmlChar * T_PV4 swish_Config * O_OBJECT5 xmlHashTablePtr O_OBJECT6 swish_Parse Data *O_OBJECT7 xmlBufferPtr T_IV8 swish_WordList * O_OBJECT9 swish_DocInfo * O_OBJECT10 swish_Word * O_OBJECT11 swish_Analyzer * O_OBJECT12 swish_Parser * O_OBJECT13 swish_NamedBuffer * O_OBJECT14 swish_3 * O_OBJECT2 const char* T_PV 3 xmlChar* T_PV 4 swish_Config* O_OBJECT 5 xmlHashTablePtr O_OBJECT 6 swish_ParserData* 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 15 15 16 16 INPUT … … 28 28 # char* having the name of the package for the blessing. 29 29 O_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 6 6 /* C code to make writing XS easier */ 7 7 8 /* a SWISH::3 object is a blessed C struct */ 8 /* sp_Obj is a struct that makes refcounting easier */ 9 10 typedef struct sp_Obj sp_Obj; 11 struct sp_Obj 12 { 13 void *c_ptr; 14 SV *perl_obj; 15 }; 9 16 10 17 static SV* sp_hv_store( HV* h, const char* key, SV* val); … … 19 26 static SV* sp_hvref_delete( SV* h, const char* key ); 20 27 static void sp_hvref_replace( SV * hashref, char* key, SV* value ); 21 static SV* sp_ ptr_to_object( char* CLASS, IV c_ptr );28 static SV* sp_bless_ptr( char* CLASS, IV c_ptr ); 22 29 static char* sp_get_objects_class( SV* object ); 23 30 static HV* sp_extract_hash_from_object( SV* object ); 24 static void sp_dump_hash( SV* hash_ref);31 static void sp_dump_hash( SV* hash_ref ); 25 32 static void sp_describe_object( SV* object ); 26 static IV sp_ ptr_from_object( SV* object );33 static IV sp_extract_ptr( SV* object ); 27 34 static SV* sp_accessor( SV* object, char* name ); 28 static void sp_store_xml2_pair_in_perl_hash( xmlChar* val, HV* perl_hash, xmlChar* key);35 static void sp_store_xml2_pair_in_perl_hash( xmlChar* val, HV* perl_hash, xmlChar* key ); 29 36 static HV* sp_xml2_hash_to_perl_hash( xmlHashTablePtr xml2_hash ); 30 37 static void sp_add_key_to_array(xmlChar* val, AV* mykeys, xmlChar* key); … … 32 39 static void sp_nb_hash_to_phash(xmlBufferPtr buf, HV *phash, xmlChar *key); 33 40 static HV* sp_nb_to_hash( swish_NamedBuffer* nb ); 34 static void sp_test_handler( swish_Parse Data* parse_data );35 static void sp_handler( swish_Parse Data* parse_data );41 static void sp_test_handler( swish_ParserData* parse_data ); 42 static void sp_handler( swish_ParserData* parse_data ); 36 43 static swish_WordList* sp_tokenize( swish_Analyzer* analyzer, xmlChar* str, ... ); 37 44 static void sp_token_handler( swish_Token *token ); … … 42 49 static swish_Analyzer* sp_new_analyzer(); 43 50 51 static sp_Obj* sp_new_Obj( char* class, void* c_ptr); 52 static SV* sp_Obj_to_perl(sp_Obj* self); 53 static void sp_Obj_inc_refcount(sp_Obj *self); 54 static void sp_Obj_dec_refcount(sp_Obj *self); 55 static int sp_Obj_get_refcount(sp_Obj *self); 56 57 static int 58 sp_Obj_get_refcount(sp_Obj *self) 59 { 60 return SvREFCNT(self->perl_obj); 61 } 62 63 static void 64 sp_Obj_inc_refcount(sp_Obj *self) 65 { 66 SvREFCNT_inc(self->perl_obj); 67 } 68 69 static void 70 sp_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 79 static sp_Obj* 80 sp_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 112 static SV* 113 sp_Obj_to_perl( sp_Obj* self ) 114 { 115 return newRV_inc(self->perl_obj); 116 } 117 44 118 static void 45 119 sp_SV_is_qr( SV *qr ) … … 170 244 /* make a Perl blessed object from a C pointer */ 171 245 static SV* 172 sp_ ptr_to_object( char* CLASS, IV c_ptr )246 sp_bless_ptr( char* CLASS, IV c_ptr ) 173 247 { 174 248 dTHX; … … 267 341 /* return the C pointer from a Perl blessed O_OBJECT */ 268 342 static IV 269 sp_ ptr_from_object( SV* object )343 sp_extract_ptr( SV* object ) 270 344 { 271 345 dTHX; … … 282 356 //warn("looking for %s in %s\n", name, class); 283 357 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 ); 285 359 286 360 if (!sv) 287 361 croak("no %s in %s object!", name, class); 288 362 289 return *sv;363 return sv; 290 364 } 291 365 … … 386 460 387 461 void 388 sp_test_handler( swish_Parse Data* parse_data )462 sp_test_handler( swish_ParserData* parse_data ) 389 463 { 390 464 dTHX; … … 403 477 */ 404 478 static void 405 sp_handler( swish_Parse Data* parse_data )479 sp_handler( swish_ParserData* parse_data ) 406 480 { 407 481 dTHX; 408 482 dSP; 409 483 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; 414 488 415 489 //warn("sp_handler called"); … … 420 494 //sp_describe_object(s3->stash); 421 495 422 handler = sp_hvref_fetch( s3->stash, HANDLER_KEY);496 handler = sp_hvref_fetch((SV*)s3->stash, HANDLER_KEY); 423 497 424 498 //warn("got handler and s3"); 425 499 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); 427 501 428 502 //warn("data_class = %s", data_class); 429 503 430 obj = sp_ ptr_to_object( data_class, (IV)parse_data );504 obj = sp_bless_ptr( data_class, (IV)parse_data ); 431 505 432 506 //sp_describe_object(obj); … … 446 520 447 521 SV* obj; 448 obj = sp_ ptr_to_object( TOKEN_CLASS, (IV)token );522 obj = sp_bless_ptr( TOKEN_CLASS, (IV)token ); 449 523 450 524 PUSHMARK(SP); … … 646 720 config = swish_init_config(); 647 721 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) ); 650 724 651 725 return config; libswish3/trunk/src/libswish3/analyzer.c
r2027 r2028 37 37 a->minwordlen = SWISH_MIN_WORD_LEN; 38 38 a->lc = 1; 39 a->ref_cnt = 1;39 a->ref_cnt = 0; 40 40 41 41 if (xmlStrEqual((xmlChar*)SWISH_DEFAULT_VALUE, … … 80 80 swish_free_analyzer( swish_Analyzer * a ) 81 81 { 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"); 85 87 } 86 88 swish_xfree(a); libswish3/trunk/src/libswish3/config.c
r2027 r2028 99 99 swish_xfree(config->flags); 100 100 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) { 103 106 SWISH_WARN("possible memory leak: config->stash was not freed"); 104 107 } … … 217 220 218 221 config->conf = c; 219 config->ref_cnt = 1;222 config->ref_cnt = 0; 220 223 config->stash = NULL; 221 224 config->flags = flags; libswish3/trunk/src/libswish3/namedbuffer.c
r2027 r2028 69 69 swish_NamedBuffer * nb = swish_xmalloc(sizeof(swish_NamedBuffer)); 70 70 nb->stash = NULL; 71 nb->ref_cnt = 1;71 nb->ref_cnt = 0; 72 72 nb->hash = xmlHashCreate(8); /* will grow as needed */ 73 73 … … 85 85 xmlHashFree(nb->hash, (xmlHashDeallocator)free_name_from_hash); 86 86 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 } 90 90 91 91 if (nb->stash != NULL) libswish3/trunk/src/libswish3/parser.c
r2027 r2028 155 155 swish_Parser * p = (swish_Parser*) swish_xmalloc(sizeof(swish_Parser)); 156 156 p->handler = handler; 157 p->ref_cnt = 1;157 p->ref_cnt = 0; 158 158 159 159 /* libxml2 stuff */ … … 170 170 swish_free_parser( swish_Parser * p ) 171 171 { 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); 175 174 } 176 175 xmlCleanupParser(); libswish3/trunk/src/libswish3/swish.c
r2027 r2028 31 31 swish_init(); 32 32 s3 = swish_xmalloc(sizeof(swish_3)); 33 s3->ref_cnt = 1;33 s3->ref_cnt = 0; 34 34 s3->config = swish_init_config(); 35 s3->config->ref_cnt++; 35 36 s3->analyzer = swish_init_analyzer(s3->config); 37 s3->analyzer->ref_cnt++; 36 38 s3->parser = swish_init_parser(handler); 39 s3->parser->ref_cnt++; 37 40 s3->stash = stash; 38 41 return s3; … … 42 45 swish_free_swish3(swish_3* s3) 43 46 { 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 50 65 //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); 54 68 } 55 69 swish_xfree(s3); … … 77 91 if (!SWISH_DEBUG) { 78 92 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 } 80 96 SWISH_DEBUG += (int)strtol(getenv("SWISH_DEBUG_CONFIG"), (char**)NULL, 10); 81 97 SWISH_DEBUG += (int)strtol(getenv("SWISH_DEBUG_DOCINFO"), (char**)NULL, 10); libswish3/trunk/src/libs
