/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/mm.h

  • Committer: Gustav Hartvigsson
  • Date: 2016-02-01 14:11:35 UTC
  • mto: This revision was merged to the branch mainline in revision 122.
  • Revision ID: gustav.hartvigsson@gmail.com-20160201141135-pcl4m4gcf8r7t11e
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
* replaced:
  * malloc ->   s_malloc
  * free ->     s_free
  * realloc ->  s_realloc
  * calloc ->   s_calloc

* Made s_[malloc, calloc, free, realloc] functions and moved them to their own file.
  This can be used os basis for other memory stuffs in the future, if we want to use
  a Slab allocator or something else.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __H_MM__
 
2
#define __H_MM__
 
3
 
 
4
#include "defs.h"
 
5
 
 
6
#if USE_GC
 
7
#include <gc/gc.h>
 
8
#endif
 
9
 
 
10
S_BEGIN_DECLS
 
11
 
 
12
/**
 
13
 * @defgroup MemoryM Memory Management
 
14
 * @addtogroup MemoryM
 
15
 * @{
 
16
 * @brief Memory management stuff.
 
17
 *
 
18
 * By default libssts does not use a GC, it can be enabled with by setting the
 
19
 * USE_GC switch in cmake.
 
20
 *
 
21
 * Even if you have GC enabled, it is important to make sure that when you do
 
22
 * not use the GC it will still cleanup all the pointers.
 
23
 *
 
24
 * General rule: for every @ref s_malloc() have a @ref s_free() even if you know
 
25
 * you are running a GC.
 
26
 */
 
27
 
 
28
/**
 
29
 * Represents what sort of memory management should be used. Used by
 
30
 * @ref s_mm_init.
 
31
 */
 
32
typedef enum {
 
33
  S_MM_TYPE_NONE,
 
34
  S_MM_TYPE_LIBC,
 
35
  S_MM_TYPE_GC
 
36
} SMMType;
 
37
 
 
38
/**
 
39
 * The names of for the enum @ref SMMType.
 
40
 */
 
41
S_UNUSED static
 
42
schar * SMMTypeName[] = {
 
43
  "NONE",
 
44
  "libc",
 
45
  "GC",
 
46
  0x0,
 
47
  0x0
 
48
};
 
49
 
 
50
/**
 
51
 * Get the name of SMMType.
 
52
 *
 
53
 * For used in bindings.
 
54
 */
 
55
schar *
 
56
s_mm_type_get_name (SMMType type);
 
57
 
 
58
/**
 
59
 * Set how memory should be dealth with.
 
60
 *
 
61
 * Only has effect if you compile with GC enabled.
 
62
 */
 
63
void
 
64
s_mm_init (SMMType type);
 
65
 
 
66
/**
 
67
 * Clean up memory management.
 
68
 * Does nothing.
 
69
 */
 
70
void
 
71
s_mm_cleanup ();
 
72
 
 
73
/**
 
74
 * Get what sort of memory management is used.
 
75
 */
 
76
SMMType
 
77
s_mm_get_type ();
 
78
 
 
79
/**
 
80
 * By default this is this is just a normal call to malloc. But with
 
81
 * GC enabled it will use GC_MALLOC to allocate memory.
 
82
 */
 
83
spointer
 
84
s_malloc (size_t size);
 
85
 
 
86
/**
 
87
 * by default this acts just as a normal call to free. If GC is enabled this
 
88
 * will set the pointer to NULL.
 
89
 */
 
90
void
 
91
s_free (spointer pointer);
 
92
 
 
93
/**
 
94
 * By default this will act just as a normal call to realloc. But if GC is
 
95
 * enabled this will use GC_REALLOC.
 
96
 */
 
97
spointer
 
98
s_realloc (spointer pointer, size_t size);
 
99
 
 
100
/**
 
101
 * By default this acts as a normal call to calloc. If GC in enabled this
 
102
 * will just wrap GC_MALLOC, as there is no GC_CALLOC.
 
103
 */
 
104
spointer
 
105
s_calloc (size_t num, size_t size);
 
106
 
 
107
/** @} */
 
108
 
 
109
/**
 
110
 * @}
 
111
 */
 
112
 
 
113
S_END_DECLS
 
114
 
 
115
#endif /* __H_MM__ */