LibJuno 0.22.0
LibJuno is a lightweight C99 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
memory_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
27#ifndef JUNO_MEMORY_API_H
28#define JUNO_MEMORY_API_H
29#include "juno/status.h"
30#include "juno/module.h"
31#include <stddef.h>
32#ifdef __cplusplus
33extern "C"
34{
35#endif
40#define JUNO_MEMORY_BLOCK(name, type, length) static type name[length] = {}
41
45#define JUNO_MEMORY_BLOCK_METADATA(name, length) static JUNO_MEMORY_BLOCK_METADATA_T name[length] = {}
46
48#define JUNO_REF(name) REF##name
50#define JUNO_NEW_REF(name) JUNO_MEMORY_T *JUNO_REF(name)
51
53typedef struct JUNO_MEMORY_BLOCK_TAG JUNO_MEMORY_BLOCK_T;
55
61
65{
67 void *pvAddr;
69 size_t zSize;
71 size_t iRefCount;
72};
73
75
76JUNO_MODULE_DECLARE(JUNO_MEMORY_ALLOC_T);
77JUNO_MODULE_BASE_DECLARE(JUNO_MEMORY_ALLOC_BASE_T);
78
80
82{
89 JUNO_STATUS_T (*Get)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *pvRetAddr, size_t zSize);
90
97 JUNO_STATUS_T (*Update)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *ptMemory, size_t zNewSize);
98
104 JUNO_STATUS_T (*Put)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *pvAddr);
105};
106
107
115static inline JUNO_MEMORY_T * Juno_MemoryGetRef(JUNO_MEMORY_T *ptMemory)
116{
117 if(ptMemory->iRefCount)
118 {
119 ptMemory->iRefCount += 1;
120 }
121 return ptMemory;
122}
123
131static inline void Juno_MemoryPutRef(JUNO_MEMORY_T *ptMemory)
132{
133 if(ptMemory->iRefCount)
134 {
135 ptMemory->iRefCount -= 1;
136 }
137}
138
139
140
141#ifdef __cplusplus
142}
143#endif
144#endif // JUNO_MEMORY_API_H
struct JUNO_MEMORY_BLOCK_TAG JUNO_MEMORY_BLOCK_T
Definition memory_api.h:53
#define JUNO_MODULE_BASE(name, API, members)
Definition module.h:81
#define JUNO_MODULE_EMPTY
Definition module.h:50
#define JUNO_MODULE_BASE_DECLARE(name)
Definition module.h:31
#define JUNO_MODULE_DECLARE(name)
Definition module.h:26
enum JUNO_STATUS_TAG JUNO_STATUS_T
Definition memory_api.h:82
JUNO_STATUS_T(* Update)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *ptMemory, size_t zNewSize)
Updates an existing memory allocation to a new size.
Definition memory_api.h:97
JUNO_STATUS_T(* Get)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *pvRetAddr, size_t zSize)
Allocates memory using the specified memory allocation method.
Definition memory_api.h:89
JUNO_STATUS_T(* Put)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *pvAddr)
Frees an allocated memory block.
Definition memory_api.h:104
The memory metadata.
Definition memory_api.h:58
uint8_t * ptFreeMem
Definition memory_api.h:59
Structure for an allocated memory segment. Describes the allocated memory with a pointer to the start...
Definition memory_api.h:65
void * pvAddr
Pointer to the allocated memory.
Definition memory_api.h:67
size_t iRefCount
The reference count for this memory.
Definition memory_api.h:71
size_t zSize
Size of the allocated memory, in bytes.
Definition memory_api.h:69