pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:18:      #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.94 2000/08/10 18:00:09 grubba Exp $"); + RCSID("$Id: stralloc.c,v 1.95 2000/08/10 18:23:17 grubba 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 unsigned INT32 full_hash_value; + 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 unsigned int low_do_hash(const void *s, ptrdiff_t len, int size_shift) + static size_t low_do_hash(const void *s, ptrdiff_t len, int size_shift)   {    full_hash_value=hashmem(s,len<<size_shift,HASH_PREFIX<<size_shift);    return full_hash_value % htable_size;   }    - static INLINE unsigned int do_hash(struct pike_string *s) + 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)   {    while(--len>=0)    if(s[len]>=256)    return 1;
pike.git/src/stralloc.c:280:   #else   #define locate_problem(X)   #endif      /* Find a string in the shared string table.    * This assumes that the string is minimized!!!!    */   static struct pike_string *internal_findstring(const char *s,    ptrdiff_t len,    int size_shift, -  int h) +  ptrdiff_t h)   {    struct pike_string *curr,**prev, **base;   #ifndef HASH_PREFIX    unsigned int depth=0;   #endif       for(base = prev = base_table + h;( curr=*prev ); prev=&curr->next)    {   #ifdef PIKE_DEBUG    if(curr->refs<1)
pike.git/src/stralloc.c:316:    curr->next = *base;    *base = curr;    return curr; /* pointer to string */    }   #ifndef HASH_PREFIX    depth++;   #endif    }   #ifndef HASH_PREFIX    /* These heuruistics might require tuning! /Hubbe */ -  if((depth > HASH_PREFIX) && (HASH_PREFIX < (unsigned int)len)) +  if((depth > HASH_PREFIX) && (HASH_PREFIX < (size_t)len))    {    need_more_hash_prefix++;   /* fprintf(stderr,"depth=%d num_strings=%d need_more_hash_prefix=%d HASH_PREFIX=%d\n",depth,num_strings,need_more_hash_prefix,HASH_PREFIX); */    }else{    if(need_more_hash_prefix)    need_more_hash_prefix--;    }   #endif    return 0; /* not found */   }
pike.git/src/stralloc.c:343:   PMOD_EXPORT struct pike_string *findstring(const char *foo)   {    return binary_findstring(foo, strlen(foo));   }      /*    * find a string that is already shared and move it to the head    * of that list in the hastable    */   static struct pike_string *propagate_shared_string(const struct pike_string *s, -  int h) +  ptrdiff_t h)   {    struct pike_string *curr, **prev, **base;       for(base = prev = base_table + h;( curr=*prev ); prev=&curr->next)    {    if (curr == s) /* found it */    {    *prev=curr->next;    curr->next=*base;    *base=curr;
pike.git/src/stralloc.c:372:    }   #endif    }    return 0; /* not found */   }      /*** rehash ***/      static void rehash_string_backwards(struct pike_string *s)   { -  int h; +  ptrdiff_t h;    if(!s) return;    rehash_string_backwards(s->next); -  h=s->hval % htable_size; +  h = s->hval % htable_size;    s->next=base_table[h];    base_table[h]=s;   }      static void rehash(void)   {    int h,old;    struct pike_string **old_base;       old=htable_size;
pike.git/src/stralloc.c:420: Inside #if defined(PIKE_DEBUG)
   if(d_flag>10)    verify_shared_strings_tables();   #endif    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, unsigned int h) + static void link_pike_string(struct pike_string *s, size_t h)   {    s->refs = 0;    s->next = base_table[h];    base_table[h] = s;    s->hval=full_hash_value;    num_strings++;    if(num_strings > MAX_AVG_LINK_LENGTH * htable_size)    rehash();      #ifndef HASH_PREFIX
pike.git/src/stralloc.c:460: Inside #if undefined(HASH_PREFIX)
   struct pike_string *tmp2=tmp; /* First unlink */    tmp=tmp2->next;       h2=do_hash(tmp2); /* compute new hash value */    tmp2->hval=full_hash_value;       tmp2->next=base_table[h2]; /* and re-hash */    base_table[h2]=tmp2;    }    } -  full_hash_value=save_full_hash_value; +  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:562:    case 0: break;    }       return low_end_shared_string(s);   }         PMOD_EXPORT struct pike_string * debug_make_shared_binary_string(const char *str,size_t len)   {    struct pike_string *s; -  int h=StrHash(str,len); +  ptrdiff_t h = StrHash(str, len);       s = internal_findstring(str,len,0,h);    if (!s)    {    s=begin_shared_string(len);    MEMCPY(s->str, str, len);    link_pike_string(s, h);    }       add_ref(s);
pike.git/src/stralloc.c:607:   }      PMOD_EXPORT struct pike_string * debug_make_shared_binary_string0(const p_wchar0 *str,size_t len)   {    return debug_make_shared_binary_string((const char *)str, len);   }      PMOD_EXPORT struct pike_string * debug_make_shared_binary_string1(const p_wchar1 *str,size_t len)   {    struct pike_string *s; -  int h; +  ptrdiff_t h;       if(!find_magnitude1(str,len))    {    /* Wrong size, convert */    s=begin_shared_string(len);    convert_1_to_0(STR0(s),str,len);    return end_shared_string(s);    }       h=low_do_hash(str, len, 1);
pike.git/src/stralloc.c:635:    }       add_ref(s);       return s;   }      PMOD_EXPORT struct pike_string * debug_make_shared_binary_string2(const p_wchar2 *str,size_t len)   {    struct pike_string *s; -  int h; +  ptrdiff_t h;       switch(find_magnitude2(str,len))    {    case 0:    /* Wrong size, convert */    s=begin_shared_string(len);    convert_2_to_0(STR0(s),str,len);    return end_shared_string(s);       case 1:
pike.git/src/stralloc.c:695:   {    INT32 len;    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)   { -  unsigned int h=s->hval % htable_size; +  size_t h = s->hval % htable_size;    propagate_shared_string(s,h);    base_table[h]=s->next;   #ifdef PIKE_DEBUG -  s->next=(struct pike_string *)-1; +  s->next=(struct pike_string *)(ptrdiff_t)-1;   #endif    num_strings--;   }      PMOD_EXPORT void do_free_string(struct pike_string *s)   {    if (s)    free_string(s);   }      PMOD_EXPORT void really_free_string(struct pike_string *s)   {   #ifdef PIKE_DEBUG    extern int d_flag;    if (d_flag) {    if (s->refs) {    fatal("Freeing string with references!\n");    }    if(d_flag > 2)    { -  if(s->next == (struct pike_string *)-1) +  if(s->next == (struct pike_string *)(ptrdiff_t)-1)    fatal("Freeing shared string again!\n");    -  if(((long)s->next) & 1) +  if(((ptrdiff_t)s->next) & 1)    fatal("Freeing shared string again, memory corrupt or other bug!\n");    }    }   #endif    unlink_pike_string(s);    debug_free((char *)s,DMALLOC_LOCATION(),1);   }      PMOD_EXPORT void debug_free_string(struct pike_string *s)   {
pike.git/src/stralloc.c:754:    char b[200];       init_buf();       if (verbose)    {    int allocd_strings=0;    int allocd_bytes=0;    int num_distinct_strings=0;    int bytes_distinct_strings=0; -  int overhead_bytes=0; +  ptrdiff_t overhead_bytes = 0;    unsigned INT32 e;    struct pike_string *p;    for(e=0;e<htable_size;e++)    {    for(p=base_table[e];p;p=p->next)    {    num_distinct_strings++;    bytes_distinct_strings+=DO_ALIGN(p->len,sizeof(void *));    allocd_strings+=p->refs;    allocd_bytes+=p->refs*DO_ALIGN(p->len+3,sizeof(void *));
pike.git/src/stralloc.c:778:    overhead_bytes=(sizeof(struct pike_string)-1)*num_distinct_strings;    my_strcat("\nShared string hash table:\n");    my_strcat("-------------------------\t Strings Bytes\n");       sprintf(b,"Total asked for\t\t\t%8ld %8ld\n",    (long)allocd_strings, (long)allocd_bytes);    my_strcat(b);    sprintf(b,"Strings malloced\t\t%8ld %8ld + %ld overhead\n",    (long)num_distinct_strings,    (long)bytes_distinct_strings, -  (long)overhead_bytes); +  DO_NOT_WARN((long)overhead_bytes));    my_strcat(b); -  sprintf(b,"Space actually required/total string bytes %d%%\n", -  (bytes_distinct_strings + overhead_bytes)*100 / allocd_bytes); +  sprintf(b, "Space actually required/total string bytes %ld%%\n", +  DO_NOT_WARN((long)((bytes_distinct_strings + overhead_bytes)*100 / +  allocd_bytes)));    my_strcat(b);    }   /*    sprintf(b,"Searches: %ld Average search length: %6.3f\n",    (long)num_str_searches, (double)search_len / num_str_searches);    my_strcat(b);   */    return free_buf();   }   
pike.git/src/stralloc.c:817: Inside #if defined(PIKE_DEBUG)
   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; -  unsigned INT32 orig_full_hash = full_hash_value; +  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:928: Inside #if defined(PIKE_DEBUG)
  #endif    return tmp;   }      PMOD_EXPORT void debug_dump_pike_string(struct pike_string *s, INT32 max)   {    INT32 e;    fprintf(stderr,"0x%p: %ld refs, len=%ld, size_shift=%d, hval=%lux (%lx)\n",    s,    (long)s->refs, -  (long)s->len, +  DO_NOT_WARN((long)s->len),    s->size_shift, -  (unsigned long)s->hval, -  (unsigned long)StrHash(s->str, s->len)); +  DO_NOT_WARN((unsigned long)s->hval), +  DO_NOT_WARN((unsigned long)StrHash(s->str, s->len)));    fprintf(stderr," \"");    for(e=0;e<s->len && max>0;e++)    {    int c=EXTRACT_UCHAR(s->str+e);    switch(c)    {    case '\t': fprintf(stderr,"\\t"); max-=2; break;    case '\n': fprintf(stderr,"\\n"); max-=2; break;    case '\r': fprintf(stderr,"\\r"); max-=2; break;    case '\b': fprintf(stderr,"\\b"); max-=2; break;
pike.git/src/stralloc.c:975: Inside #if defined(PIKE_DEBUG)
   for(p=base_table[e];p;p=p->next)    debug_dump_pike_string(p, 70);   }      #endif         /*** String compare functions ***/      /* does not take locale into account */ - PMOD_EXPORT int low_quick_binary_strcmp(char *a,INT32 alen, -  char *b,INT32 blen) + PMOD_EXPORT int low_quick_binary_strcmp(char *a, ptrdiff_t alen, +  char *b, ptrdiff_t blen)   {    int tmp;    if(alen > blen)    {    tmp=MEMCMP(a, b, blen);    if(tmp) return tmp;    return 1;    }else if(alen < blen){    tmp=MEMCMP(a, b, alen);    if(tmp) return tmp;    return -1;    }else{    return MEMCMP(a, b, alen);    }   }         /* does not take locale into account */ - PMOD_EXPORT int generic_quick_binary_strcmp(const char *a,INT32 alen, int asize, -  const char *b,INT32 blen, int bsize) + PMOD_EXPORT ptrdiff_t generic_quick_binary_strcmp(const char *a, +  ptrdiff_t alen, int asize, +  const char *b, +  ptrdiff_t blen, int bsize)   {    if(!asize && !bsize)    {    int tmp;    if(alen > blen)    {    tmp=MEMCMP(a, b, blen);    if(tmp) return tmp;    return 1;    }else if(alen < blen){
pike.git/src/stralloc.c:1032:    }   }      PMOD_EXPORT int c_compare_string(struct pike_string *s, char *foo, int len)   {    return s->len == len && s->size_shift == 0 && !MEMCMP(s->str,foo,len);   }      #ifndef HAVE_STRCOLL   /* No locale function available */ - int low_binary_strcmp(char *a,INT32 alen, -  char *b,INT32 blen) + int low_binary_strcmp(char *a, ptrdiff_t alen, +  char *b, ptrdiff_t blen)   {    low_quick_binary_strcmp(a,alen,b,blen);   }   #else      /* takes locale into account */   static int low_binary_strcmp(char *a,INT32 alen,    char *b,INT32 blen)   {    INT32 tmp;
pike.git/src/stralloc.c:1062:    blen-=tmp;    }    if(alen==blen) return 0;    if(alen > blen) return 1;    return -1;   }   #endif      /* Does not take locale into account */    - PMOD_EXPORT int my_quick_strcmp(struct pike_string *a,struct pike_string *b) + PMOD_EXPORT ptrdiff_t my_quick_strcmp(struct pike_string *a, +  struct pike_string *b)   {    if(a==b) return 0;       return generic_quick_binary_strcmp(a->str, a->len, a->size_shift,    b->str, b->len, b->size_shift);   }      /* Does take locale into account */ - PMOD_EXPORT int my_strcmp(struct pike_string *a,struct pike_string *b) + PMOD_EXPORT ptrdiff_t my_strcmp(struct pike_string *a,struct pike_string *b)   {    if(a==b) return 0;       switch(TWO_SIZES(a->size_shift,b->size_shift))    {    case TWO_SIZES(0,0):    return low_binary_strcmp(a->str,a->len,b->str,b->len);       default:    { -  INT32 e,l=MINIMUM(a->len,b->len); +  ptrdiff_t e, l = MINIMUM(a->len, b->len);    for(e=0;e<l;e++)    {    INT32 ac=index_shared_string(a,e);    INT32 bc=index_shared_string(b,e);      #ifdef HAVE_STRCOLL    if(ac < 256 && bc < 256)    {    char atmp[2],btmp[2];    int tmp;
pike.git/src/stralloc.c:1108: Inside #if defined(HAVE_STRCOLL)
   return tmp;    }else   #endif    if(ac-bc) return ac-bc;    }    return a->len - b->len;    }    }   }    - PMOD_EXPORT struct pike_string *realloc_unlinked_string(struct pike_string *a, INT32 size) + PMOD_EXPORT struct pike_string *realloc_unlinked_string(struct pike_string *a, +  ptrdiff_t size)   {    struct pike_string *r;    r=(struct pike_string *)realloc((char *)a,    sizeof(struct pike_string)+    ((size+1)<<a->size_shift)); /* FIXME !! */       if(!r)    {    r=begin_shared_string(size);    MEMCPY(r->str, a->str, a->len<<a->size_shift);    free((char *)a);    }       r->len=size;    low_set_index(r,size,0);    return r;   }      /* Returns an unlinked string ready for end_shared_string */ - PMOD_EXPORT struct pike_string *realloc_shared_string(struct pike_string *a, INT32 size) + PMOD_EXPORT struct pike_string *realloc_shared_string(struct pike_string *a, +  ptrdiff_t size)   {    struct pike_string *r;    if(a->refs==1)    {    unlink_pike_string(a);    return realloc_unlinked_string(a, size);    }else{    r=begin_wide_shared_string(size,a->size_shift);    MEMCPY(r->str, a->str, a->len<<a->size_shift);    free_string(a);
pike.git/src/stralloc.c:1316:    tmp=MKPCHARP_STR(ret);    pike_string_cpy(tmp,a);    INC_PCHARP(tmp,a->len);    pike_string_cpy(tmp,b);    return end_shared_string(ret);   }      PMOD_EXPORT struct pike_string *add_and_free_shared_strings(struct pike_string *a,    struct pike_string *b)   { -  INT32 alen=a->len; +  ptrdiff_t alen = a->len;    if(a->size_shift == b->size_shift)    { -  a=realloc_shared_string(a,alen + b->len); +  a = realloc_shared_string(a,alen + b->len);    MEMCPY(a->str+(alen<<a->size_shift),b->str,b->len<<b->size_shift);    free_string(b);    return end_shared_string(a);    }else{    struct pike_string *ret=add_shared_strings(a,b);    free_string(a);    free_string(b);    return ret;    }   }       - PMOD_EXPORT int string_search(struct pike_string *haystack, + PMOD_EXPORT ptrdiff_t string_search(struct pike_string *haystack,    struct pike_string *needle, -  int start) +  ptrdiff_t start)   {    struct generic_mem_searcher s;    char *r;       if(needle->size_shift > haystack->size_shift)    return -1;       init_generic_memsearcher(&s,    needle->str,    needle->len,
pike.git/src/stralloc.c:1626:    void *ret;    string_build_mkspace(s, chars, mag);    if(chars<0) s->known_shift=0;    ret = s->s->str + (s->s->len<<s->s->size_shift);    s->s->len += chars;    return ret;   }      PMOD_EXPORT void string_builder_putchar(struct string_builder *s, int ch)   { -  INT32 i; +  ptrdiff_t i;    string_build_mkspace(s,1,min_magnitude(ch));    s->known_shift=MAXIMUM(min_magnitude(ch),s->known_shift); -  i=s->s->len++; +  i = s->s->len++;    low_set_index(s->s,i,ch);   }         PMOD_EXPORT void string_builder_binary_strcat(struct string_builder *s, char *str, ptrdiff_t len)   {    string_build_mkspace(s,len,0);    switch(s->s->size_shift)    {    case 0: convert_0_to_0(STR0(s->s)+s->s->len,(p_wchar0 *)str,len); break;