Introduce ashmem from libcutils of android.
authorjiseob.jang <jiseob.jang@samsung.com>
Mon, 26 Mar 2018 11:09:20 +0000 (20:09 +0900)
committer최형규/동작제어Lab(SR)/Senior Engineer/삼성전자 <hk0110.choi@samsung.com>
Mon, 26 Mar 2018 11:20:31 +0000 (20:20 +0900)
This commit introduce ashmem from libcutils of android.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
src/runtime/ref/CMakeLists.txt
src/runtime/ref/nn/depend/CMakeLists.txt
src/runtime/ref/nn/depend/libcutils/CMakeLists.txt
src/runtime/ref/nn/depend/libcutils/ashmem-host.c [new file with mode: 0644]
src/runtime/ref/nn/depend/libcutils/include/cutils/ashmem.h [new file with mode: 0644]
src/runtime/ref/nn/depend/libutils/CMakeLists.txt [new file with mode: 0644]
src/runtime/ref/nn/depend/libutils/include/utils/Compat.h [new file with mode: 0644]

index eb959c9..53c6aef 100644 (file)
@@ -1,10 +1,7 @@
-set(CMAKE_C_FLAGS "-std=c99")
-
 if(CMAKE_VERSION VERSION_LESS 3.1.0)
-  set(CMAKE_CXX_FLAGS "-std=c++14")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
 else(CMAKE_VERSION VERSION_LESS 3.1.0)
   set(CMAKE_CXX_STANDARD 14)
 endif(CMAKE_VERSION VERSION_LESS 3.1.0)
 
-#set(CMAKE_CXX_FLAGS "-std=c++14")
 add_subdirectory(nn)
index 7254e88..09bc183 100644 (file)
@@ -7,7 +7,7 @@ ADD_SUBDIRECTORY(libhidl)
 #ADD_SUBDIRECTORY(libhwbinder)
 #ADD_SUBDIRECTORY(liblog)
 #ADD_SUBDIRECTORY(libsystem)
-#ADD_SUBDIRECTORY(libutils)
+ADD_SUBDIRECTORY(libutils)
 #ADD_SUBDIRECTORY(libvndksupport)
 
 SET(INC_DIRS
index fd0cc94..230628f 100644 (file)
@@ -11,7 +11,7 @@ SET(INC_DIRS
 )
 
 SET(CUR_SRCS
-#  ${CMAKE_CURRENT_SOURCE_DIR}/ashmem-host.c
+  ${CMAKE_CURRENT_SOURCE_DIR}/ashmem-host.c
   ${CMAKE_CURRENT_SOURCE_DIR}/native_handle.c
 #  ${CMAKE_CURRENT_SOURCE_DIR}/properties.cpp
 #  ${CMAKE_CURRENT_SOURCE_DIR}/sched_policy.cpp
diff --git a/src/runtime/ref/nn/depend/libcutils/ashmem-host.c b/src/runtime/ref/nn/depend/libcutils/ashmem-host.c
new file mode 100644 (file)
index 0000000..1f9f753
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Implementation of the user-space ashmem API for the simulator, which lacks
+ * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <cutils/ashmem.h>
+#include <utils/Compat.h>
+
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
+
+int ashmem_create_region(const char *ignored __unused, size_t size)
+{
+    char template[PATH_MAX];
+    snprintf(template, sizeof(template), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
+    int fd = mkstemp(template);
+    if (fd == -1) return -1;
+
+    unlink(template);
+
+    if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
+      close(fd);
+      return -1;
+    }
+
+    return fd;
+}
+
+int ashmem_set_prot_region(int fd __unused, int prot __unused)
+{
+    return 0;
+}
+
+int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
+{
+    return 0 /*ASHMEM_NOT_PURGED*/;
+}
+
+int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
+{
+    return 0 /*ASHMEM_IS_UNPINNED*/;
+}
+
+int ashmem_get_size_region(int fd)
+{
+    struct stat buf;
+    int result = fstat(fd, &buf);
+    if (result == -1) {
+        return -1;
+    }
+
+    /*
+     * Check if this is an "ashmem" region.
+     * TODO: This is very hacky, and can easily break.
+     * We need some reliable indicator.
+     */
+    if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
+        errno = ENOTTY;
+        return -1;
+    }
+
+    return buf.st_size;
+}
diff --git a/src/runtime/ref/nn/depend/libcutils/include/cutils/ashmem.h b/src/runtime/ref/nn/depend/libcutils/include/cutils/ashmem.h
new file mode 100644 (file)
index 0000000..d80caa6
--- /dev/null
@@ -0,0 +1,34 @@
+/* cutils/ashmem.h
+ **
+ ** Copyright 2008 The Android Open Source Project
+ **
+ ** This file is dual licensed.  It may be redistributed and/or modified
+ ** under the terms of the Apache 2.0 License OR version 2 of the GNU
+ ** General Public License.
+ */
+
+#ifndef _CUTILS_ASHMEM_H
+#define _CUTILS_ASHMEM_H
+
+#include <stddef.h>
+
+#if defined(__BIONIC__)
+#include <linux/ashmem.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int ashmem_valid(int fd);
+int ashmem_create_region(const char *name, size_t size);
+int ashmem_set_prot_region(int fd, int prot);
+int ashmem_pin_region(int fd, size_t offset, size_t len);
+int ashmem_unpin_region(int fd, size_t offset, size_t len);
+int ashmem_get_size_region(int fd);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _CUTILS_ASHMEM_H */
diff --git a/src/runtime/ref/nn/depend/libutils/CMakeLists.txt b/src/runtime/ref/nn/depend/libutils/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5e35848
--- /dev/null
@@ -0,0 +1,32 @@
+
+SET(CUR_INCS
+   ${CMAKE_CURRENT_SOURCE_DIR}/include
+)
+
+
+SET(INC_DIRS
+  ${INC_DIRS}
+  ${CUR_INCS}
+  PARENT_SCOPE
+)
+
+SET(CUR_SRCS
+#  ${CMAKE_CURRENT_SOURCE_DIR}/misc.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/NativeHandle.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/RefBase.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/SharedBuffer.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/SharedBuffer.h
+#  ${CMAKE_CURRENT_SOURCE_DIR}/String16.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/String8.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/SystemClock.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/Threads.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/Timers.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/Unicode.cpp
+#  ${CMAKE_CURRENT_SOURCE_DIR}/VectorImpl.cpp
+)
+
+SET(SRCS
+  ${SRCS}
+#  ${CUR_SRCS}
+  PARENT_SCOPE
+)
diff --git a/src/runtime/ref/nn/depend/libutils/include/utils/Compat.h b/src/runtime/ref/nn/depend/libutils/include/utils/Compat.h
new file mode 100644 (file)
index 0000000..dee577e
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __LIB_UTILS_COMPAT_H
+#define __LIB_UTILS_COMPAT_H
+
+#include <unistd.h>
+
+#if defined(__APPLE__)
+
+/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
+
+typedef off_t off64_t;
+
+static inline off64_t lseek64(int fd, off64_t offset, int whence) {
+    return lseek(fd, offset, whence);
+}
+
+static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
+    return pread(fd, buf, nbytes, offset);
+}
+
+static inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes, off64_t offset) {
+    return pwrite(fd, buf, nbytes, offset);
+}
+
+static inline int ftruncate64(int fd, off64_t length) {
+    return ftruncate(fd, length);
+}
+
+#endif /* __APPLE__ */
+
+#if defined(_WIN32)
+#define O_CLOEXEC O_NOINHERIT
+#define O_NOFOLLOW 0
+#define DEFFILEMODE 0666
+#endif /* _WIN32 */
+
+#define ZD "%zd"
+#define ZD_TYPE ssize_t
+
+/*
+ * Needed for cases where something should be constexpr if possible, but not
+ * being constexpr is fine if in pre-C++11 code (such as a const static float
+ * member variable).
+ */
+#if __cplusplus >= 201103L
+#define CONSTEXPR constexpr
+#else
+#define CONSTEXPR
+#endif
+
+/*
+ * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
+ * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
+ * not already defined, then define it here.
+ */
+#ifndef TEMP_FAILURE_RETRY
+/* Used to retry syscalls that can return EINTR. */
+#define TEMP_FAILURE_RETRY(exp) ({         \
+    typeof (exp) _rc;                      \
+    do {                                   \
+        _rc = (exp);                       \
+    } while (_rc == -1 && errno == EINTR); \
+    _rc; })
+#endif
+
+#if defined(_WIN32)
+#define OS_PATH_SEPARATOR '\\'
+#else
+#define OS_PATH_SEPARATOR '/'
+#endif
+
+#endif /* __LIB_UTILS_COMPAT_H */