/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.c

  • 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
#include "mm.h"
 
2
 
 
3
static sboolean _s_mm_initialized = FALSE;
 
4
static SMMType _s_mm_type = S_MM_TYPE_NONE;
 
5
 
 
6
void
 
7
s_mm_init (SMMType type) {
 
8
  if (!_s_mm_initialized) {
 
9
    _s_mm_initialized = TRUE;
 
10
    #if USE_GC
 
11
    _s_mm_type = type;
 
12
    #else
 
13
    _s_mm_type = S_MM_TYPE_LIBC;
 
14
    #endif
 
15
  }
 
16
}
 
17
 
 
18
schar *
 
19
s_mm_type_get_name (SMMType type) {
 
20
  return SMMTypeName[type];
 
21
}
 
22
 
 
23
void
 
24
s_mm_cleanup () {
 
25
  /* Nothing... yet. */
 
26
}
 
27
 
 
28
SMMType
 
29
s_mm_get_type () {
 
30
  return _s_mm_type;
 
31
}
 
32
 
 
33
spointer
 
34
s_malloc (size_t size) {
 
35
  #if USE_GC
 
36
  if (_s_mm_type == S_MM_TYPE_GC) {
 
37
    return GC_MALLOC (size);
 
38
  }
 
39
  #endif
 
40
  return malloc (size);
 
41
}
 
42
 
 
43
void
 
44
s_free (spointer pointer) {
 
45
  #if USE_GC
 
46
  if (_s_mm_type == S_MM_TYPE_GC) {
 
47
    /* Because libgc handles the freeing of these things, we just set it to
 
48
     * NULL and be done with it.
 
49
     *
 
50
     * This also means that we can used this as a normal free and not have to
 
51
     * deal with setting it to NULL.
 
52
     */
 
53
    pointer = NULL;
 
54
    return;
 
55
  }
 
56
  #endif
 
57
  free (pointer);
 
58
}
 
59
 
 
60
spointer
 
61
s_realloc (spointer pointer, size_t size) {
 
62
  #if USE_GC
 
63
  if (_s_mm_type == S_MM_TYPE_GC) {
 
64
    return GC_REALLOC (pointer, size);
 
65
  }
 
66
  #endif
 
67
  return realloc (pointer, size);
 
68
}
 
69
 
 
70
spointer
 
71
s_calloc (size_t num, size_t size) {
 
72
  #if USE_GC
 
73
  if (_s_mm_type == S_MM_TYPE_GC) {
 
74
    /* This should work. */
 
75
    spointer ret_val = GC_MALLOC (num * size);
 
76
    return ret_val;
 
77
  }
 
78
  #endif
 
79
  return calloc (num, size);
 
80
}