Author: antonp
Date: Wed Jul 19 01:48:38 2006
New Revision: 423404
URL: http://svn.apache.org/viewvc?rev=423404&view=rev
Log:
2006-07-19 Anton Pevtsov <[EMAIL PROTECTED]>
* allocator.cpp (_rw_throw_exception): Removed as obsolete
(allocate, funcall): The calls to _rw_throw_exception() replaced
with calls to rw_throw()
* new.cpp (BadAlloc): Removed because of another one
is defined in exception.cpp
(operator_new): The throw statement replaced with call
to rw_throw(ex_bad_alloc, ..)
* rw_streambuf.h (MyStreambuf): Added new method memfun_inx()
which returns the index of the method in array ncalls_;
added the member throw_when_ to define which virtual
method and when should throw the exception;
added the member allthrows_ to count the total number
of thrown exceptions;
added the member allcalls_ to count the total number of
calls to the virtual methods
(test): The throw statement replaced with call
to rw_throw(ex_stream, ...)
Modified:
incubator/stdcxx/trunk/tests/include/rw_streambuf.h
incubator/stdcxx/trunk/tests/src/allocator.cpp
incubator/stdcxx/trunk/tests/src/new.cpp
Modified: incubator/stdcxx/trunk/tests/include/rw_streambuf.h
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/include/rw_streambuf.h?rev=423404&r1=423403&r2=423404&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/include/rw_streambuf.h (original)
+++ incubator/stdcxx/trunk/tests/include/rw_streambuf.h Wed Jul 19 01:48:38 2006
@@ -29,11 +29,12 @@
#define RW_STREAMBUF_H_INCLUDED
-#include <cstring> // for memset()
-#include <streambuf> // for basic_streambuf
+#include <cstring> // for memset()
+#include <streambuf> // for basic_streambuf
#include <testdefs.h>
-#include <rw_char.h> // for make_char
+#include <rw_char.h> // for make_char()
+#include <rw_exception.h> // for rw_throw()
enum MemFun {
@@ -56,6 +57,10 @@
Unknown = 0x4000
};
+static const char* const streambuf_func_names[] = {
+ "setbuf", "seekoff", "seekpos", "showmanyc", "xsgetn", "underflow",
+ "uflow", "overflow", "pbackfail", "xsputn", "sync"
+};
template <class charT, class Traits>
struct MyStreambuf: std::basic_streambuf<charT, Traits>
@@ -163,16 +168,20 @@
public:
int ncalls (MemFun) const;
+ int memfun_inx (MemFun) const;
char_type *buf_;
std::streamsize bufsize_;
- int throw_set_; // functions that should throw
- int fail_set_; // functions that should fail
- MemFun threw_; // which function threw
- MemFun failed_; // which function failed
+ int throw_set_; // functions that should throw
+ int fail_set_; // functions that should fail
+ MemFun threw_; // which function threw
+ MemFun failed_; // which function failed
+
+ int fail_when_; // call number on which to fail
- int fail_when_; // call number on which to fail
+ int throw_when_ [11]; // call number on which to throw for each func
+ int allthrows_; // total number of thrown exceptions
// max size of the pending input sequence
static std::streamsize in_pending_;
@@ -183,7 +192,10 @@
private:
bool test (MemFun) const;
+
int ncalls_ [11]; // number of calls made to each function
+
+ int allcalls_; // total number of calls
};
@@ -202,11 +214,14 @@
MyStreambuf (std::streamsize bufsize, int fail_set, int when)
: Base (), buf_ (0), bufsize_ (bufsize),
throw_set_ (0), fail_set_ (0), threw_ (None), failed_ (None),
- fail_when_ (when)
+ fail_when_ (when), allthrows_ (0), allcalls_ (0)
{
// reset the member function call counters
std::memset (ncalls_, 0, sizeof ncalls_);
+ // reset the member function throw counters
+ std::memset (throw_when_, 0, sizeof throw_when_);
+
// allocate a (possibly wide) character buffer for output
buf_ = new charT [bufsize_];
@@ -232,11 +247,14 @@
MyStreambuf (const char *buf, std::streamsize bufsize, int fail_set, int when)
: Base (), buf_ (0), bufsize_ (bufsize),
throw_set_ (0), fail_set_ (0), threw_ (None), failed_ (None),
- fail_when_ (when)
+ fail_when_ (when), allthrows_ (0), allcalls_ (0)
{
// reset the member function call counters
std::memset (ncalls_, 0, sizeof ncalls_);
+ // reset the member function throw counters
+ std::memset (throw_when_, 0, sizeof throw_when_);
+
// as a convenience, if `bufsize == -1' compute the size
// from the length of `buf'
if (std::streamsize (-1) == bufsize_)
@@ -396,11 +414,10 @@
return traits_type::eof ();
}
-
template <class charT, class Traits>
int
MyStreambuf<charT, Traits>::
-ncalls (MemFun which) const
+memfun_inx (MemFun which) const
{
int inx = -1;
@@ -413,7 +430,19 @@
}
}
- return ncalls_ [inx];
+ return inx;
+}
+
+template <class charT, class Traits>
+int
+MyStreambuf<charT, Traits>::
+ncalls (MemFun which) const
+{
+ int inx = memfun_inx (which);
+ if (0 <= inx)
+ return ncalls_ [inx];
+
+ return -1;
}
@@ -424,35 +453,34 @@
{
MyStreambuf* const self = _RWSTD_CONST_CAST (MyStreambuf*, this);
- int inx = -1;
-
- for (unsigned i = 0; i < sizeof (which) * _RWSTD_CHAR_BIT; ++i) {
- if (which & (1U << i)) {
- if (inx < 0)
- inx = i;
- else
- return true;
- }
- }
+ int inx = memfun_inx (which);
+ if (-1 == inx)
+ return true;
// increment the counter tracking the number of calls made
// to each member function; do so regardless of whether
// an exception will be thrown below
- const int callno = self->ncalls_ [inx]++;
+ self->ncalls_ [inx] ++;
+ self->allcalls_ ++;
+ const int callno = self->ncalls_ [inx];
#ifndef _RWSTD_NO_EXCEPTIONS
// if the call counter is equal to the `fail_when_' watermark
// and `shich' is set in the `throw_set_' bitmask, throw an
// exception with the value of the member id
- if (callno == fail_when_ && throw_set_ & which) {
+ if (callno == throw_when_ [inx] && throw_set_ & which) {
self->threw_ = which;
- throw which;
+ self->allthrows_++;
+
+ rw_throw (ex_stream, __FILE__, __LINE__,
+ streambuf_func_names [inx],
+ "%s", "test exception");
}
#else // if defined (_RWSTD_NO_EXCEPTIONS)
- if (callno == fail_when_ && throw_set_ & which) {
+ if (callno == throw_when_ [inx] && throw_set_ & which) {
self->threw_ = which;
return false;
}
Modified: incubator/stdcxx/trunk/tests/src/allocator.cpp
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/src/allocator.cpp?rev=423404&r1=423403&r2=423404&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/allocator.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/allocator.cpp Wed Jul 19 01:48:38 2006
@@ -34,6 +34,7 @@
#include <new> // for bad_alloc, placement new
#include <stdarg.h> // for va_arg(), va_list
#include <string.h> // for memset()
+#include <rw_exception.h> // for rw_throw()
/**************************************************************************/
@@ -55,36 +56,6 @@
static int _rw_id_gen; // generates unique ids
-/**************************************************************************/
-
-_TEST_EXPORT int
-rw_vasnprintf (char**, size_t*, const char*, va_list);
-
-
-static void
-_rw_throw_exception (const char *file, int line, const char *fmt, ...)
-{
- struct BadSharedAlloc: _RWSTD_BAD_ALLOC {
- char what_ [4096];
-
- /* virtual */ const char* what () const _THROWS (()) {
- return what_;
- }
- };
-
- BadSharedAlloc ex;
-
- va_list va;
- va_start (va, fmt);
-
- char *buf = ex.what_;
- size_t bufsize = sizeof ex.what_;
- rw_vasnprintf (&buf, &bufsize, fmt, va);
-
- va_end (va);
-
- throw ex;
-}
/**************************************************************************/
@@ -136,14 +107,14 @@
const size_t nbytes = nelems * size;
if (max_blocks_ < n_blocks_ + nelems)
- _rw_throw_exception (__FILE__, __LINE__,
- "allocate (%zu): reached block limit of %zu",
- nbytes, max_blocks_);
+ rw_throw (ex_bad_alloc, __FILE__, __LINE__, 0,
+ "allocate (%zu): reached block limit of %zu",
+ nbytes, max_blocks_);
if (max_bytes_ < n_bytes_ + nbytes)
- _rw_throw_exception (__FILE__, __LINE__,
- "allocate (%zu): reached size limit of %zu",
- nbytes, max_bytes_);
+ rw_throw (ex_bad_alloc, __FILE__, __LINE__, 0,
+ "allocate (%zu): reached size limit of %zu",
+ nbytes, max_bytes_);
return operator_new (nbytes, false);
}
@@ -229,9 +200,9 @@
// increment the exception counter for this function
++n_throws_ [mf];
- _rw_throw_exception (__FILE__, __LINE__,
- "UserAlloc::%s: reached call limit of %zu",
- _rw_func_names [mf], throw_at_calls_);
+ rw_throw (ex_bad_alloc, __FILE__, __LINE__, 0,
+ "UserAlloc::%s: reached call limit of %zu",
+ _rw_func_names [mf], throw_at_calls_);
RW_ASSERT (!"logic error: should not reach");
}
Modified: incubator/stdcxx/trunk/tests/src/new.cpp
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/src/new.cpp?rev=423404&r1=423403&r2=423404&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/new.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/new.cpp Wed Jul 19 01:48:38 2006
@@ -28,14 +28,13 @@
// expand _TEST_EXPORT macros
#define _RWSTD_TEST_SRC
-#include <new> // for bad_alloc
-
-#include <stdlib.h> // for abort(), free(), getenv(), malloc()
+#include <stdlib.h> // for abort(), getenv()
#include <string.h> // for memset()
#include <driver.h>
#include <rw_printf.h>
#include <rw_new.h>
+#include <rw_exception.h> // for rw_throw()
/************************************************************************/
@@ -272,16 +271,6 @@
}
-struct BadAlloc: _RWSTD_BAD_ALLOC
-{
- char what_ [4096];
-
- /* virtual */ const char* what () const _THROWS (()) {
- return what_;
- }
-};
-
-
static size_t seq_gen; // sequence number generator
@@ -365,48 +354,47 @@
|| reached_block_limit
|| reached_size_limit) {
- BadAlloc ex;
-
#ifndef _RWSTD_NO_EXCEPTIONS
- rw_snprintfa (ex.what_, 4096U, "%s:%d: %s (%zu) ",
- __FILE__, __LINE__, name [array], nbytes);
-
- strcat (ex.what_, "threw bad_alloc: ");
-
- if (reached_call_limit)
-
- rw_snprintfa (
- ex.what_ + strlen (ex.what_), 4096U - strlen (ex.what_),
- "reached call limit of %zu", pst->new_calls_ [array]);
-
- else if (reached_block_limit)
-
- rw_snprintfa (
- ex.what_ + strlen (ex.what_), 4096U - strlen (ex.what_),
- "reached block limit of %zu: %zu",
- *pst->throw_at_blocks_ [array], pst->blocks_ [array]);
+ try {
+ const char * threw = "threw bad_alloc:";
- else if (reached_size_limit)
-
- rw_snprintfa (
- ex.what_ + strlen (ex.what_), 4096U - strlen (ex.what_),
- "reached size limit of %zu: %zu",
- *pst->throw_at_bytes_ [array], pst->bytes_ [array]);
-
- if (trace_sequence [0] <= seq_gen && seq_gen < trace_sequence [1])
- rw_fprintf (rw_stderr, "%s\n", ex.what ());
+ if (reached_call_limit)
+ rw_throw (ex_bad_alloc,
+ __FILE__, __LINE__, name [array],
+ "(%zu) %s reached call limit of %zu",
+ nbytes, threw, pst->new_calls_ [array]);
+
+ else if (reached_block_limit)
+ rw_throw (ex_bad_alloc,
+ __FILE__, __LINE__, name [array],
+ "(%zu) %s reached block limit of %zu: %zu",
+ nbytes, threw, *pst->throw_at_blocks_ [array],
+ pst->blocks_ [array]);
+
+ else if (reached_size_limit)
+ rw_throw (ex_bad_alloc,
+ __FILE__, __LINE__, name [array],
+ "(%zu) %s reached size limit of %zu: %zu",
+ nbytes, threw, *pst->throw_at_bytes_ [array],
+ pst->bytes_ [array]);
+ }
+ catch (const std::exception & ex) {
+ if (trace_sequence [0] <= seq_gen && seq_gen < trace_sequence [1])
+ rw_fprintf (rw_stderr, "%s\n", ex.what ());
- throw ex;
+ throw;
+ }
#else // if defined (_RWSTD_NO_EXCEPTIONS)
if (reached_breakpoint) {
- rw_snprintfa (
- ex.what_ + strlen (ex.what_), 4096U - strlen (ex.what_),
+ char buf[4096];
+
+ rw_snprintfa (buf, sizeof(buf),
"reached a breakpoint at of %zu calls", *pst->break_at_seqno_);
- _RW::__rw_assert_fail (ex.what_, __FILE__, __LINE__, name [array]);
+ _RW::__rw_assert_fail (buf, __FILE__, __LINE__, name [array]);
}
if (trace_sequence [0] <= seq_gen && seq_gen < trace_sequence [1])
@@ -426,16 +414,21 @@
ptr = malloc (block_size);
if (!ptr) {
- BadAlloc ex;
- rw_snprintfa (ex.what_, 4096U,
- "%s:%d: %s (%zu) threw bad_alloc: malloc() returned 0",
- __FILE__, __LINE__, name [array], nbytes);
-
- if (trace_sequence [0] <= seq_gen && seq_gen < trace_sequence [1])
- rw_fprintf (rw_stderr, "%s\n", ex.what ());
#ifndef _RWSTD_NO_EXCEPTIONS
- throw ex;
+
+ try {
+ rw_throw (ex_bad_alloc,
+ __FILE__, __LINE__, name [array],
+ "(%zu): malloc() returned 0", nbytes);
+ }
+ catch (const std::exception & ex) {
+ if (trace_sequence [0] <= seq_gen && seq_gen < trace_sequence [1])
+ rw_fprintf (rw_stderr, "%s\n", ex.what ());
+
+ throw;
+ }
+
#else // if defined (_RWSTD_NO_EXCEPTIONS)
return ptr;
#endif // _RWSTD_NO_EXCEPTIONS
@@ -514,7 +507,6 @@
Header* const hdr = _rw_find_block (ptr, true, name [array]);
if (!hdr) {
-
// hdr should never be 0 except under special circumstances
// such as when the compiler's runtime library itself passes
// the wrong argument to operator delete (such as libcxx