allocator: return a view, not a pointer. for safety.

This commit is contained in:
Danny Robson 2018-04-20 15:03:35 +10:00
parent 443112b33c
commit cbce5803b5

View File

@ -11,16 +11,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_ALLOC_ALLOCATOR_HPP #pragma once
#define __UTIL_ALLOC_ALLOCATOR_HPP
#include "../view.hpp"
#include <cstddef> #include <cstddef>
#include <utility> #include <utility>
// C++11 allocator concept conformant allocator adaptor, going from our // C++11 allocator concept conformant(ish) allocator adaptor, going from our
// allocator interface to that of the STL and friends. // allocator interface to that of the STL and friends.
namespace util::alloc { namespace util::alloc {
template <typename ValueT, typename BackingT> template <typename ValueT, typename BackingT>
@ -35,12 +36,18 @@ namespace util::alloc {
{ ; } { ; }
ValueT* util::view<ValueT*>
allocate (size_t count) allocate (size_t count)
{ {
return reinterpret_cast<ValueT*> ( return {
m_backing.template allocate (sizeof (ValueT) * count) reinterpret_cast<ValueT*> (
); m_backing.template allocate (
sizeof (ValueT) * count,
alignof (ValueT)
)
),
count
};
} }
@ -65,5 +72,3 @@ namespace util::alloc {
return allocator<ValueT,BackingT> (backing); return allocator<ValueT,BackingT> (backing);
} }
} }
#endif