• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2011 John Maddock
2 *
3 * Use, modification and distribution is subject to the
4 * Boost Software License, Version 1.0. (See accompanying
5 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
6 */
7 
8 // Test of bug #1252 (https://svn.boost.org/trac/boost/ticket/1252)
9 
10 #include <boost/pool/pool.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/type_traits/alignment_of.hpp>
13 #include <cstring>
14 
15 struct limited_allocator_new_delete
16 {
17    typedef std::size_t size_type;
18    typedef std::ptrdiff_t difference_type;
19 
BOOST_PREVENT_MACRO_SUBSTITUTIONlimited_allocator_new_delete20    static char * malloc BOOST_PREVENT_MACRO_SUBSTITUTION(const size_type bytes)
21    {
22       if(bytes > 1510 * 32)
23          return 0;
24       return new (std::nothrow) char[bytes];
25    }
BOOST_PREVENT_MACRO_SUBSTITUTIONlimited_allocator_new_delete26    static void free BOOST_PREVENT_MACRO_SUBSTITUTION(char * const block)
27    {
28       delete [] block;
29    }
30 };
31 
32 template <class T>
test_alignment(T)33 void test_alignment(T)
34 {
35    unsigned align = boost::alignment_of<T>::value;
36    boost::pool<> p(sizeof(T));
37    unsigned limit = 100000;
38    for(unsigned i = 0; i < limit; ++i)
39    {
40       void* ptr = (p.malloc)();
41       BOOST_TEST(reinterpret_cast<std::size_t>(ptr) % align == 0);
42       // Trample over the memory just to be sure the allocated block is big enough,
43       // if it's not, we'll trample over the next block as well (and our internal housekeeping).
44       std::memset(ptr, 0xff, sizeof(T));
45    }
46 }
47 
48 
main()49 int main()
50 {
51    boost::pool<limited_allocator_new_delete> po(1501);
52    void* p = (po.malloc)();
53    BOOST_TEST(p != 0);
54 
55    test_alignment(char(0));
56    test_alignment(short(0));
57    test_alignment(int(0));
58    test_alignment(long(0));
59    test_alignment(double(0));
60    test_alignment(float(0));
61    test_alignment((long double)(0));
62 #ifndef BOOST_NO_LONG_LONG
63    test_alignment((long long)(0));
64 #endif
65    return boost::report_errors();
66 }
67