libcruft-util/cruft/util/alloc/null.hpp

68 lines
1.7 KiB
C++
Raw Permalink Normal View History

2015-11-13 17:17:37 +11:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2015-11-13 17:17:37 +11:00
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
2015-11-13 17:17:37 +11:00
#include "../view.hpp"
2015-11-13 17:17:37 +11:00
#include <cstddef>
namespace cruft::alloc {
2015-11-13 17:17:37 +11:00
// 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:
null () = default;
null (const null&) = delete;
null& operator= (const null&) = delete;
template <typename T>
cruft::view<T*>
allocate (size_t count)
{
return allocate<T> (count, alignof (T));
}
template <typename T>
cruft::view<T*>
allocate (size_t count, size_t align)
{
(void)count;
(void)align;
throw std::bad_alloc ();
}
template <typename T>
void
deallocate (cruft::view<T*> ptr)
{
(void)ptr;
CHECK_EQ (ptr.data (), static_cast<const void*> (nullptr));
}
2016-06-22 19:51:18 +10:00
cruft::view<std::byte*> data (void);
cruft::view<const std::byte*> data (void) const;
std::byte* begin (void);
const std::byte* begin (void) const;
std::byte* end (void);
const std::byte* end (void) const;
size_t offset (const void*) const;
void reset (void);
size_t capacity (void) const;
size_t used (void) const;
size_t remain (void) const;
2015-11-13 17:17:37 +11:00
};
2016-10-10 17:58:59 +11:00
}