pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:20:      #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.133 2001/09/05 01:42:47 hubbe Exp $"); + RCSID("$Id: stralloc.c,v 1.134 2001/09/06 08:07:10 hubbe Exp $");    -  + /* #define STRALLOC_USE_PRIMES */ +  + #ifdef STRALLOC_USE_PRIMES +  + #define SET_HSIZE(X) htable_size=hashprimes[(X)] + #define HMODULO(X) ((X) % htable_size) +  + #else +  + #define SET_HSIZE(X) htable_mask=(htable_size=(1<<(X)))-1 + #define HMODULO(X) ((X) & (htable_mask)) +  + unsigned INT32 htable_mask; +  + #endif +  +    #if PIKE_RUN_UNLOCKED   /* Make this bigger when we get lightweight threads */   #define BUCKET_LOCKS 2048   static PIKE_MUTEX_T *bucket_locks;      #define BUCKETLOCK(HVAL) \ -  (bucket_locks + ((hval__ % htable_size) & (BUCKET_LOCKS-1))) +  (bucket_locks + (HMODULO(hval__) & (BUCKET_LOCKS-1)))      #define LOCK_BUCKET(HVAL) do { \    size_t hval__=(HVAL); \    PIKE_MUTEX_T *bucket_lock; \    while(1) \    { \    bucket_lock=BUCKETLOCK(hval__); \    mt_lock(bucket_lock); \    if(bucket_lock == BUCKETLOCK(hval__)) \    break; \
pike.git/src/stralloc.c:334:    ptrdiff_t len,    int size_shift,    size_t hval)   {    struct pike_string *curr,**prev, **base;   #ifndef HASH_PREFIX    unsigned int depth=0;   #endif    size_t h;    LOCK_BUCKET(hval); -  h=hval % htable_size; +  h=HMODULO(hval);    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
pike.git/src/stralloc.c:425:    return 0; /* not found */   }      /*** rehash ***/      static void rehash_string_backwards(struct pike_string *s)   {    ptrdiff_t h;    if(!s) return;    rehash_string_backwards(s->next); -  h = s->hval % htable_size; +  h = HMODULO(s->hval);    s->next=base_table[h];    base_table[h]=s;   }      static void stralloc_rehash(void)   {    int h,old;    struct pike_string **old_base;       old=htable_size;
pike.git/src/stralloc.c:454: Inside #if defined(PIKE_RUN_UNLOCKED)
   return;    }       /* Now that we have bucket zero, the hash table    * cannot change, go ahead and lock ALL buckets.    * NOTE: bucket zero is already locked    */    for(h=1;h<BUCKET_LOCKS;h++) mt_lock(bucket_locks+h);   #endif    -  htable_size=hashprimes[++hashprimes_entry]; +  SET_HSIZE( ++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);       for(h=0;h<old;h++)    rehash_string_backwards(old_base[h]);       if(old_base)    free((char *)old_base);   
pike.git/src/stralloc.c:546:    t->str[len]=0;    t->len=len;    t->size_shift=0;    return t;   }      static void link_pike_string(struct pike_string *s, size_t hval)   {    size_t h;    LOCK_BUCKET(hval); -  h=hval % htable_size; +  h=HMODULO(hval);    s->refs = 0;    s->next = base_table[h];    base_table[h] = s;    s->hval=hval;    num_strings++;    UNLOCK_BUCKET(hval);       if(num_strings > MAX_AVG_LINK_LENGTH * htable_size)    stralloc_rehash();   
pike.git/src/stralloc.c:591: Inside #if undefined(HASH_PREFIX)
   {    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;       tmp2->hval=do_hash(tmp2); /* compute new hash value */ -  h2=tmp2->hval % htable_size; +  h2=HMODULO(tmp2->hval);       tmp2->next=base_table[h2]; /* and re-hash */    base_table[h2]=tmp2;    }    }   #ifdef PIKE_RUN_UNLOCKED    for(h=0;h<BUCKET_LOCKS;h++) mt_unlock(bucket_locks + h);   #endif    }   #endif
pike.git/src/stralloc.c:871:    for(len=0;str[len];len++);    return debug_make_shared_binary_string2(str,len);   }      /*** Free strings ***/      PMOD_EXPORT void unlink_pike_string(struct pike_string *s)   {    size_t h;    LOCK_BUCKET(s->hval); -  h= s->hval % htable_size; +  h= HMODULO(s->hval);    propagate_shared_string(s,h);   #ifdef PIKE_DEBUG    if (base_table[h] != s) {    fatal("propagate_shared_string() failed. Probably got bogus pike_string.\n");    }   #endif /* PIKE_DEBUG */    base_table[h]=s->next;   #ifdef PIKE_DEBUG    s->next=(struct pike_string *)(ptrdiff_t)-1;   #endif
pike.git/src/stralloc.c:1061: Inside #if defined(PIKE_DEBUG)
      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) != s->hval)    fatal("Shared string hashed to other number.\n");    -  if((s->hval % htable_size) != e) +  if(HMODULO(s->hval) != e)    {    locate_problem(wrong_hash);    fatal("Shared string hashed to wrong place.\n");    }       if(h>10000)    {    struct pike_string *s2;    for(s2=s;s2;s2=s2->next)    if(s2 == s)
pike.git/src/stralloc.c:1106: Inside #if defined(PIKE_DEBUG)
   }    }    UNLOCK_BUCKET(e);    }    return 0;   }      PMOD_EXPORT struct pike_string *debug_findstring(const struct pike_string *foo)   {    struct pike_string *tmp; -  tmp=propagate_shared_string(foo, foo->hval % htable_size); +  tmp=propagate_shared_string(foo, HMODULO(foo->hval));      #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);       LOCK_BUCKET(foo->hval);    fprintf(stderr,"------ %p %ld\n", -  base_table[foo->hval %htable_size], +  base_table[HMODULO(foo->hval)],    foo->hval); -  for(tmp2=base_table[foo->hval % htable_size];tmp2;tmp2=tmp2->next) +  for(tmp2=base_table[HMODULO(foo->hval)];tmp2;tmp2=tmp2->next)    {    if(tmp2 == tmp)    fprintf(stderr,"!!%p!!->",tmp2);    else    fprintf(stderr,"%p->",tmp2);    }    fprintf(stderr,"0\n");    UNLOCK_BUCKET(foo->hval);       for(e=0;e<htable_size;e++)    {    LOCK_BUCKET(e);    for(tmp2=base_table[e];tmp2;tmp2=tmp2->next)    {    if(tmp2 == tmp)    fprintf(stderr,"String found in hashbin %ld (not %ld)\n",    (long)e, -  (long)(foo->hval % htable_size)); +  (long)HMODULO(foo->hval));    }    UNLOCK_BUCKET(e);    }    }   #endif    return tmp;   }      PMOD_EXPORT void debug_dump_pike_string(struct pike_string *s, INT32 max)   {
pike.git/src/stralloc.c:1765:    return end_shared_string(ret);   }      /*** init/exit memory ***/   void init_shared_string_table(void)   {    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]; +  SET_HSIZE(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 PIKE_RUN_UNLOCKED    {    int h;    bucket_locks=(PIKE_MUTEX_T *)xalloc(sizeof(PIKE_MUTEX_T)*BUCKET_LOCKS);    for(h=0;h<BUCKET_LOCKS;h++) mt_init(bucket_locks + h);    }   #endif   }
pike.git/src/stralloc.c:1896:   }      struct pike_string *next_pike_string (struct pike_string *s)   {    struct pike_string *next = s->next;    if (!next) {    size_t h = s->hval;    do {    h++;    LOCK_BUCKET(h); -  h %= htable_size; +  h = HMODULO(h);    next = base_table[h];    UNLOCK_BUCKET(h);    } while (!next);    }    return next;   }      PMOD_EXPORT void init_string_builder(struct string_builder *s, int mag)   {    s->malloced=256;