ashmem-dev.c revision dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0
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/*
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 <unistd.h>
24#include <string.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28#include <fcntl.h>
29
30#include <linux/ashmem.h>
31#include <cutils/ashmem.h>
32
33#define ASHMEM_DEVICE	"/dev/ashmem"
34
35/*
36 * ashmem_create_region - creates a new ashmem region and returns the file
37 * descriptor, or <0 on error
38 *
39 * `name' is an optional label to give the region (visible in /proc/pid/maps)
40 * `size' is the size of the region, in page-aligned bytes
41 */
42int ashmem_create_region(const char *name, size_t size)
43{
44	int fd, ret;
45
46	fd = open(ASHMEM_DEVICE, O_RDWR);
47	if (fd < 0)
48		return fd;
49
50	if (name) {
51		char buf[ASHMEM_NAME_LEN];
52
53		strlcpy(buf, name, sizeof(buf));
54		ret = ioctl(fd, ASHMEM_SET_NAME, buf);
55		if (ret < 0)
56			goto error;
57	}
58
59	ret = ioctl(fd, ASHMEM_SET_SIZE, size);
60	if (ret < 0)
61		goto error;
62
63	return fd;
64
65error:
66	close(fd);
67	return ret;
68}
69
70int ashmem_set_prot_region(int fd, int prot)
71{
72	return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
73}
74
75int ashmem_pin_region(int fd, size_t offset, size_t len)
76{
77	struct ashmem_pin pin = { offset, len };
78	return ioctl(fd, ASHMEM_PIN, &pin);
79}
80
81int ashmem_unpin_region(int fd, size_t offset, size_t len)
82{
83	struct ashmem_pin pin = { offset, len };
84	return ioctl(fd, ASHMEM_UNPIN, &pin);
85}
86