pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: stralloc.c,v 1.214 2008/05/01 21:44:33 mast Exp $ + || $Id: stralloc.c,v 1.215 2008/05/02 00:40:21 mast Exp $   */      #include "global.h"   #include "stralloc.h"   #include "pike_macros.h"   #include "dynamic_buffer.h"   #include "pike_macros.h"   #include "pike_memory.h"   #include "pike_error.h"   #include "gc.h"
pike.git/src/stralloc.c:536:    if(old_base)    free((char *)old_base);      #ifdef PIKE_RUN_UNLOCKED    for(h=0;h<BUCKET_LOCKS;h++) mt_unlock(bucket_locks + h);   #endif   }      /* Allocation of strings */    + /* Without the str at the end, to get the size of the header. */ + struct pike_string_hdr { +  PIKE_STRING_CONTENTS; + }; +    /* Allocate some fixed string sizes with BLOCK_ALLOC. */      /* Use the BLOCK_ALLOC() stuff for short strings */      #undef INIT_BLOCK   #define INIT_BLOCK(NEW_STR) do { \    (NEW_STR)->refs = 1; \    (NEW_STR)->flags = \    STRING_NOT_HASHED|STRING_NOT_SHARED|STRING_IS_SHORT; \    } while(0)
pike.git/src/stralloc.c:608:   {    struct pike_string *t;   #ifdef PIKE_DEBUG    extern int d_flag;    if(d_flag>10)    verify_shared_strings_tables();   #endif    if (len <= SHORT_STRING_THRESHOLD) {    t=(struct pike_string *)alloc_short_pike_string0();    } else { -  t=(struct pike_string *)xalloc(len + sizeof(struct pike_string)); +  t=(struct pike_string *)xalloc(len + 1 + sizeof(struct pike_string_hdr));    t->flags = STRING_NOT_HASHED | STRING_NOT_SHARED;    }    t->refs = 1;    t->str[len]=0;    t->len=len;    t->size_shift=0;    DO_IF_DEBUG(t->next = NULL);    return t;   }   
pike.git/src/stralloc.c:724:    } else if (shift == 1) {    t = (struct pike_string *)alloc_short_pike_string1();   #ifdef PIKE_DEBUG    } else if (shift != 2) {    Pike_fatal("Unsupported string shift: %d\n", shift);   #endif /* PIKE_DEBUG */    } else {    t = (struct pike_string *)alloc_short_pike_string2();    }    } else { -  t=(struct pike_string *)xalloc((len<<shift) + sizeof(struct pike_string)); +  t=(struct pike_string *)xalloc(((len + 1)<<shift) + +  sizeof(struct pike_string_hdr));    t->flags = STRING_NOT_HASHED|STRING_NOT_SHARED;    }    t->refs = 1;    t->len=len;    t->size_shift=shift;    DO_IF_DEBUG(t->next = NULL);    low_set_index(t,len,0);    return t;   }   
pike.git/src/stralloc.c:1094:    LOCK_BUCKET(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 *));    }    UNLOCK_BUCKET(e);    } -  overhead_bytes=(sizeof(struct pike_string)-1)*num_distinct_strings; +  overhead_bytes=sizeof(struct pike_string_hdr)*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,    DO_NOT_WARN((long)overhead_bytes));
pike.git/src/stralloc.c:1567:       if (a->len <= SHORT_STRING_THRESHOLD) {    if (size <= SHORT_STRING_THRESHOLD) {    /* There's already place enough. */    a->len = size;    low_set_index(a, size, 0);    return a;    }    } else if (size > SHORT_STRING_THRESHOLD) {    r=(struct pike_string *)realloc((char *)a, -  sizeof(struct pike_string)+ +  sizeof(struct pike_string_hdr)+    ((size+1)<<a->size_shift));    }       if(!r)    {    r=begin_wide_shared_string(size, a->size_shift);    if (a->len <= size) {    MEMCPY(r->str, a->str, a->len<<a->size_shift);    } else {    MEMCPY(r->str, a->str, size<<a->size_shift);
pike.git/src/stralloc.c:2068:    base_table=0;    num_strings=0;      #ifdef DO_PIKE_CLEANUP    free_all_short_pike_string0_blocks();    free_all_short_pike_string1_blocks();    free_all_short_pike_string2_blocks();   #endif /* DO_PIKE_CLEANUP */   }    + static INLINE size_t memory_in_string (struct pike_string *s) + { +  if (s->len <= SHORT_STRING_THRESHOLD) +  switch (s->size_shift) { +  case 0: return sizeof (struct short_pike_string0); +  case 1: return sizeof (struct short_pike_string1); +  default: return sizeof (struct short_pike_string2); +  } +  else +  return sizeof (struct pike_string_hdr) + ((s->len + 1) << s->size_shift); + } +    void count_memory_in_strings(size_t *num, size_t *size)   {    unsigned INT32 e;    size_t num_=0, size_=0;    if(!base_table)    {    *num=*size=0;    return;    }    size_+=htable_size * sizeof(struct pike_string *);    for(e=0;e<htable_size;e++)    {    struct pike_string *p;    LOCK_BUCKET(e);    for(p=base_table[e];p;p=p->next)    {    num_++; -  size_+=sizeof(struct pike_string)+(p->len<<p->size_shift); +  size_ += memory_in_string (p);    }    UNLOCK_BUCKET(e);    }   #ifdef PIKE_DEBUG    if(num_strings != num_)    Pike_fatal("Num strings is wrong! %d!=%d.\n",num_strings, num_);   #endif    num[0]=num_;    size[0]=size_;   }