1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#pragma once
/*
Copyright (c) 2013-2016 Gustav Hartvigsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "defs.h"
#if USE_GC
#include <gc/gc.h>
#endif
S_BEGIN_DECLS
/**
* @file
* @defgroup MemoryM Memory Management
* @addtogroup MemoryM
* @{
* @brief Memory management stuff.
*
* By default libssts does not use a GC, it can be enabled with by setting the
* USE_GC switch in cmake.
*
* Even if you have GC enabled, it is important to make sure that when you do
* not use the GC it will still cleanup all the pointers.
*
* General rule: for every @ref s_malloc() have a @ref s_free() even if you know
* you are running a GC.
*
* The Mark and Sweep method interacts with the the mainloop, freeing the all
* data marked for freeing in one sweep.
*
* In Mark and Sweep s_free adds the pointers to a list of pointers to be freed.
* This method may cause 'stuttering' in the sweep phase of the loop, and is not
* immediate. It may fill up memory and then sweep it all in one go.
*
* When using the Mark and Sweep method you have to run s_mm_cleanup at the end
* of the program.
*/
/**
* Represents what sort of memory management should be used. Used by
* @ref s_mm_init.
*/
typedef enum {
S_MM_TYPE_NONE, /*< NONE, use standard. */
S_MM_TYPE_LIBC, /*< Use libc's malloc/free stuffs */
S_MM_TYPE_GC, /*< Use libgc's garbage collector. */
S_MM_TYPE_MARK_AND_SWEEP /*< Use a 'mark and sweep' style system. */
} SMMType;
/**
* The names of for the enum @ref SMMType.
*/
S_UNUSED static
schar * SMMTypeName[] = {
"NONE",
"libc",
"GC",
"Mark and Sweep",
0x0,
0x0
};
/**
* Get the name of SMMType.
*
* For used in bindings.
*/
S_EXPORTED
schar *
s_mm_type_get_name (SMMType type);
/**
* Set how memory should be dealth with.
*
* Only has effect if you compile with GC enabled.
*/
S_EXPORTED
void
s_mm_init (SMMType type);
/**
* Clean up memory management.
*
* This should not be called directly, use s_teardown() to instead.
*/
S_HIDDEN
void
s_mm_cleanup ();
/**
* Get what sort of memory management is used.
*/
S_EXPORTED
SMMType
s_mm_get_type ();
/**
* By default this is this is just a normal call to malloc. But with
* GC enabled it will use GC_MALLOC to allocate memory.
*/
S_EXPORTED
spointer
s_malloc (size_t size);
/**
* by default this acts just as a normal call to free. If GC is enabled this
* will set the pointer to NULL.
*
* In Mark and Sweep mode this will add the pointers to a list of pointers to
* be freed later in the loop. When this is can not be guaranteed.
*/
S_EXPORTED
void
s_free (spointer pointer);
/**
* By default this will act just as a normal call to realloc. But if GC is
* enabled this will use GC_REALLOC.
*/
S_EXPORTED
spointer
s_realloc (spointer pointer, size_t size);
/**
* By default this acts as a normal call to calloc. If GC in enabled this
* will just wrap GC_MALLOC, as there is no GC_CALLOC.
*/
S_HIDDEN
spointer
s_calloc (size_t num, size_t size);
/** @} */
/**
* @}
*/
S_END_DECLS
|