pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:19:      #include <errno.h>   #include <float.h>   #include <ctype.h>   #include <math.h>      #ifndef HUGE   #define HUGE HUGE_VAL   #endif /*!HUGE*/    - RCSID("$Id: stralloc.c,v 1.118 2001/03/28 10:02:43 hubbe Exp $"); + RCSID("$Id: stralloc.c,v 1.119 2001/03/29 01:12:38 hubbe Exp $");      #define BEGIN_HASH_SIZE 997   #define MAX_AVG_LINK_LENGTH 3      /* Experimental dynamic hash length */   #ifndef HASH_PREFIX   static unsigned int HASH_PREFIX=64;   static unsigned int need_more_hash_prefix=0;   #endif      unsigned INT32 htable_size=0;   static unsigned int hashprimes_entry=0;   static struct pike_string **base_table=0; - static size_t full_hash_value; +    unsigned INT32 num_strings=0;      /*** Main string hash function ***/    -  +    #define StrHash(s,len) low_do_hash(s,len,0)    - static INLINE size_t low_do_hash(const void *s, ptrdiff_t len__, + static INLINE size_t low_do_hash(const void *s, +  ptrdiff_t len__,    int size_shift)   { -  DO_HASHMEM(full_hash_value, s, len__<<size_shift, HASH_PREFIX<<size_shift); -  return full_hash_value % htable_size; +  size_t h; +  DO_HASHMEM(h, s, len__<<size_shift, HASH_PREFIX<<size_shift); +  return h;   }      static INLINE size_t do_hash(struct pike_string *s)   {    return low_do_hash(s->str, s->len, s->size_shift);   }         static INLINE int find_magnitude1(const unsigned INT16 *s, ptrdiff_t len)   {
pike.git/src/stralloc.c:271:    DM(fprintf(stderr,"More Plausible problem location(s):\n"));    DM(dump_memhdr_locations(yes,no,0));   }      static int has_zero_refs(struct pike_string *s)   {    return s->refs<=0;   }   static int wrong_hash(struct pike_string *s)   { -  return (s->hval % htable_size) != do_hash(s); +  return s->hval != do_hash(s);   }   static int improper_zero_termination(struct pike_string *s)   {    return index_shared_string(s,s->len);   }   #else   #define locate_problem(X)   #endif      /* Find a string in the shared string table.    * This assumes that the string is minimized!!!!    */   static INLINE struct pike_string *internal_findstring(const char *s,    ptrdiff_t len,    int size_shift, -  ptrdiff_t h) +  ptrdiff_t hval)   {    struct pike_string *curr,**prev, **base;   #ifndef HASH_PREFIX    unsigned int depth=0;   #endif -  +  size_t h=hval % htable_size;    for(base = prev = base_table + h;( curr=*prev ); prev=&curr->next)    {   #ifdef PIKE_DEBUG    if(curr->refs<1)    {    debug_dump_pike_string(curr, 70);    locate_problem(has_zero_refs);    fatal("String with no references.\n");    }   #endif    debug_malloc_touch(curr);    -  if (full_hash_value == curr->hval && +  if (hval == curr->hval &&    len==curr->len &&    size_shift==curr->size_shift &&    ( curr->str == s ||    !MEMCMP(curr->str, s,len<<size_shift))) /* found it */    {    *prev = curr->next;    curr->next = *base;    *base = curr;    return curr; /* pointer to string */    }
pike.git/src/stralloc.c:484:    t=(struct pike_string *)alloc_short_pike_string0();    } else {    t=(struct pike_string *)xalloc(len + sizeof(struct pike_string));    }    t->str[len]=0;    t->len=len;    t->size_shift=0;    return t;   }    - static void link_pike_string(struct pike_string *s, size_t h) + static void link_pike_string(struct pike_string *s, size_t hval)   { -  +  size_t h; +  h=hval % htable_size;    s->refs = 0;    s->next = base_table[h];    base_table[h] = s; -  s->hval=full_hash_value; +  s->hval=hval;    num_strings++;    if(num_strings > MAX_AVG_LINK_LENGTH * htable_size)    rehash();      #ifndef HASH_PREFIX    /* These heuruistics might require tuning! /Hubbe */    if(need_more_hash_prefix > ( htable_size >> 4))    {    /* This could in theory have a pretty ugly complexity */    /* /Hubbe    */ -  size_t save_full_hash_value = full_hash_value; +        need_more_hash_prefix=0;    HASH_PREFIX=HASH_PREFIX*2;   /* fprintf(stderr,"Doubling HASH_PREFIX to %d and rehashing\n",HASH_PREFIX); */       for(h=0;h<htable_size;h++)    {    struct pike_string *tmp=base_table[h];    base_table[h]=0;    while(tmp)    {    size_t h2;    struct pike_string *tmp2=tmp; /* First unlink */    tmp=tmp2->next;    -  h2=do_hash(tmp2); /* compute new hash value */ -  tmp2->hval=full_hash_value; +  tmp2->hval=do_hash(tmp2); /* compute new hash value */ +  h2=tmp2->hval % htable_size;       tmp2->next=base_table[h2]; /* and re-hash */    base_table[h2]=tmp2;    }    } -  full_hash_value = save_full_hash_value; +     }   #endif   }      PMOD_EXPORT struct pike_string *debug_begin_wide_shared_string(size_t len, int shift)   {    struct pike_string *t;   #ifdef PIKE_DEBUG    extern int d_flag;    if(d_flag>10)
pike.git/src/stralloc.c:910:    my_strcat(b);   */    return free_buf();   }      /*** PIKE_DEBUG ***/   #ifdef PIKE_DEBUG      PMOD_EXPORT void check_string(struct pike_string *s)   { -  do_hash(s); -  if(full_hash_value != s->hval) +  if(do_hash(s) != s->hval)    {    locate_problem(wrong_hash);    fatal("Hash value changed?\n");    }       if(debug_findstring(s) !=s)    fatal("Shared string not shared.\n");       if(index_shared_string(s,s->len))    {    locate_problem(improper_zero_termination);    fatal("Shared string is not zero terminated properly.\n");    }   }      PMOD_EXPORT void verify_shared_strings_tables(void)   {    unsigned INT32 e, h, num=0; -  size_t orig_full_hash = full_hash_value; +     struct pike_string *s;       for(e=0;e<htable_size;e++)    {    h=0;    for(s=base_table[e];s;s=s->next)    {    num++;    h++;    if(s->len < 0)
pike.git/src/stralloc.c:955: Inside #if defined(PIKE_DEBUG)
   locate_problem(has_zero_refs);    fatal("Shared string had too few references.\n");    }       if(index_shared_string(s,s->len))    {    locate_problem(improper_zero_termination);    fatal("Shared string didn't end with a zero.\n");    }    -  if(do_hash(s) != e) +  if(do_hash(s) != s->hval) +  fatal("Shared string hashed to other number.\n"); +  +  if((s->hval % htable_size) != e)    {    locate_problem(wrong_hash);    fatal("Shared string hashed to wrong place.\n");    }    -  if(s->hval != full_hash_value) -  fatal("Shared string hashed to other number.\n"); -  +     if(h>10000)    {    struct pike_string *s2;    for(s2=s;s2;s2=s2->next)    if(s2 == s)    fatal("Shared string table is cyclic.\n");    h=0;    }    }    }    if(num != num_strings)    fatal("Num strings is wrong %d!=%d\n",num,num_strings); -  full_hash_value = orig_full_hash; +    }      PMOD_EXPORT int safe_debug_findstring(struct pike_string *foo)   {    unsigned INT32 e;    if(!base_table) return 0;    for(e=0;e<htable_size;e++)    {    struct pike_string *p;    for(p=base_table[e];p;p=p->next)
pike.git/src/stralloc.c:1007: Inside #if defined(PIKE_DEBUG) and #if 0
  #if 0    if(!tmp)    {    unsigned INT32 e;    struct pike_string *tmp2;    fprintf(stderr,"String %p %ld %ld %s\n",    foo,    (long)foo->hval,    (long)foo->len,    foo->str); -  StrHash(foo->str,foo->len); +     fprintf(stderr,"------ %p %ld\n",    base_table[foo->hval %htable_size], -  (long)full_hash_value); +  foo->hval);    for(tmp2=base_table[foo->hval % htable_size];tmp2;tmp2=tmp2->next)    {    if(tmp2 == tmp)    fprintf(stderr,"!!%p!!->",tmp2);    else    fprintf(stderr,"%p->",tmp2);    }    fprintf(stderr,"0\n");       for(e=0;e<htable_size;e++)
pike.git/src/stralloc.c:1642:    }    generic_memcpy(r,MKPCHARP(s,str->size_shift),(end-s)>>str->size_shift);       mojt.vtab->freeme(mojt.data);    return end_shared_string(ret);   }      /*** init/exit memory ***/   void init_shared_string_table(void)   { + #ifdef PIKE_RUN_UNLOCKED +  mt_init(&stralloc_mutex); + #endif    init_short_pike_string0_blocks();    init_short_pike_string1_blocks();    init_short_pike_string2_blocks();    for(hashprimes_entry=0;hashprimes[hashprimes_entry]<BEGIN_HASH_SIZE;hashprimes_entry++);    htable_size=hashprimes[hashprimes_entry];    base_table=(struct pike_string **)xalloc(sizeof(struct pike_string *)*htable_size);    MEMSET((char *)base_table,0,sizeof(struct pike_string *)*htable_size);   }      #ifdef DEBUG_MALLOC