pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:51:   static struct pike_string **base_table=0;   static unsigned INT32 num_strings=0;   PMOD_EXPORT struct pike_string *empty_pike_string = 0;      /*** Main string hash function ***/      #define StrHash(s,len) low_do_hash(s,len,0)   #define low_do_hash(STR,LEN,SHIFT) low_hashmem( (STR), (LEN)<<(SHIFT), hash_prefix_len<<(SHIFT), hashkey )   #define do_hash(STR) low_do_hash(STR->str,STR->len,STR->size_shift)    + static INLINE int __attribute((unused)) string_is_block_allocated(const struct pike_string * s) { +  return (s->flags & STRING_ALLOC_MASK) == STRING_ALLOC_BA; + } +  + static INLINE int __attribute((unused)) string_is_malloced(const struct pike_string * s) { +  return (s->flags & STRING_ALLOC_MASK) == STRING_ALLOC_MALLOC; + } +  + static INLINE int __attribute((unused)) string_is_static(const struct pike_string * s) { +  return (s->flags & STRING_ALLOC_MASK) == STRING_ALLOC_STATIC; + } +  + static INLINE int __attribute((unused)) string_may_modify(const struct pike_string * s) { +  return !string_is_static(s) && s->refs == 1; + } +  +    /* Returns true if str could contain n. */   PMOD_EXPORT int string_range_contains( struct pike_string *str, int n )   {    INT32 min, max;    check_string_range( str, 1, &min, &max );    if( n >= min && n <= max )    return 1;    return 0;   }   
pike.git/src/stralloc.c:741: Inside #if defined(PIKE_DEBUG)
  #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    */    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. +  */    t->str = ba_alloc(&string_allocator);    t->flags |= STRING_ALLOC_BA;    } else { -  t->str = xalloc(bytes); +  t->str = malloc(bytes); +  if (!t->str) { +  ba_free(&string_allocator, t); +  Pike_error("Out of memory.\n"); +  }    t->flags |= STRING_ALLOC_MALLOC;    }    t->refs = 0; -  +  t->size_shift=shift;    add_ref(t); /* For DMALLOC */    t->len=len; -  t->size_shift=shift; +     DO_IF_DEBUG(t->next = NULL);    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) {    struct pike_string * t = ba_alloc(&string_allocator);       t->flags = STRING_NOT_HASHED|STRING_NOT_SHARED|STRING_ALLOC_STATIC;
pike.git/src/stralloc.c:2151:    }    free(base_table);    base_table=0;    num_strings=0;      #ifdef DO_PIKE_CLEANUP    ba_destroy(&string_allocator);   #endif /* DO_PIKE_CLEANUP */   }    - static INLINE size_t memory_in_string (const struct pike_string *s) - { -  return sizeof (struct pike_string) + ((s->len + 1) << s->size_shift); + size_t count_memory_in_string(const struct pike_string * s) { +  size_t size = sizeof(struct pike_string); +  +  switch (s->flags & STRING_ALLOC_MASK) { +  case STRING_ALLOC_BA: +  size += sizeof(struct pike_string); +  break; +  case STRING_ALLOC_MALLOC: +  size += PIKE_ALIGNTO(((s->len + 1) << s->size_shift), 4); +  break;    }    - static void cnt_strings(struct ba_iterator * it, void * data) { -  size_t * size = data; -  size_t n = 0; +  return size; + }    -  do { -  struct pike_string * s = ba_it_val(it); -  n += memory_in_string(s); -  } while (ba_it_step(it)); + void count_memory_in_strings(size_t *num, size_t *_size) + { +  unsigned INT32 e; +  size_t size = 0; +  *num = num_strings;    -  *size += n; +  size+=htable_size * sizeof(struct pike_string *); +  +  for (e = 0; e < htable_size; e++) { +  struct pike_string * s; +  +  for (s = base_table[e]; s; s = s->next) { +  size += count_memory_in_string(s);    } -  +  }    - void count_memory_in_strings(size_t *num, size_t *size) - { -  ba_count_all(&string_allocator, num, size); -  ba_walk(&string_allocator, cnt_strings, size); -  *size+=htable_size * sizeof(struct pike_string *); +  *_size = size;   }      PMOD_EXPORT void visit_string (struct pike_string *s, int action, void *extra)   {    visit_enter(s, T_STRING, extra);    switch (action & VISIT_MODE_MASK) {   #ifdef PIKE_DEBUG    default:    Pike_fatal ("Unknown visit action %d.\n", action);    case VISIT_NORMAL:    case VISIT_COMPLEX_ONLY:    break;   #endif    case VISIT_COUNT_BYTES: -  mc_counted_bytes += memory_in_string (s); +  mc_counted_bytes += count_memory_in_string (s);    break;    }    visit_leave(s, T_STRING, extra);   }      #ifdef PIKE_DEBUG   unsigned gc_touch_all_strings(void)   {    unsigned INT32 e;    unsigned n = 0;