From 9db5b8e8e1409ce968e23650e1ec9ff693d19667 Mon Sep 17 00:00:00 2001 From: Dmitry Moskalchuk Date: Mon, 21 Dec 2015 13:08:18 +0300 Subject: [PATCH] [libcrystax] Add posix_memalign implementation Signed-off-by: Dmitry Moskalchuk --- sources/crystax/src/posix_memalign.c | 54 +++++++++++++++++++ .../common.mk | 4 ++ .../host/.gitignore | 2 + .../host/GNUmakefile | 2 + .../jni/Android.mk | 9 ++++ .../jni/Application.mk | 1 + .../jni/main.c | 40 ++++++++++++++ 7 files changed, 112 insertions(+) create mode 100644 sources/crystax/src/posix_memalign.c create mode 100644 tests/device/crystax-issue1194-posix_memalign/common.mk create mode 100644 tests/device/crystax-issue1194-posix_memalign/host/.gitignore create mode 100644 tests/device/crystax-issue1194-posix_memalign/host/GNUmakefile create mode 100644 tests/device/crystax-issue1194-posix_memalign/jni/Android.mk create mode 100644 tests/device/crystax-issue1194-posix_memalign/jni/Application.mk create mode 100644 tests/device/crystax-issue1194-posix_memalign/jni/main.c diff --git a/sources/crystax/src/posix_memalign.c b/sources/crystax/src/posix_memalign.c new file mode 100644 index 000000000..acfec139c --- /dev/null +++ b/sources/crystax/src/posix_memalign.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2011-2015 CrystaX. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY CrystaX ''AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CrystaX OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of CrystaX. + */ + +#include +#include + +int posix_memalign(void **memptr, size_t alignment, size_t size) +{ + void *ptr; + size_t acheck; + + if (memptr == NULL || alignment == 0 || size == 0) + return EINVAL; + + for (acheck = alignment; acheck > 0; acheck /= 2) { + if (acheck == sizeof(void*)) + break; + if (acheck % 2 != 0) + return EINVAL; + } + + ptr = memalign(alignment, size); + if (!ptr) + return ENOMEM; + + *memptr = ptr; + return 0; +} diff --git a/tests/device/crystax-issue1194-posix_memalign/common.mk b/tests/device/crystax-issue1194-posix_memalign/common.mk new file mode 100644 index 000000000..dcc99a117 --- /dev/null +++ b/tests/device/crystax-issue1194-posix_memalign/common.mk @@ -0,0 +1,4 @@ +SRCFILES := main.c + +CFLAGS := -Wall -Wextra -Werror +CFLAGS += -UNDEBUG diff --git a/tests/device/crystax-issue1194-posix_memalign/host/.gitignore b/tests/device/crystax-issue1194-posix_memalign/host/.gitignore new file mode 100644 index 000000000..cd42ee34e --- /dev/null +++ b/tests/device/crystax-issue1194-posix_memalign/host/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ diff --git a/tests/device/crystax-issue1194-posix_memalign/host/GNUmakefile b/tests/device/crystax-issue1194-posix_memalign/host/GNUmakefile new file mode 100644 index 000000000..9e4573c06 --- /dev/null +++ b/tests/device/crystax-issue1194-posix_memalign/host/GNUmakefile @@ -0,0 +1,2 @@ +include ../common.mk +include $(or $(NDK),../../../..)/tests/onhost.mk diff --git a/tests/device/crystax-issue1194-posix_memalign/jni/Android.mk b/tests/device/crystax-issue1194-posix_memalign/jni/Android.mk new file mode 100644 index 000000000..1f8e6d8ca --- /dev/null +++ b/tests/device/crystax-issue1194-posix_memalign/jni/Android.mk @@ -0,0 +1,9 @@ +LOCAL_PATH := $(call my-dir) +include $(LOCAL_PATH)/../common.mk + +include $(CLEAR_VARS) +LOCAL_MODULE := test-posix_memalign +LOCAL_SRC_FILES := $(SRCFILES) +LOCAL_C_INCLUDES := $(LOCAL_PATH) +LOCAL_CFLAGS := $(CFLAGS) +include $(BUILD_EXECUTABLE) diff --git a/tests/device/crystax-issue1194-posix_memalign/jni/Application.mk b/tests/device/crystax-issue1194-posix_memalign/jni/Application.mk new file mode 100644 index 000000000..a252a72d7 --- /dev/null +++ b/tests/device/crystax-issue1194-posix_memalign/jni/Application.mk @@ -0,0 +1 @@ +APP_ABI := all diff --git a/tests/device/crystax-issue1194-posix_memalign/jni/main.c b/tests/device/crystax-issue1194-posix_memalign/jni/main.c new file mode 100644 index 000000000..2c26e8c4f --- /dev/null +++ b/tests/device/crystax-issue1194-posix_memalign/jni/main.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +int main() +{ + int rc; + void *ptr = NULL; + + rc = posix_memalign(&ptr, 0, 0); + assert(rc == EINVAL); + + rc = posix_memalign(&ptr, 1, 0); + assert(rc == EINVAL); + + rc = posix_memalign(&ptr, 1, 4); + assert(rc == EINVAL); + + rc = posix_memalign(&ptr, 5*sizeof(void*), 4); + assert(rc == EINVAL); + + rc = posix_memalign(&ptr, sizeof(void*), 4); + assert(rc == 0); + assert(ptr != NULL); + free(ptr); + + rc = posix_memalign(&ptr, 2*sizeof(void*), 4); + assert(rc == 0); + assert(ptr != NULL); + free(ptr); + + rc = posix_memalign(&ptr, 1024*sizeof(void*), 4); + assert(rc == 0); + assert(ptr != NULL); + free(ptr); + + return 0; +}