File: test_emmalloc_high_align.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (34 lines) | stat: -rw-r--r-- 1,088 bytes parent folder | download | duplicates (2)
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
#include <assert.h>
#include <emscripten/console.h>
#include <stdlib.h>
#include <unistd.h>

size_t sizeT(void* p) {
  return (size_t)p;
}

int main() {
  const size_t MB = 1024 * 1024;
  const size_t ALIGN = 4 * MB;
  const size_t SIZE = 32 * MB;

  // Allocate a very large chunk of memory (32MB) with very high alignment (4
  // MB). This is similar to what mimalloc does in practice.
  void* before = sbrk(0);
  void* p = aligned_alloc(ALIGN, SIZE);
  // aligned_alloc returns NULL on error; validate we actually allocated.
  assert(p);
  void* after = sbrk(0);
  emscripten_console_logf("before: %p  after: %p  p: %p\n", before, after, p);

  // The allocation must be properly aligned.
  assert(sizeT(p) % ALIGN == 0);

  // We should only have sbrk'd a reasonable amount (this is a regression test
  // for #20645 where we sbrk'd double the necessary amount). We expect at most
  // 36 MB (the size of the allocation plus the alignment) plus a few bytes of
  // general overhead.
  assert(sizeT(after) - sizeT(before) < ALIGN + SIZE + 100);

  emscripten_console_log("success");
}