e576bb2002-10-11Martin Nilsson /* || 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. */
aedfb12002-10-09Martin Nilsson 
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "global.h" #include "dynamic_buffer.h" #include "stralloc.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
9aa6fa1997-05-19Fredrik Hübinette (Hubbe) #include "pike_memory.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
a5a3342006-07-05Martin Stjernholm PMOD_EXPORT dynamic_buffer pike_global_buffer;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
310dd52008-07-16Martin Stjernholm PMOD_EXPORT char *low_make_buf_space(ptrdiff_t space, dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) { char *ret;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5aad932002-08-15Marcus Comstedt  if(!buf->s.str) Pike_fatal("ARRRRGH! Deadly Trap!\n");
6ff3e91996-04-13Fredrik Hübinette (Hubbe) #endif
1b3a7f1997-09-11Henrik Grubbström (Grubba)  if(buf->s.len+space >= buf->bufsize)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
38145a1996-02-18Fredrik Hübinette (Hubbe)  if(!buf->bufsize) buf->bufsize=1;
5267b71995-08-09Fredrik Hübinette (Hubbe)  do{ buf->bufsize*=2;
1b3a7f1997-09-11Henrik Grubbström (Grubba)  }while(buf->s.len+space >= buf->bufsize);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
1b3a7f1997-09-11Henrik Grubbström (Grubba)  buf->s.str=(char *)realloc(buf->s.str, buf->bufsize);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(!buf->s.str)
e4591b2009-04-25Martin Stjernholm  Pike_fatal("Out of memory.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
1b3a7f1997-09-11Henrik Grubbström (Grubba)  ret = buf->s.str + buf->s.len; buf->s.len += space;
5267b71995-08-09Fredrik Hübinette (Hubbe)  return ret; }
7e877a2003-04-02Martin Stjernholm PMOD_EXPORT void low_my_putchar(int b,dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(!buf->s.str)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Error in internal buffering.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif low_make_buf_space(1,buf)[0]=b; }
d9ab782000-08-04Henrik Grubbström (Grubba) PMOD_EXPORT void low_my_binary_strcat(const char *b, size_t l, dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(!buf->s.str)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Error in internal buffering.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif
d9ab782000-08-04Henrik Grubbström (Grubba)  MEMCPY(low_make_buf_space(l, buf),b, l);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void debug_initialize_buf(dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
e43ca21996-11-15Fredrik Hübinette (Hubbe)  buf->s.str=(char *)xalloc((buf->bufsize=BUFFER_BEGIN_SIZE));
5267b71995-08-09Fredrik Hübinette (Hubbe)  *(buf->s.str)=0; buf->s.len=0; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void low_reinit_buf(dynamic_buffer *buf)
e43ca21996-11-15Fredrik Hübinette (Hubbe) { if(!buf->s.str) { initialize_buf(buf); }else{ *(buf->s.str)=0; buf->s.len=0; } }
17ceb42002-10-21Marcus Comstedt PMOD_EXPORT void low_init_buf_with_string(dynbuf_string s, dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) { if(buf->s.str) { free(buf->s.str); buf->s.str=NULL; } buf->s=s;
82ac181996-11-26Fredrik Hübinette (Hubbe)  if(!buf->s.str) initialize_buf(buf);
5267b71995-08-09Fredrik Hübinette (Hubbe)  /* if the string is an old buffer, this realloc will set the old the bufsize back */ for(buf->bufsize=BUFFER_BEGIN_SIZE;buf->bufsize<buf->s.len;buf->bufsize*=2); buf->s.str=realloc(buf->s.str,buf->bufsize);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
82ac181996-11-26Fredrik Hübinette (Hubbe)  if(!buf->s.str)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Realloc failed.\n");
82ac181996-11-26Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) }
9fa0ee2003-11-09Martin Stjernholm PMOD_EXPORT dynbuf_string complex_free_buf(dynamic_buffer *old_buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
17ceb42002-10-21Marcus Comstedt  dynbuf_string tmp;
9fa0ee2003-11-09Martin Stjernholm  if(!pike_global_buffer.s.str) return pike_global_buffer.s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  my_putchar(0);
9fa0ee2003-11-09Martin Stjernholm  pike_global_buffer.s.len--; tmp=pike_global_buffer.s; pike_global_buffer = *old_buf;
5267b71995-08-09Fredrik Hübinette (Hubbe)  return tmp; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void toss_buffer(dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) { if(buf->s.str) free(buf->s.str); buf->s.str=0; }
9fa0ee2003-11-09Martin Stjernholm PMOD_EXPORT char *simple_free_buf(dynamic_buffer *old_buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
9fa0ee2003-11-09Martin Stjernholm  if(!pike_global_buffer.s.str) return 0; return complex_free_buf(old_buf).str;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct pike_string *debug_low_free_buf(dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *q;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(!buf->s.str) return 0; q=make_shared_binary_string(buf->s.str,buf->s.len); free(buf->s.str); buf->s.str=0; buf->s.len=0; return q; }
9fa0ee2003-11-09Martin Stjernholm PMOD_EXPORT struct pike_string *debug_free_buf(dynamic_buffer *old_buf) { struct pike_string *res = low_free_buf(&pike_global_buffer); pike_global_buffer = *old_buf; return res; }
66f3632011-01-15Henrik Grubbström (Grubba) PMOD_EXPORT void abandon_buf(dynamic_buffer *old_buf) { toss_buffer(&pike_global_buffer); pike_global_buffer = *old_buf; }
9fa0ee2003-11-09Martin Stjernholm PMOD_EXPORT char *make_buf_space(INT32 space) { return low_make_buf_space(space,&pike_global_buffer); } PMOD_EXPORT void my_putchar(int b) { low_my_putchar(b,&pike_global_buffer); } PMOD_EXPORT void my_binary_strcat(const char *b, ptrdiff_t l) { low_my_binary_strcat(b,l,&pike_global_buffer); } PMOD_EXPORT void my_strcat(const char *b) { my_binary_strcat(b,strlen(b)); } PMOD_EXPORT void init_buf(dynamic_buffer *old_buf) { *old_buf = pike_global_buffer; pike_global_buffer.s.str = NULL; initialize_buf(&pike_global_buffer); } PMOD_EXPORT void init_buf_with_string(dynamic_buffer *old_buf, dynbuf_string s) { *old_buf = pike_global_buffer; pike_global_buffer.s.str = NULL; low_init_buf_with_string(s,&pike_global_buffer); } PMOD_EXPORT void save_buffer (dynamic_buffer *save_buf) { *save_buf = pike_global_buffer; pike_global_buffer.s.str = NULL; } PMOD_EXPORT void restore_buffer (dynamic_buffer *save_buf) { #ifdef PIKE_DEBUG if (pike_global_buffer.s.str) Pike_fatal ("Global buffer already in use.\n"); #endif pike_global_buffer = *save_buf; } #if 0
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT char *debug_return_buf(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) { my_putchar(0);
9fa0ee2003-11-09Martin Stjernholm  return pike_global_buffer.s.str;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
9fa0ee2003-11-09Martin Stjernholm #endif /* int my_get_buf_size() { return pike_global_buffer->s.len; } */