LibJuno 1.0.1
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
55struct JUNO_POINTER_TAG JUNO_TRAIT_ROOT(JUNO_POINTER_API_T,
57 void *pvAddr;
59 size_t zSize;
61 size_t zAlignment;
62);
63
85
86
94{
97 JUNO_RESULT_BOOL_T (*Equals)(const JUNO_POINTER_T tLeft, const JUNO_POINTER_T tRight);
98};
99
103JUNO_MODULE_RESULT(JUNO_RESULT_POINTER_T, JUNO_POINTER_T);
107JUNO_MODULE_OPTION(JUNO_OPTION_POINTER_T, JUNO_POINTER_T);
108
118#define JunoMemory_PointerInit(ptApi, TYPE_T, pvAddr) (JUNO_POINTER_T){ptApi, pvAddr, sizeof(TYPE_T), alignof(TYPE_T)}
128#define JunoMemory_PointerVerifyType(pointer, type, tApi) \
129(( \
130 JunoMemory_PointerVerify(pointer) == JUNO_STATUS_SUCCESS && \
131 pointer.ptApi == &tApi && \
132 pointer.zSize == sizeof(type) && \
133 pointer.zAlignment == alignof(type) && \
134 (uintptr_t) pointer.pvAddr % pointer.zAlignment == 0 \
135)?JUNO_STATUS_SUCCESS:JUNO_STATUS_ERR)
136
146#define JUNO_ASSERT_POINTER_TYPE(tStatus, tPointer, tType, tApi) JUNO_ASSERT_SUCCESS((tStatus = JunoMemory_PointerVerifyType(tPointer, tType, tApi)), return tStatus)
147
155{
157 ptPointerApi &&
158 ptPointerApi->Copy &&
159 ptPointerApi->Reset
160 );
161 return JUNO_STATUS_SUCCESS;
162}
163
171{
173 ptPointerApi &&
174 ptPointerApi->Equals
175 );
176 return JUNO_STATUS_SUCCESS;
177}
178
186{
188 tPointer.ptApi &&
189 tPointer.pvAddr &&
190 tPointer.zSize
191 );
192 JUNO_STATUS_T tStatus = JunoMemory_PointerApiVerify(tPointer.ptApi);
193 JUNO_ASSERT_SUCCESS(tStatus, return tStatus);
194 return tStatus;
195}
196
197#ifdef __cplusplus
198}
199#endif
200#endif // JUNO_POINTER_API_H
201
#define JUNO_ASSERT_EXISTS(ptr)
Returns JUNO_STATUS_NULLPTR_ERROR if the expression is falsy.
Definition macros.h:50
#define JUNO_ASSERT_SUCCESS(tStatus,...)
Execute the provided failure operation(s) if status is not success.
Definition macros.h:87
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:170
static JUNO_STATUS_T JunoMemory_PointerApiVerify(const JUNO_POINTER_API_T *ptPointerApi)
Verify that a pointer API provides required functions.
Definition pointer_api.h:154
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:185
#define JUNO_MODULE_RESULT(NAME_T, OK_T)
Define a result type combining a status and a success payload.
Definition module.h:193
#define JUNO_TRAIT_ROOT(API_T,...)
Define a trait root carrying only the API pointer and members.
Definition module.h:142
#define JUNO_MODULE_OPTION(NAME_T, SOME_T)
Define an option type combining a presence flag and a payload.
Definition module.h:236
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:56
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:49
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:74
JUNO_STATUS_T(* Reset)(JUNO_POINTER_T tPointer)
Reset the memory at the pointer (e.g., zero-initialize).
Definition pointer_api.h:83
JUNO_STATUS_T(* Copy)(JUNO_POINTER_T tDest, const JUNO_POINTER_T tSrc)
Copy memory from source to destination.
Definition pointer_api.h:79
Value-pointer operations API (equality by contents).
Definition pointer_api.h:94
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:97
Common module result type aliases used throughout LibJuno.