CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
Raii.h File Reference

Shared RAII wrappers for firmware resources. More...

#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "nvs.h"
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>

Go to the source code of this file.

Classes

struct  cdc::core::CapsFreeDeleter
 Deleter for buffers allocated via heap_caps_malloc. More...
struct  cdc::core::CStdFreeDeleter
 Deleter for buffers allocated via malloc/realloc. More...
struct  cdc::core::FileCloseDeleter
 Deleter for FILE* opened with std::fopen. More...
class  cdc::core::NvsScope
 RAII wrapper for an NVS handle. More...
class  cdc::core::MutexGuard
 RAII wrapper for a FreeRTOS semaphore / mutex. More...
class  cdc::core::RecursiveMutexGuard
 Scoped guard for a FreeRTOS recursive mutex. More...

Namespaces

namespace  cdc
namespace  cdc::core

Typedefs

template<typename T>
using cdc::core::PsramUniquePtr = std::unique_ptr<T[], CapsFreeDeleter>
template<typename T>
using cdc::core::CStdUniquePtr = std::unique_ptr<T, CStdFreeDeleter>
 unique_ptr for buffers allocated via realloc/malloc (e.g. esp_http_client body chunks).
using cdc::core::FilePtr = std::unique_ptr<std::FILE, FileCloseDeleter>
 unique_ptr for FILE* handles. Destructor calls std::fclose.

Functions

template<typename T>
PsramUniquePtr< T > cdc::core::psramAlloc (std::size_t count) noexcept
 Allocate count elements of T in PSRAM (8-bit capable region).
FilePtr cdc::core::openFile (const char *path, const char *mode) noexcept
 Open a FILE* and wrap it in a FilePtr.

Detailed Description

Shared RAII wrappers for firmware resources.

Single source of truth for resource ownership across the codebase. Each type wraps a raw handle so destructors run on every exit path and manual cleanup pairs disappear. Mirrors Rust's Box<T> + Drop idiom: ownership is unique, copying is forbidden, moving transfers the handle.

Provided wrappers
  • PsramUniquePtr<T> - PSRAM array allocations via heap_caps
  • CStdUniquePtr<T> - malloc/realloc buffers freed with std::free
  • FilePtr - FILE* freed with std::fclose
  • NvsScope - nvs_handle_t opened on construction, closed on destruction
  • MutexGuard - FreeRTOS semaphore taken on construction, given on destruction

Definition in file Raii.h.