libcruft-util/buffer/paged.hpp

61 lines
1.7 KiB
C++
Raw Normal View History

2015-11-11 16:56:35 +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-11 16:56:35 +11:00
*
2018-12-17 15:20:21 +11:00
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
2015-11-11 16:56:35 +11:00
*/
2018-12-17 14:47:11 +11:00
#pragma once
#include "../std.hpp"
#include "../view.hpp"
2015-11-11 16:56:35 +11:00
#include <cstddef>
2018-12-17 14:47:11 +11:00
namespace cruft::buffer {
2018-12-17 14:47:11 +11:00
/// Implements a manually paged memory buffer of a fixed length.
///
/// It can be used to ensure a contiguous memory region of bounded size
/// is available at a later point in execution.
2015-11-11 16:56:35 +11:00
class paged {
public:
2018-12-17 14:47:11 +11:00
using value_type = u08;
2015-11-11 16:56:35 +11:00
2018-12-17 14:47:11 +11:00
paged (size_t bytes);
2015-11-11 16:56:35 +11:00
~paged ();
paged (const paged&) = delete;
2018-12-17 14:47:11 +11:00
paged (paged &&);
2015-11-11 16:56:35 +11:00
paged& operator= (const paged&) = delete;
2018-12-17 15:20:31 +11:00
paged& operator= (paged &&);
2015-11-11 16:56:35 +11:00
2018-12-17 14:47:11 +11:00
value_type* begin (void)&;
value_type* end (void)&;
2015-11-11 16:56:35 +11:00
2018-12-17 14:47:11 +11:00
const value_type* cbegin (void)&;
const value_type* cend (void)&;
2015-11-11 16:56:35 +11:00
2018-12-17 14:47:11 +11:00
const value_type* begin (void) const&;
const value_type* end (void) const&;
2015-11-11 16:56:35 +11:00
2018-12-17 14:47:11 +11:00
void commit (value_type *ptr) { return commit ({ ptr, 1 }); }
void release (value_type *ptr) { return release ({ ptr, 1 }); }
2015-11-11 16:56:35 +11:00
2018-12-17 14:47:11 +11:00
/// Populates the memory range and ensures it is read/write.
void commit (cruft::view<value_type*>);
/// Returns the memory range to the system.
void release (cruft::view<value_type*>);
auto size (void) const { return m_data.size (); }
auto capacity (void) const { return m_data.size (); }
2015-11-11 16:56:35 +11:00
private:
2018-12-17 14:47:11 +11:00
/// Applies memory protection flags to the given memory region.
void apply_prot (cruft::view<value_type*>, int prot);
2015-11-11 16:56:35 +11:00
2018-12-17 14:47:11 +11:00
cruft::view<value_type*> m_data;
2015-11-11 16:56:35 +11:00
};
2017-01-05 15:06:49 +11:00
}