Travis Vitek wrote:
Martin Sebor wrote
Travis Vitek wrote:
Martin Sebor wrote:

[EMAIL PROTECTED] wrote:
URL: http://svn.apache.org/viewvc?rev=647908&view=rev
Log:
2008-04-14  Travis Vitek  <[EMAIL PROTECTED]>

        STDCXX-857
        * tests/src/fmt_defs.h: Add flag to struct Buffer to indicate
        who owns the allocated buffer.
I wonder if this flag is really necessary give the maxsize
member of Buffer whose purpose was to avoid reallocation
(the implementation may have fallen short of that goal):
http://svn.apache.org/viewvc?view=rev&revision=381880

I don't think I can use the maxsize field for this, but I have a bit of confusion about this maxsize member.

If maxsize != _RWSTD_UINT_MAX, then the user has provided the necessary format specifier to tell us not to grow the
buffer past a set length. We are still allowed to grow the
buffer, just not past this limit. Since we are allowed to
grow the buffer, even if maxsize is set, I need some way to
indicate that I don't know who allocated the buffer or
where it came from.
Wouldn't (*pbufsize == maxsize) in that case so that _rw_bufcat()
would never need to allocate new space to begin with (or it would
fail trying).


That only tells me if I've reached the maximum allowed buffer size. It
tells me nothing about where the *pbufsize bytes were allocated from,
and that is exactly what I need, right?

Right. But nothing in printf.cpp should ever try to deallocate
the buffer except _rw_bufcat() to reallocate it, and _rw_bufcat()
should avoid reallocating when (*pbufsize == maxsize).


I mean there are cases where (*pbufsize != maxsize) and we don't want to
deallocate the buffer. Consider this case which is taken from the test
library...

  static int
  _rw_vsystem (const char *cmd, va_list va)
  {
      RW_ASSERT (0 != cmd);

      char buffer [256];
      char *buf = buffer;

      size_t bufsize = sizeof buffer;

      rw_vasnprintf (&buf, &bufsize, cmd, va);

      // <snip>
   }

We definitely don't want `buf' to be deallocated, that much is clear.
But how can `maxsize' possibly tell us anything about `buf'?

Here, rw_vasnprintf() should arrange for maxsize to be set to
bufsize so that _rw_bufcat() doesn't try to reallocate it when
it runs out of space. I realize that changes would be need to
make it work this way but it seems that it should be doable
with the existing machinery, no?

Btw., this patch (applied to printf.cpp before rev 647908)
*seems* to do what I'm describing except that rw_printf()
croaks with the error:

stdcxx/tests/src/printf.cpp:1027: rw_vasnprintf(0x7fbffff538, 0x7fbffff530, "%s", va_list) error: errno = 12: Cannot allocate memory

Replacing it with a return statement with the short count
shouldn't be too hard.

Index: tests/src/printf.cpp
===================================================================
--- tests/src/printf.cpp        (revision 647907)
+++ tests/src/printf.cpp        (working copy)
@@ -955,11 +955,18 @@
 #define vacpy DONT_TOUCH_ME

     size_t default_bufsize = 1024;
-    if (0 == pbufsize)
-        pbufsize = &default_bufsize;
+    size_t maxbufsize;

-    Buffer buf = { pbuf, pbufsize, _RWSTD_SIZE_MAX, 0 };
+    if (0 == pbufsize) {
+        pbufsize   = &default_bufsize;
+        maxbufsize = _RWSTD_SIZE_MAX;
+    }
+    else {
+        maxbufsize = *pbufsize;
+    }

+    Buffer buf = { pbuf, pbufsize, maxbufsize, 0 };


Martin

Reply via email to