Software APIs
Macros | Functions
memory.h File Reference

(9e8c3c8)

OpenTitan Device Memory Library. More...

#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define ARRAYSIZE(array)   (sizeof(array) / sizeof(array[0]))
 Computes the number of elements in the given array. More...
 

Functions

uint32_t read_32 (const void *ptr)
 Load a word from memory directly, bypassing aliasing rules. More...
 
void write_32 (uint32_t value, void *ptr)
 Store a word to memory directly, bypassing aliasing rules. More...
 
void * memcpy (void *restrict dest, const void *restrict src, size_t len)
 Copy memory between non-overlapping regions. More...
 
void * memset (void *dest, int value, size_t len)
 Set a region of memory to a particular byte value. More...
 
int memcmp (const void *lhs, const void *rhs, size_t len)
 Compare two (potentially overlapping) regions of memory for byte-wise lexicographic order. More...
 
void * memchr (const void *ptr, int value, size_t len)
 Search a region of memory for the first occurence of a particular byte value. More...
 
void * memrchr (const void *ptr, int value, size_t len)
 Search a region of memory for the last occurence of a particular byte value. More...
 

Detailed Description

OpenTitan Device Memory Library.

This library provides memory functions for aligned word accesses, and some useful functions from the C library's <string.h>.

Definition in file memory.h.

Macro Definition Documentation

◆ ARRAYSIZE

#define ARRAYSIZE (   array)    (sizeof(array) / sizeof(array[0]))

Computes the number of elements in the given array.

Note that this can only determine the length of fixed-size arrays. Due to limitations of C, it will incorrectly compute the size of an array passed as a function argument, because those automatically decay into pointers. This function can only be used correctly with:

  • Arrays declared as stack variables.
  • Arrays declared at global scope.
  • Arrays that are members of a struct or union.
Parameters
arrayThe array expression to measure.
Returns
The number of elements in the array, as a size_t.

Definition at line 38 of file memory.h.

Function Documentation

◆ memchr()

void* memchr ( const void *  ptr,
int  value,
size_t  len 
)

Search a region of memory for the first occurence of a particular byte value.

This function conforms to the semantics defined in ISO C11 S7.24.5.1.

Since libbase does not provide a strlen() function, this function can be used as an approximation: memchr(my_str, 0, SIZE_MAX) - my_str.

This function will be provided by the platform's libc implementation for host builds.

Parameters
ptrthe region to search.
valuethe value, converted to a byte, to search for.
lenthe length of the region, in bytes.
Returns
a pointer to the found value, or NULL.

Definition at line 64 of file memory.c.

◆ memcmp()

int memcmp ( const void *  lhs,
const void *  rhs,
size_t  len 
)

Compare two (potentially overlapping) regions of memory for byte-wise lexicographic order.

This function conforms to the semantics defined in ISO C11 S7.24.4.1.

This function will be provided by the platform's libc implementation for host builds.

Parameters
lhsthe left-hand-side of the comparison.
rhsthe right-hand-side of the comparison.
lenthe length of both regions, in bytes.
Returns
a zero, positive, or negative integer, corresponding to the contingencies of lhs == rhs, lhs > rhs, and lhs < rhs (as buffers, not pointers), respectively.

Definition at line 49 of file memory.c.

◆ memcpy()

void* memcpy ( void *restrict  dest,
const void *restrict  src,
size_t  len 
)

Copy memory between non-overlapping regions.

This function conforms to the semantics defined in ISO C11 S7.23.2.1.

This function will be provided by the platform's libc implementation for host builds.

Parameters
destthe region to copy to.
srcthe region to copy from.
lenthe number of bytes to copy.
Returns
the value of dest.

Definition at line 21 of file memory.c.

◆ memrchr()

void* memrchr ( const void *  ptr,
int  value,
size_t  len 
)

Search a region of memory for the last occurence of a particular byte value.

This function is not specified by C11, but is named for a well-known glibc extension.

Parameters
ptrthe region to search.
valuethe value, converted to a byte, to search for.
lenthe length of the region, in bytes.
Returns
a pointer to the found value, or NULL.

Definition at line 76 of file memory.c.

◆ memset()

void* memset ( void *  dest,
int  value,
size_t  len 
)

Set a region of memory to a particular byte value.

This function conforms to the semantics defined in ISO C11 S7.23.6.1.

This function will be provided by the platform's libc implementation for host builds.

Parameters
destthe region to write to.
valuethe value, converted to a byte, to write to each byte cell.
lenthe number of bytes to write.
Returns
the value of dest.

Definition at line 32 of file memory.c.

◆ read_32()

uint32_t read_32 ( const void *  ptr)
inline

Load a word from memory directly, bypassing aliasing rules.

ISO C forbids, in general, casting a pointer to non-character types and reading them, though it is frequently necessary to read exactly one word out of a void *. This function performs that action in a manner which is well-defined.

Of course, ptr must point to word-aligned memory that is at least one word wide. To do otherwise is Undefined Behavior. It goes eithout saying that the memory this function intents to read must be initialized.

This function has reordering properties as weak as a normal, non-atomic, non-volatile load.

Parameters
ptra word-aligned pointer pointed to at least four bytes of memory.
Returns
the word ptr points to.

Definition at line 58 of file memory.h.

◆ write_32()

void write_32 ( uint32_t  value,
void *  ptr 
)
inline

Store a word to memory directly, bypassing aliasing rules.

ISO C forbids, in general, casting a pointer to non-character types and reading them, though it is frequently necessary to write exactly one word to a void *. This function performs that action in a manner which is well-defined.

Of course, ptr must point to word-aligned memory that is at least one word wide. To do otherwise is Undefined Behavior.

This function has reordering properties as weak as a normal, non-atomic, non-volatile load.

Parameters
valuethe value to store.
ptra word-aligned pointer pointed to at least four bytes of memory.

Definition at line 89 of file memory.h.