pike.git / src / array.c

version» Context lines:

pike.git/src/array.c:12:   #include "interpret.h"   #include "language.h"   #include "error.h"   #include "pike_types.h"   #include "fsort.h"   #include "builtin_functions.h"   #include "pike_memory.h"   #include "gc.h"   #include "main.h"    - RCSID("$Id: array.c,v 1.41 1998/10/14 05:48:44 hubbe Exp $"); + RCSID("$Id: array.c,v 1.42 1998/11/22 11:02:31 hubbe Exp $");      struct array empty_array=   {    1, /* Never free */    &empty_array, /* Next */    &empty_array, /* previous (circular) */    0, /* Size = 0 */    0, /* malloced Size = 0 */    0, /* no types */    0, /* no flags */
pike.git/src/array.c:97:       GC_FREE();   }      /*    * Free an array, call this when the array has zero references    */   void really_free_array(struct array *v)   {    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(v == & empty_array)    fatal("Tried to free the empty_array.\n");   #endif    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(v);   #endif       add_ref(v);    free_svalues(ITEM(v), v->size, v->type_field);    v->refs--;    array_free_no_free(v);   }      void do_free_array(struct array *a)   {    free_array(a);   }      /*    * Extract an svalue from an array    */   void array_index_no_free(struct svalue *s,struct array *v,INT32 index)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(index<0 || index>=v->size)    fatal("Illegal index in low level index routine.\n");   #endif       assign_svalue_no_free(s, ITEM(v) + index);   }      /*    * Extract an svalue from an array    */   void array_index(struct svalue *s,struct array *v,INT32 index)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(index<0 || index>=v->size)    fatal("Illegal index in low level index routine.\n");   #endif       add_ref(v);    assign_svalue(s, ITEM(v) + index);    free_array(v);   }      void simple_array_index_no_free(struct svalue *s,
pike.git/src/array.c:183:    default:    error("Index is not an integer.\n");    }   }      /*    * Extract an svalue from an array    */   void array_free_index(struct array *v,INT32 index)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(index<0 || index>=v->size)    fatal("Illegal index in low level free index routine.\n");   #endif       free_svalue(ITEM(v) + index);   }      /*    * Set an index in an array    */   void array_set_index(struct array *v,INT32 index, struct svalue *s)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(index<0 || index>v->size)    fatal("Illegal index in low level array set routine.\n");   #endif       add_ref(v);    check_destructed(s);       v->type_field = (v->type_field & ~BIT_UNFINISHED) | (1 << s->type);    assign_svalue( ITEM(v) + index, s);    free_array(v);
pike.git/src/array.c:232:    }    }    array_set_index(a,i,s);   }      /*    * Insert an svalue into an array, grow the array if nessesary    */   struct array *array_insert(struct array *v,struct svalue *s,INT32 index)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(index<0 || index>v->size)    fatal("Illegal index in low level insert routine.\n");   #endif       /* Can we fit it into the existing block? */    if(v->refs<=1 && v->malloced_size > v->size)    {    MEMMOVE((char *)(ITEM(v)+index+1),    (char *)(ITEM(v)+index),    (v->size-index) * sizeof(struct svalue));
pike.git/src/array.c:277:    array_set_index(v,index,s);       return v;   }      /*    * resize array, resize an array destructively    */   struct array *resize_array(struct array *a, INT32 size)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(a);   #endif       if(a->size == size) return a;    if(size > a->size)    {    /* We should grow the array */       if(a->malloced_size >= size)    {
pike.git/src/array.c:320:    }   }      /*    * Shrink an array destructively    */   struct array *array_shrink(struct array *v,INT32 size)   {    struct array *a;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(v->refs>2) /* Odd, but has to be two */    fatal("Array shrink on array with many references.\n");       if(size > v->size)    fatal("Illegal argument to array_shrink.\n");   #endif       if(size*2 < v->malloced_size + 4) /* Should we realloc it? */    {    a=allocate_array_no_init(size,0);
pike.git/src/array.c:352:    }   }      /*    * Remove an index from an array and shrink the array    */   struct array *array_remove(struct array *v,INT32 index)   {    struct array *a;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(v->refs>1)    fatal("Array remove on array with many references.\n");       if(index<0 || index >= v->size)    fatal("Illegal argument to array_remove.\n");   #endif       array_free_index(v, index);    if(v->size!=1 &&    v->size*2 + 4 < v->malloced_size ) /* Should we realloc it? */
pike.git/src/array.c:395:    }   }      /*    * Search for in svalue in an array.    * return the index if found, -1 otherwise    */   INT32 array_search(struct array *v, struct svalue *s,INT32 start)   {    INT32 e; - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(start<0)    fatal("Start of find_index is less than zero.\n");   #endif       check_destructed(s);       /* Why search for something that is not there? */ - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(v);   #endif    if(v->type_field & (1 << s->type))    {    if(start)    {    for(e=start;e<v->size;e++)    if(is_eq(ITEM(v)+e,s)) return e;    }else{    TYPE_FIELD t=0;
pike.git/src/array.c:433:   }      /*    * Slice a pice of an array (nondestructively)    * return an array consisting of v[start..end-1]    */   struct array *slice_array(struct array *v,INT32 start,INT32 end)   {    struct array *a;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(start > end || end>v->size || start<0)    fatal("Illegal arguments to slice_array()\n");       if(d_flag > 1) array_check_type_field(v);   #endif       if(start==0 && v->refs==1) /* Can we use the same array? */    {    add_ref(v);    return array_shrink(v,end);
pike.git/src/array.c:462:   }      /*    * Slice a pice of an array (nondestructively)    * return an array consisting of v[start..end-1]    */   struct array *friendly_slice_array(struct array *v,INT32 start,INT32 end)   {    struct array *a;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(start > end || end>v->size || start<0)    fatal("Illegal arguments to slice_array()\n");       if(d_flag > 1) array_check_type_field(v);   #endif       a=allocate_array_no_init(end-start,0);    a->type_field = v->type_field;       assign_svalues_no_free(ITEM(a), ITEM(v)+start, end-start, v->type_field);
pike.git/src/array.c:501:      /*    * Clean an array from destructed objects    */   void check_array_for_destruct(struct array *v)   {    int e;    INT16 types;       types = 0; - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(v);   #endif    if(v->type_field & (BIT_OBJECT | BIT_FUNCTION))    {    for(e=0; e<v->size; e++)    {    if((ITEM(v)[e].type == T_OBJECT ||    (ITEM(v)[e].type == T_FUNCTION &&    ITEM(v)[e].subtype!=FUNCTION_BUILTIN)) &&    (!ITEM(v)[e].u.object->prog))
pike.git/src/array.c:536:      /*    * This function finds the index of any destructed object in a set    * it could be optimized to search out the object part with a binary    * search lookup if the array is mixed    */   INT32 array_find_destructed_object(struct array *v)   {    INT32 e;    TYPE_FIELD types; - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(v);   #endif    if(v->type_field & (BIT_OBJECT | BIT_FUNCTION))    {    types=0;    for(e=0; e<v->size; e++)    {    if((ITEM(v)[e].type == T_OBJECT ||    (ITEM(v)[e].type == T_FUNCTION &&    ITEM(v)[e].subtype!=FUNCTION_BUILTIN)) &&    (!ITEM(v)[e].u.object->prog))    return e;    types |= 1<<ITEM(v)[e].type;    }    v->type_field = types;    } - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(v);   #endif    return -1;   }      static int internal_cmpfun(INT32 *a,    INT32 *b,    cmpfun current_cmpfun,    struct svalue *current_array_p)   {
pike.git/src/array.c:758:    b=c;    else    return c;    }    if(a<v->size && fun(ITEM(v)+a,s)<0) a++;    return ~a;   }      INT32 set_lookup(struct array *a, struct svalue *s)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(a);   #endif    /* face it, it's not there */    if( (((2 << s->type) -1) & a->type_field) == 0)    return -1;       /* face it, it's not there */    if( ((BIT_MIXED << s->type) & BIT_MIXED & a->type_field) == 0)    return ~a->size;       return low_lookup(a,s,set_svalue_cmpfun);   }      INT32 switch_lookup(struct array *a, struct svalue *s)   {    /* face it, it's not there */ - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(a);   #endif    if( (((2 << s->type) -1) & a->type_field) == 0)    return -1;       /* face it, it's not there */    if( ((BIT_MIXED << s->type) & BIT_MIXED & a->type_field) == 0)    return ~a->size;       return low_lookup(a,s,switch_svalue_cmpfun);
pike.git/src/array.c:831:    t=0;       if(v->flags & ARRAY_LVALUE)    {    v->type_field=BIT_MIXED;    return;    }       for(e=0; e<v->size; e++) t |= 1 << ITEM(v)[e].type;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(t & ~(v->type_field))    {    describe(v);    fatal("Type field out of order!\n");    }   #endif    v->type_field = t;   }    - #ifdef DEBUG + #ifdef PIKE_DEBUG   /* Maybe I should have a 'clean' flag for this computation */   void array_check_type_field(struct array *v)   {    int e;    TYPE_FIELD t;       t=0;       if(v->flags & ARRAY_LVALUE)    return;
pike.git/src/array.c:920:    * to maintain the order.    * the first item in this array is the size of the result    * the rest is n >= 0 for a[ n ]    * or n < 0 for b[ ~n ]    */   INT32 * merge(struct array *a,struct array *b,INT32 opcode)   {    INT32 ap,bp,i,*ret,*ptr;       ap=bp=0; - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1)    {    array_check_type_field(a);    array_check_type_field(b);    }   #endif    if(!(a->type_field & b->type_field))    {    /* do smart optimizations */    switch(opcode)    { -  case OP_AND: +  case PIKE_ARRAY_OP_AND:    ret=(INT32 *)xalloc(sizeof(INT32));    *ret=0;    return ret;    -  case OP_SUB: +  case PIKE_ARRAY_OP_SUB:    ptr=ret=(INT32 *)xalloc(sizeof(INT32)*(a->size+1));    *(ptr++)=a->size;    for(i=0;i<a->size;i++) *(ptr++)=i;    return ret;    }    }       ptr=ret=(INT32 *)xalloc(sizeof(INT32)*(a->size + b->size + 1));    ptr++;       while(ap < a->size && bp < b->size)    {    i=set_svalue_cmpfun(ITEM(a)+ap,ITEM(b)+bp);    if(i < 0)    i=opcode >> 8;    else if(i > 0)    i=opcode;    else    i=opcode >> 4;    -  if(i & OP_A) *(ptr++)=ap; -  if(i & OP_B) *(ptr++)=~bp; -  if(i & OP_SKIP_A) ap++; -  if(i & OP_SKIP_B) bp++; +  if(i & PIKE_ARRAY_OP_A) *(ptr++)=ap; +  if(i & PIKE_ARRAY_OP_B) *(ptr++)=~bp; +  if(i & PIKE_ARRAY_OP_SKIP_A) ap++; +  if(i & PIKE_ARRAY_OP_SKIP_B) bp++;    }    -  if((opcode >> 8) & OP_A) while(ap<a->size) *(ptr++)=ap++; -  if(opcode & OP_B) while(bp<b->size) *(ptr++)=~(bp++); +  if((opcode >> 8) & PIKE_ARRAY_OP_A) while(ap<a->size) *(ptr++)=ap++; +  if(opcode & PIKE_ARRAY_OP_B) while(bp<b->size) *(ptr++)=~(bp++);       *ret=(ptr-ret-1);       return ret;   }      /*    * This routine merges two arrays in the order specified by 'zipper'    * zipper normally produced by merge() above    */
pike.git/src/array.c:1039:      int array_equal_p(struct array *a, struct array *b, struct processing *p)   {    struct processing curr;    INT32 e;       if(a == b) return 1;    if(a->size != b->size) return 0;    if(!a->size) return 1;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1)    {    array_check_type_field(a);    array_check_type_field(b);    }   #endif       /* This could be done much better if I KNEW that    * the type fields didn't contain types that    * really aren't in the array
pike.git/src/array.c:1162:    ret=array_zip(tmpa,tmpb,zipper);    free_array(tmpa);    free_array(tmpb);    free((char *)zipper);    return ret;   }      /* subtract an array from another */   struct array *subtract_arrays(struct array *a, struct array *b)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1)    {    array_check_type_field(b);    }   #endif    check_array_for_destruct(a);       if(a->type_field & b->type_field)    { -  return merge_array_with_order(a, b, OP_SUB); +  return merge_array_with_order(a, b, PIKE_ARRAY_OP_SUB);    }else{    if(a->refs == 1)    {    add_ref(a);    return a;    }    return slice_array(a,0,a->size);    }   }      /* and two arrays */   struct array *and_arrays(struct array *a, struct array *b)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1)    {    array_check_type_field(b);    }   #endif    check_array_for_destruct(a);       if(a->type_field & b->type_field)    { -  return merge_array_without_order(a, b, OP_AND); +  return merge_array_without_order(a, b, PIKE_ARRAY_OP_AND);    }else{    return allocate_array_no_init(0,0);    }   }      int check_that_array_is_constant(struct array *a)   {    array_fix_type_field(a);    if(a->type_field & (BIT_FUNCTION | BIT_OBJECT))    return 0;
pike.git/src/array.c:1462:    }    }    return low_end_shared_string(ret);   }      struct array *copy_array_recursively(struct array *a,struct processing *p)   {    struct processing doing;    struct array *ret;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(a);   #endif       doing.next=p;    doing.pointer_a=(void *)a;    for(;p;p=p->next)    {    if(p->pointer_a == (void *)a)    {    ret=(struct array *)p->pointer_b;
pike.git/src/array.c:1529:      void array_replace(struct array *a,    struct svalue *from,    struct svalue *to)   {    INT32 i = -1;       while((i=array_search(a,from,i+1)) >= 0) array_set_index(a,i,to);   }    - #ifdef DEBUG + #ifdef PIKE_DEBUG   void check_array(struct array *a)   {    INT32 e;       if(a->next->prev != a)    fatal("Array check: a->next->prev != a\n");       if(a->size > a->malloced_size)    fatal("Array is larger than malloced block!\n");   
pike.git/src/array.c:1566: Inside #if defined(DEBUG)
   a=&empty_array;    do    {    check_array(a);       a=a->next;    if(!a)    fatal("Null pointer in array list.\n");    } while (a != & empty_array);   } - #endif /* DEBUG */ + #endif /* PIKE_DEBUG */         void gc_mark_array_as_referenced(struct array *a)   {    if(gc_mark(a))    if(a->type_field & BIT_COMPLEX)    gc_mark_svalues(ITEM(a), a->size);   }      void gc_check_all_arrays(void)   {    struct array *a;    a=&empty_array;    do    { - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(a);   #endif    if(a->type_field & BIT_COMPLEX)    {    TYPE_FIELD t;    t=debug_gc_check_svalues(ITEM(a), a->size, T_ARRAY, a);       /* Ugly, but we are not allowed to change type_field    * at the same time as the array is being built...    * Actually we just need beter primitives for building arrays.
pike.git/src/array.c:1644:       free_array(a);    a=next;    }else{    a=a->next;    }    } while (a != & empty_array);   }       - #ifdef DEBUG + #ifdef PIKE_DEBUG      void debug_dump_type_field(TYPE_FIELD t)   {    int e;    for(e=0;e<=MAX_TYPE;e++)    if(t & (1<<e))    fprintf(stderr," %s",get_name_of_type(e));       for(;e<16;e++)    if(t & (1<<e))
pike.git/src/array.c:1683:         void zap_all_arrays(void)   {    struct array *a,*next;       a=&empty_array;    do    {    - #if defined(DEBUG) && defined(DEBUG_MALLOC) + #if defined(PIKE_DEBUG) && defined(DEBUG_MALLOC)    if(verbose_debug_exit && a!=&empty_array)    describe(a);   #endif       add_ref(a);    free_svalues(ITEM(a), a->size, a->type_field);    a->size=0;       if(!(next=a->next))    fatal("Null pointer in array list.\n");
pike.git/src/array.c:1791:    b->type_field);    size+=b->size;    }    ret->type_field|=ITEM(a)[e].u.array->type_field;    assign_svalues_no_free(ITEM(ret)+size,    ITEM(ITEM(a)[e].u.array),    ITEM(a)[e].u.array->size,    ITEM(a)[e].u.array->type_field);    size+=ITEM(a)[e].u.array->size;    } - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(size != ret->size)    fatal("Implode_array failed miserably\n");   #endif    return ret;   }