pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:744:    base_table[h2]=tmp2;    }    }    }   }      PMOD_EXPORT struct pike_string *debug_begin_wide_shared_string(size_t len, int shift)   {    struct pike_string *t = NULL;    size_t bytes = (len+1) << shift; +  ONERROR fe;   #ifdef PIKE_DEBUG    if(d_flag>10)    verify_shared_strings_tables();   #endif   #ifdef PIKE_DEBUG    if (shift > 2)    Pike_fatal("Unsupported string shift: %d\n", shift);   #endif /* PIKE_DEBUG */    t=(struct pike_string *)ba_alloc(&string_allocator); -  /* we mark the string as static here, to avoid double free if the allocations -  * fail +  /* we mark the string as static here, to avoid double free if the +  * allocations fail    */    t->flags = STRING_NOT_HASHED|STRING_NOT_SHARED|STRING_ALLOC_STATIC; -  if (bytes <= sizeof(struct pike_string)) { -  /* FIXME: This can fail with an exception, which would leak -  * the string t. We should have ba_alloc and ba_xalloc, really. -  */ +  SET_ONERROR(fe,free_unlinked_pike_string,t); +  if (bytes <= sizeof(struct pike_string)) +  {    t->str = ba_alloc(&string_allocator);    t->flags |= STRING_ALLOC_BA;    } else { -  t->str = malloc(bytes); -  if (!t->str) { -  ba_free(&string_allocator, t); -  Pike_error("Out of memory.\n"); -  } +  t->str = xalloc(bytes);    t->flags |= STRING_ALLOC_MALLOC;    }    t->refs = 0;    t->size_shift=shift;    add_ref(t); /* For DMALLOC */    t->len=len;    DO_IF_DEBUG(t->next = NULL); -  +  UNSET_ONERROR(&fe);    low_set_index(t,len,0);    return t;   }    - PMOD_EXPORT struct pike_string * make_static_string(const char * str, size_t len, -  enum size_shift shift) { + static struct pike_string * make_static_string(const char * str, size_t len, +  enum size_shift shift) + {    struct pike_string * t = ba_alloc(&string_allocator);       t->flags = STRING_NOT_HASHED|STRING_NOT_SHARED|STRING_ALLOC_STATIC;    t->str = str;    t->refs = 0;    t->len = len;    t->size_shift = shift;    add_ref(t); /* For DMALLOC */       return t;
pike.git/src/stralloc.c:806:   {    struct pike_string *s;    ptrdiff_t h = StrHash(str, len);       s = internal_findstring(str,len,shift,h);       if (!s) {    s = make_static_string(str, len, shift);    link_pike_string(s, h);    } else { -  +     if (!string_is_static(s)) {    if (string_is_block_allocated(s)) {    ba_free(&string_allocator, s->str);    } else {    free(s->str);    }    s->flags &= ~STRING_ALLOC_MASK;    s->flags |= STRING_ALLOC_STATIC;    s->str = str;    } -  +     add_ref(s);    }       return s;   }      /*    * This function assumes that the shift size is already the minimum it    * can be.    */
pike.git/src/stralloc.c:891:   }      /*    * This function checks if the shift size can be decreased before    * entering the string in the shared string table    */   PMOD_EXPORT struct pike_string *end_shared_string(struct pike_string *s)   {    struct pike_string *s2;    -  switch(s->size_shift) +  switch(UNLIKELY(s->size_shift))    {    default:   #ifdef PIKE_DEBUG    Pike_fatal("ARGHEL! size_shift:%d\n", s->size_shift);       case 2:   #endif    switch(find_magnitude2(STR2(s),s->len))    {    case 0:
pike.git/src/stralloc.c:1082:      PMOD_EXPORT struct pike_string *debug_make_shared_string2(const p_wchar2 *str)   {    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) + static void unlink_pike_string(struct pike_string *s)   { -  size_t h; -  h= HMODULO(s->hval); -  propagate_shared_string(s,h); - #ifdef PIKE_DEBUG -  if (base_table[h] != s) { -  Pike_fatal("propagate_shared_string() failed. Probably got bogus pike_string.\n"); +  size_t h = HMODULO(s->hval); +  struct pike_string *tmp = base_table[h], *p = NULL; +  +  while( tmp ) +  { +  if( tmp == s ) +  { +  if( p ) +  p->next = s->next; +  else +  base_table[h] = s->next; +  break;    } - #endif /* PIKE_DEBUG */ -  base_table[h]=s->next; +  p = tmp; +  tmp = tmp->next; +  }   #ifdef PIKE_DEBUG -  +  if( !tmp ) +  Pike_fatal("unlink on non-shared string\n");    s->next=(struct pike_string *)(ptrdiff_t)-1;   #endif    num_strings--;    s->flags |= STRING_NOT_SHARED;   }      PMOD_EXPORT void do_free_string(struct pike_string *s)   {    if (s)    free_string(s);
pike.git/src/stralloc.c:1397: Inside #if defined(PIKE_DEBUG)
   {    if(p==foo)    {    return 1;    }    }    }    return 0;   }    + /* for once, this is actually a debug function! */   struct pike_string *debug_findstring(const struct pike_string *foo)   { -  struct pike_string *tmp; -  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); -  -  fprintf(stderr,"------ %p %ld\n", -  base_table[HMODULO(foo->hval)], -  foo->hval); -  for(tmp2=base_table[HMODULO(foo->hval)];tmp2;tmp2=tmp2->next) -  { -  if(tmp2 == tmp) -  fprintf(stderr,"!!%p!!->",tmp2); -  else -  fprintf(stderr,"%p->",tmp2); +  return propagate_shared_string(foo, HMODULO(foo->hval));   } -  fprintf(stderr,"0\n"); +     -  for(e=0;e<htable_size;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)HMODULO(foo->hval)); -  } -  } -  } - #endif /* 0 */ -  return tmp; - } -  +    PMOD_EXPORT void debug_dump_pike_string(const 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,    DO_NOT_WARN((long)s->len),    s->size_shift,    DO_NOT_WARN((unsigned long)s->hval),    DO_NOT_WARN((unsigned long)StrHash(s->str, s->len)));
pike.git/src/stralloc.c:1610:      struct pike_string *realloc_unlinked_string(struct pike_string *a,    ptrdiff_t size)   {    char * s = NULL;    size_t nbytes = (size_t)(size+1) << a->size_shift;    size_t obytes = (size_t)(a->len+1) << a->size_shift;       switch ((a->flags & STRING_ALLOC_MASK) | (nbytes <= sizeof(struct pike_string))) {    case STRING_ALLOC_MALLOC: -  s=realloc(a->str, nbytes); -  if (!s) -  Pike_error("Out of memory in realloc_unlinked_string. Could not allocate %"PRINTSIZET" bytes.\n", -  nbytes); +  s=xrealloc(a->str, nbytes);    break;    case STRING_ALLOC_BA: // old string was short    s = xalloc(nbytes);    a->flags &= ~STRING_ALLOC_MASK;    a->flags |= STRING_ALLOC_MALLOC;    memcpy(s, a->str, obytes);    ba_free(&string_allocator, a->str);    break;    case STRING_ALLOC_MALLOC|1:    s = ba_alloc(&string_allocator);
pike.git/src/stralloc.c:1709:   struct pike_string *modify_shared_string(struct pike_string *a,    INT32 index,    INT32 c)   {    INT32 old_value;   #ifdef PIKE_DEBUG    if(index<0 || index>=a->len)    Pike_fatal("Index out of range in modify_shared_string()\n");   #endif    -  +     old_value=index_shared_string(a,index);    if(old_value==c) return a;       /* First test if the string needs to be grown:    * ie; the new value does not fit in the char size of    * the old string    */       if(min_magnitude(c) > a->size_shift)    {