summaryrefslogtreecommitdiff
path: root/src/alloc-override-osx.c
blob: 83211d355a791c07f9a63acf4b37ff3720dda428 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* ----------------------------------------------------------------------------
Copyright (c) 2018, Microsoft Research, Daan Leijen
This is free software; you can redistribute it and/or modify it under the
terms of the MIT license. A copy of the license can be found in the file
"license.txt" at the root of this distribution.
-----------------------------------------------------------------------------*/

#include "mimalloc.h"
#include "mimalloc-internal.h"

#if defined(MI_MALLOC_OVERRIDE) 

#if !defined(__APPLE__)
#error "this file should only be included on MacOSX"
#endif

/* ------------------------------------------------------
   Override system malloc on MacOSX
   This is done through the malloc zone interface.
------------------------------------------------------ */

#include <AvailabilityMacros.h>
#include <malloc/malloc.h>

#if defined(MAC_OS_X_VERSION_10_6) && \
    MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
// only available from OSX 10.6
extern malloc_zone_t* malloc_default_purgeable_zone(void) __attribute__((weak_import));
#endif


/* ------------------------------------------------------
   malloc zone members
------------------------------------------------------ */

static size_t zone_size(malloc_zone_t* zone, const void* p) {
  return 0; // as we cannot guarantee that `p` comes from us, just return 0
}

static void* zone_malloc(malloc_zone_t* zone, size_t size) {
  return mi_malloc(size);
}

static void* zone_calloc(malloc_zone_t* zone, size_t count, size_t size) {
  return mi_calloc(count, size);
}

static void* zone_valloc(malloc_zone_t* zone, size_t size) {
  return mi_malloc_aligned(size, _mi_os_page_size());
}

static void zone_free(malloc_zone_t* zone, void* p) {
  return mi_free(p);
}

static void* zone_realloc(malloc_zone_t* zone, void* p, size_t newsize) {
  return mi_realloc(p, newsize);
}

static void* zone_memalign(malloc_zone_t* zone, size_t alignment, size_t size) {
  return mi_malloc_aligned(size,alignment);
}

static void zone_destroy(malloc_zone_t* zone) {
  // todo: ignore for now?
}

static size_t zone_batch_malloc(malloc_zone_t* zone, size_t size, void** ps, size_t count) {
  size_t i;
  for (i = 0; i < count; i++) {
    ps[i] = zone_malloc(zone, size);
    if (ps[i] == NULL) break;
  }
  return i;
}

static void zone_batch_free(malloc_zone_t* zone, void** ps, size_t count) {
  for(size_t i = 0; i < count; i++) {
    zone_free(zone, ps[i]);
    ps[i] = NULL;
  }
}

static size_t zone_pressure_relief(malloc_zone_t* zone, size_t size) {
  mi_collect(false);
  return 0;
}

static void zone_free_definite_size(malloc_zone_t* zone, void* p, size_t size) {
  zone_free(zone,p);
}


/* ------------------------------------------------------
   Introspection members
------------------------------------------------------ */

static kern_return_t intro_enumerator(task_t task, void* p,
                            unsigned type_mask, vm_address_t zone_address,
                            memory_reader_t reader,
                            vm_range_recorder_t recorder)
{
  // todo: enumerate all memory
  return KERN_SUCCESS;
}

static size_t intro_good_size(malloc_zone_t* zone, size_t size) {
  return mi_good_size(size);
}

static boolean_t intro_check(malloc_zone_t* zone) {
  return true;
}

static void intro_print(malloc_zone_t* zone, boolean_t verbose) {
  mi_stats_print(NULL);
}

static void intro_log(malloc_zone_t* zone, void* p) {
  // todo?
}

static void intro_force_lock(malloc_zone_t* zone) {
  // todo?
}

static void intro_force_unlock(malloc_zone_t* zone) {
  // todo?
}

static void intro_statistics(malloc_zone_t* zone, malloc_statistics_t* stats) {
  // todo...
  stats->blocks_in_use = 0;
  stats->size_in_use = 0;
  stats->max_size_in_use = 0;
  stats->size_allocated = 0;
}

static boolean_t intro_zone_locked(malloc_zone_t* zone) {
  return false;
}


/* ------------------------------------------------------
  At process start, override the default allocator
------------------------------------------------------ */

static malloc_zone_t* mi_get_default_zone()
{
  // The first returned zone is the real default
  malloc_zone_t** zones = NULL;
  size_t count = 0;
  kern_return_t ret = malloc_get_all_zones(0, NULL, (vm_address_t**)&zones, &count);
  if (ret == KERN_SUCCESS && count > 0) {
    return zones[0];
  }
  else {
    // fallback
    return malloc_default_zone();
  }
}


static void __attribute__((constructor)) _mi_macosx_override_malloc()
{
  static malloc_introspection_t intro;
  memset(&intro, 0, sizeof(intro));

  intro.enumerator = &intro_enumerator;
  intro.good_size = &intro_good_size;
  intro.check = &intro_check;
  intro.print = &intro_print;
  intro.log = &intro_log;
  intro.force_lock = &intro_force_lock;
  intro.force_unlock = &intro_force_unlock;

  static malloc_zone_t zone;
  memset(&zone, 0, sizeof(zone));

  zone.version = 4;
  zone.zone_name = "mimalloc";
  zone.size = &zone_size;
  zone.introspect = &intro;
  zone.malloc = &zone_malloc;
  zone.calloc = &zone_calloc;
  zone.valloc = &zone_valloc;
  zone.free = &zone_free;
  zone.realloc = &zone_realloc;
  zone.destroy = &zone_destroy;
  zone.batch_malloc = &zone_batch_malloc;
  zone.batch_free = &zone_batch_free;

  malloc_zone_t* purgeable_zone = NULL;

#if defined(MAC_OS_X_VERSION_10_6) && \
    MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  // rwitch to version 9 on OSX 10.6 to support memalign.
  zone.version = 9;
  zone.memalign = &zone_memalign;
  zone.free_definite_size = &zone_free_definite_size;
  zone.pressure_relief = &zone_pressure_relief;
  intro.zone_locked = &intro_zone_locked;

  // force the purgeable zone to exist to avoid strange bugs
  if (malloc_default_purgeable_zone) {
    purgeable_zone = malloc_default_purgeable_zone();
  }
#endif

  // Register our zone
  malloc_zone_register(&zone);

  // Unregister the default zone, this makes our zone the new default
  // as that was the last registered.
  malloc_zone_t *default_zone = mi_get_default_zone();
  malloc_zone_unregister(default_zone);

  // Reregister the default zone so free and realloc in that zone keep working.
  malloc_zone_register(default_zone);

  // Unregister, and re-register the purgeable_zone to avoid bugs if it occurs
  // earlier than the default zone.
  if (purgeable_zone != NULL) {
    malloc_zone_unregister(purgeable_zone);
    malloc_zone_register(purgeable_zone);
  }
}

#endif // MI_MALLOC_OVERRIDE