pike.git / src / array.c

version» Context lines:

pike.git/src/array.c:15:   #include "pike_error.h"   #include "pike_types.h"   #include "fsort.h"   #include "builtin_functions.h"   #include "pike_memory.h"   #include "gc.h"   #include "main.h"   #include "cyclic.h"   #include "multiset.h"   #include "mapping.h" + #include "bignum.h" + #include "pike_search.h"      /** The empty array. */   PMOD_EXPORT struct array empty_array=   {    PIKE_CONSTANT_MEMOBJ_INIT(1, PIKE_T_ARRAY), /* Never free */    0, /* Size = 0 */    0, /* malloced Size = 0 */    0, /* no types */    0, /* no flags */    &weak_empty_array, /* Next */
pike.git/src/array.c:74:    *    * @param size The size of the new array, in elements.    * @param extra_space The number of extra elements space    * should be reserved for.    * @return A pointer to the allocated array struct.    */   PMOD_EXPORT struct array *real_allocate_array(ptrdiff_t size,    ptrdiff_t extra_space)   {    struct array *v; +  size_t length = size;    -  if(size+extra_space == 0) +  if (DO_SIZE_T_ADD_OVERFLOW(length, (size_t)extra_space, &length)) goto TOO_BIG; +  +  if(length == 0)    {    add_ref(&empty_array);    return &empty_array;    }    -  /* Limits size to (1<<29)-4 */ -  if( (size_t)(size+extra_space-1) > -  (LONG_MAX-sizeof(struct array))/sizeof(struct svalue) ) -  Pike_error("Too large array (size %ld exceeds %ld).\n", -  (long)(size+extra_space-1), -  (long)((LONG_MAX-sizeof(struct array))/sizeof(struct svalue)) ); -  v=malloc(sizeof(struct array)+ -  (size+extra_space-1)*sizeof(struct svalue)); -  if(!v) -  Pike_error(msg_out_of_mem_2, sizeof(struct array)+ -  (size+extra_space-1)*sizeof(struct svalue)); +  /* +  * Do we really need this limit? +  * - arne +  */ +  if (length > 1U<<29) goto TOO_BIG;    -  GC_ALLOC(v); +  /* struct array contains one svalue already */ +  length --;    -  +  if (DO_SIZE_T_MUL_OVERFLOW(length, sizeof(struct svalue), &length) || +  DO_SIZE_T_ADD_OVERFLOW(length, sizeof(struct array), &length)) goto TOO_BIG;    -  if (size+extra_space) +  v=xcalloc(length, 1); +  +  GC_ALLOC(v); +  gc_init_marker(v); +     /* for now, we don't know what will go in here */    v->type_field = BIT_MIXED | BIT_UNFINISHED; -  else -  v->type_field = 0; -  v->flags=0; +        v->malloced_size = (INT32)(size + extra_space);    v->item=v->real_item;    v->size = (INT32)size;    INIT_PIKE_MEMOBJ(v, T_ARRAY);    DOUBLELINK (first_array, v);    -  { -  struct svalue *item = ITEM(v); -  struct svalue *item_end = item + v->size; -  while (item < item_end) -  *item++ = svalue_int_zero; -  } -  +     return v; -  + TOO_BIG: +  Pike_error("Too large array (size %ld is too big).\n", length);   }      /**    * Free an array without freeing the values inside it.    * Any values inside of the array will be kept.    * @param v The array to be freed.    */   static void array_free_no_free(struct array *v)   {    DOUBLEUNLINK (first_array, v);
pike.git/src/array.c:291:   {    switch(TYPEOF(*ind))    {    case T_INT: {    INT_TYPE p = ind->u.integer;    INT_TYPE i = p < 0 ? p + a->size : p;    if(i<0 || i>=a->size) {    struct svalue tmp;    SET_SVAL(tmp, T_ARRAY, 0, array, a);    if (a->size) { -  index_error(0,0,0,&tmp,ind, +  index_error(0,0,&tmp,ind,    "Index %"PRINTPIKEINT"d is out of array range "    "%d..%d.\n", p, -a->size, a->size-1);    } else { -  index_error(0,0,0,&tmp,ind, +  index_error(0,0,&tmp,ind,    "Attempt to index the empty array with %"PRINTPIKEINT"d.\n", p);    }    }    array_index_no_free(s,a,i);    break;    }       case T_STRING:    {    SET_SVAL(*s, T_ARRAY, 0, array, array_column(a, ind, 0));    break;    }       default:    {    struct svalue tmp;    SET_SVAL(tmp, T_ARRAY, 0, array, a); -  index_error(0,0,0,&tmp,ind,"Array index is neither int nor string.\n"); +  index_error(0,0,&tmp,ind,"Array index is neither int nor string.\n");    }    }   }      /**    * Extract an svalue from an array.    */   PMOD_EXPORT void array_free_index(struct array *v,INT32 index)   {   #ifdef PIKE_DEBUG
pike.git/src/array.c:373:    assign_lvalue(Pike_sp-2, s);    }    pop_n_elems(2);    break;    }       default:    {    struct svalue tmp;    SET_SVAL(tmp, T_ARRAY, 0, array, a); -  index_error(0,0,0,&tmp,ind,"Array index is neither int nor string.\n"); +  index_error(0,0,&tmp,ind,"Array index is neither int nor string.\n");    }    }   }      /**    * Insert an svalue into an array and grow the array if necessary.    */   PMOD_EXPORT struct array *array_insert(struct array *v,struct svalue *s,INT32 index)   {   #ifdef PIKE_DEBUG
pike.git/src/array.c:1415:    return ~a->size;    }       return low_lookup(a,s,switch_svalue_cmpfun);   }         /**    * Reorganize an array in the order specified by 'order'.    */ - PMOD_EXPORT struct array *order_array(struct array *v, INT32 *order) + PMOD_EXPORT struct array *order_array(struct array *v, const INT32 *order)   {    reorder((char *)ITEM(v),v->size,sizeof(struct svalue),order);    return v;   }         /**    * Copy and reorganize an array.    */ - PMOD_EXPORT struct array *reorder_and_copy_array(struct array *v, INT32 *order) + PMOD_EXPORT struct array *reorder_and_copy_array(const struct array *v, const INT32 *order)   {    INT32 e;    struct array *ret;    ret=allocate_array_no_init(v->size, 0);    ret->type_field = v->type_field;       for(e=0;e<v->size;e++)    assign_svalue_no_free(ITEM(ret)+e, ITEM(v)+order[e]);       return ret;
pike.git/src/array.c:2386:    ret->size=0;       mojt=compile_memsearcher(MKPCHARP_STR(del),    del->len,    str->len,    del);    SET_ONERROR (uwp, do_free_object, mojt.container);       switch(str->size_shift)    { -  case 0: f=(explode_searchfunc)mojt.vtab->func0; break; -  case 1: f=(explode_searchfunc)mojt.vtab->func1; break; -  case 2: f=(explode_searchfunc)mojt.vtab->func2; break; +  case eightbit: f=(explode_searchfunc)mojt.vtab->func0; break; +  case sixteenbit: f=(explode_searchfunc)mojt.vtab->func1; break; +  case thirtytwobit: f=(explode_searchfunc)mojt.vtab->func2; break; +  default: Pike_fatal("Invalid size_shift: %d.\n", str->size_shift);    }       while((tmp = f(mojt.data, s, (end-s)>> str->size_shift)))    {    if(ret->size == ret->malloced_size)    {    e=ret->size;    ACCEPT_UNFINISHED_TYPE_FIELDS {    ret=resize_array(ret, e * 2);    } END_ACCEPT_UNFINISHED_TYPE_FIELDS;
pike.git/src/array.c:2672:    * @param a The weak array to be garbage collected.    * @return The number of freed elements.    *    * @see do_gc    */   ptrdiff_t do_gc_weak_array(struct array *a)   {    INT32 e;    ptrdiff_t res = 0;    -  if (!a->flags & ARRAY_WEAK_FLAG) { +  if (!(a->flags & ARRAY_WEAK_FLAG)) {    return 0;    }       for (e = 0; e < a->size; e++) {    struct svalue *s = ITEM(a) + e;    if (!REFCOUNTED_TYPE(TYPEOF(*s)) || (*s->u.refs > 1)) {    continue;    }    /* NB: cf svalue.c:ZAP_SVALUE(). */    free_svalue(s);
pike.git/src/array.c:2791:    if (a->flags & ARRAY_WEAK_FLAG) {    gc_check_weak_svalues(ITEM(a), a->size);    gc_checked_as_weak(a);    }    else    gc_check_svalues(ITEM(a), a->size);    }    } GC_LEAVE;   }    - void gc_mark_array_as_referenced(struct array *a) + PMOD_EXPORT void gc_mark_array_as_referenced(struct array *a)   {    if(gc_mark(a, T_ARRAY))    GC_ENTER (a, T_ARRAY) {    if (a == gc_mark_array_pos)    gc_mark_array_pos = a->next;    if (a == gc_internal_array)    gc_internal_array = a->next;    else {    DOUBLEUNLINK (first_array, a);    DOUBLELINK (first_array, a); /* Linked in first. */
pike.git/src/array.c:2836:    a->type_field = t;    else    a->type_field |= t;    }    gc_assert_checked_as_nonweak(a);    }    }    } GC_LEAVE;   }    - void real_gc_cycle_check_array(struct array *a, int weak) + PMOD_EXPORT void real_gc_cycle_check_array(struct array *a, int weak)   {    GC_CYCLE_ENTER(a, T_ARRAY, weak) {   #ifdef PIKE_DEBUG    if (!gc_destruct_everything &&    (a == &empty_array || a == &weak_empty_array))    Pike_fatal("Trying to gc cycle check some *_empty_array.\n");   #endif       if (a->type_field & BIT_COMPLEX)    {