< prev index next >

src/share/vm/utilities/ostream.cpp

Print this page
rev 9831 : 8146905 - cleanup ostream, staticBufferStream
Summary: get rid of staticBufferStream and implement the use-caller-provided-scratch-buffer feature in a simpler way.


  27 #include "gc/shared/gcId.hpp"
  28 #include "gc/shared/gcId.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/os.hpp"
  32 #include "runtime/vm_version.hpp"
  33 #include "utilities/defaultStream.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "utilities/ostream.hpp"
  36 #include "utilities/top.hpp"
  37 #include "utilities/xmlstream.hpp"
  38 
  39 extern "C" void jio_print(const char* s); // Declarationtion of jvm method
  40 
  41 outputStream::outputStream(int width) {
  42   _width       = width;
  43   _position    = 0;
  44   _newlines    = 0;
  45   _precount    = 0;
  46   _indentation = 0;


  47 }
  48 
  49 outputStream::outputStream(int width, bool has_time_stamps) {
  50   _width       = width;
  51   _position    = 0;
  52   _newlines    = 0;
  53   _precount    = 0;
  54   _indentation = 0;


  55   if (has_time_stamps)  _stamp.update();
  56 }
  57 
  58 void outputStream::update_position(const char* s, size_t len) {
  59   for (size_t i = 0; i < len; i++) {
  60     char ch = s[i];
  61     if (ch == '\n') {
  62       _newlines += 1;
  63       _precount += _position + 1;
  64       _position = 0;
  65     } else if (ch == '\t') {
  66       int tw = 8 - (_position & 7);
  67       _position += tw;
  68       _precount -= tw-1;  // invariant:  _precount + _position == total count
  69     } else {
  70       _position += 1;
  71     }
  72   }
  73 }
  74 


 102     result = buffer;
 103     if (written < (int) buflen && written >= 0) {
 104       result_len = written;
 105     } else {
 106       DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");)
 107       result_len = buflen - 1;
 108       buffer[result_len] = 0;
 109     }
 110   }
 111   if (add_cr) {
 112     if (result != buffer) {
 113       memcpy(buffer, result, result_len);
 114       result = buffer;
 115     }
 116     buffer[result_len++] = '\n';
 117     buffer[result_len] = 0;
 118   }
 119   return result;
 120 }
 121 
 122 void outputStream::print(const char* format, ...) {
 123   char buffer[O_BUFLEN];
 124   va_list ap;
 125   va_start(ap, format);
 126   size_t len;
 127   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, false, len);






 128   write(str, len);














 129   va_end(ap);
 130 }
 131 
 132 void outputStream::print_cr(const char* format, ...) {
 133   char buffer[O_BUFLEN];
 134   va_list ap;
 135   va_start(ap, format);
 136   size_t len;
 137   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, true, len);
 138   write(str, len);
 139   va_end(ap);
 140 }
 141 
 142 void outputStream::vprint(const char *format, va_list argptr) {
 143   char buffer[O_BUFLEN];
 144   size_t len;
 145   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, false, len);
 146   write(str, len);
 147 }
 148 
 149 void outputStream::vprint_cr(const char* format, va_list argptr) {
 150   char buffer[O_BUFLEN];
 151   size_t len;
 152   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, true, len);
 153   write(str, len);
 154 }
 155 
 156 void outputStream::fill_to(int col) {
 157   int need_fill = col - position();
 158   sp(need_fill);
 159 }
 160 
 161 void outputStream::move_to(int col, int slop, int min_space) {
 162   if (position() >= col + slop)
 163     cr();
 164   int need_fill = col - position();
 165   if (need_fill < min_space)
 166     need_fill = min_space;
 167   sp(need_fill);
 168 }
 169 
 170 void outputStream::put(char ch) {
 171   assert(ch != 0, "please fix call site");
 172   char buf[] = { ch, '\0' };
 173   write(buf, 1);


 939           delete tty;
 940       }
 941       if (defaultStream::instance != NULL) {
 942           delete defaultStream::instance;
 943       }
 944   }
 945   tty = NULL;
 946   xtty = NULL;
 947   defaultStream::instance = NULL;
 948 }
 949 
 950 // ostream_abort() is called by os::abort() when VM is about to die.
 951 void ostream_abort() {
 952   // Here we can't delete tty, just flush its output
 953   if (tty) tty->flush();
 954 
 955   if (defaultStream::instance != NULL) {
 956     static char buf[4096];
 957     defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
 958   }
 959 }
 960 
 961 staticBufferStream::staticBufferStream(char* buffer, size_t buflen,
 962                                        outputStream *outer_stream) {
 963   _buffer = buffer;
 964   _buflen = buflen;
 965   _outer_stream = outer_stream;
 966   // compile task prints time stamp relative to VM start
 967   _stamp.update_to(1);
 968 }
 969 
 970 void staticBufferStream::write(const char* c, size_t len) {
 971   _outer_stream->print_raw(c, (int)len);
 972 }
 973 
 974 void staticBufferStream::flush() {
 975   _outer_stream->flush();
 976 }
 977 
 978 void staticBufferStream::print(const char* format, ...) {
 979   va_list ap;
 980   va_start(ap, format);
 981   size_t len;
 982   const char* str = do_vsnprintf(_buffer, _buflen, format, ap, false, len);
 983   write(str, len);
 984   va_end(ap);
 985 }
 986 
 987 void staticBufferStream::print_cr(const char* format, ...) {
 988   va_list ap;
 989   va_start(ap, format);
 990   size_t len;
 991   const char* str = do_vsnprintf(_buffer, _buflen, format, ap, true, len);
 992   write(str, len);
 993   va_end(ap);
 994 }
 995 
 996 void staticBufferStream::vprint(const char *format, va_list argptr) {
 997   size_t len;
 998   const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, false, len);
 999   write(str, len);
1000 }
1001 
1002 void staticBufferStream::vprint_cr(const char* format, va_list argptr) {
1003   size_t len;
1004   const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, true, len);
1005   write(str, len);
1006 }
1007 
1008 bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() {
1009   buffer_length = initial_size;
1010   buffer        = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
1011   buffer_pos    = 0;
1012   buffer_fixed  = false;
1013   buffer_max    = bufmax;
1014 }
1015 
1016 bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() {
1017   buffer_length = fixed_buffer_size;
1018   buffer        = fixed_buffer;
1019   buffer_pos    = 0;
1020   buffer_fixed  = true;
1021   buffer_max    = bufmax;
1022 }
1023 
1024 void bufferedStream::write(const char* s, size_t len) {
1025 




  27 #include "gc/shared/gcId.hpp"
  28 #include "gc/shared/gcId.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/os.hpp"
  32 #include "runtime/vm_version.hpp"
  33 #include "utilities/defaultStream.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "utilities/ostream.hpp"
  36 #include "utilities/top.hpp"
  37 #include "utilities/xmlstream.hpp"
  38 
  39 extern "C" void jio_print(const char* s); // Declarationtion of jvm method
  40 
  41 outputStream::outputStream(int width) {
  42   _width       = width;
  43   _position    = 0;
  44   _newlines    = 0;
  45   _precount    = 0;
  46   _indentation = 0;
  47   _scratch     = NULL;
  48   _scratch_len = 0;
  49 }
  50 
  51 outputStream::outputStream(int width, bool has_time_stamps) {
  52   _width       = width;
  53   _position    = 0;
  54   _newlines    = 0;
  55   _precount    = 0;
  56   _indentation = 0;
  57   _scratch     = NULL;
  58   _scratch_len = 0;
  59   if (has_time_stamps)  _stamp.update();
  60 }
  61 
  62 void outputStream::update_position(const char* s, size_t len) {
  63   for (size_t i = 0; i < len; i++) {
  64     char ch = s[i];
  65     if (ch == '\n') {
  66       _newlines += 1;
  67       _precount += _position + 1;
  68       _position = 0;
  69     } else if (ch == '\t') {
  70       int tw = 8 - (_position & 7);
  71       _position += tw;
  72       _precount -= tw-1;  // invariant:  _precount + _position == total count
  73     } else {
  74       _position += 1;
  75     }
  76   }
  77 }
  78 


 106     result = buffer;
 107     if (written < (int) buflen && written >= 0) {
 108       result_len = written;
 109     } else {
 110       DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");)
 111       result_len = buflen - 1;
 112       buffer[result_len] = 0;
 113     }
 114   }
 115   if (add_cr) {
 116     if (result != buffer) {
 117       memcpy(buffer, result, result_len);
 118       result = buffer;
 119     }
 120     buffer[result_len++] = '\n';
 121     buffer[result_len] = 0;
 122   }
 123   return result;
 124 }
 125 
 126 void outputStream::do_vsnprintf_and_write_with_automatic_buffer(const char* format, va_list ap, bool add_cr) {
 127   char buffer[O_BUFLEN];


 128   size_t len;
 129   const char* str = do_vsnprintf(buffer, sizeof(buffer), format, ap, add_cr, len);
 130   write(str, len);
 131 }
 132 
 133 void outputStream::do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) {
 134   size_t len;
 135   const char* str = do_vsnprintf(_scratch, _scratch_len, format, ap, add_cr, len);
 136   write(str, len);
 137 }
 138 
 139 void outputStream::do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) {
 140   if (_scratch) {
 141     do_vsnprintf_and_write_with_scratch_buffer(format, ap, add_cr);
 142   } else {
 143     do_vsnprintf_and_write_with_automatic_buffer(format, ap, add_cr);
 144   }
 145 }
 146 
 147 void outputStream::print(const char* format, ...) {
 148   va_list ap;
 149   va_start(ap, format);
 150   do_vsnprintf_and_write(format, ap, false);
 151   va_end(ap);
 152 }
 153 
 154 void outputStream::print_cr(const char* format, ...) {

 155   va_list ap;
 156   va_start(ap, format);
 157   do_vsnprintf_and_write(format, ap, true);


 158   va_end(ap);
 159 }
 160 
 161 void outputStream::vprint(const char *format, va_list argptr) {
 162   do_vsnprintf_and_write(format, argptr, false);



 163 }
 164 
 165 void outputStream::vprint_cr(const char* format, va_list argptr) {
 166   do_vsnprintf_and_write(format, argptr, true);



 167 }
 168 
 169 void outputStream::fill_to(int col) {
 170   int need_fill = col - position();
 171   sp(need_fill);
 172 }
 173 
 174 void outputStream::move_to(int col, int slop, int min_space) {
 175   if (position() >= col + slop)
 176     cr();
 177   int need_fill = col - position();
 178   if (need_fill < min_space)
 179     need_fill = min_space;
 180   sp(need_fill);
 181 }
 182 
 183 void outputStream::put(char ch) {
 184   assert(ch != 0, "please fix call site");
 185   char buf[] = { ch, '\0' };
 186   write(buf, 1);


 952           delete tty;
 953       }
 954       if (defaultStream::instance != NULL) {
 955           delete defaultStream::instance;
 956       }
 957   }
 958   tty = NULL;
 959   xtty = NULL;
 960   defaultStream::instance = NULL;
 961 }
 962 
 963 // ostream_abort() is called by os::abort() when VM is about to die.
 964 void ostream_abort() {
 965   // Here we can't delete tty, just flush its output
 966   if (tty) tty->flush();
 967 
 968   if (defaultStream::instance != NULL) {
 969     static char buf[4096];
 970     defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
 971   }















































 972 }
 973 
 974 bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() {
 975   buffer_length = initial_size;
 976   buffer        = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
 977   buffer_pos    = 0;
 978   buffer_fixed  = false;
 979   buffer_max    = bufmax;
 980 }
 981 
 982 bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() {
 983   buffer_length = fixed_buffer_size;
 984   buffer        = fixed_buffer;
 985   buffer_pos    = 0;
 986   buffer_fixed  = true;
 987   buffer_max    = bufmax;
 988 }
 989 
 990 void bufferedStream::write(const char* s, size_t len) {
 991 


< prev index next >