LibJuno 0.42.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
37#define JUNO_REF(name) REF##name
39#define JUNO_NEW_REF(name) JUNO_MEMORY_T *JUNO_REF(name)
40
42
48
52{
54 void *pvAddr;
56 size_t zSize;
58 size_t iRefCount;
59};
60
62
63typedef union JUNO_MEMORY_ALLOC_TAG JUNO_MEMORY_ALLOC_T;
64typedef struct JUNO_MEMORY_ALLOC_ROOT_TAG JUNO_MEMORY_ALLOC_ROOT_T;
65
67
69{
76 JUNO_STATUS_T (*Get)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *pvRetAddr, size_t zSize);
77
84 JUNO_STATUS_T (*Update)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *ptMemory, size_t zNewSize);
85
92};
93
94
103{
104 if(ptMemory->iRefCount)
105 {
106 ptMemory->iRefCount += 1;
107 }
108 return ptMemory;
109}
110
118static inline void Juno_MemoryPutRef(JUNO_MEMORY_T *ptMemory)
119{
120 if(ptMemory->iRefCount)
121 {
122 ptMemory->iRefCount -= 1;
123 }
124}
125
126
127
128#ifdef __cplusplus
129}
130#endif
131#endif // JUNO_MEMORY_API_H
union JUNO_MEMORY_ALLOC_TAG JUNO_MEMORY_ALLOC_T
Definition memory_api.h:63
static JUNO_MEMORY_T * Juno_MemoryGetRef(JUNO_MEMORY_T *ptMemory)
Definition memory_api.h:102
static void Juno_MemoryPutRef(JUNO_MEMORY_T *ptMemory)
Definition memory_api.h:118
struct JUNO_MEMORY_ALLOC_ROOT_TAG JUNO_MEMORY_ALLOC_ROOT_T
Definition memory_api.h:64
#define JUNO_MODULE_EMPTY
Definition module.h:126
#define JUNO_MODULE_ROOT(API_T,...)
Definition module.h:182
enum JUNO_STATUS_TAG JUNO_STATUS_T
Definition memory_api.h:69
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:84
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:76
JUNO_STATUS_T(* Put)(JUNO_MEMORY_ALLOC_T *ptMem, JUNO_MEMORY_T *pvAddr)
Frees an allocated memory block.
Definition memory_api.h:91
The memory metadata.
Definition memory_api.h:45
uint8_t * ptFreeMem
Definition memory_api.h:46
Structure for an allocated memory segment. Describes the allocated memory with a pointer to the start...
Definition memory_api.h:52
void * pvAddr
Pointer to the allocated memory.
Definition memory_api.h:54
size_t iRefCount
The reference count for this memory.
Definition memory_api.h:58
size_t zSize
Size of the allocated memory, in bytes.
Definition memory_api.h:56