nrfcxx
0.1.0
C++-17 Framework for Nordic nRF5 Devices
|
Go to the documentation of this file. 6 #ifndef NRFCXX_UTILITY_HPP 7 #define NRFCXX_UTILITY_HPP 11 #include <type_traits> 17 extern const uint32_t __PersistBase;
18 extern const uint32_t __PersistLimit;
43 template <
unsigned int ALIGN_BITS = 2>
50 static constexpr
size_t ignore_mask{(1U << ALIGN_BITS) - 1};
53 static constexpr uintptr_t align_mask = ~static_cast<uintptr_t>(ignore_mask);
96 scoped (
const scoped&) =
delete;
97 scoped& operator= (
const scoped&) =
delete;
98 scoped (scoped&&) =
delete;
99 scoped& operator= (scoped&) =
delete;
161 begin_ = reinterpret_cast<unsigned char*>(align_mask & (ignore_mask + reinterpret_cast<uintptr_t>(begin)));
165 end_ = reinterpret_cast<unsigned char*>(align_mask & reinterpret_cast<uintptr_t>(end));
169 if (end_ <= begin_) {
188 return (end_ - begin_);
194 return (head_ - begin_);
200 return (end_ - head_);
214 span = align_mask & (span + ignore_mask);
215 if ((!head_) || ((head_ + span) > end_)) {
218 void* rv = reinterpret_cast<void *>(head_);
225 unsigned char* begin_;
233 unsigned char* head_;
243 template <
typename T>
251 void display_data<uint8_t> (
const uint8_t* dp,
257 void display_data<uint32_t> (
const uint32_t* dp,
276 template <
typename T>
281 using base_type =
typename std::remove_volatile<T>::type;
335 static constexpr
const uint32_t*
REGION_END = &__PersistLimit;
357 static_assert(
sizeof(*
this) ==
sizeof(uint32_t),
358 "header is too big");
363 #if (51 == NRF_SERIES) || (NRFCXX_DOXYGEN - 0) 375 #elif (52 == NRF_SERIES) 376 static constexpr
unsigned int PAGE_SIZE = 4096;
377 static constexpr
unsigned int BLOCK_SIZE = 512;
418 const uint32_t* end);
481 ptrdiff_t
extract (header_type* buffer =
nullptr,
482 size_t length = 0)
const;
500 int restore (
const header_type* buffer,
537 return erased_ *
sizeof(uint32_t);
545 void erase_ (
const header_type* hp);
548 const header_type*
find_ (uint16_t tag);
Class supporting persistence of tagged records to non-volatile memory.
Definition: utility.hpp:313
int erase(uint16_t tag)
Remove the record with the given tag.
Definition: utility.hpp:437
void erase_(const header_type *hp)
Mark an existing record as erased.
static constexpr unsigned int BLOCK_SIZE
Number of bytes in a flash block.
Definition: utility.hpp:374
void failsafe(FailSafeCode code)
Record a critical system failure and reset the system.
Definition: core.hpp:1692
static constexpr uint16_t TAG_UNUSED
header_type::tag value for the placeholder header of the next record to be written.
Definition: utility.hpp:322
static constexpr const uint32_t * REGION_END
pointer to the first word after the PERSIST memory region.
Definition: utility.hpp:335
size_t available() const
Return the number of unallocated bytes in the region.
Definition: utility.hpp:198
static constexpr uint16_t TAG_ERASED
header_type::tag value for a record that has been erased.
Definition: utility.hpp:318
unsigned int erased_
Number of words in the region covered by erased records.
Definition: utility.hpp:577
constexpr Persist(unsigned int code)
Alternative constructor for uncast failsafe codes.
Definition: utility.hpp:395
~scoped()
Contained pool is cleared on destruction.
Definition: utility.hpp:84
Class supporting allocation from a fixed-size region.
Definition: utility.hpp:44
bool is_cleared() const
Return true iff the region is configured and starts with a used record.
Definition: utility.hpp:512
const header_type * first_
Pointer to the first used and unerased record in the region.
Definition: utility.hpp:564
Default code for violation of a persisted memory region.
constexpr Persist(FailSafeCode code=FailSafeCode::PERSIST_VIOLATION)
Construct the wrapper class.
Definition: utility.hpp:385
const header_type * begin_
Inclusive lower bound of the persisted region.
Definition: utility.hpp:554
static constexpr const uint32_t * REGION_BEGIN
pointer to the first word of the PERSIST memory region.
Definition: utility.hpp:328
const header_type * find_(uint16_t tag)
Locate a record by tag.
void configure(void *begin, void *end, unsigned int code=FailSafeCode::MEMORY_POOL)
Provide the allocation region and failsafe code.
Definition: utility.hpp:150
Exceeded allocated space of a memory pool.
size_t capacity() const
Return the total number of bytes in the region (allocated plus unallocated).
Definition: utility.hpp:186
const header_type * end_
Exclusive upper bound of the persisted region.
Definition: utility.hpp:557
static constexpr unsigned int PAGE_SIZE
Number of bytes in a flash page.
Definition: utility.hpp:368
void clear()
Clear all data from the persisted region.
void display_data(const T *dp, size_t count, uintptr_t base)
Display a block of data on the console.
Definition: utility.hpp:277
constexpr memory_pool()
Construct the instance.
Definition: utility.hpp:126
ptrdiff_t extract(header_type *buffer=nullptr, size_t length=0) const
Extract the active records from the region.
scoped make_scoped()
Construct an RAII holder that maintains the instance as a scratch pool.
Definition: utility.hpp:113
const header_type * unused_
Pointer to the region's unused record.
Definition: utility.hpp:573
const header_type * addReplace(uint16_t tag, const void *data, size_t count)
Add or replace the record with the given tag.
size_t erased() const
Return the number of bytes that can be reclaimed if the region is extracted and restored.
Definition: utility.hpp:535
int gc()
Extract, clear, and restore the page contents.
int restore(const header_type *buffer, size_t length)
Replay extracted records into the persisted region.
size_t size() const
Return the number of allocated bytes in the region.
Definition: utility.hpp:192
const header_type * find(uint16_t tag)
Find the record with the given tag, if it is present.
Definition: utility.hpp:426
Primary header for nrfcxx interface dependencies.
static constexpr unsigned int align_bits
The number of bits used for alignment of the return pointers.
Definition: utility.hpp:60
void clear()
Release all allocations from the pool.
Definition: utility.hpp:179
FailSafeCode
Enumerated constants used in failsafe() calls.
Definition: core.hpp:719
const FailSafeCode code_
Code used in failsafe resets related to persistence failures.
Definition: utility.hpp:551
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
size_t available() const
Return the number of bytes available for payload in a new record.
Definition: utility.hpp:523
int configure(const uint32_t *begin, const uint32_t *end)
Configure the instance to operate on a specific persisted region.
void * allocate(size_t span)
Allocate a block of at least span bytes.
Definition: utility.hpp:211
RAII class to maintain a shared memory pool within a block scope.
Definition: utility.hpp:81