diff options
author | Rickard Green <[email protected]> | 2013-09-25 16:26:42 +0200 |
---|---|---|
committer | Rickard Green <[email protected]> | 2013-09-25 16:26:42 +0200 |
commit | 3eff4c3108492366c8bde3e007f648fd99bd04a4 (patch) | |
tree | f6e3501ad0fb687c0a7e7cba377b3e2adc9de1c4 /erts/emulator/sys/unix | |
parent | 20641fe0f2ea745873fc7557448d3a7deb1bd639 (diff) | |
parent | cabfe2cdc0b8d879431f81233addd2a43ff1f742 (diff) | |
download | otp-3eff4c3108492366c8bde3e007f648fd99bd04a4.tar.gz otp-3eff4c3108492366c8bde3e007f648fd99bd04a4.tar.bz2 otp-3eff4c3108492366c8bde3e007f648fd99bd04a4.zip |
Merge branch 'rickard/aligned-sys_alloc-carriers/OTP-11318' into rickard/aligned-sys_alloc-carriers_maint/OTP-11318
Conflicts:
erts/emulator/beam/erl_alloc.c
erts/emulator/beam/erl_alloc_util.c
erts/emulator/beam/erl_alloc_util.h
Diffstat (limited to 'erts/emulator/sys/unix')
-rw-r--r-- | erts/emulator/sys/unix/erl_unix_sys.h | 4 | ||||
-rw-r--r-- | erts/emulator/sys/unix/sys.c | 46 |
2 files changed, 50 insertions, 0 deletions
diff --git a/erts/emulator/sys/unix/erl_unix_sys.h b/erts/emulator/sys/unix/erl_unix_sys.h index c8fcec8547..2c47aa06c2 100644 --- a/erts/emulator/sys/unix/erl_unix_sys.h +++ b/erts/emulator/sys/unix/erl_unix_sys.h @@ -107,6 +107,10 @@ #endif #include <netdb.h> +#ifdef HAVE_POSIX_MEMALIGN +# define ERTS_HAVE_ERTS_SYS_ALIGNED_ALLOC 1 +#endif + /* * Make sure that MAXPATHLEN is defined. */ diff --git a/erts/emulator/sys/unix/sys.c b/erts/emulator/sys/unix/sys.c index a7ea4b2490..fdc3167c62 100644 --- a/erts/emulator/sys/unix/sys.c +++ b/erts/emulator/sys/unix/sys.c @@ -2524,6 +2524,52 @@ void erts_sys_alloc_init(void) { } +#if ERTS_HAVE_ERTS_SYS_ALIGNED_ALLOC +void *erts_sys_aligned_alloc(UWord alignment, UWord size) +{ +#ifdef HAVE_POSIX_MEMALIGN + void *ptr = NULL; + int error; + ASSERT(alignment && (alignment & ~alignment) == 0); /* power of 2 */ + error = posix_memalign(&ptr, (size_t) alignment, (size_t) size); +#if HAVE_ERTS_MSEG + if (error || !ptr) { + erts_mseg_clear_cache(); + error = posix_memalign(&ptr, (size_t) alignment, (size_t) size); + } +#endif + if (error) { + errno = error; + return NULL; + } + if (!ptr) + errno = ENOMEM; + ASSERT(!ptr || (((UWord) ptr) & (alignment - 1)) == 0); + return ptr; +#else +# error "Missing erts_sys_aligned_alloc() implementation" +#endif +} + +void erts_sys_aligned_free(UWord alignment, void *ptr) +{ + ASSERT(alignment && (alignment & ~alignment) == 0); /* power of 2 */ + free(ptr); +} + +void *erts_sys_aligned_realloc(UWord alignment, void *ptr, UWord size, UWord old_size) +{ + void *new_ptr = erts_sys_aligned_alloc(alignment, size); + if (new_ptr) { + UWord copy_size = old_size < size ? old_size : size; + sys_memcpy(new_ptr, ptr, (size_t) copy_size); + erts_sys_aligned_free(alignment, ptr); + } + return new_ptr; +} + +#endif + void *erts_sys_alloc(ErtsAlcType_t t, void *x, Uint sz) { void *res = malloc((size_t) sz); |