Skip to content

Commit

Permalink
[libcrystax] Add posix_memalign implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Moskalchuk <dm@crystax.net>
  • Loading branch information
dmsck committed Dec 21, 2015
1 parent f9ccca1 commit 9db5b8e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
54 changes: 54 additions & 0 deletions sources/crystax/src/posix_memalign.c
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include <errno.h>

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;
}
4 changes: 4 additions & 0 deletions tests/device/crystax-issue1194-posix_memalign/common.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SRCFILES := main.c

CFLAGS := -Wall -Wextra -Werror
CFLAGS += -UNDEBUG
2 changes: 2 additions & 0 deletions tests/device/crystax-issue1194-posix_memalign/host/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
obj/
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ../common.mk
include $(or $(NDK),../../../..)/tests/onhost.mk
9 changes: 9 additions & 0 deletions tests/device/crystax-issue1194-posix_memalign/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ABI := all
40 changes: 40 additions & 0 deletions tests/device/crystax-issue1194-posix_memalign/jni/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

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;
}

0 comments on commit 9db5b8e

Please sign in to comment.