cb65012014-10-01Martin Nilsson struct _Buffer
8cccac2014-08-28Per Hedbor { unsigned char *buffer; size_t offset; /* reading */ size_t len, allocated; /* writing */
52f2302021-03-13Stephen R. van den Berg  struct object *this;
9629ad2014-09-01Per Hedbor  struct program *error_mode;
fbc3bf2014-09-08Per Hedbor 
52f2302021-03-13Stephen R. van den Berg  union { struct pike_string *str; /* PIKE_T_STRING */ struct object *obj; /* PIKE_T_OBJECT */ struct _Buffer *parent; /* PIKE_T_MIXED */ } source;
7822002021-03-20Stephen R. van den Berg  INT32 sourcetype; /* type for the source union */
fbc3bf2014-09-08Per Hedbor  INT32 locked, locked_move;
9171842015-07-13Per Hedbor  float max_waste;
d0cbab2021-03-15Stephen R. van den Berg 
7822002021-03-20Stephen R. van den Berg  struct svalue output;
d0cbab2021-03-15Stephen R. van den Berg #ifdef PIKE_DEBUG
2312132021-03-22Stephen R. van den Berg  INT32 num_malloc, num_move; /* debug mainly, for testsuite*/
d0cbab2021-03-15Stephen R. van den Berg #endif
8cccac2014-08-28Per Hedbor };
fbc3bf2014-09-08Per Hedbor  struct rewind_to {
cb65012014-10-01Martin Nilsson  struct _Buffer *io;
fbc3bf2014-09-08Per Hedbor  size_t rewind_to; #ifdef PIKE_DEBUG int old_locked_move; #endif };
cb65012014-10-01Martin Nilsson typedef struct _Buffer Buffer;
8cccac2014-08-28Per Hedbor  extern void init_stdio_buffer(void); extern void exit_stdio_buffer(void);
2312132021-03-22Stephen R. van den Berg extern unsigned char *io_add_space_do_something( Buffer *io, size_t bytes, int force ); extern void io_actually_trigger_output( Buffer *io );
307d112015-11-21Arne Goedeke  PMOD_EXPORT Buffer *io_buffer_from_object(struct object *o); PIKE_UNUSED_ATTRIBUTE static size_t io_len( Buffer *io ) {
2312132021-03-22Stephen R. van den Berg  return io->len - io->offset;
307d112015-11-21Arne Goedeke } PIKE_UNUSED_ATTRIBUTE static unsigned char *io_read_pointer(Buffer *io) { return io->buffer + io->offset; } PIKE_UNUSED_ATTRIBUTE static unsigned char *io_add_space( Buffer *io, size_t bytes, int force ) {
804c712021-03-15Stephen R. van den Berg  if (LIKELY(!io->locked)) { if (UNLIKELY(io->len == io->offset) && LIKELY(!io->locked_move)) io->offset = io->len = 0; if (LIKELY(!force && io->len+bytes < io->allocated
2312132021-03-22Stephen R. van den Berg  && io->len+bytes >= io->len))
804c712021-03-15Stephen R. van den Berg  return io->buffer+io->len; }
307d112015-11-21Arne Goedeke  return io_add_space_do_something( io, bytes, force ); } PIKE_UNUSED_ATTRIBUTE
3466262017-06-29Arne Goedeke static INT_TYPE io_consume( Buffer *io, ptrdiff_t num )
307d112015-11-21Arne Goedeke { io->offset += num; return io_len(io); }
04f8b82017-09-24Arne Goedeke PIKE_UNUSED_ATTRIBUTE
ecc59b2019-02-02Tobias S. Josefowitz static void io_trigger_output( Buffer *io )
04f8b82017-09-24Arne Goedeke {
4e739f2021-03-20Stephen R. van den Berg  if (UNLIKELY(TYPEOF(io->output) == PIKE_T_OBJECT))
ecc59b2019-02-02Tobias S. Josefowitz  io_actually_trigger_output(io);
04f8b82017-09-24Arne Goedeke }