pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:530:      /* 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. */    + #define SHORT_STRING_BLOCK 256 + #define SHORT_STRING_THRESHOLD 15 /* % 4 === -1 */ +  + struct short_pike_string0 { +  PIKE_STRING_CONTENTS; +  p_wchar0 str[SHORT_STRING_THRESHOLD+1]; + }; +  + struct short_pike_string1 { +  PIKE_STRING_CONTENTS; +  p_wchar1 str[SHORT_STRING_THRESHOLD+1]; + }; +  + struct short_pike_string2 { +  PIKE_STRING_CONTENTS; +  p_wchar2 str[SHORT_STRING_THRESHOLD+1]; + }; +  + #ifndef PIKE_NEW_BLOCK_ALLOC   /* Use the BLOCK_ALLOC() stuff for short strings */      #undef INIT_BLOCK   #ifdef ATOMIC_SVALUE   #define INIT_BLOCK(NEW_STR) do { \    (NEW_STR)->ref_type = T_STRING; \    (NEW_STR)->refs = 0; \    add_ref((NEW_STR)); \    (NEW_STR)->flags = \    STRING_NOT_HASHED|STRING_NOT_SHARED|STRING_IS_SHORT; \    } while(0)   #else /* !ATOMIC_SVALUE */   #define INIT_BLOCK(NEW_STR) do { \    (NEW_STR)->refs = 0; \    add_ref((NEW_STR)); \    (NEW_STR)->flags = \    STRING_NOT_HASHED|STRING_NOT_SHARED|STRING_IS_SHORT; \    } while(0)   #endif    - #define SHORT_STRING_BLOCK 256 - #define SHORT_STRING_THRESHOLD 15 /* % 4 === -1 */ -  - struct short_pike_string0 { -  PIKE_STRING_CONTENTS; -  p_wchar0 str[SHORT_STRING_THRESHOLD+1]; - }; -  - struct short_pike_string1 { -  PIKE_STRING_CONTENTS; -  p_wchar1 str[SHORT_STRING_THRESHOLD+1]; - }; -  - struct short_pike_string2 { -  PIKE_STRING_CONTENTS; -  p_wchar2 str[SHORT_STRING_THRESHOLD+1]; - }; -  +    BLOCK_ALLOC(short_pike_string0, SHORT_STRING_BLOCK)   BLOCK_ALLOC(short_pike_string1, SHORT_STRING_BLOCK)   BLOCK_ALLOC(short_pike_string2, SHORT_STRING_BLOCK)      #undef INIT_BLOCK   #define INIT_BLOCK(x)      #define really_free_short_pike_string(s) do { \    if (!s->size_shift) { \    really_free_short_pike_string0((struct short_pike_string0 *)s); \
pike.git/src/stralloc.c:589:    really_free_short_pike_string1((struct short_pike_string1 *)s); \    DO_IF_DEBUG( \    } else if (s->size_shift != 2) { \    Pike_fatal("Unsupported string shift: %d\n", s->size_shift); \    ) \    } else { \    really_free_short_pike_string2((struct short_pike_string2 *)s); \    } \    } while(0)    + #else + #include "gjalloc.h" +  + static struct block_allocator string_allocator[] = { +  BA_INIT(sizeof(struct short_pike_string0), SHORT_STRING_BLOCK), +  BA_INIT(sizeof(struct short_pike_string1), SHORT_STRING_BLOCK), +  BA_INIT(sizeof(struct short_pike_string2), SHORT_STRING_BLOCK) + }; +  + #define really_free_short_pike_string(s) do { \ +  DO_IF_DEBUG( \ +  if (s->size_shift > 2) \ +  Pike_fatal("Unsupported string shift: %d\n", s->size_shift); \ +  ) \ +  ba_free(string_allocator + s->size_shift, s); \ +  } while(0) + #endif +    #define free_unlinked_pike_string(s) do { \    if (s->len <= SHORT_STRING_THRESHOLD) { \    really_free_short_pike_string(s); \    } else { \    debug_free((char *)s, DMALLOC_LOCATION(), 1); \    } \    } while(0)      /* note that begin_shared_string expects the _exact_ size of the string,    * not the maximum size    */   PMOD_EXPORT struct pike_string *debug_begin_shared_string(size_t len)   {    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) { -  + #ifndef PIKE_NEW_BLOCK_ALLOC    t=(struct pike_string *)alloc_short_pike_string0(); -  + #else +  t=(struct pike_string *)ba_alloc(string_allocator); +  t->flags = STRING_NOT_HASHED | STRING_NOT_SHARED | STRING_IS_SHORT; + #endif    } else {    t=(struct pike_string *)xalloc(len + 1 + sizeof(struct pike_string_hdr));    t->flags = STRING_NOT_HASHED | STRING_NOT_SHARED;    }   #ifdef ATOMIC_SVALUE    t->ref_type = T_STRING;   #endif    t->refs = 0;    add_ref(t); /* For DMALLOC */    t->str[len]=0;
pike.git/src/stralloc.c:738:      PMOD_EXPORT struct pike_string *debug_begin_wide_shared_string(size_t len, int shift)   {    struct pike_string *t = NULL;   #ifdef PIKE_DEBUG    extern int d_flag;    if(d_flag>10)    verify_shared_strings_tables();   #endif    if (len <= SHORT_STRING_THRESHOLD) { + #ifndef PIKE_NEW_BLOCK_ALLOC    if (!shift) {    t = (struct pike_string *)alloc_short_pike_string0();    } 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 + #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+shift); +  t->flags = STRING_NOT_HASHED|STRING_NOT_SHARED|STRING_IS_SHORT; + #endif    } else {    t=(struct pike_string *)xalloc(((len + 1)<<shift) +    sizeof(struct pike_string_hdr));    t->flags = STRING_NOT_HASHED|STRING_NOT_SHARED;    }   #ifdef ATOMIC_SVALUE    t->ref_type = T_STRING;   #endif    t->refs = 0;    add_ref(t); /* For DMALLOC */
pike.git/src/stralloc.c:2068:    }    generic_memcpy(r,MKPCHARP(s,str->size_shift),(end-s)>>str->size_shift);       CALL_AND_UNSET_ONERROR (mojt_uwp);    return end_shared_string(ret);   }      /*** init/exit memory ***/   void init_shared_string_table(void)   { + #ifndef PIKE_NEW_BLOCK_ALLOC    init_short_pike_string0_blocks();    init_short_pike_string1_blocks();    init_short_pike_string2_blocks(); -  + #endif    for(hashprimes_entry=0;hashprimes[hashprimes_entry]<BEGIN_HASH_SIZE;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);    }
pike.git/src/stralloc.c:2165:    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_short_pike_strings(size_t *num, size_t *size)   {    size_t num_=0, size_=0; + #ifndef PIKE_NEW_BLOCK_ALLOC    count_memory_in_short_pike_string0s(num, size);    count_memory_in_short_pike_string1s(&num_, &size_); -  + #else +  ba_count_all(string_allocator, num, size); +  ba_count_all(string_allocator+1, &num_, &size_); + #endif    *num += num_;    *size += size_; -  + #ifndef PIKE_NEW_BLOCK_ALLOC    count_memory_in_short_pike_string2s(&num_, &size_); -  + #else +  ba_count_all(string_allocator+2, &num_, &size_); + #endif    *num += num_;    *size += size_;   }      void count_memory_in_strings(size_t *num, size_t *size)   {    unsigned INT32 e;    size_t num_=0, size_=0;    if(!base_table)    {