vey-mimalloc-sys 0.3.2+3.7.0

Rust FFI binding to mimalloc
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*----------------------------------------------------------------------------
Copyright (c) 2018-2026, 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" at the root of this distribution.
-----------------------------------------------------------------------------*/

/* -----------------------------------------------------------
  Definition of page queues for each block size
----------------------------------------------------------- */

#include "mimalloc.h"
#include "mimalloc/types.h"
#include "page-queue.h"

/* -----------------------------------------------------------
  Minimal alignment in machine words (i.e. `sizeof(size_t)`)
----------------------------------------------------------- */

#if (MI_MAX_ALIGN_SIZE > 4*MI_SIZE_SIZE)
  #error "define alignment for more than 4x word size for this platform"
#elif (MI_MAX_ALIGN_SIZE > 2*MI_SIZE_SIZE)
  #define MI_ALIGN4W   // 4 machine words minimal alignment
#elif (MI_MAX_ALIGN_SIZE > MI_SIZE_SIZE)
  #define MI_ALIGN2W   // 2 machine words minimal alignment
#else
  // ok, default alignment is 1 word
#endif

// Minimal block size in machine words (a block must be able to hold a pointer, and be a multiple of the memory tag unit size)
#if (MI_INTPTR_SIZE > MI_SIZE_SIZE) || MI_MTE
  #if (MI_INTPTR_SIZE > 2*MI_SIZE_SIZE) || !(defined(MI_ALIGN2W) || defined(MI_ALIGN4W))
  #error "define the minimal block size for this platform"
  #endif
  #define MI_MIN_BLOCK_WSIZE  2   // as on CHERI, or with memory tagging (16-byte units)
#else
  #define MI_MIN_BLOCK_WSIZE  1
#endif


/* -----------------------------------------------------------
  Bins
----------------------------------------------------------- */

// Return the bin for a given field size.
// Returns MI_BIN_HUGE if the size is too large.
// We use `wsize` for the size in "machine word sizes",
// i.e. byte size == `wsize*sizeof(size_t)`.
static size_t mi_bin(size_t size) {
  size_t wsize = mi_wsize_from_size(size);
  #if defined(MI_ALIGN4W)
  if mi_likely(wsize <= 4) {
      return (wsize <= MI_MIN_BLOCK_WSIZE ? MI_MIN_BLOCK_WSIZE : (wsize+1)&~1); // round to double word sizes
  }
  #elif defined(MI_ALIGN2W)
  if mi_likely(wsize <= 8) {
      return (wsize <= MI_MIN_BLOCK_WSIZE ? MI_MIN_BLOCK_WSIZE : (wsize+1)&~1); // round to double word sizes
  }
  #else
  if mi_likely(wsize <= 8) {
    return (wsize == 0 ? 1 : wsize);
  }
  #endif
  else if mi_unlikely(wsize > MI_LARGE_MAX_OBJ_WSIZE) {
    return MI_BIN_HUGE;
  }
  else {
    #if defined(MI_ALIGN4W)
    if (wsize <= 16) { wsize = (wsize+3)&~3; } // round to 4x word sizes
    #endif
    wsize--;
    // find the highest bit
    const size_t b = (MI_SIZE_BITS - 1 - mi_clz(wsize));  // note: wsize != 0
    // and use the top 3 bits to determine the bin (~12.5% worst internal fragmentation).
    // - adjust with 3 because we use do not round the first 8 sizes
    //   which each get an exact bin
    const size_t bin = ((b << 2) + ((wsize >> (b - 2)) & 0x03)) - 3;
    mi_assert_internal(bin > 0 && bin < MI_BIN_HUGE);
    return bin;
  }
}



/* -----------------------------------------------------------
  Queue of pages with free blocks
----------------------------------------------------------- */

size_t _mi_bin(size_t size) {
  return mi_bin(size);
}

size_t _mi_bin_size(size_t bin) {
  mi_assert_internal(bin <= MI_BIN_HUGE);
  return _mi_theap_empty.pages[bin].block_size;
}

// Good size for allocation
mi_decl_nodiscard mi_decl_export size_t mi_good_size(size_t size) mi_attr_noexcept {
  if (size <= MI_LARGE_MAX_OBJ_SIZE - MI_PADDING_SIZE) {
    return _mi_bin_size(mi_bin(size + MI_PADDING_SIZE));
  }
  else if (size <= MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE) {
    return mi_align_up(size + MI_PADDING_SIZE,_mi_os_page_size());
  }
  else {
    return size;
  }
}

#if (MI_DEBUG>1)
bool mi_page_queue_contains(mi_page_queue_t* queue, const mi_page_t* page) {
  mi_assert_internal(page != NULL);
  mi_page_t* list = queue->first;
  while (list != NULL) {
    mi_assert_internal(list->next == NULL || list->next->prev == list);
    mi_assert_internal(list->prev == NULL || list->prev->next == list);
    if (list == page) break;
    list = list->next;
  }
  return (list == page);
}

#endif

bool _mi_page_queue_is_valid(mi_theap_t* theap, const mi_page_queue_t* pq) {
  MI_UNUSED_RELEASE(theap);
  if (pq==NULL) return false;
  size_t count = 0; MI_UNUSED_RELEASE(count);
  mi_page_t* prev = NULL; MI_UNUSED_RELEASE(prev);
  for (mi_page_t* page = pq->first; page != NULL; page = page->next) {
    mi_assert_internal(page->prev == prev);
    if (mi_page_is_in_full(page)) {
      mi_assert_internal(mi_wsize_from_size(pq->block_size) == MI_LARGE_MAX_OBJ_WSIZE + 2);
    }
    else if (mi_page_is_huge(page)) {
      mi_assert_internal(mi_wsize_from_size(pq->block_size) == MI_LARGE_MAX_OBJ_WSIZE + 1);
    }
    else {
      mi_assert_internal(mi_page_block_size(page) == pq->block_size);
    }
    mi_assert_internal(page->theap == theap);
    if (page->next == NULL) {
      mi_assert_internal(pq->last == page);
    }
    count++;
    prev = page;
  }
  mi_assert_internal(pq->count == count);
  return true;
}

static size_t mi_page_bin(const mi_page_t* page) {
  const size_t bin = (mi_page_is_in_full(page) ? MI_BIN_FULL : (mi_page_is_huge(page) ? MI_BIN_HUGE : mi_bin(mi_page_block_size(page))));
  mi_assert_internal(bin <= MI_BIN_FULL);
  return bin;
}

// returns the page bin without using MI_BIN_FULL for statistics
size_t _mi_page_stats_bin(const mi_page_t* page) {
  const size_t bin = (mi_page_is_huge(page) ? MI_BIN_HUGE : mi_bin(mi_page_block_size(page)));
  mi_assert_internal(bin <= MI_BIN_HUGE);
  return bin;
}

mi_page_queue_t* mi_theap_page_queue_of(mi_theap_t* theap, const mi_page_t* page) {
  mi_assert_internal(theap!=NULL);
  const size_t bin = mi_page_bin(page);
  mi_page_queue_t* pq = &theap->pages[bin];
  mi_assert_internal((mi_page_block_size(page) == pq->block_size) ||
                       (mi_page_is_huge(page) && mi_page_queue_is_huge(pq)) ||
                         (mi_page_is_in_full(page) && mi_page_queue_is_full(pq)));
  return pq;
}

// The current small page array is for efficiency and for each
// small size (up to 256) it points directly to the page for that
// size without having to compute the bin. This means when the
// current free page queue is updated for a small bin, we need to update a
// range of entries in `_mi_page_small_free`.
static inline void mi_theap_queue_first_update(mi_theap_t* theap, const mi_page_queue_t* pq) {
  mi_assert_internal(mi_theap_contains_queue(theap,pq));
  const size_t size = pq->block_size;
  if (size > MI_SMALL_SIZE_MAX) return;

  mi_page_t* page = pq->first;
  if (pq->first == NULL) page = _mi_page_empty_get();

  // find index in the right direct page array
  const size_t idx = mi_wsize_from_size(size);
  mi_page_t** const pages_free = theap->pages_free_direct;
  if (pages_free[idx] == page) return;  // already set

  // find start slot
  size_t start;
  if (idx <= MI_MIN_BLOCK_WSIZE) {
    start = 0;
  }
  else {
    // find previous size; due to minimal alignment upto 3 previous bins may need to be skipped
    mi_assert_internal(pq > &theap->pages[0]); // since idx > MI_MIN_BLOCK_WSIZE
    size_t bin = mi_bin(size);
    const mi_page_queue_t* prev = pq - 1;
    while( bin == mi_bin(prev->block_size) && prev > &theap->pages[0]) {
      prev--;
    }
    start = 1 + mi_wsize_from_size(prev->block_size);
    if (start > idx) start = idx;
  }

  // set size range to the right page
  mi_assert(start <= idx);
  for (size_t sz = start; sz <= idx; sz++) {
    pages_free[sz] = page;
  }
}

/*
static bool mi_page_queue_is_empty(mi_page_queue_t* queue) {
  return (queue->first == NULL);
}
*/

void mi_page_queue_remove(mi_page_queue_t* queue, mi_page_t* page) {
  mi_assert_internal(page != NULL);
  mi_assert_expensive(mi_page_queue_contains(queue, page));
  mi_assert_internal(queue->count >= 1);
  mi_assert_internal(mi_page_block_size(page) == queue->block_size ||
                      (mi_page_is_huge(page) && mi_page_queue_is_huge(queue)) ||
                        (mi_page_is_in_full(page) && mi_page_queue_is_full(queue)));
  mi_theap_t* theap = mi_page_theap(page);
  mi_list_unlink_check(page, prev, next, queue->first, queue->last);
  if (page->prev != NULL) page->prev->next = page->next;
  if (page->next != NULL) page->next->prev = page->prev;
  if (page == queue->last)  queue->last = page->prev;
  if (page == queue->first) {
    queue->first = page->next;
    // update first
    mi_assert_internal(mi_theap_contains_queue(theap, queue));
    mi_theap_queue_first_update(theap,queue);
  }
  theap->page_count--;
  if (page->memid.memkind != MI_MEM_ARENA) {
    mi_assert_internal(theap->page_count_os > 0);
    theap->page_count_os--;
  }
  queue->count--;
  page->next = NULL;
  page->prev = NULL;
  mi_page_set_in_full(page,false);
}


void mi_page_queue_push(mi_theap_t* theap, mi_page_queue_t* queue, mi_page_t* page) {
  mi_assert_internal(mi_page_theap(page) == theap);
  mi_assert_internal(!mi_page_queue_contains(queue, page));
  #if MI_HUGE_PAGE_ABANDON
  mi_assert_internal(_mi_page_segment(page)->page_kind != MI_PAGE_HUGE);
  #endif
  mi_assert_internal(mi_page_block_size(page) == queue->block_size ||
                      (mi_page_is_huge(page) && mi_page_queue_is_huge(queue)) ||
                        (mi_page_is_in_full(page) && mi_page_queue_is_full(queue)));

  mi_page_set_in_full(page, mi_page_queue_is_full(queue));

  page->next = queue->first;
  page->prev = NULL;
  if (queue->first != NULL) {
    mi_assert_internal(queue->first->prev == NULL);
    queue->first->prev = page;
    queue->first = page;
  }
  else {
    queue->first = queue->last = page;
  }
  queue->count++;

  // update direct
  mi_theap_queue_first_update(theap, queue);
  theap->page_count++;
  if (page->memid.memkind != MI_MEM_ARENA) { theap->page_count_os++; }
}

void mi_page_queue_push_at_end(mi_theap_t* theap, mi_page_queue_t* queue, mi_page_t* page) {
  mi_assert_internal(mi_page_theap(page) == theap);
  mi_assert_internal(!mi_page_queue_contains(queue, page));

  mi_assert_internal(mi_page_block_size(page) == queue->block_size ||
                      (mi_page_is_huge(page) && mi_page_queue_is_huge(queue)) ||
                       (mi_page_is_in_full(page) && mi_page_queue_is_full(queue)));

  mi_page_set_in_full(page, mi_page_queue_is_full(queue));

  page->prev = queue->last;
  page->next = NULL;
  if (queue->last != NULL) {
    mi_assert_internal(queue->last->next == NULL);
    queue->last->next = page;
    queue->last = page;
  }
  else {
    queue->first = queue->last = page;
  }
  queue->count++;

  // update direct
  if (queue->first == page) {
    mi_theap_queue_first_update(theap, queue);
  }
  theap->page_count++;
  if (page->memid.memkind != MI_MEM_ARENA) { theap->page_count_os++; }
}

// Move a page from the `from` queue to the end (or front) of the `to` queue.
// The queues can be the same in which case the page is moved within the queue.
static void mi_page_queue_enqueue_from_ex(mi_page_queue_t* to, mi_page_queue_t* from, bool enqueue_at_end, mi_page_t* page) {
  mi_assert_internal(page != NULL);
  mi_assert_internal(from->count >= 1);
  mi_assert_expensive(mi_page_queue_contains(from, page));
  mi_assert_expensive(to == from || !mi_page_queue_contains(to, page));
  const size_t bsize = mi_page_block_size(page);
  MI_UNUSED(bsize);
  mi_assert_internal((bsize == to->block_size && bsize == from->block_size) ||
                     (bsize == to->block_size && mi_page_queue_is_full(from)) ||
                     (bsize == from->block_size && mi_page_queue_is_full(to)) ||
                     (mi_page_is_huge(page) && mi_page_queue_is_huge(to)) ||
                     (mi_page_is_huge(page) && mi_page_queue_is_full(to)));

  mi_theap_t* theap = mi_page_theap(page);

  // delete from `from`
  mi_list_unlink_check(page, prev, next, from->first, from->last);
  if (page->prev != NULL) page->prev->next = page->next;
  if (page->next != NULL) page->next->prev = page->prev;
  if (page == from->last)  from->last = page->prev;
  if (page == from->first) {
    from->first = page->next;
    // update first
    mi_assert_internal(mi_theap_contains_queue(theap, from));
    mi_theap_queue_first_update(theap, from);
  }
  from->count--;

  // insert into `to`
  to->count++;
  if (enqueue_at_end) {
    // enqueue at the end
    page->prev = to->last;
    page->next = NULL;
    if (to->last != NULL) {
      mi_assert_internal(theap == mi_page_theap(to->last));
      to->last->next = page;
      to->last = page;
    }
    else {
      to->first = page;
      to->last = page;
      mi_theap_queue_first_update(theap, to);
    }
  }
  else {
    // enqueue at the front
    page->prev = NULL;
    page->next = to->first;
    if (to->first != NULL) {
      mi_assert_internal(theap == mi_page_theap(to->first));
      to->first->prev = page;
    }
    else {
      to->last = page;
    }
    to->first = page;
    mi_theap_queue_first_update(theap, to);
  }

  if (to != from) {  // avoid the atomic flag update when moving within a queue
    mi_page_set_in_full(page, mi_page_queue_is_full(to));
  }
}

void mi_page_queue_enqueue_from(mi_page_queue_t* to, mi_page_queue_t* from, mi_page_t* page) {
  mi_page_queue_enqueue_from_ex(to, from, true /* enqueue at the end */, page);
}

void mi_page_queue_enqueue_from_full(mi_page_queue_t* to, mi_page_queue_t* from, mi_page_t* page) {
  // note: we could insert at the front to increase reuse, but it slows down certain benchmarks (like `alloc-test`)
  mi_page_queue_enqueue_from_ex(to, from, true /* enqueue at the end of the `to` queue? */, page);
}

void mi_page_queue_move_to_front(mi_theap_t* theap, mi_page_queue_t* queue, mi_page_t* page) {
  mi_assert_internal(mi_page_theap(page) == theap);
  mi_assert_internal(mi_page_queue_contains(queue, page));
  MI_UNUSED(theap);
  if (queue->first == page) return;
  mi_page_queue_enqueue_from_ex(queue, queue, false /* at the front */, page);
  mi_assert_internal(queue->first == page);
}

void mi_page_queue_move_to_back(mi_theap_t* theap, mi_page_queue_t* queue, mi_page_t* page) {
  mi_assert_internal(mi_page_theap(page) == theap);
  mi_assert_internal(mi_page_queue_contains(queue, page));
  MI_UNUSED(theap);
  if (queue->last == page) return;
  mi_page_queue_enqueue_from_ex(queue, queue, true /* at the end */, page);
  mi_assert_internal(queue->last == page);
}

// Rotate the full or huge queue such that `page` becomes the first page (and the pages before it move to the back)
void mi_page_queue_rotate_to(mi_page_queue_t* queue, mi_page_t* page) {
  mi_assert_internal(mi_page_queue_is_special(queue));
  mi_assert_expensive(mi_page_queue_contains(queue, page));
  if (queue->first == page) return;
  queue->last->next = queue->first;
  queue->first->prev = queue->last;
  queue->last = page->prev;
  queue->last->next = NULL;
  page->prev = NULL;
  queue->first = page;
}