Changeset 2104

Show
Ignore:
Timestamp:
03/28/08 23:00:02 (2 months ago)
Author:
karpet
Message:

add some mem debugging and clean up swish_words example

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/libswish3/analyzer.c

    r2103 r2104  
    5555 
    5656    a->stash = NULL; 
     57     
     58    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { 
     59        SWISH_DEBUG_MSG("analyzer ptr 0x%x", (int)a); 
     60    } 
    5761 
    5862    return a; 
     
    7579    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { 
    7680        SWISH_DEBUG_MSG("free analyzer"); 
     81        swish_mem_debug(); 
    7782    } 
    7883    swish_xfree(a); 
  • libswish3/trunk/src/libswish3/config.c

    r2103 r2104  
    158158        SWISH_DEBUG_MSG("freeing config"); 
    159159        SWISH_DEBUG_MSG("ptr addr: 0x%x  %d", (int)config, (int)config); 
     160        swish_mem_debug(); 
    160161    } 
    161162 
     
    207208/* misc default flags */ 
    208209    config->flags->tokenize = 1; 
     210     
     211    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { 
     212        SWISH_DEBUG_MSG("config ptr 0x%x", (int)config); 
     213    } 
    209214 
    210215    return config; 
  • libswish3/trunk/src/libswish3/mem.c

    r2103 r2104  
    7171 
    7272    memcount++; 
    73     if (SWISH_DEBUG > 20) 
     73    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { 
    7474        SWISH_DEBUG_MSG("memcount = %ld", memcount); 
     75        SWISH_DEBUG_MSG("xmalloc address: 0x%x", (int)ptr); 
     76    } 
    7577 
    7678    return ptr; 
     
    8385{ 
    8486    memcount++; 
    85     if (SWISH_DEBUG > 20
     87    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY
    8688        SWISH_DEBUG_MSG("memcount = %ld", memcount); 
    8789    return (xmlStrdup(ptr)); 
     
    9597{ 
    9698    memcount++; 
    97     if (SWISH_DEBUG > 20
     99    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY
    98100        SWISH_DEBUG_MSG("memcount = %ld", memcount); 
    99101    return (xmlStrndup(ptr, len)); 
     
    110112        return; 
    111113    } 
     114     
     115    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) 
     116        SWISH_DEBUG_MSG("freeing %s 0x%x", ptr, (int)ptr); 
    112117 
    113118    xmlFree(ptr); 
     
    115120    memcount--; 
    116121 
    117     if (SWISH_DEBUG > 20
     122    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY
    118123        SWISH_DEBUG_MSG("memcount = %ld", memcount); 
    119124} 
  • libswish3/trunk/src/libswish3/parser.c

    r2103 r2104  
    256256     */ 
    257257    get_env_vars(); 
     258     
     259    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { 
     260        SWISH_DEBUG_MSG("parser ptr 0x%x", (int)p); 
     261    } 
    258262 
    259263    return p; 
     
    265269) 
    266270{ 
     271    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { 
     272        SWISH_DEBUG_MSG("freeing parser"); 
     273        swish_mem_debug(); 
     274    } 
    267275    if (p->ref_cnt != 0) { 
    268276        SWISH_WARN("parser ref_cnt != 0: %d\n", p->ref_cnt); 
  • libswish3/trunk/src/libswish3/swish.c

    r2103 r2104  
    4141    s3->parser->ref_cnt++; 
    4242    s3->stash = stash; 
     43     
     44    if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { 
     45        SWISH_DEBUG_MSG("s3 ptr 0x%x", (int)s3); 
     46    } 
     47     
    4348    return s3; 
    4449} 
     
    5156    s3->parser->ref_cnt--; 
    5257    if (s3->parser->ref_cnt < 1) { 
    53 /* SWISH_DEBUG_MSG("freeing parser"); */ 
    5458        swish_free_parser(s3->parser); 
    5559    } 
     
    5761    s3->analyzer->ref_cnt--; 
    5862    if (s3->analyzer->ref_cnt < 1) { 
    59 /* SWISH_DEBUG_MSG("freeing analyzer"); */ 
    6063        swish_free_analyzer(s3->analyzer); 
    6164    } 
     
    6366    s3->config->ref_cnt--; 
    6467    if (s3->config->ref_cnt < 1) { 
    65 /* SWISH_DEBUG_MSG("freeing config"); */ 
    6668        swish_free_config(s3->config); 
    6769    } 
    6870 
    69 /* SWISH_DEBUG_MSG("freeing s3"); */ 
    7071    if (s3->ref_cnt != 0) { 
    7172        SWISH_WARN("s3 ref_cnt != 0: %d\n", s3->ref_cnt); 
  • libswish3/trunk/src/swish_words.c

    r2010 r2104  
    1818 */ 
    1919 
    20  
    2120/* test word parser */ 
    2221 
     
    2827#include <wctype.h> 
    2928#include <ctype.h> 
    30 #include <libxml/hash.h> 
    3129#include <getopt.h> 
    3230 
    3331#include "libswish3.h" 
    3432 
    35 static struct option longopts[] = 
    36 
     33static struct option longopts[] = { 
    3734    {"file", required_argument, 0, 'f'}, 
    38     {"debug", required_argument, 0, 'd'}, 
    3935    {"help", no_argument, 0, 'h'}, 
    4036    {0, 0, 0, 0} 
    4137}; 
    4238 
    43 void            print_list(swish_WordList * list); 
    44 int             main(int argc, char **argv); 
    45 int             usage(); 
     39 
     40int main( 
     41    int argc, 
     42    char **argv 
     43); 
     44int usage( 
     45); 
    4646 
    4747extern int SWISH_DEBUG; 
    4848 
    49  
    5049int 
    51 usage() 
     50usage( 
     51
    5252{ 
    5353 
    54     char  *descr = "swish_words is an example program for testing the libswish3 tokenizer\n"; 
     54    char *descr = 
     55        "swish_words is an example program for testing the libswish3 tokenizer\n"; 
    5556    printf("swish_words [opts] [string(s)]\n"); 
    56     printf("opts:\n --file file.txt\n --debug\n"); 
     57    printf("opts:\n --file file.txt\n"); 
    5758    printf("\n%s\n\n", descr); 
    5859    exit(1); 
     
    6061 
    6162int 
    62 main(int argc, char **argv) 
     63main( 
     64    int argc, 
     65    char **argv 
     66
    6367{ 
    64     int             i, ch; 
    65     int             option_index; 
    66     extern char    *optarg; 
    67     extern int      optind; 
    68     xmlChar        *string; 
     68    int i, ch; 
     69    int option_index; 
     70    extern char *optarg; 
     71    extern int optind; 
     72    xmlChar *string; 
    6973    swish_WordList *list; 
    70     xmlChar        *meta; 
    71     swish_3        *s3; 
     74    xmlChar *meta; 
     75    swish_3 *s3; 
     76 
     77    meta = (xmlChar *)SWISH_DEFAULT_METANAME; 
     78    option_index = 0; 
     79    string = NULL; 
    7280     
    73     meta            = (xmlChar*)SWISH_DEFAULT_METANAME; 
    74     option_index    = 0; 
    75     string          = NULL; 
     81    s3 = swish_init_swish3(NULL, NULL); 
    7682 
    77     while ((ch = getopt_long(argc, argv, "d:f:h", longopts, &option_index)) != -1) 
    78     { 
    79         /* printf("switch is %c\n",   ch); */ 
    80         /* printf("optarg is %s\n", optarg); */ 
    81         /* printf("optind = %d\n",  optind); */ 
     83    while ((ch = getopt_long(argc, argv, "f:h", longopts, &option_index)) != -1) { 
    8284 
    83         switch (ch) 
    84         { 
    85         case 0:    /* If this option set a flag, do nothing else now. */ 
     85        switch (ch) { 
     86        case 0:                /* If this option set a flag, do nothing else now. */ 
    8687            if (longopts[option_index].flag != 0) 
    8788                break; 
     
    9495        case 'f': 
    9596            printf("reading %s\n", optarg); 
    96                      
    97             string = swish_slurp_file((xmlChar *) optarg); 
    98              
    99             break; 
    100  
    101         case 'd': 
    102             printf("turning on debug mode: %s\n", optarg); 
    103  
    104             if (!isdigit(optarg[0])) 
    105                 err(1, "-d option requires a positive integer as argument\n"); 
    106  
    107             SWISH_DEBUG = (int) strtol(optarg, (char **) NULL, 10); 
     97            string = swish_slurp_file((xmlChar *)optarg); 
    10898            break; 
    10999 
     
    117107    } 
    118108 
    119     s3 = swish_init_swish3( NULL, NULL );   /* call after we have set optional debug flag */ 
    120     i  = optind; 
    121          
    122     for (; i < argc; i++) 
    123     { 
    124         list = swish_tokenize( s3->analyzer, (xmlChar *) argv[i], 0, 0, meta, meta ); 
     109    i = optind; 
     110 
     111    for (; i < argc; i++) { 
     112        list = swish_tokenize(s3->analyzer, (xmlChar *)argv[i], 0, 0, meta, meta); 
    125113        printf("parsed: %s\n", argv[i]); 
    126114        swish_debug_wordlist(list); 
     115        list->ref_cnt--; 
    127116        swish_free_wordlist(list); 
    128117    } 
    129      
    130     if (string != NULL) 
    131     { 
    132         list = swish_tokenize( s3->analyzer, string, 0, 0, meta, meta ); 
    133         printf("parsed: %s\n", string); 
    134         swish_debug_wordlist(list); 
     118 
     119    if (string != NULL) { 
     120        list = swish_tokenize(s3->analyzer, string, 0, 0, meta, meta); 
     121         
     122        if (SWISH_DEBUG & SWISH_DEBUG_WORDLIST) 
     123            swish_debug_wordlist(list); 
     124             
     125        list->ref_cnt--; 
    135126        swish_free_wordlist(list); 
    136127        swish_xfree(string); 
    137128    } 
    138      
    139     swish_free_swish3( s3 ); 
     129 
     130    swish_free_swish3(s3);     
    140131     
    141132    return (0);