mruby/c VM Source Code master (2026/08/06)
Loading...
Searching...
No Matches
alloc.h
Go to the documentation of this file.
1
16
17#ifndef MRBC_SRC_ALLOC_H_
18#define MRBC_SRC_ALLOC_H_
19
20/***** Feature test switches ************************************************/
21/***** System headers *******************************************************/
22//@cond
23#if defined(MRBC_ALLOC_LIBC)
24#include <stdlib.h>
25#endif
26//@endcond
27
28/***** Local headers ********************************************************/
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33/***** Constant values ******************************************************/
34/***** Macros ***************************************************************/
35/***** Typedefs *************************************************************/
40 unsigned int total;
41 unsigned int used;
42 unsigned int free;
43 unsigned int fragmentation;
44};
45
51 unsigned long initial;
52 unsigned long max;
53 unsigned long min;
54};
55
56
57struct VM;
58
59/***** Global variables *****************************************************/
60/***** Function prototypes and inline functions *****************************/
61//@cond
62#if !defined(MRBC_ALLOC_LIBC)
63/*
64 Normally enabled
65*/
66void mrbc_init_alloc(void *ptr, unsigned int size);
67void mrbc_cleanup_alloc(void);
68void *mrbc_raw_alloc(unsigned int size);
69void *mrbc_raw_alloc_no_free(unsigned int size);
70void *mrbc_raw_calloc(unsigned int nmemb, unsigned int size);
71void mrbc_raw_free(void *ptr);
72void *mrbc_raw_realloc(void *ptr, unsigned int size);
73#define mrbc_free(vm,ptr) mrbc_raw_free(ptr)
74#define mrbc_realloc(vm,ptr,size) mrbc_raw_realloc(ptr, size)
75unsigned int mrbc_alloc_usable_size(void *ptr);
76
77#if defined(MRBC_ALLOC_VMID)
78// Enables memory management by VMID.
79void *mrbc_alloc(const struct VM *vm, unsigned int size);
80void *mrbc_calloc(const struct VM *vm, unsigned int nmemb, unsigned int size);
81void mrbc_free_all(const struct VM *vm);
82void mrbc_set_vm_id(void *ptr, int vm_id);
83int mrbc_get_vm_id(void *ptr);
84
85# else
86#define mrbc_alloc(vm,size) mrbc_raw_alloc(size)
87#define mrbc_free_all(vm) ((void)0)
88#define mrbc_set_vm_id(ptr,id) ((void)0)
89#define mrbc_get_vm_id(ptr) 0
90#endif
91
93void mrbc_alloc_start_profiling(void);
94void mrbc_alloc_stop_profiling(void);
95void mrbc_alloc_get_profiling(struct MRBC_ALLOC_PROF *prof);
96void mrbc_alloc_print_statistics(void);
97void mrbc_alloc_print_pool_header(void *pool_header);
98void mrbc_alloc_print_memory_block(void *pool_header);
99void mrbc_alloc_print_memory_pool(void);
100
101
102
103#elif defined(MRBC_ALLOC_LIBC)
104/*
105 use the system (libc) memory allocator.
106*/
107#if defined(MRBC_ALLOC_VMID)
108#error "Can't use MRBC_ALLOC_LIBC with MRBC_ALLOC_VMID"
109#endif
110
111static inline void mrbc_init_alloc(void *ptr, unsigned int size) {}
112static inline void mrbc_cleanup_alloc(void) {}
113static inline void *mrbc_raw_alloc(unsigned int size) {
114 return malloc(size);
115}
116static inline void *mrbc_raw_alloc_no_free(unsigned int size) {
117 return malloc(size);
118}
119static inline void *mrbc_raw_calloc(unsigned int nmemb, unsigned int size) {
120 return calloc(nmemb, size);
121}
122static inline void mrbc_raw_free(void *ptr) {
123 free(ptr);
124}
125static inline void *mrbc_raw_realloc(void *ptr, unsigned int size) {
126 return realloc(ptr, size);
127}
128/*
129 * When MRBC_ALLOC_LIBC is defined, you can not use mrbc_alloc_usable_size()
130 * as malloc_usable_size() is not defined in C99.
131 * If you want to use mrbc_alloc_usable_size(), you need to define like this:
132 *
133 * unsigned int mrbc_alloc_usable_size(void* ptr) {
134 * return malloc_usable_size(ptr);
135 * }
136*/
137static inline void mrbc_free(const struct VM *vm, void *ptr) {
138 free(ptr);
139}
140static inline void * mrbc_realloc(const struct VM *vm, void *ptr, unsigned int size) {
141 return realloc(ptr, size);
142}
143static inline void *mrbc_alloc(const struct VM *vm, unsigned int size) {
144 return malloc(size);
145}
146static inline void mrbc_free_all(const struct VM *vm) {
147}
148static inline void mrbc_set_vm_id(void *ptr, int vm_id) {
149}
150static inline int mrbc_get_vm_id(void *ptr) {
151 return 0;
152}
153#endif // MRBC_ALLOC_LIBC
154//@endcond
155
156
157#ifdef __cplusplus
158}
159#endif
160#endif
void * mrbc_raw_alloc(unsigned int size)
Definition alloc.c:514
void mrbc_alloc_statistics(struct MRBC_ALLOC_STATISTICS *ret)
Definition alloc.c:984
void * mrbc_raw_alloc_no_free(unsigned int size)
Definition alloc.c:631
void * mrbc_raw_realloc(void *ptr, unsigned int size)
Definition alloc.c:806
void mrbc_init_alloc(void *ptr, unsigned int size)
Definition alloc.c:452
unsigned int mrbc_alloc_usable_size(void *ptr)
Definition alloc.c:883
void * mrbc_raw_calloc(unsigned int nmemb, unsigned int size)
Definition alloc.c:686
void mrbc_raw_free(void *ptr)
Definition alloc.c:707
void mrbc_cleanup_alloc(void)
Definition alloc.c:495
for memory allocation profiling functions. if you use this, define MRBC_USE_ALLOC_PROF pre-processor ...
Definition alloc.h:50
unsigned long max
Definition alloc.h:52
unsigned long min
Definition alloc.h:53
unsigned long initial
Definition alloc.h:51
Return value structure for mrbc_alloc_statistics function.
Definition alloc.h:39
unsigned int total
returns total memory.
Definition alloc.h:40
unsigned int used
returns used memory.
Definition alloc.h:41
unsigned int fragmentation
returns memory fragmentation count.
Definition alloc.h:43
unsigned int free
returns free memory.
Definition alloc.h:42
Virtual Machine.
Definition vm.h:150
uint8_t vm_id
vm_id : 1..MAX_VM_COUNT
Definition vm.h:154