mirror of
https://github.com/cc65/cc65.git
synced 2024-12-27 00:29:31 +00:00
Merge pull request #2383 from colinleroy/pmemalign-size-opt
Optimize -36 bytes out of posix_memalign
This commit is contained in:
commit
ee3a542539
@ -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
|
||||
|
31
test/val/lib_common_pmemalign.c
Normal file
31
test/val/lib_common_pmemalign.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#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
|
Loading…
Reference in New Issue
Block a user