Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions system/lib/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#define USE_SPIN_LOCKS 0 // Ensure we use pthread_mutex_t.
#endif

/* `mallloc`ed pointers must be aligned at least as strictly as max_align_t. */
#ifndef MALLOC_ALIGNMENT
#include <stddef.h>
#define MALLOC_ALIGNMENT (__alignof__(max_align_t))
#endif

#endif


Expand Down
3 changes: 2 additions & 1 deletion system/lib/emmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* malloc.
*/

#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <memory.h>
Expand All @@ -61,7 +62,7 @@ extern "C"

// Configuration: specifies the minimum alignment that malloc()ed memory outputs. Allocation requests with smaller alignment
// than this will yield an allocation with this much alignment.
#define MALLOC_ALIGNMENT 8
#define MALLOC_ALIGNMENT __alignof__(max_align_t)

// Configuration: If EMMALLOC_USE_64BIT_OPS is specified, emmalloc uses 64 buckets for free memory regions instead of just 32.
// When building to target asm.js/wasm2js, 64-bit ops are disabled, but in Wasm builds, 64-bit ops are enabled. (this is
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_emmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void test_realloc() {
// realloc copies
char* ptr = (char*)malloc(10);
*ptr = 123;
for (int i = 5; i <= 16; i++) {
for (int i = 5; i <= 24; i++) {
char* temp = (char*)realloc(ptr, i);
assert(*temp == 123);
assert(temp == ptr);
Expand All @@ -123,7 +123,7 @@ void test_realloc() {
malloc(1);
malloc(100);
{
char* temp = (char*)realloc(ptr, 17);
char* temp = (char*)realloc(ptr, 25);
assert(*temp == 123);
assert(temp != ptr);
ptr = temp;
Expand Down
21 changes: 21 additions & 0 deletions tests/core/test_malloc_alignment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2019 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <assert.h>
#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

int main(int argc, char **argv) {
for(int i = 0; i < 16; ++i) {
void *p = malloc(i);
assert(((uintptr_t)p & (alignof(max_align_t) - 1)) == 0);
}

return 0;
}
10 changes: 10 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8570,6 +8570,16 @@ def test():
self.emcc_args += ['-DPOOL']
test()

@parameterized({
'dlmalloc': ['dlmalloc'],
'emmalloc': ['emmalloc'],
})
def test_malloc_alignment(self, malloc):
self.set_setting('MALLOC', malloc)
self.emcc_args += ['-std=gnu11']
src = open(path_from_root('tests', 'core', 'test_malloc_alignment.c')).read()
self.do_run(src, '', basename='src.c')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These days you can just write self.do_runf(path_from_root('tests', 'core', 'test_malloc_alignment.c'))



# Generate tests for everything
def make_run(name, emcc_args, settings=None, env=None):
Expand Down