/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
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
#include "mm.h"

static sboolean _s_mm_initialized = FALSE;
static SMMType _s_mm_type = S_MM_TYPE_NONE;

void
s_mm_init (SMMType type) {
  if (!_s_mm_initialized) {
    _s_mm_initialized = TRUE;
    #if USE_GC
    _s_mm_type = type;
    #else
    _s_mm_type = S_MM_TYPE_LIBC;
    #endif
  }
}

schar *
s_mm_type_get_name (SMMType type) {
  return SMMTypeName[type];
}

void
s_mm_cleanup () {
  /* Nothing... yet. */
}

SMMType
s_mm_get_type () {
  return _s_mm_type;
}

spointer
s_malloc (size_t size) {
  #if USE_GC
  if (_s_mm_type == S_MM_TYPE_GC) {
    return GC_MALLOC (size);
  }
  #endif
  return malloc (size);
}

void
s_free (spointer pointer) {
  #if USE_GC
  if (_s_mm_type == S_MM_TYPE_GC) {
    /* Because libgc handles the freeing of these things, we just set it to
     * NULL and be done with it.
     *
     * This also means that we can used this as a normal free and not have to
     * deal with setting it to NULL.
     */
    pointer = NULL;
    return;
  }
  #endif
  free (pointer);
}

spointer
s_realloc (spointer pointer, size_t size) {
  #if USE_GC
  if (_s_mm_type == S_MM_TYPE_GC) {
    return GC_REALLOC (pointer, size);
  }
  #endif
  return realloc (pointer, size);
}

spointer
s_calloc (size_t num, size_t size) {
  #if USE_GC
  if (_s_mm_type == S_MM_TYPE_GC) {
    /* This should work. */
    spointer ret_val = GC_MALLOC (num * size);
    return ret_val;
  }
  #endif
  return calloc (num, size);
}