bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
1 |
#include "mm.h" |
130.1.4
by Gustav Hartvigsson
* Inpremented the manual mark and sweep thingys. |
2 |
#include "mm_mark_and_sweep.h" |
3 |
#include "utils.h" |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
4 |
|
130.1.3
by Gustav Hartvigsson
* pragma once gawd damn it! |
5 |
/*
|
6 |
Copyright (c) 2013-2016 Gustav Hartvigsson
|
|
7 |
||
8 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9 |
of this software and associated documentation files (the "Software"), to deal
|
|
10 |
in the Software without restriction, including without limitation the rights
|
|
11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12 |
copies of the Software, and to permit persons to whom the Software is
|
|
13 |
furnished to do so, subject to the following conditions:
|
|
14 |
||
15 |
The above copyright notice and this permission notice shall be included in
|
|
16 |
all copies or substantial portions of the Software.
|
|
17 |
||
18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
24 |
THE SOFTWARE.
|
|
25 |
*/
|
|
26 |
||
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
27 |
static sboolean _s_mm_initialized = FALSE; |
28 |
static SMMType _s_mm_type = S_MM_TYPE_NONE; |
|
29 |
||
30 |
void
|
|
31 |
s_mm_init (SMMType type) { |
|
32 |
if (!_s_mm_initialized) { |
|
33 |
_s_mm_initialized = TRUE; |
|
34 |
_s_mm_type = type; |
|
130.1.4
by Gustav Hartvigsson
* Inpremented the manual mark and sweep thingys. |
35 |
if (type == S_MM_TYPE_MARK_AND_SWEEP) { |
36 |
s_mm_mark_and_sweep_init (); |
|
37 |
} |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
38 |
} |
39 |
}
|
|
40 |
||
41 |
schar * |
|
42 |
s_mm_type_get_name (SMMType type) { |
|
43 |
return SMMTypeName[type]; |
|
44 |
}
|
|
45 |
||
46 |
void
|
|
47 |
s_mm_cleanup () { |
|
130.1.4
by Gustav Hartvigsson
* Inpremented the manual mark and sweep thingys. |
48 |
if (_s_mm_type == S_MM_TYPE_MARK_AND_SWEEP) { |
49 |
s_mm_mark_and_sweep_cleanup (); |
|
50 |
} |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
51 |
}
|
52 |
||
53 |
SMMType
|
|
54 |
s_mm_get_type () { |
|
55 |
return _s_mm_type; |
|
56 |
}
|
|
57 |
||
58 |
spointer
|
|
59 |
s_malloc (size_t size) { |
|
60 |
#if USE_GC |
|
61 |
if (_s_mm_type == S_MM_TYPE_GC) { |
|
62 |
return GC_MALLOC (size); |
|
63 |
} |
|
64 |
#endif |
|
65 |
return malloc (size); |
|
66 |
}
|
|
67 |
||
68 |
void
|
|
69 |
s_free (spointer pointer) { |
|
130.1.4
by Gustav Hartvigsson
* Inpremented the manual mark and sweep thingys. |
70 |
switch (_s_mm_type) { |
71 |
case (S_MM_TYPE_GC): |
|
72 |
#if USE_GC |
|
73 |
pointer = NULL; |
|
74 |
break; |
|
75 |
#endif |
|
76 |
case (S_MM_TYPE_NONE): |
|
77 |
case (S_MM_TYPE_LIBC): |
|
78 |
free (pointer); |
|
79 |
break; |
|
80 |
case (S_MM_TYPE_MARK_AND_SWEEP): |
|
81 |
s_mm_mark_and_sweep_add_pointer_to_list (pointer); |
|
82 |
break; |
|
83 |
default: |
|
84 |
s_err_print ("Reaching a place that should not be reached."); |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
85 |
} |
86 |
}
|
|
87 |
||
88 |
spointer
|
|
89 |
s_realloc (spointer pointer, size_t size) { |
|
90 |
#if USE_GC |
|
91 |
if (_s_mm_type == S_MM_TYPE_GC) { |
|
92 |
return GC_REALLOC (pointer, size); |
|
93 |
} |
|
94 |
#endif |
|
95 |
return realloc (pointer, size); |
|
96 |
}
|
|
97 |
||
98 |
spointer
|
|
99 |
s_calloc (size_t num, size_t size) { |
|
100 |
#if USE_GC |
|
101 |
if (_s_mm_type == S_MM_TYPE_GC) { |
|
102 |
/* This should work. */ |
|
103 |
spointer ret_val = GC_MALLOC (num * size); |
|
104 |
return ret_val; |
|
105 |
} |
|
106 |
#endif |
|
107 |
return calloc (num, size); |
|
108 |
}
|