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