LibJuno 1.0.4
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
pointer_api.h
Go to the documentation of this file.
1/*
2 MIT License
3
4 Copyright (c) 2025 Robin A. Onsay
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16*/
17
35#ifndef JUNO_POINTER_API_H
36#define JUNO_POINTER_API_H
37#include "juno/macros.h"
38#include "juno/status.h"
39#include "juno/module.h"
40#include <stddef.h>
41#include <stdalign.h>
42#include <stdint.h> // for uintptr_t used in type/alignment verification macros
43#include "juno/types.h"
44#ifdef __cplusplus
45extern "C"
46{
47#endif
48
49typedef struct JUNO_POINTER_TAG JUNO_POINTER_T;
52
53
55// @{"req": ["REQ-POINTER-001"]}
56struct JUNO_POINTER_TAG JUNO_TRAIT_ROOT(JUNO_POINTER_API_T,
58 void *pvAddr;
60 size_t zSize;
62 size_t zAlignment;
63);
64
86
87
95{
98 JUNO_RESULT_BOOL_T (*Equals)(const JUNO_POINTER_T tLeft, const JUNO_POINTER_T tRight);
99};
100
104JUNO_MODULE_RESULT(JUNO_RESULT_POINTER_T, JUNO_POINTER_T);
108JUNO_MODULE_OPTION(JUNO_OPTION_POINTER_T, JUNO_POINTER_T);
109
119#define JunoMemory_PointerInit(ptApi, TYPE_T, pvAddr) (JUNO_POINTER_T){ptApi, pvAddr, sizeof(TYPE_T), alignof(TYPE_T)}
129// @{"req": ["REQ-POINTER-005"]}
130#define JunoMemory_PointerVerifyType(pointer, type, tApi) \
131(( \
132 JunoMemory_PointerVerify(pointer) == JUNO_STATUS_SUCCESS && \
133 pointer.ptApi == &tApi && \
134 pointer.zSize == sizeof(type) && \
135 pointer.zAlignment == alignof(type) && \
136 (uintptr_t) pointer.pvAddr % pointer.zAlignment == 0 \
137)?JUNO_STATUS_SUCCESS:JUNO_STATUS_ERR)
138
148#define JUNO_ASSERT_POINTER_TYPE(tStatus, tPointer, tType, tApi) JUNO_ASSERT_SUCCESS((tStatus = JunoMemory_PointerVerifyType(tPointer, tType, tApi)), return tStatus)
149
156// @{"req": ["REQ-POINTER-002", "REQ-POINTER-003"]}
158{
160 ptPointerApi &&
161 ptPointerApi->Copy &&
162 ptPointerApi->Reset
163 );
164 return JUNO_STATUS_SUCCESS;
165}
166
174{
176 ptPointerApi &&
177 ptPointerApi->Equals
178 );
179 return JUNO_STATUS_SUCCESS;
180}
181
188// @{"req": ["REQ-POINTER-004"]}
190{
192 tPointer.ptApi &&
193 tPointer.pvAddr &&
194 tPointer.zSize
195 );
196 JUNO_STATUS_T tStatus = JunoMemory_PointerApiVerify(tPointer.ptApi);
197 JUNO_ASSERT_SUCCESS(tStatus, return tStatus);
198 return tStatus;
199}
200
201#ifdef __cplusplus
202}
203#endif
204#endif // JUNO_POINTER_API_H
205
#define JUNO_ASSERT_EXISTS(ptr)
Returns JUNO_STATUS_NULLPTR_ERROR if the expression is falsy.
Definition macros.h:51
#define JUNO_ASSERT_SUCCESS(tStatus,...)
Execute the provided failure operation(s) if status is not success.
Definition macros.h:89
static JUNO_STATUS_T JunoMemory_ValuePointerApiVerify(const JUNO_VALUE_POINTER_API_T *ptPointerApi)
Verify that a value-pointer API provides required functions.
Definition pointer_api.h:173
static JUNO_STATUS_T JunoMemory_PointerApiVerify(const JUNO_POINTER_API_T *ptPointerApi)
Verify that a pointer API provides required functions.
Definition pointer_api.h:157
static JUNO_STATUS_T JunoMemory_PointerVerify(const JUNO_POINTER_T tPointer)
Verify a pointer descriptor (API non-null, address non-null, size non-zero).
Definition pointer_api.h:189
#define JUNO_MODULE_RESULT(NAME_T, OK_T)
Define a result type combining a status and a success payload.
Definition module.h:198
#define JUNO_TRAIT_ROOT(API_T,...)
Define a trait root carrying only the API pointer and members.
Definition module.h:145
#define JUNO_MODULE_OPTION(NAME_T, SOME_T)
Define an option type combining a presence flag and a payload.
Definition module.h:242
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:59
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:51
Common assertion and helper macros for LibJuno modules.
Module system and dependency injection primitives for LibJuno.
struct JUNO_POINTER_TAG JUNO_POINTER_T
Definition pointer_api.h:49
Status codes and failure-handling helpers for LibJuno.
Pointer operations API (copy/reset).
Definition pointer_api.h:75
JUNO_STATUS_T(* Reset)(JUNO_POINTER_T tPointer)
Reset the memory at the pointer (e.g., zero-initialize).
Definition pointer_api.h:84
JUNO_STATUS_T(* Copy)(JUNO_POINTER_T tDest, const JUNO_POINTER_T tSrc)
Copy memory from source to destination.
Definition pointer_api.h:80
Value-pointer operations API (equality by contents).
Definition pointer_api.h:95
JUNO_RESULT_BOOL_T(* Equals)(const JUNO_POINTER_T tLeft, const JUNO_POINTER_T tRight)
Compare two pointer values for equality (by contents).
Definition pointer_api.h:98
Common module result type aliases used throughout LibJuno.