pike.git / src / encode.c

version» Context lines:

pike.git/src/encode.c:37:      /* Use the old encoding method for programs. */   /* #define OLD_PIKE_ENCODE_PROGRAM */      #ifdef ENCODE_DEBUG   /* Pass a nonzero integer as the third arg to encode_value,    * encode_value_canonic and decode_value to activate this debug. It    * both enables debug messages and also lessens the pickyness to    * sort-of be able to decode programs with the wrong codec. */   #define EDB(N,X) do { debug_malloc_touch(data); if (data->debug>=N) {X;} } while (0) + #define ENCODE_WERR_COMMENT(COMMENT, X...) do { \ +  string_builder_sprintf_disassembly(data->debug_buf, \ +  data->debug_pos, \ +  (((char *)data->buf.end) - \ +  data->buf.length) + \ +  data->debug_pos, \ +  data->buf.dst, \ +  COMMENT, X); \ +  data->debug_pos = data->buf.length - \ +  (((char *)data->buf.end) - ((char *)data->buf.dst)); \ +  } while(0) + #define ENCODE_WERR(X...) ENCODE_WERR_COMMENT(NULL, X) + #define ENCODE_FLUSH() do { \ +  size_t end_pos = data->buf.length - \ +  (((char *)data->buf.end) - ((char *)data->buf.dst)); \ +  if (data->debug_pos < end_pos) { \ +  string_builder_append_disassembly_data(data->debug_buf, \ +  data->debug_pos, \ +  (((char *)data->buf.end) - \ +  data->buf.length) + \ +  data->debug_pos, \ +  data->buf.dst, \ +  NULL); \ +  data->debug_pos = end_pos; \ +  } \ +  } while(0)   #ifndef PIKE_DEBUG   #error ENCODE_DEBUG requires PIKE_DEBUG   #endif   #else   #define EDB(N,X) do { debug_malloc_touch(data); } while (0)   #endif      #ifdef _AIX   #include <net/nh.h>   #endif
pike.git/src/encode.c:156:    int canonic;    struct object *codec;    struct svalue counter;    struct mapping *encoded;    /* The encoded mapping maps encoded things to their entry IDs. A    * value less than COUNTER_START means that it's a forward reference    * to a thing not yet encoded. */    struct array *delayed;    struct byte_buffer buf;   #ifdef ENCODE_DEBUG +  struct string_builder *debug_buf; +  size_t debug_pos;    int debug, depth;   #endif   };      static struct object *encoder_codec (struct encode_data *data)   {    struct pike_string *encoder_str;    if (data->codec) return data->codec;    MAKE_CONST_STRING (encoder_str, "Encoder");    return data->codec = lookup_codec (encoder_str);
pike.git/src/encode.c:516:   {    static struct svalue dested = SVALUE_INIT (T_INT, NUMBER_DESTRUCTED, 0);    INT32 i;    struct svalue *tmp;    struct svalue entry_id;      #ifdef ENCODE_DEBUG    data->depth += 2;   #endif    +  EDB(1, { +  ENCODE_FLUSH(); +  ENCODE_WERR("# Encoding a %s.", +  get_name_of_type(TYPEOF(*val))); +  }); +     if((TYPEOF(*val) == T_OBJECT ||    (TYPEOF(*val) == T_FUNCTION && SUBTYPEOF(*val) != FUNCTION_BUILTIN)) &&    !val->u.object->prog)    val = &dested;       if((tmp=low_mapping_lookup(data->encoded, val)))    {    entry_id = *tmp; /* It's always a small integer. */    if (entry_id.u.integer < COUNTER_START)    entry_id.u.integer = CONVERT_ENTRY_ID (entry_id.u.integer);
pike.git/src/encode.c:539:    data->depth, "", entry_id.u.integer);    if(data->debug == 1)    {    fprintf(stderr,"TAG%d", TYPEOF(*val));    }else{    print_svalue(stderr, val);    }    fputc('\n', stderr););    code_entry (TAG_DELAYED, entry_id.u.integer, data);    tmp->u.integer = entry_id.u.integer; +  EDB(1, { +  ENCODE_WERR(".tag delayed, %ld", entry_id.u.integer); +  });    }    else {    EDB(1,fprintf(stderr, "%*sEncoding TAG_AGAIN from <%d>\n",    data->depth, "", entry_id.u.integer));    code_entry(TAG_AGAIN, entry_id.u.integer, data); -  +  EDB(1, { +  ENCODE_WERR(".tag again, %ld", entry_id.u.integer); +  });    goto encode_done;    }    }else {   #ifdef PIKE_DEBUG    if (force_encode == 2)    Pike_fatal ("Didn't find old entry for delay encoded thing.\n");   #endif    if (TYPEOF(*val) != T_TYPE) {    entry_id = data->counter; /* It's always a small integer. */    EDB(1,fprintf(stderr, "%*sEncoding to <%d>: ",    data->depth, "", entry_id.u.integer);    if(data->debug == 1)    {    fprintf(stderr,"TAG%d", TYPEOF(*val));    }else{    print_svalue(stderr, val);    }    fputc('\n', stderr);); -  +  EDB(1, { +  ENCODE_WERR("# Encoding to tag #%ld", entry_id.u.integer); +  });    if( TYPEOF(*val) < MIN_REF_TYPE || val->u.dummy->refs > 1 )    mapping_insert(data->encoded, val, &entry_id);    data->counter.u.integer++;    }    }       switch(TYPEOF(*val))    {    case T_INT:    /* NOTE: Doesn't encode NUMBER_UNDEFINED et al. */
pike.git/src/encode.c:1737:    }    break;    }    }      encode_done:;      #ifdef ENCODE_DEBUG    data->depth -= 2;   #endif +  EDB(1, { +  ENCODE_FLUSH(); +  });   }      static void free_encode_data(struct encode_data *data)   {    buffer_free(& data->buf);    if (data->codec) free_object (data->codec);    free_mapping(data->encoded);    free_array(data->delayed);   }   
pike.git/src/encode.c:1788:    *! when Pike programs are actually dumped in full, then the string    *! can probably only be read by the same pike version.    *!    *! @seealso    *! @[decode_value()], @[sprintf()], @[encode_value_canonic()]    */   void f_encode_value(INT32 args)   {    ONERROR tmp;    struct encode_data d, *data; +  struct string_builder debug_buf;    int i;    data=&d;       check_all_args(NULL, args,    BIT_MIXED,    BIT_VOID | BIT_OBJECT | BIT_ZERO,   #ifdef ENCODE_DEBUG    /* This argument is only an internal debug helper.    * It's intentionally not part of the function    * prototype, to keep the argument position free for
pike.git/src/encode.c:1811:    0);       buffer_init(&data->buf);    data->canonic = 0;    data->encoded=allocate_mapping(128);    data->encoded->data->flags |= MAPPING_FLAG_NO_SHRINK;    data->delayed = allocate_array (0);    SET_SVAL(data->counter, T_INT, NUMBER_NUMBER, integer, COUNTER_START);      #ifdef ENCODE_DEBUG +  data->debug_buf = NULL; +  data->debug_pos = 0;    data->debug = args > 2 ? Pike_sp[2-args].u.integer : 0;    data->depth = -2; -  +  if (data->debug) { +  init_string_builder(&debug_buf, 0); +  data->debug_buf = &debug_buf; +  }   #endif       if(args > 1 && TYPEOF(Pike_sp[1-args]) == T_OBJECT)    {    if (SUBTYPEOF(Pike_sp[1-args]))    Pike_error("The codec may not be a subtyped object yet.\n");       data->codec=Pike_sp[1-args].u.object;    add_ref (data->codec);    }else{    data->codec=NULL;    }       SET_ONERROR(tmp, free_encode_data, data);    addstr("\266ke0", 4);    -  +  EDB(1, { +  string_builder_append_disassembly(data->debug_buf, +  data->debug_pos, +  ((char *)data->buf.end) - +  (data->buf.length - data->debug_pos), +  data->buf.dst, +  ".format 0", +  NULL, +  "Encoding #0."); +  data->debug_pos = data->buf.length - +  (((char *)data->buf.end) - ((char *)data->buf.dst)); +  }); +     encode_value2(Pike_sp-args, data, 1);    -  for (i = 0; i < data->delayed->size; i++) +  for (i = 0; i < data->delayed->size; i++) { +  EDB(1, { +  string_builder_sprintf(data->debug_buf, +  "0x%016x # Delayed item #%d\n", +  data->debug_pos, i); +  });    encode_value2 (ITEM(data->delayed) + i, data, 2); -  +  }       UNSET_ONERROR(tmp);    -  +  EDB(1, { +  ENCODE_FLUSH(); +  }); +     if (data->codec) free_object (data->codec);    free_mapping(data->encoded);    free_array (data->delayed);    -  +  EDB(1, { +  write_and_reset_string_builder(2, data->debug_buf); +  free_string_builder(data->debug_buf); +  }); +     pop_n_elems(args);    push_string(buffer_finish_pike_string(&data->buf));   }      /*! @decl string encode_value_canonic(mixed value, object|void codec)    *!    *! Code a value into a string on canonical form.    *!    *! Takes a value and converts it to a string on canonical form, much like    *! @[encode_value()]. The canonical form means that if an identical value is