blob: ae6810324804919f38ff58c6d7d087a49f121278 [file] [log] [blame]
djsollen@google.com276a2952012-11-19 19:34:23 +00001/*
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/*
18 * Implementation of the user-space ashmem API for devices, which have our
19 * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version,
20 * used by the simulator.
21 */
22
23#include <android/ashmem.h>
24
25#include <unistd.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30#include <fcntl.h>
31
32#include <linux/ashmem.h>
33
34#define ASHMEM_DEVICE "/dev/ashmem"
35
36/*
37 * ashmem_create_region - creates a new ashmem region and returns the file
38 * descriptor, or <0 on error
39 *
40 * `name' is an optional label to give the region (visible in /proc/pid/maps)
41 * `size' is the size of the region, in page-aligned bytes
42 */
43int ashmem_create_region(const char *name, size_t size)
44{
45 int fd, ret;
46
47 fd = open(ASHMEM_DEVICE, O_RDWR);
48 if (fd < 0)
49 return fd;
50
51 if (name) {
52 char buf[ASHMEM_NAME_LEN];
53
54 strlcpy(buf, name, sizeof(buf));
55 ret = ioctl(fd, ASHMEM_SET_NAME, buf);
56 if (ret < 0)
57 goto error;
58 }
59
60 ret = ioctl(fd, ASHMEM_SET_SIZE, size);
61 if (ret < 0)
62 goto error;
63
64 return fd;
65
66error:
67 close(fd);
68 return ret;
69}
70
71int ashmem_set_prot_region(int fd, int prot)
72{
73 return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
74}
75
76int ashmem_pin_region(int fd, size_t offset, size_t len)
77{
78 struct ashmem_pin pin = { offset, len };
79 return ioctl(fd, ASHMEM_PIN, &pin);
80}
81
82int ashmem_unpin_region(int fd, size_t offset, size_t len)
83{
84 struct ashmem_pin pin = { offset, len };
85 return ioctl(fd, ASHMEM_UNPIN, &pin);
86}
87
88int ashmem_get_size_region(int fd)
89{
90 return ioctl(fd, ASHMEM_GET_SIZE, NULL);
91}