alloc: add initial allocator stubs
This commit is contained in:
parent
584b57b669
commit
1ec9582240
19
Makefile.am
19
Makefile.am
@ -11,6 +11,23 @@ AM_CXXFLAGS = $(BOOST_CPPFLAGS) $(ZLIB_CFLAGS)
|
||||
UTIL_FILES = \
|
||||
adapter.hpp \
|
||||
adapter.cpp \
|
||||
alloc/affix.cpp \
|
||||
alloc/affix.hpp \
|
||||
alloc/allocator.cpp \
|
||||
alloc/allocator.hpp \
|
||||
alloc/arena.cpp \
|
||||
alloc/arena.hpp \
|
||||
alloc/arena.ipp \
|
||||
alloc/fallback.cpp \
|
||||
alloc/fallback.hpp \
|
||||
alloc/linear.cpp \
|
||||
alloc/linear.hpp \
|
||||
alloc/malloc.cpp \
|
||||
alloc/malloc.hpp \
|
||||
alloc/null.cpp \
|
||||
alloc/null.hpp \
|
||||
alloc/stack.cpp \
|
||||
alloc/stack.hpp \
|
||||
backtrace.hpp \
|
||||
bezier.cpp \
|
||||
bezier.hpp \
|
||||
@ -377,6 +394,8 @@ AM_LDFLAGS += $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(BOOST_SYSTEM_LIB)
|
||||
AM_CXXFLAGS += -I$(top_srcdir)
|
||||
|
||||
TEST_BIN = \
|
||||
test/alloc/stack \
|
||||
test/alloc/linear \
|
||||
test/backtrace \
|
||||
test/bezier \
|
||||
test/bitwise \
|
||||
|
22
alloc/affix.cpp
Normal file
22
alloc/affix.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "./affix.hpp"
|
||||
|
||||
using util::alloc::affix;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
32
alloc/affix.hpp
Normal file
32
alloc/affix.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_AFFIX_HPP
|
||||
#define __UTIL_ALLOC_AFFIX_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace util { namespace alloc {
|
||||
template <class parent, class prefix, class suffix>
|
||||
class affix {
|
||||
void* allocate (size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
void deallocate (void *ptr, size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
};
|
||||
} }
|
||||
|
||||
#include "./affix.hpp"
|
||||
|
||||
#endif
|
1
alloc/allocator.cpp
Normal file
1
alloc/allocator.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "./allocator.hpp"
|
44
alloc/allocator.hpp
Normal file
44
alloc/allocator.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_ALLOCATOR_HPP
|
||||
#define __UTIL_ALLOC_ALLOCATOR_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <utility>
|
||||
|
||||
// C++11 allocator concept conformant allocator adaptor, going from our
|
||||
// allocator interface to that of the STL and friends.
|
||||
namespace util { namespace alloc {
|
||||
template <class B, class T>
|
||||
class allocator {
|
||||
public:
|
||||
typedef T value_type;
|
||||
|
||||
template <typename ...Args>
|
||||
allocator (Args&& ...args);
|
||||
|
||||
T* allocate (std::size_t count);
|
||||
void deallocate (T*, std::size_t count);
|
||||
|
||||
private:
|
||||
B &m_backing;
|
||||
};
|
||||
} }
|
||||
|
||||
#include "./allocator.ipp"
|
||||
|
||||
#endif
|
48
alloc/allocator.ipp
Normal file
48
alloc/allocator.ipp
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __UTIL_ALLOC_ALLOCATOR_IPP
|
||||
#error
|
||||
#endif
|
||||
|
||||
#define __UTIL_ALLOC_ALLOCATOR_IPP
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <class B, class T>
|
||||
template <typename ...Args>
|
||||
util::alloc::allocator<B,T>::allocator (Args&& ...args):
|
||||
m_backing (std::forward (args)...)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <class B, class T>
|
||||
T*
|
||||
util::alloc::allocator<B,T>::allocate (std::size_t count)
|
||||
{
|
||||
return m_backing.template allocate<T> (count);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <class B, class T>
|
||||
void
|
||||
util::alloc::allocator<B,T>::deallocate (T *t, std::size_t count)
|
||||
{
|
||||
return m_backing.template deallocate (t, count);
|
||||
}
|
1
alloc/arena.cpp
Normal file
1
alloc/arena.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "./arena.hpp"
|
44
alloc/arena.hpp
Normal file
44
alloc/arena.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_ARENA_HPP
|
||||
#define __UTIL_ALLOC_ARENA_HPP
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace util { namespace alloc {
|
||||
template <class T>
|
||||
class arena {
|
||||
public:
|
||||
arena (T &store);
|
||||
|
||||
template <typename U>
|
||||
U* acquire (void);
|
||||
|
||||
template <typename U>
|
||||
std::unique_ptr<U> unique (void);
|
||||
|
||||
template <typename U>
|
||||
void release (U*);
|
||||
|
||||
private:
|
||||
T &m_store;
|
||||
};
|
||||
} }
|
||||
|
||||
#include "./arena.hpp"
|
||||
|
||||
#endif
|
68
alloc/arena.ipp
Normal file
68
alloc/arena.ipp
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_ALLOC_ARENA_IPP
|
||||
#error
|
||||
#endif
|
||||
|
||||
#define __UTIL_ALLOC_ARENA_IPP
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <class T>
|
||||
util::alloc::arena<T>::arena (T &store):
|
||||
m_store (store)
|
||||
{ ; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <class T>
|
||||
template <typename U, typename ...Args>
|
||||
U*
|
||||
util::alloc::arena<T>::acquire (Args&& ...args)
|
||||
{
|
||||
U *data = m_store.allocate (sizeof (U), alignof (U));
|
||||
|
||||
try {
|
||||
new (data) U (std::forward (args)...);
|
||||
} catch (...) {
|
||||
m_store.deallocate (data, sizeof (U));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <class T>
|
||||
template <typename U, typename ...Args>
|
||||
std::unique_ptr<U>
|
||||
util::alloc::arena<T>::unique (Args&& ...args)
|
||||
{
|
||||
return std::unique_ptr<U> (acquire (std::forward (args)...));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <class T>
|
||||
template <typename U>
|
||||
void
|
||||
util::alloc::arena<T>::release (U *u)
|
||||
{
|
||||
u->~U ();
|
||||
m_store.deallocate (u);
|
||||
}
|
||||
|
||||
|
1
alloc/fallback.cpp
Normal file
1
alloc/fallback.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "./fallback.hpp"
|
33
alloc/fallback.hpp
Normal file
33
alloc/fallback.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_FALLBACK_HPP
|
||||
#define __UTIL_ALLOC_FALLBACK_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace util { namespace alloc {
|
||||
template <class A, class B>
|
||||
class fallback {
|
||||
public:
|
||||
fallback (A&, B&);
|
||||
|
||||
void* allocate (size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
void deallocate (void *ptr, size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
};
|
||||
} }
|
||||
|
||||
#endif
|
63
alloc/linear.cpp
Normal file
63
alloc/linear.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "./linear.hpp"
|
||||
|
||||
#include "../pointer.hpp"
|
||||
#include "../debug.hpp"
|
||||
|
||||
using util::alloc::linear;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
linear::linear (void *begin, void *end):
|
||||
m_begin (reinterpret_cast<char*> (begin)),
|
||||
m_end (reinterpret_cast<char*> (end)),
|
||||
m_cursor (reinterpret_cast<char*> (begin))
|
||||
{
|
||||
CHECK_LE (begin, end);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void*
|
||||
linear::allocate (size_t bytes, size_t alignment)
|
||||
{
|
||||
auto ptr = align (m_cursor, alignment);
|
||||
if (ptr + bytes > m_end)
|
||||
throw std::bad_alloc ();
|
||||
|
||||
m_cursor = ptr + bytes;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
linear::deallocate (void *ptr, size_t bytes, size_t alignment)
|
||||
{
|
||||
(void)ptr;
|
||||
(void)bytes;
|
||||
(void)alignment;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
linear::reset (void)
|
||||
{
|
||||
m_cursor = m_begin;
|
||||
}
|
48
alloc/linear.hpp
Normal file
48
alloc/linear.hpp
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_LINEAR_HPP
|
||||
#define __UTIL_ALLOC_LINEAR_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace util { namespace alloc {
|
||||
class linear {
|
||||
public:
|
||||
linear (const linear&) = delete;
|
||||
linear (linear&&) = delete;
|
||||
linear& operator= (const linear&) = delete;
|
||||
linear& operator= (linear&&) = delete;
|
||||
|
||||
linear (void *begin, void *end);
|
||||
|
||||
void* allocate (size_t bytes, size_t alignment = alignof (std::max_align_t));
|
||||
void deallocate (void *ptr, size_t bytes, size_t alignment = alignof (std::max_align_t));
|
||||
|
||||
void reset (void);
|
||||
|
||||
size_t capacity (void) const;
|
||||
size_t size (void) const;
|
||||
|
||||
protected:
|
||||
char *m_begin, *m_end, *m_cursor;
|
||||
};
|
||||
} }
|
||||
|
||||
#include "./linear.hpp"
|
||||
|
||||
#endif
|
45
alloc/malloc.cpp
Normal file
45
alloc/malloc.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "./malloc.hpp"
|
||||
|
||||
#include "../debug.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
using util::alloc::malloc;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void*
|
||||
malloc::allocate (size_t bytes, size_t align)
|
||||
{
|
||||
// C malloc guarantees maximal alignment
|
||||
(void)align;
|
||||
|
||||
return ::malloc (bytes);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
malloc::deallocate (void *ptr, size_t bytes, size_t align)
|
||||
{
|
||||
(void)bytes;
|
||||
(void)align;
|
||||
|
||||
::free (ptr);
|
||||
}
|
32
alloc/malloc.hpp
Normal file
32
alloc/malloc.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_MALLOC_HPP
|
||||
#define __UTIL_ALLOC_MALLOC_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace util { namespace alloc {
|
||||
class malloc {
|
||||
public:
|
||||
void* allocate (size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
void deallocate (void *ptr, size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
};
|
||||
} }
|
||||
|
||||
|
||||
#endif
|
50
alloc/null.cpp
Normal file
50
alloc/null.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
|
||||
#include "./null.hpp"
|
||||
|
||||
#include "../debug.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using util::alloc::null;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void*
|
||||
null::allocate (size_t bytes, size_t align)
|
||||
{
|
||||
(void)bytes;
|
||||
(void)align;
|
||||
|
||||
throw std::bad_alloc ();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// calling deallocate with a non-null pointer is undefined, but we may as well
|
||||
// let the application continuing running if we're not in a debug context.
|
||||
void
|
||||
null::deallocate (void *ptr, size_t bytes, size_t align)
|
||||
{
|
||||
(void)ptr;
|
||||
(void)bytes;
|
||||
(void)align;
|
||||
|
||||
// cast to void* to assist some of the printing machinary in the assertion
|
||||
CHECK_EQ (ptr, (void*)nullptr);
|
||||
}
|
34
alloc/null.hpp
Normal file
34
alloc/null.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_NULL_HPP
|
||||
#define __UTIL_ALLOC_NULL_HPP
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace util { namespace alloc {
|
||||
// allocator that always fails, throwing bad_alloc. deallocate will
|
||||
// succeed with nullptr as with delete, but is undefined with other values
|
||||
// (it is likely to at least assert).
|
||||
class null {
|
||||
public:
|
||||
void* allocate (size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
void deallocate (void *ptr, size_t bytes, size_t align = alignof (std::max_align_t));
|
||||
};
|
||||
} }
|
||||
|
||||
#endif
|
101
alloc/stack.cpp
Normal file
101
alloc/stack.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "./stack.hpp"
|
||||
|
||||
#include "../debug.hpp"
|
||||
#include "../pointer.hpp"
|
||||
|
||||
using util::alloc::stack;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
stack::stack (void *begin, void *end):
|
||||
m_begin (reinterpret_cast<char*> (begin)),
|
||||
m_end (reinterpret_cast<char*> (end)),
|
||||
m_cursor (reinterpret_cast<char*> (begin))
|
||||
{
|
||||
CHECK_LE (m_begin, m_end);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
union record {
|
||||
using offset_t = uint32_t;
|
||||
|
||||
char *as_bytes;
|
||||
offset_t *as_uint32;
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
constexpr auto MIN_ALIGNMENT = sizeof (record::offset_t);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void*
|
||||
stack::allocate (size_t bytes, size_t alignment)
|
||||
{
|
||||
alignment = util::max (MIN_ALIGNMENT, alignment);
|
||||
|
||||
// reserve space at the front of the allocation to record the total
|
||||
// allocation size so we can account for alignment if required.
|
||||
auto ptr = m_cursor + sizeof (record::offset_t);
|
||||
|
||||
// align the outgoing pointer if required
|
||||
ptr = align (ptr, alignment);
|
||||
|
||||
// ensure we haven't overrun our allocated segment
|
||||
if (ptr + bytes > m_end)
|
||||
throw std::bad_alloc ();
|
||||
|
||||
// store the total size and record the new stack head
|
||||
record record;
|
||||
record.as_bytes = ptr - sizeof (record::offset_t);
|
||||
*record.as_uint32 = ptr - m_cursor;
|
||||
|
||||
m_cursor = ptr + bytes;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
stack::deallocate (void *_ptr, size_t bytes, size_t alignment)
|
||||
{
|
||||
(void)bytes;
|
||||
|
||||
alignment = util::max (MIN_ALIGNMENT, alignment);
|
||||
|
||||
auto ptr = reinterpret_cast<char*> (_ptr);
|
||||
|
||||
record record;
|
||||
record.as_bytes = ptr - sizeof (record::offset_t);
|
||||
|
||||
CHECK_LE (bytes, *record.as_uint32);
|
||||
CHECK_GE (m_cursor - *record.as_uint32, m_begin);
|
||||
|
||||
m_cursor -= *record.as_uint32 + bytes;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
stack::reset (void)
|
||||
{
|
||||
m_cursor = m_begin;
|
||||
}
|
45
alloc/stack.hpp
Normal file
45
alloc/stack.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_ALLOC_STACK_HPP
|
||||
#define __UTIL_ALLOC_STACK_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace util { namespace alloc {
|
||||
class stack {
|
||||
public:
|
||||
stack (const stack&) = delete;
|
||||
stack (stack&&) = delete;
|
||||
stack& operator= (const stack&) = delete;
|
||||
stack& operator= (stack&&) = delete;
|
||||
|
||||
stack (void *begin, void *end);
|
||||
|
||||
//[[gnu::alloc_align (2), gnu::alloc_size (1), gnu::returns_nonnull, gnu::warn_unused_result]
|
||||
void *allocate (size_t bytes, size_t alignment = alignof (std::max_align_t));
|
||||
void deallocate (void *ptr, size_t bytes, size_t alignment = alignof (std::max_align_t));
|
||||
|
||||
void reset (void);
|
||||
|
||||
private:
|
||||
char *m_begin, *m_end, *m_cursor;
|
||||
};
|
||||
} }
|
||||
|
||||
#endif
|
23
alloc/stack.ipp
Normal file
23
alloc/stack.ipp
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifdef __UTIL_ALLOC_STACK_IPP
|
||||
#error
|
||||
#endif
|
||||
|
||||
#define __UTIL_ALLOC_STACK_IPP
|
||||
|
||||
|
38
test/alloc/linear.cpp
Normal file
38
test/alloc/linear.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "tap.hpp"
|
||||
#include "alloc/linear.hpp"
|
||||
|
||||
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
util::TAP::logger tap;
|
||||
|
||||
constexpr size_t BUFFER_SIZE = 1024;
|
||||
|
||||
alignas (std::max_align_t) char memory[BUFFER_SIZE];
|
||||
util::alloc::linear store (std::begin (memory), std::end (memory));
|
||||
|
||||
tap.expect_throw<std::bad_alloc> (
|
||||
[&] (void) { store.allocate (BUFFER_SIZE + 1, 1); },
|
||||
"excessive allocation throws bad_alloc"
|
||||
);
|
||||
|
||||
tap.expect_nothrow (
|
||||
[&] (void) { store.allocate (BUFFER_SIZE); },
|
||||
"maximum allocation succeeds"
|
||||
);
|
||||
|
||||
tap.expect_throw<std::bad_alloc> (
|
||||
[&] (void) { store.allocate (1, 1); },
|
||||
"minimum allocation fails after exhaustion"
|
||||
);
|
||||
|
||||
store.reset ();
|
||||
|
||||
tap.expect_nothrow (
|
||||
[&] (void) { store.allocate (1, 1); },
|
||||
"minimum allocation succeeds after reset"
|
||||
);
|
||||
|
||||
return tap.status ();
|
||||
}
|
68
test/alloc/stack.cpp
Normal file
68
test/alloc/stack.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
#include "tap.hpp"
|
||||
#include "alloc/stack.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
n_allocations (util::alloc::stack &store,
|
||||
unsigned count,
|
||||
size_t bytes,
|
||||
size_t alignment = alignof (std::max_align_t))
|
||||
{
|
||||
for (unsigned i = 0; i < count; ++i) {
|
||||
auto ptr = store.allocate (bytes, alignment);
|
||||
store.deallocate (ptr, bytes, alignment);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
util::TAP::logger tap;
|
||||
|
||||
constexpr size_t BUFFER_AVAILABLE = 32;
|
||||
constexpr size_t BUFFER_REQUEST = BUFFER_AVAILABLE - alignof (std::max_align_t);
|
||||
constexpr size_t BUFFER_PAD = 32;
|
||||
constexpr size_t BUFFER_SIZE = BUFFER_AVAILABLE + BUFFER_PAD;
|
||||
|
||||
// alignment is kinda important, so make it a little easier and ensure
|
||||
// something suitable right off the bat.
|
||||
alignas (std::max_align_t) char memory[BUFFER_SIZE];
|
||||
std::fill (std::begin (memory), std::end (memory), 0);
|
||||
|
||||
util::alloc::stack store (memory, memory + BUFFER_AVAILABLE);
|
||||
|
||||
// larger than total allocations should throw
|
||||
tap.expect_throw<std::bad_alloc> (
|
||||
[&store] (void) { store.allocate (BUFFER_AVAILABLE + 1, 1); },
|
||||
"excessive allocation throws bad_alloc"
|
||||
);
|
||||
|
||||
// try a large number of allocations so we exercise the frame handling and
|
||||
// alignment routines.
|
||||
tap.expect_nothrow (
|
||||
[&store] (void) { n_allocations (store, BUFFER_AVAILABLE, BUFFER_REQUEST); },
|
||||
"repeated allocation/deallocation"
|
||||
);
|
||||
|
||||
// perform two near maximum allocations and check for exhaustion through
|
||||
// bad_alloc
|
||||
auto ptr = store.allocate (BUFFER_REQUEST);
|
||||
(void)ptr;
|
||||
|
||||
tap.expect_throw<std::bad_alloc> (
|
||||
[&store] (void) { store.allocate (BUFFER_REQUEST); },
|
||||
"bad_alloc thrown on exhaustion"
|
||||
);
|
||||
|
||||
// try many allocations again after resetting the allocator to zero usage
|
||||
store.reset ();
|
||||
tap.expect_nothrow (
|
||||
[&store] (void) { n_allocations (store, BUFFER_AVAILABLE, BUFFER_REQUEST); },
|
||||
"no bad_alloc after reset"
|
||||
);
|
||||
|
||||
return tap.status ();
|
||||
}
|
Loading…
Reference in New Issue
Block a user