pike.git / src / dynamic_buffer.h

version» Context lines:

pike.git/src/dynamic_buffer.h: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.   */      #ifndef DYNAMIC_BUFFER_H   #define DYNAMIC_BUFFER_H    - #define BUFFER_BEGIN_SIZE 4080 + #include "buffer.h"    - struct dynbuf_string_s - { -  char *str; -  SIZE_T len; - }; + typedef struct byte_buffer dynamic_buffer; + typedef struct byte_buffer dynbuf_string;    - typedef struct dynbuf_string_s dynbuf_string; + #define MACRO static inline PIKE_UNUSED_ATTRIBUTE    - struct dynamic_buffer_s - { -  dynbuf_string s; -  SIZE_T bufsize; - }; + /* Prototypes begin here */ + MACRO char *low_make_buf_space(ptrdiff_t space, dynamic_buffer *buf) { +  if (space < 0) { +  void * ret = buffer_dst(buf); +  buffer_remove(buf, -space); +  return ret; +  } else { +  return buffer_alloc(buf, space); +  } + } + MACRO void low_my_putchar(int b,dynamic_buffer *buf) { +  buffer_add_char(buf, b); + } + MACRO void low_my_binary_strcat(const char *b, size_t l, dynamic_buffer *buf) { +  buffer_memcpy(buf, b, l); + }    - typedef struct dynamic_buffer_s dynamic_buffer; + // NOTE: dynamic buffer did zero terminate + MACRO void initialize_buf(dynamic_buffer *buf) { +  buffer_init(buf); + } + MACRO void low_reinit_buf(dynamic_buffer *buf) { +  buffer_clear(buf); + } + MACRO void toss_buffer(dynamic_buffer *buf) { +  buffer_free(buf); + } + MACRO struct pike_string *low_free_buf(dynamic_buffer *buf) { +  return buffer_finish_pike_string(buf); + }    -  + /* GLOBAL BUFFER LOGIC */ +    PMOD_EXPORT extern dynamic_buffer pike_global_buffer;    - /* Prototypes begin here */ - PMOD_EXPORT char *low_make_buf_space(ptrdiff_t space, dynamic_buffer *buf); - PMOD_EXPORT void low_my_putchar(int b,dynamic_buffer *buf); - PMOD_EXPORT void low_my_binary_strcat(const char *b, size_t l, dynamic_buffer *buf); - PMOD_EXPORT void debug_initialize_buf(dynamic_buffer *buf); - PMOD_EXPORT void low_reinit_buf(dynamic_buffer *buf); - PMOD_EXPORT void low_init_buf_with_string(dynbuf_string s, dynamic_buffer *buf); - PMOD_EXPORT void toss_buffer(dynamic_buffer *buf); - PMOD_EXPORT struct pike_string *debug_low_free_buf(dynamic_buffer *buf); + MACRO dynbuf_string complex_free_buf(dynamic_buffer *old_buf) { +  dynbuf_string tmp; +  if(!buffer_ptr(&pike_global_buffer)) return pike_global_buffer; +  tmp=pike_global_buffer; +  buffer_add_char(&tmp, 0); +  buffer_remove(&tmp, 1); +  pike_global_buffer = *old_buf; +  return tmp; + } + MACRO char *simple_free_buf(dynamic_buffer *old_buf) { +  dynbuf_string tmp; +  if(!buffer_ptr(&pike_global_buffer)) return 0; +  tmp = complex_free_buf(old_buf); +  buffer_add_char(&tmp, 0); +  return buffer_ptr(&tmp); + } + MACRO struct pike_string *free_buf(dynamic_buffer *old_buf) { +  struct pike_string *res = low_free_buf(&pike_global_buffer); +  pike_global_buffer = *old_buf; +  return res; + }    - PMOD_EXPORT dynbuf_string complex_free_buf(dynamic_buffer *old_buf); - PMOD_EXPORT char *simple_free_buf(dynamic_buffer *old_buf); - PMOD_EXPORT struct pike_string *debug_free_buf(dynamic_buffer *old_buf); - PMOD_EXPORT void abandon_buf(dynamic_buffer *old_buf); - PMOD_EXPORT char *make_buf_space(INT32 space); - PMOD_EXPORT void my_putchar(int b); - PMOD_EXPORT void my_binary_strcat(const char *b, ptrdiff_t l); - PMOD_EXPORT void my_strcat(const char *b); - PMOD_EXPORT void init_buf(dynamic_buffer *old_buf); - PMOD_EXPORT void init_buf_with_string(dynamic_buffer *old_buf, dynbuf_string s); - PMOD_EXPORT void save_buffer (dynamic_buffer *save_buf); - PMOD_EXPORT void restore_buffer (dynamic_buffer *save_buf); - /* PMOD_EXPORT char *debug_return_buf(void); */ - /* Prototypes end here */ + MACRO void abandon_buf(dynamic_buffer *old_buf) { +  toss_buffer(&pike_global_buffer); +  pike_global_buffer = *old_buf; + }    - #ifdef DEBUG_MALLOC - #define initialize_buf(X) \ -  do { dynamic_buffer *b_=(X); debug_initialize_buf(b_); \ -  debug_malloc_touch(b_->s.str); } while(0) - #define low_free_buf(X) \ -  ((struct pike_string *)debug_malloc_pass(debug_low_free_buf(X))) + MACRO char *make_buf_space(INT32 space) { +  return low_make_buf_space(space,&pike_global_buffer); + }    - #define free_buf(OLD_BUF) \ -  ((struct pike_string *)debug_malloc_pass(debug_free_buf(OLD_BUF))) + MACRO void my_putchar(int b) { +  low_my_putchar(b,&pike_global_buffer); + }    - #define return_buf() \ -  ((char *)debug_malloc_pass(debug_return_buf())) -  - #else - #define initialize_buf debug_initialize_buf - #define low_free_buf debug_low_free_buf - #define free_buf debug_free_buf - #define return_buf debug_return_buf + MACRO void my_binary_strcat(const char *b, ptrdiff_t l) { +  low_my_binary_strcat(b,l,&pike_global_buffer); + } + MACRO void my_strcat(const char *b) { +  my_binary_strcat(b,strlen(b)); + } + MACRO void init_buf(dynamic_buffer *old_buf) { +  *old_buf = pike_global_buffer; +  initialize_buf(&pike_global_buffer); + } + MACRO void save_buffer (dynamic_buffer *save_buf) { +  *save_buf = pike_global_buffer; +  initialize_buf(&pike_global_buffer); + } + MACRO void restore_buffer (const dynamic_buffer *save_buf) { + #ifdef PIKE_DEBUG +  if (buffer_content_length(&pike_global_buffer)) +  Pike_fatal ("Global buffer already in use.\n");   #endif -  +  pike_global_buffer = *save_buf; + }      #endif