summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2017-11-10 12:22:07 -0600
committerElliott Hughes2017-11-10 15:18:10 -0600
commit8e9aeb9053695a14539653093937158d15385ca6 (patch)
treeb02f7ad2149812eea2cb2802b7f238856b66619c /libcutils/ashmem-host.cpp
parent6707ef139d9786887649e3e3c2e3e251a95dc96d (diff)
downloadplatform-system-core-8e9aeb9053695a14539653093937158d15385ca6.tar.gz
platform-system-core-8e9aeb9053695a14539653093937158d15385ca6.tar.xz
platform-system-core-8e9aeb9053695a14539653093937158d15385ca6.zip
Move libcutils source to C++.
Just the minimial changes to get this to actually build, because otherwise we always bog down trying to rewrite everything (when the real answer is usually "stop using libcutils, it's awful"). This doesn't move a handful of files: two are basically just BSD libc source, a couple have outstanding code reviews, and one can be deleted (but I'll do that in a separate change). I'm also skipping the presubmit hooks because otherwise clang-format wants to reformat everything. I'll follow up with that... Bug: N/A Test: builds Change-Id: I06403f465b67c8e493bad466dd76b1151eed5993
Diffstat (limited to 'libcutils/ashmem-host.cpp')
-rw-r--r--libcutils/ashmem-host.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/libcutils/ashmem-host.cpp b/libcutils/ashmem-host.cpp
new file mode 100644
index 000000000..d2c28f393
--- /dev/null
+++ b/libcutils/ashmem-host.cpp
@@ -0,0 +1,93 @@
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cutils/ashmem.h>
18
19/*
20 * Implementation of the user-space ashmem API for the simulator, which lacks
21 * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
22 */
23
24#include <errno.h>
25#include <fcntl.h>
26#include <limits.h>
27#include <stdbool.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <time.h>
34#include <unistd.h>
35
36#include <utils/Compat.h>
37
38#ifndef __unused
39#define __unused __attribute__((__unused__))
40#endif
41
42int ashmem_create_region(const char *ignored __unused, size_t size)
43{
44 char pattern[PATH_MAX];
45 snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
46 int fd = mkstemp(pattern);
47 if (fd == -1) return -1;
48
49 unlink(pattern);
50
51 if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
52 close(fd);
53 return -1;
54 }
55
56 return fd;
57}
58
59int ashmem_set_prot_region(int fd __unused, int prot __unused)
60{
61 return 0;
62}
63
64int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
65{
66 return 0 /*ASHMEM_NOT_PURGED*/;
67}
68
69int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
70{
71 return 0 /*ASHMEM_IS_UNPINNED*/;
72}
73
74int ashmem_get_size_region(int fd)
75{
76 struct stat buf;
77 int result = fstat(fd, &buf);
78 if (result == -1) {
79 return -1;
80 }
81
82 /*
83 * Check if this is an "ashmem" region.
84 * TODO: This is very hacky, and can easily break.
85 * We need some reliable indicator.
86 */
87 if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
88 errno = ENOTTY;
89 return -1;
90 }
91
92 return buf.st_size;
93}