mirror of
https://github.com/cc65/cc65.git
synced 2025-01-10 19:29:45 +00:00
Optimize -36 bytes out of posix_memalign
And add a unit test
This commit is contained in:
parent
57e65a6bf6
commit
b87df9e1c6
@ -50,7 +50,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size)
|
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
|
/* 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
|
** 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 rawsize;
|
||||||
size_t uppersize;
|
size_t uppersize;
|
||||||
size_t lowersize;
|
size_t lowersize;
|
||||||
|
char err;
|
||||||
register struct usedblock* b; /* points to raw Block */
|
register struct usedblock* b; /* points to raw Block */
|
||||||
register struct usedblock* u; /* points to User block */
|
register struct usedblock* u; /* points to User block */
|
||||||
register struct usedblock* p; /* Points to upper block */
|
register struct usedblock* p; /* Points to upper block */
|
||||||
|
|
||||||
/* Handle requests for zero-sized blocks */
|
/* Handle requests for zero-sized blocks */
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
|
err_einval:
|
||||||
|
err = EINVAL;
|
||||||
|
err_out:
|
||||||
*memptr = NULL;
|
*memptr = NULL;
|
||||||
return EINVAL;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test alignment: is it a power of two? There must be only one bit set. */
|
/* Test alignment: is it a power of two? There must be one and only one bit set. */
|
||||||
if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
|
if (alignment == 0) {
|
||||||
*memptr = NULL;
|
goto err_einval;
|
||||||
return EINVAL;
|
}
|
||||||
|
|
||||||
|
if (alignment & (alignment - 1)) {
|
||||||
|
goto err_einval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Augment the block size up to the alignment, and allocate memory.
|
/* 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 */
|
/* Handle out-of-memory */
|
||||||
if (b == NULL) {
|
if (b == NULL) {
|
||||||
*memptr = NULL;
|
err = ENOMEM;
|
||||||
return ENOMEM;
|
goto err_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create (and return) a new pointer that points to the user-visible
|
/* 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…
x
Reference in New Issue
Block a user