BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
sublibc.h File Reference

Utility functions for memory management and string operations. More...

#include <string.h>
#include <cstddef>
#include <cstdio>
#include <string>
#include <beep8.h>
#include <mt.h>

Go to the source code of this file.

Macros

#define memsetz(addr_, bytesize_)
 

Functions

template<typename TYPE , std::size_t SIZE>
std::size_t numof (const TYPE(&)[SIZE])
 Retrieve the size of an array.
 
template<class T >
constexpr auto wrap_at (const T &c, std::size_t i) -> const typename T::value_type &
 Returns a wrapped reference to an element at index i in container c.
 
template<class T >
constexpr auto clamp_to_edge (const T &c, std::size_t i) -> const typename T::value_type &
 Returns a clamped reference to an element at index i in container c.
 
template<typename T , std::size_t N>
constexpr const T & random_at (const T(&array)[N])
 
template<typename T >
constexpr const T::value_type & random_at (const T &container, const typename T::value_type &default_value=typename T::value_type{})
 
std::string fs (const char *format,...)
 Format a string with variable arguments.
 
void * mallocz (size_t bytesize_)
 Allocate and zero-initialize a block of memory.
 
void print_mallinfo ()
 Print memory allocation information.
 

Detailed Description

Utility functions for memory management and string operations.

This module provides a collection of helper functions for memory management and string operations in C++ programs. The functions include:

  • print_mallinfo: Displays current dynamic memory allocation statistics.
  • mallocz: Allocates a block of memory and initializes it to zero.
  • fs: Formats a string with variable arguments similar to printf.
  • memsetz: Zero-initializes a block of memory.
  • numof: Returns the number of elements in a static array.

These functions are intended to simplify common tasks and improve code readability and maintainability.

Note: This module is not thread-safe and is intended for use in single-threaded environments or where thread-safety is managed externally.

Example usage:

#include <sublibc.h>
#include <iostream>
int main() {
// Allocate and zero-initialize memory
size_t size = 100;
void* ptr = mallocz(size);
if (ptr != nullptr) {
std::cout << "Memory allocated and zero-initialized." << std::endl;
free(ptr);
}
// Print memory allocation information
// Format a string
std::string formatted = fs("Hello, %s! You have %d new messages.", "Alice", 5);
std::cout << formatted << std::endl;
// Get the number of elements in an array
int arr[10];
size_t arr_size = numof(arr);
std::cout << "Array size: " << arr_size << std::endl;
return 0;
}
Utility functions for memory management and string operations.
void * mallocz(size_t bytesize_)
Allocate and zero-initialize a block of memory.
Definition sublibc.cpp:25
std::size_t numof(const TYPE(&)[SIZE])
Retrieve the size of an array.
Definition sublibc.h:77
void print_mallinfo()
Print memory allocation information.
Definition sublibc.cpp:10
std::string fs(const char *format,...)
Format a string with variable arguments.
Definition sublibc.cpp:32

The functions provided by this module are not mandatory for use but are designed to aid in the development and debugging of applications by offering convenient utilities.

Note
This module is intended for debugging purposes and its use is optional.

Macro Definition Documentation

◆ memsetz

#define memsetz ( addr_,
bytesize_ )
Value:
memset(addr_,0x00,bytesize_)

Function Documentation

◆ clamp_to_edge()

template<class T >
auto clamp_to_edge ( const T & c,
std::size_t i ) -> const typename T::value_type&
constexpr

Returns a clamped reference to an element at index i in container c.

This function accesses the element at index i in the container. If i is greater than or equal to c.size(), the function returns the last element (c.back()). If the container is empty, the behavior is undefined.

Template Parameters
TThe type of the container. It must provide:
  • a size() member function returning a std::size_t count of elements
  • an index operator operator[]
  • a back() member function returning a reference to the last element
Parameters
cThe container to be accessed.
iThe index to be clamped. If i is within the range [0, c.size() - 1], the function returns c[i]. Otherwise, it returns c.back().
Returns
A const reference to the element at position i if i is in range, or a reference to the last element if i is out of range.
Note
The behavior is undefined if c is empty (i.e., if c.size() == 0).
// Example container for animation frames
constexpr std::array<u8, 7> animFrames = {0,1,2,2,0,3,4};
// Example usage:
// Suppose we want to safely access a frame at position x,
// clamping out-of-range indices to the last frame:
u8 frame = clamp_to_edge(animFrames, x);
// If x = 9 (out of range), the function returns animFrames.back() (i.e., 4).
// If x = 3 (in range), the function returns animFrames[3] (i.e., 2).
constexpr auto clamp_to_edge(const T &c, std::size_t i) -> const typename T::value_type &
Returns a clamped reference to an element at index i in container c.
Definition sublibc.h:149
Examples
/Users/kzm/git/beep8-sdk/sdk/b8helper/include/sublibc.h.

◆ fs()

std::string fs ( const char * format,
... )
extern

Format a string with variable arguments.

This function takes a format string and a variable number of arguments, formats the string, and returns the resulting std::string.

Parameters
formatThe format string (same format as printf)
...Variable arguments to be used in the format string
Returns
The formatted std::string
Note
The function uses a fixed-size internal buffer of 256 characters. Output that exceeds this size will be truncated.
Examples
/Users/kzm/git/beep8-sdk/sdk/b8helper/include/sublibc.h.

◆ mallocz()

void * mallocz ( size_t bytesize_)
extern

Allocate and zero-initialize a block of memory.

This function allocates a block of memory of the specified size, and then initializes it to zero.

Parameters
bytesize_The size of the memory block to allocate, in bytes.
Returns
A pointer to the allocated and zero-initialized memory block, or NULL if the allocation failed.
Examples
/Users/kzm/git/beep8-sdk/sdk/b8helper/include/sublibc.h.

◆ numof()

template<typename TYPE , std::size_t SIZE>
std::size_t numof ( const TYPE(&)[SIZE])

Retrieve the size of an array.

This template function returns the number of elements in a given array.

Template Parameters
TYPEThe type of the array elements.
SIZEThe size of the array.
Parameters
arrayThe array whose size is to be determined.
Returns
The number of elements in the array.
Examples
/Users/kzm/git/beep8-sdk/sdk/b8helper/include/sublibc.h.

◆ print_mallinfo()

void print_mallinfo ( )
extern

Print memory allocation information.

This function retrieves memory allocation information from the system and prints it to the standard output. The information includes various metrics related to the current state of dynamic memory allocation.

Examples
/Users/kzm/git/beep8-sdk/sdk/b8helper/include/sublibc.h.

◆ wrap_at()

template<class T >
auto wrap_at ( const T & c,
std::size_t i ) -> const typename T::value_type&
constexpr

Returns a wrapped reference to an element at index i in container c.

This function accesses the element at i modulo the size of the container, allowing cyclic (wrapping) indexing. If i is larger than c.size(), it wraps around to the beginning.

Template Parameters
TThe type of the container. It must provide:
  • a size() member function returning a std::size_t count of elements
  • an index operator operator[]
Parameters
cThe container to be accessed.
iThe index to be wrapped. If i >= c.size(), the index wraps around so the final access is c[i % c.size()].
Returns
A const reference to the element at position i % c.size().
Note
The behavior is undefined if c is empty (i.e., if c.size() == 0).
// Example container for animation frames
constexpr std::array<u8, 7> animFrames = {0,1,2,2,0,3,4};
// Example usage:
// Suppose we want to index based on xPos shifted right by 1,
// but in a cyclical manner.
// We can retrieve the wrapped element like so:
u8 frame = wrap_at(animFrames, x);
constexpr auto wrap_at(const T &c, std::size_t i) -> const typename T::value_type &
Returns a wrapped reference to an element at index i in container c.
Definition sublibc.h:111
Examples
/Users/kzm/git/beep8-sdk/sdk/b8helper/include/sublibc.h.