Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / third_party / ashmem / ashmem-dev.c
blob2303369d81678af9a3c351040419b9d2e6e39b79
1 /*
2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
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>
30 #include <linux/ashmem.h>
31 #include "ashmem.h"
33 #define ASHMEM_DEVICE "/dev/ashmem"
36 * ashmem_create_region - creates a new ashmem region and returns the file
37 * descriptor, or <0 on error
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
42 int ashmem_create_region(const char *name, size_t size)
44 int fd, ret;
46 fd = open(ASHMEM_DEVICE, O_RDWR);
47 if (fd < 0)
48 return fd;
50 if (name) {
51 char buf[ASHMEM_NAME_LEN];
53 strlcpy(buf, name, sizeof(buf));
54 ret = ioctl(fd, ASHMEM_SET_NAME, buf);
55 if (ret < 0)
56 goto error;
59 ret = ioctl(fd, ASHMEM_SET_SIZE, size);
60 if (ret < 0)
61 goto error;
63 return fd;
65 error:
66 close(fd);
67 return ret;
70 int ashmem_set_prot_region(int fd, int prot)
72 return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
75 int ashmem_pin_region(int fd, size_t offset, size_t len)
77 struct ashmem_pin pin = { offset, len };
78 return ioctl(fd, ASHMEM_PIN, &pin);
81 int ashmem_unpin_region(int fd, size_t offset, size_t len)
83 struct ashmem_pin pin = { offset, len };
84 return ioctl(fd, ASHMEM_UNPIN, &pin);
87 int ashmem_get_size_region(int fd)
89 return ioctl(fd, ASHMEM_GET_SIZE, NULL);
92 int ashmem_purge_all(void)
94 const int fd = open(ASHMEM_DEVICE, O_RDWR);
95 if (fd < 0)
96 return fd;
97 const int ret = ioctl(fd, ASHMEM_PURGE_ALL_CACHES, 0);
98 close(fd);
99 return ret;