Joe's BOOST_STATIC_ASSERT addition
This commit is contained in:
parent
ca7de05e62
commit
dc06c88758
@ -26,6 +26,7 @@
|
||||
#include <boost/noncopyable.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
@ -43,6 +44,9 @@ namespace persistent_data {
|
||||
template <uint32_t Size = DEFAULT_BUFFER_SIZE, uint32_t Alignment = 512>
|
||||
class buffer : private boost::noncopyable {
|
||||
public:
|
||||
BOOST_STATIC_ASSERT_MSG((Alignment > 1) & !(Alignment & (Alignment - 1)),
|
||||
"Alignment must be a power of two.");
|
||||
|
||||
typedef boost::shared_ptr<buffer> ptr;
|
||||
typedef boost::shared_ptr<buffer const> const_ptr;
|
||||
|
||||
@ -73,7 +77,11 @@ namespace persistent_data {
|
||||
// Allocates size bytes and returns a pointer to the
|
||||
// allocated memory. The memory address will be a
|
||||
// multiple of 'Alignment', which must be a power of two
|
||||
return memalign(Alignment, s);
|
||||
void *mem = memalign(Alignment, s);
|
||||
if (!mem)
|
||||
throw std::bad_alloc();
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
static void operator delete(void *p) {
|
||||
|
@ -79,24 +79,18 @@ BOOST_AUTO_TEST_CASE(buffer_8_a_8_access)
|
||||
buffer<sz, align>::ptr b = create_buffer<sz, align>();
|
||||
|
||||
(*b)[0] = 0;
|
||||
BOOST_CHECK_EQUAL((*b)[0], '\0');
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(buffer_8_a_8_oob)
|
||||
{
|
||||
uint32_t const sz = 8, align = 8;
|
||||
buffer<sz, align>::ptr b = create_buffer<sz, align>();
|
||||
|
||||
BOOST_CHECK_NO_THROW((*b)[8] = 0);
|
||||
BOOST_CHECK_EQUAL((*b)[0], 0);
|
||||
}
|
||||
|
||||
#if 0
|
||||
BOOST_AUTO_TEST_CASE(buffer_8_a_8_const_access)
|
||||
{
|
||||
uint32_t const sz = 8, align = 8;
|
||||
buffer<sz, align>::const_ptr b = create_buffer<sz, align>();
|
||||
|
||||
// (*b)[0] = 0; // Compile time error accessing read-only location
|
||||
(*b)[0] = 0; // Compile time error accessing read-only location
|
||||
}
|
||||
#endif
|
||||
|
||||
// 8 byte buffer size, varying alignment from 1 - 7
|
||||
BOOST_AUTO_TEST_CASE(buffer_128_a_1_fails)
|
||||
@ -105,6 +99,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_1_fails)
|
||||
buffer<sz, align>::ptr b = create_buffer<sz, align>();
|
||||
|
||||
BOOST_CHECK(!b);
|
||||
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(buffer_128_a_2_succeeds)
|
||||
|
Loading…
Reference in New Issue
Block a user