From b87df9e1c6326b4983a821d3035f8bafba221805 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Fri, 12 Jan 2024 23:04:14 +0100 Subject: [PATCH] Optimize -36 bytes out of posix_memalign And add a unit test --- libsrc/common/pmemalign.c | 22 ++++++++++++++-------- test/val/lib_common_pmemalign.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 test/val/lib_common_pmemalign.c diff --git a/libsrc/common/pmemalign.c b/libsrc/common/pmemalign.c index 52adb240d..4499084d1 100644 --- a/libsrc/common/pmemalign.c +++ b/libsrc/common/pmemalign.c @@ -50,7 +50,6 @@ */ - int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size) /* Allocate a block of memory with the given "size", which is aligned to a ** memory address that is a multiple of "alignment". "alignment" MUST NOT be @@ -64,20 +63,27 @@ int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size) size_t rawsize; size_t uppersize; size_t lowersize; + char err; register struct usedblock* b; /* points to raw Block */ register struct usedblock* u; /* points to User block */ register struct usedblock* p; /* Points to upper block */ /* Handle requests for zero-sized blocks */ if (size == 0) { +err_einval: + err = EINVAL; +err_out: *memptr = NULL; - return EINVAL; + return err; } - /* Test alignment: is it a power of two? There must be only one bit set. */ - if (alignment == 0 || (alignment & (alignment - 1)) != 0) { - *memptr = NULL; - return EINVAL; + /* Test alignment: is it a power of two? There must be one and only one bit set. */ + if (alignment == 0) { + goto err_einval; + } + + if (alignment & (alignment - 1)) { + goto err_einval; } /* Augment the block size up to the alignment, and allocate memory. @@ -90,8 +96,8 @@ int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size) /* Handle out-of-memory */ if (b == NULL) { - *memptr = NULL; - return ENOMEM; + err = ENOMEM; + goto err_out; } /* Create (and return) a new pointer that points to the user-visible diff --git a/test/val/lib_common_pmemalign.c b/test/val/lib_common_pmemalign.c new file mode 100644 index 000000000..0e9e5f52f --- /dev/null +++ b/test/val/lib_common_pmemalign.c @@ -0,0 +1,31 @@ +#include +#include +#include "unittest.h" + +TEST +{ + void *buf; + int r; + + r = posix_memalign(&buf, 123, 1024); + ASSERT_IsTrue(r == EINVAL, "posix_memalign did not return EINVAL with wrong alignment"); + ASSERT_IsTrue(buf == NULL, "posix_memalign did not set buf to NULL with wrong alignment"); + + r = posix_memalign(&buf, 0, 1024); + ASSERT_IsTrue(r == EINVAL, "posix_memalign did not return EINVAL with 0 alignment"); + ASSERT_IsTrue(buf == NULL, "posix_memalign did not set buf to NULL with 0 alignment"); + + r = posix_memalign(&buf, 256, 0); + ASSERT_IsTrue(r == EINVAL, "posix_memalign did not return EINVAL with 0 size"); + ASSERT_IsTrue(buf == NULL, "posix_memalign did not set buf to NULL with 0 size"); + + r = posix_memalign(&buf, 256, 32768U); + ASSERT_IsTrue(r == 0, "posix_memalign did not return 0 on correct call"); + ASSERT_IsTrue(buf != NULL, "posix_memalign left buf set to NULL on correct call"); + ASSERT_IsTrue(((unsigned int)buf & 0x00FF) == 0x00, "posix_memalign did not align memory"); + + r = posix_memalign(&buf, 256, 32768U); + ASSERT_IsTrue(r == ENOMEM, "posix_memalign did not return ENOMEM when no memory is available"); + ASSERT_IsTrue(buf == NULL, "posix_memalign did not set buf to NULL when no memory is available"); +} +ENDTEST