Boost logo

Boost :

From: Matthew Hurd (matt_at_[hidden])
Date: 2004-03-25 11:07:35


I'm a simple fellow and get confused about who is responsible for deleting a
raw buffer, is it me, the library, should it be free or delete? Maybe I get
a pointer the buffer and I must free it. May I get the pointer to the
buffer and I should do nothing... When I create the buffer and send it to
the library, the lib will delete, so I shouldn't but should I malloc or new
the thing?

Any way, to kill annoying mozzie with a hammer I wrote a little policy
buffer, perhaps others might find this useful and it might make its way into
a utility section somewhere in boost.

I also had a goal of the potential for binary compatibility with a struct
buffer { size t size; char * data; } as per WSABUF on win32, and many other
standardish buffers, which limited the choices a little.

hpp attached. Test that passes on vc7.1 below:

Seem trivial enough but there are plenty of colours on the bike shed that
might be wrong ;-)

Comments? If it is desirable I can write a one page doc and put it in the
queue for a mini-review.

Matt Hurd.
__________________________________________

    void data_test()
    {
        net::data_standard b(6, "Hello");

        CPPUNIT_ASSERT_EQUAL( b[0], 'H');
        CPPUNIT_ASSERT_EQUAL( b[1], 'e');
        CPPUNIT_ASSERT_EQUAL( b[2], 'l');
        CPPUNIT_ASSERT_EQUAL( b[3], 'l');
        CPPUNIT_ASSERT_EQUAL( b[4], 'o');
        CPPUNIT_ASSERT_EQUAL( *b.begin(), 'H');
        
        net::data_standard::iterator i = b.begin();
        CPPUNIT_ASSERT_EQUAL( *i, 'H');
        CPPUNIT_ASSERT_EQUAL( *(i+4), 'o');
        ++i;
        CPPUNIT_ASSERT_EQUAL( *i, 'e');
        i++;
        CPPUNIT_ASSERT_EQUAL( *i, 'l');
        --i;
        CPPUNIT_ASSERT_EQUAL( *i, 'e');
        i = b.end();
        i = i - 3;
        CPPUNIT_ASSERT_EQUAL( *i, 'l');
        i = b.begin();
        *i = 'h';
        CPPUNIT_ASSERT_EQUAL( b[0], 'h');
        b[1] = 'u';
        CPPUNIT_ASSERT_EQUAL( std::string( b ), std::string("hullo") );
        std::string c = b;
        CPPUNIT_ASSERT_EQUAL( c, std::string("hullo") );

        net::data_standard::const_iterator ci = b.begin();
        CPPUNIT_ASSERT_EQUAL( *ci, 'h');
        ci = b.end();
        ci = ci - 3;
        CPPUNIT_ASSERT_EQUAL( *ci, 'l');
        CPPUNIT_ASSERT( b.end() - b.begin() == 6);

        // malloc / free example where one side creates and the other
destroys
        net::data<4096, net::create_malloc, net::kill_nothing > source;

        std::string bye = "Goodbye";
        source.overwrite(bye.length() + 1 , bye.c_str());

        net::data<4096, net::create_refer, net::kill_free > dest(
source.size() , source.begin() );

        std::string bye_bye = dest;
        CPPUNIT_ASSERT_EQUAL(bye, bye_bye);
    }
______________

2004-Mar-25 Thu 15:43:55 | net.data | TRACE | 2984 |
Data<4096> constructed for char with 6 incoming |
d:\finray\simple_server\data.hpp:158
2004-Mar-25 Thu 15:43:55 | net.data | TRACE | 2984 | Empty
data<4096> constructed for char |
d:\finray\simple_server\data.hpp:145
2004-Mar-25 Thu 15:43:55 | net.data | TRACE | 2984 |
Data<4096> constructed for char with 8 incoming |
d:\finray\simple_server\data.hpp:158
2004-Mar-25 Thu 15:43:55 | net.data | TRACE | 2984 |
Data<4096> for char has the fat lady singing |
d:\finray\simple_server\data.hpp:165
2004-Mar-25 Thu 15:43:55 | net.data | TRACE | 2984 |
Data<4096> for char has the fat lady singing |
d:\finray\simple_server\data.hpp:165
2004-Mar-25 Thu 15:43:55 | net.data | TRACE | 2984 |
Data<4096> for char has the fat lady singing |
d:\finray\simple_server\data.hpp:165
OK (1)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk