pike.git / src / array.c

version» Context lines:

pike.git/src/array.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: array.c,v 1.158 2004/04/06 15:37:54 nilsson Exp $ + || $Id: array.c,v 1.159 2004/09/16 12:04:57 grubba Exp $   */      #include "global.h"   #include "svalue.h"   #include "array.h"   #include "object.h"   #include "las.h"   #include "stralloc.h"   #include "interpret.h"   #include "opcodes.h"
pike.git/src/array.c:18:   #include "fsort.h"   #include "builtin_functions.h"   #include "pike_memory.h"   #include "gc.h"   #include "main.h"   #include "pike_security.h"   #include "stuff.h"   #include "bignum.h"   #include "cyclic.h"   #include "multiset.h" + #include "mapping.h"    - RCSID("$Id: array.c,v 1.158 2004/04/06 15:37:54 nilsson Exp $"); + RCSID("$Id: array.c,v 1.159 2004/09/16 12:04:57 grubba Exp $");      PMOD_EXPORT struct array empty_array=   {    PIKE_CONSTANT_MEMOBJ_INIT(1), /* Never free */    &weak_empty_array, /* Next */    0, /* previous */    0, /* Size = 0 */    0, /* malloced Size = 0 */    0, /* no types */    0, /* no flags */
pike.git/src/array.c:83:   {    struct array *v;    ptrdiff_t e;       if(size+extra_space == 0)    {    add_ref(&empty_array);    return &empty_array;    }    +  if( (size+extra_space-1) > +  (ULONG_MAX-sizeof(struct array))/sizeof(struct svalue) ) +  Pike_error("Too large array (memory size exceeds size of size_t)\n");    v=(struct array *)malloc(sizeof(struct array)+    (size+extra_space-1)*sizeof(struct svalue));    if(!v) -  Pike_error("Couldn't allocate array, out of memory.\n"); +  Pike_error(msg_out_of_mem);       GC_ALLOC(v);          if (size)    /* 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;
pike.git/src/array.c:392:    free_array(v);    v=ret;    }       array_set_index(v,index,s);       return v;   }      /* +  * Shrink an array destructively +  */ + PMOD_EXPORT struct array *array_shrink(struct array *v, ptrdiff_t size) + { +  struct array *a; +  + #ifdef PIKE_DEBUG +  if(v->refs>2) /* Odd, but has to be two */ +  Pike_fatal("Array shrink on array with many references.\n"); +  +  if(size > v->size) +  Pike_fatal("Illegal argument to array_shrink.\n"); + #endif +  +  if(!size || (size*4 < v->malloced_size + 4)) /* Should we realloc it? */ +  { +  a = array_set_flags(allocate_array_no_init(size, 0), v->flags); +  if (a->size) { +  a->type_field = v->type_field; +  } +  +  free_svalues(ITEM(v) + size, v->size - size, v->type_field); +  MEMCPY(ITEM(a), ITEM(v), size*sizeof(struct svalue)); +  v->size=0; +  free_array(v); +  return a; +  }else{ +  free_svalues(ITEM(v) + size, v->size - size, v->type_field); +  v->size=size; +  return v; +  } + } +  + /*    * resize array, resize an array destructively    */   PMOD_EXPORT struct array *resize_array(struct array *a, INT32 size)   {   #ifdef PIKE_DEBUG    if(d_flag > 1) array_check_type_field(a);   #endif       if(a->size == size) return a;    if(size > a->size)
pike.git/src/array.c:415:    if(a->malloced_size >= size)    {    for(;a->size < size; a->size++)    {    ITEM(a)[a->size].type=T_INT;    ITEM(a)[a->size].subtype=NUMBER_NUMBER;    ITEM(a)[a->size].u.integer=0;    }    a->type_field |= BIT_INT;    return a; -  }else{ +  } else {    struct array *ret; -  ret=low_allocate_array(size, (size>>1) + 4); -  MEMCPY(ITEM(ret),ITEM(a),sizeof(struct svalue)*a->size); +  ret = array_set_flags(low_allocate_array(size, (size>>1) + 4), +  a->flags); +  MEMCPY(ITEM(ret), ITEM(a), sizeof(struct svalue)*a->size);    ret->type_field = DO_NOT_WARN((TYPE_FIELD)(a->type_field | BIT_INT));    a->size=0;    free_array(a);    return ret;    } -  }else{ -  /* We should shrink the array */ -  free_svalues(ITEM(a)+size, a->size - size, a->type_field); -  a->size = size; -  return a; +  } else { +  return array_shrink(a, size);    }   }      /* -  * Shrink an array destructively -  */ - PMOD_EXPORT struct array *array_shrink(struct array *v, ptrdiff_t size) - { -  struct array *a; -  - #ifdef PIKE_DEBUG -  if(v->refs>2) /* Odd, but has to be two */ -  Pike_fatal("Array shrink on array with many references.\n"); -  -  if(size > v->size) -  Pike_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); -  a->type_field = v->type_field; -  -  free_svalues(ITEM(v) + size, v->size - size, v->type_field); -  MEMCPY(ITEM(a), ITEM(v), size*sizeof(struct svalue)); -  v->size=0; -  free_array(v); -  return a; -  }else{ -  free_svalues(ITEM(v) + size, v->size - size, v->type_field); -  v->size=size; -  return v; -  } - } -  - /* +     * Remove an index from an array and shrink the array    */   PMOD_EXPORT struct array *array_remove(struct array *v,INT32 index)   {    struct array *a;      #ifdef PIKE_DEBUG    if(index<0 || index >= v->size)    Pike_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? */ +  if (v->size == 1) { +  v->size = 0; +  /* NOTE: The following uses the fact that array_set_flags() +  * will reallocate the array if it has zero size! +  */ +  return array_set_flags(v, v->flags); +  } else if(v->size*4 + 4 < v->malloced_size ) /* Should we realloc it? */    { -  a=allocate_array_no_init(v->size-1, 0); +  a = array_set_flags(allocate_array_no_init(v->size-1, 0), v->flags);    a->type_field = v->type_field;       if(index>0)    MEMCPY(ITEM(a), ITEM(v), index*sizeof(struct svalue));    if(v->size-index>1)    MEMCPY(ITEM(a)+index,    ITEM(v)+index+1,    (v->size-index-1)*sizeof(struct svalue));    v->size=0;    free_array(v);    return a; -  }else{ +  } else {    if(v->size-index>1)    {    MEMMOVE((char *)(ITEM(v)+index),    (char *)(ITEM(v)+index+1),    (v->size-index-1)*sizeof(struct svalue));    }    v->size--;    return v;    }   }
pike.git/src/array.c:549:    if(is_eq(ITEM(v)+e,s)) return e;    t |= 1<<ITEM(v)[e].type;    }    v->type_field=t;    }    }    return -1;   }      /* -  * Slice a pice of an array (nondestructively) +  * Slice a piece of an array (nondestructively)    * return an array consisting of v[start..end-1]    */   PMOD_EXPORT struct array *slice_array(struct array *v, ptrdiff_t start,    ptrdiff_t end)   {    struct array *a;      #ifdef PIKE_DEBUG    if(start > end || end>v->size || start<0)    Pike_fatal("Illegal arguments to slice_array()\n");
pike.git/src/array.c:587: Inside #if 1 and #if defined(PIKE_DEBUG)
  #ifdef PIKE_DEBUG    if(d_flag>1)    check_array(v);   #endif    return v;    }    }   #endif       a=allocate_array_no_init(end-start,0); +  if (end-start) {    a->type_field = v->type_field;       assign_svalues_no_free(ITEM(a), ITEM(v)+start, end-start, v->type_field); -  +  }       return a;   }      /* -  * Slice a pice of an array (nondestructively) +  * Slice a piece of an array (nondestructively)    * return an array consisting of v[start..end-1]    */   PMOD_EXPORT struct array *friendly_slice_array(struct array *v,    ptrdiff_t start,    ptrdiff_t end)   {    struct array *a;      #ifdef PIKE_DEBUG    if(start > end || end>v->size || start<0)