pike.git / src / mapping.c

version» Context lines:

pike.git/src/mapping.c:1:   /*\   ||| This file a part of Pike, and is copyright by Fredrik Hubinette   ||| Pike is distributed as GPL (General Public License)   ||| See the files COPYING and DISCLAIMER for more information.   \*/   #include "global.h" - RCSID("$Id: mapping.c,v 1.36 1998/08/05 22:48:36 hubbe Exp $"); + RCSID("$Id: mapping.c,v 1.37 1998/11/22 11:03:02 hubbe Exp $");   #include "main.h"   #include "object.h"   #include "mapping.h"   #include "svalue.h"   #include "array.h"   #include "pike_macros.h"   #include "language.h"   #include "error.h"   #include "pike_memory.h"   #include "dynamic_buffer.h"
pike.git/src/mapping.c:20:   #include "gc.h"   #include "stralloc.h"      #define AVG_LINK_LENGTH 4   #define MIN_LINK_LENGTH 1   #define MAP_SLOTS(X) ((X)?((X)+((X)>>4)+8):0)      struct mapping *first_mapping;       - #ifdef DEBUG + #ifdef PIKE_DEBUG   /* This function checks that the type field isn't lacking any bits.    * It is used for debugging purposes only.    */   static void check_mapping_type_fields(struct mapping *m)   {    INT32 e;    struct keypair *k,**prev;    TYPE_FIELD ind_types, val_types;       ind_types=val_types=0;
pike.git/src/mapping.c:57:   /* This function allocates the hash table and svalue space for a mapping    * struct. The size is the max number of indices that can fit in the    * allocated space.    */   static void init_mapping(struct mapping *m, INT32 size)   {    char *tmp;    INT32 e;    INT32 hashsize,hashspace;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(size < 0) fatal("init_mapping with negative value.\n");   #endif    if(size)    {    hashsize=size / AVG_LINK_LENGTH + 1;    if(!(hashsize & 1)) hashsize++;    hashspace=hashsize+1;    e=sizeof(struct keypair)*size+ sizeof(struct keypair *)*hashspace;    tmp=(char *)xalloc(e);   
pike.git/src/mapping.c:118:    return m;   }      /* This function should only be called by the free_mapping() macro.    * It frees the storate used by the mapping.    */   void really_free_mapping(struct mapping *m)   {    INT32 e;    struct keypair *k; - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(m->refs)    fatal("really free mapping on mapping with nonzero refs.\n");   #endif       MAPPING_LOOP(m)    {    free_svalue(& k->val);    free_svalue(& k->ind);    }   
pike.git/src/mapping.c:172:    m->ind_types |= 1 << p->ind.type;    m->val_types |= 1 << p->val.type;   }      /* This function re-allocates a mapping. It adjusts the max no. of    * values can be fitted into the mapping. It takes a bit of time to    * run, but is used seldom enough not to degrade preformance significantly.    */   static struct mapping *rehash(struct mapping *m, int new_size)   { - #ifdef DEBUG + #ifdef PIKE_DEBUG    INT32 tmp=m->size;   #endif    INT32 e,hashsize;    struct keypair *k,**hash;       hashsize=m->hashsize;    hash=m->hash;       init_mapping(m, new_size);       if(hash)    {    for(e=0;e<hashsize;e++)    mapping_rehash_backwards(m, hash[e]);       free((char *)hash);    }    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(m->size != tmp)    fatal("Rehash failed, size not same any more.\n");   #endif    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       return m;   }      /* This function brings the type fields in the mapping up to speed.    * I only use it when the type fields MUST be correct, which is not    * very often.    */
pike.git/src/mapping.c:221:    TYPE_FIELD ind_types, val_types;       val_types = ind_types = 0;       MAPPING_LOOP(m)    {    val_types |= 1 << k->val.type;    ind_types |= 1 << k->ind.type;    }    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(val_types & ~(m->val_types))    fatal("Mapping value types out of order!\n");       if(ind_types & ~(m->ind_types))    fatal("Mapping indices types out of order!\n");   #endif    m->val_types = val_types;    m->ind_types = ind_types;   }   
pike.git/src/mapping.c:247:    struct svalue *val)   {    unsigned INT32 h,h2;    struct keypair *k, **prev;       h2=hash_svalue(key);    if(m->hashsize)    {    h=h2 % m->hashsize;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif    if(m->ind_types & (1 << key->type))    {    for(prev= m->hash + h;(k=*prev);prev=&k->next)    {    if(is_eq(& k->ind, key))    {    *prev=k->next;    k->next=m->hash[h];    m->hash[h]=k;       m->val_types |= 1 << val->type;    assign_svalue(& k->val, val);    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif    return;    }    }    }    }else{    h=0;    }   
pike.git/src/mapping.c:290:       m->free_list=k->next;    k->next=m->hash[h];    m->hash[h]=k;    m->ind_types |= 1 << key->type;    m->val_types |= 1 << val->type;    assign_svalue_no_free(& k->ind, key);    assign_svalue_no_free(& k->val, val);    m->size++;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif   }      union anything *mapping_get_item_ptr(struct mapping *m,    struct svalue *key,    TYPE_T t)   {    unsigned INT32 h, h2;    struct keypair *k, **prev;       h2=hash_svalue(key);       if(m->hashsize)    {    h=h2 % m->hashsize;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       for(prev= m->hash + h;(k=*prev);prev=&k->next)    {    if(is_eq(& k->ind, key))    {    *prev=k->next;    k->next=m->hash[h];    m->hash[h]=k;       if(k->val.type == t) return & ( k->val.u );    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       return 0;    }    }    }else{    h=0;    }   
pike.git/src/mapping.c:353:    assign_svalue_no_free(& k->ind, key);    k->val.type=T_INT;    k->val.subtype=NUMBER_NUMBER;    k->val.u.integer=0;    m->ind_types |= 1 << key->type;    m->val_types |= BIT_INT;    m->size++;       if(t != T_INT) return 0;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       return & ( k->val.u );   }      void map_delete(struct mapping *m,    struct svalue *key)   {    unsigned INT32 h;
pike.git/src/mapping.c:384:    *prev=k->next;    free_svalue(& k->ind);    free_svalue(& k->val);    k->next=m->free_list;    m->free_list=k;    m->size--;       if(m->size < (m->hashsize + 1) * MIN_LINK_LENGTH)    rehash(m, MAP_SLOTS(m->size));    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif    return;    }    }   }      void check_mapping_for_destruct(struct mapping *m)   {    INT32 e;    struct keypair *k, **prev;    TYPE_FIELD ind_types, val_types;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       if(!m->size) return;       if((m->ind_types | m->val_types) & (BIT_OBJECT | BIT_FUNCTION))    {    val_types = ind_types = 0;    m->val_types |= BIT_INT;    for(e=0;e<m->hashsize;e++)
pike.git/src/mapping.c:435:    ind_types |= 1 << k->ind.type;    prev=&k->next;    }    }    }    if(MAP_SLOTS(m->size) < m->hashsize * MIN_LINK_LENGTH)    rehash(m, MAP_SLOTS(m->size));       m->val_types = val_types;    m->ind_types = ind_types; - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif    }   }      struct svalue *low_mapping_lookup(struct mapping *m,    struct svalue *key)   {    unsigned INT32 h;    struct keypair *k, **prev;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif    if(!m->size) return 0;       if((1 << key->type) & m->ind_types)    {    h=hash_svalue(key) % m->hashsize;       for(prev= m->hash + h;(k=*prev);prev=&k->next)    {
pike.git/src/mapping.c:533:    struct svalue *s;    struct keypair *k;       a=allocate_array(m->size);    s=ITEM(a);       MAPPING_LOOP(m) assign_svalue(s++, & k->ind);       a->type_field = m->ind_types;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       return a;   }      struct array *mapping_values(struct mapping *m)   {    INT32 e;    struct keypair *k;    struct array *a;    struct svalue *s;       a=allocate_array(m->size);    s=ITEM(a);       MAPPING_LOOP(m) assign_svalue(s++, & k->val);       a->type_field = m->val_types;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       return a;   }      struct array *mapping_to_array(struct mapping *m)   {    INT32 e;    struct keypair *k;
pike.git/src/mapping.c:594:   {    INT32 e;    struct keypair *k;       MAPPING_LOOP(m)    if(is_eq(& k->val, from))    assign_svalue(& k->val, to);       m->val_types |= 1 << to->type;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif      }      struct mapping *mkmapping(struct array *ind, struct array *val)   {    struct mapping *m;    struct svalue *i,*v;    INT32 e;    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(ind->size != val->size)    fatal("mkmapping on different sized arrays.\n");   #endif       m=allocate_mapping(MAP_SLOTS(ind->size));    i=ITEM(ind);    v=ITEM(val);    for(e=0;e<ind->size;e++) mapping_insert(m, i++, v++);       return m;
pike.git/src/mapping.c:925:    k=m->hash[++h];    }    }       to->type=T_INT;    to->subtype=NUMBER_UNDEFINED;    to->u.integer=0;   }       - #ifdef DEBUG + #ifdef PIKE_DEBUG      void check_mapping(struct mapping *m)   {    int e,num;    struct keypair *k;       if(m->refs <=0)    fatal("Mapping has zero refs.\n");       if(m->next && m->next->prev != m)
pike.git/src/mapping.c:1038:    * the index is a destructed object or function    */    if(((1 << k->ind.type) & (BIT_OBJECT | BIT_FUNCTION)) &&    !(k->ind.u.object->prog))    continue;       debug_gc_check_svalues(&k->ind, 1, T_MAPPING, m);    m->val_types |= debug_gc_check_svalues(&k->val, 1, T_MAPPING, m);    }    - #ifdef DEBUG + #ifdef PIKE_DEBUG    if(d_flag > 1) check_mapping_type_fields(m);   #endif       }    }   }      void gc_mark_all_mappings(void)   {    struct mapping *m;
pike.git/src/mapping.c:1097:       next=m->next;       free_mapping(m);    }else{    next=m->next;    }    }   }    - #ifdef DEBUG + #ifdef PIKE_DEBUG      void simple_describe_mapping(struct mapping *m)   {    char *s;    init_buf();    describe_mapping(m,0,2);    s=simple_free_buf();    fprintf(stderr,"%s\n",s);    free(s);   }
pike.git/src/mapping.c:1138:   void zap_all_mappings(void)   {    INT32 e;    struct keypair *k;    struct mapping *m,*next;       for(m=first_mapping;m;m=next)    {    add_ref(m);    - #if defined(DEBUG) && defined(DEBUG_MALLOC) + #if defined(PIKE_DEBUG) && defined(DEBUG_MALLOC)    if(verbose_debug_exit)    debug_dump_mapping(m);   #endif       for(e=0;e<m->hashsize;e++)    {    while((k=m->hash[e]))    {    m->hash[e]=k->next;    k->next=m->free_list;