blob: 1e512be2b1cc394cd78563d6b7f04c930f85090e [file] [log] [blame]
djsollen@google.com276a2952012-11-19 19:34:23 +00001/*
reed@google.comdd335ae2012-12-13 19:24:05 +00002 * Copyright 2008 The Android Open Source Project
djsollen@google.com276a2952012-11-19 19:34:23 +00003 *
reed@google.comdd335ae2012-12-13 19:24:05 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
djsollen@google.com276a2952012-11-19 19:34:23 +00006 */
7
8/*
9 * Implementation of the user-space ashmem API for devices, which have our
10 * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version,
11 * used by the simulator.
12 */
13
14#include <android/ashmem.h>
15
16#include <unistd.h>
17#include <string.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <sys/ioctl.h>
21#include <fcntl.h>
22
23#include <linux/ashmem.h>
24
commit-bot@chromium.orge9e62d52014-04-17 17:28:20 +000025#include <SkTypes.h> // SkASSERT
26
djsollen@google.com276a2952012-11-19 19:34:23 +000027#define ASHMEM_DEVICE "/dev/ashmem"
28
29/*
30 * ashmem_create_region - creates a new ashmem region and returns the file
31 * descriptor, or <0 on error
32 *
33 * `name' is an optional label to give the region (visible in /proc/pid/maps)
34 * `size' is the size of the region, in page-aligned bytes
35 */
36int ashmem_create_region(const char *name, size_t size)
37{
38 int fd, ret;
39
40 fd = open(ASHMEM_DEVICE, O_RDWR);
41 if (fd < 0)
42 return fd;
43
44 if (name) {
45 char buf[ASHMEM_NAME_LEN];
46
47 strlcpy(buf, name, sizeof(buf));
48 ret = ioctl(fd, ASHMEM_SET_NAME, buf);
49 if (ret < 0)
50 goto error;
51 }
52
53 ret = ioctl(fd, ASHMEM_SET_SIZE, size);
54 if (ret < 0)
55 goto error;
56
57 return fd;
58
59error:
60 close(fd);
61 return ret;
62}
63
64int ashmem_set_prot_region(int fd, int prot)
65{
66 return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
67}
68
69int ashmem_pin_region(int fd, size_t offset, size_t len)
70{
commit-bot@chromium.orge9e62d52014-04-17 17:28:20 +000071 // Skia only calls this when offset=len=0.
72 struct ashmem_pin pin = { static_cast<__u32>(offset),
73 static_cast<__u32>(len) };
74 SkASSERT(pin.offset == offset && pin.len == len);
djsollen@google.com276a2952012-11-19 19:34:23 +000075 return ioctl(fd, ASHMEM_PIN, &pin);
76}
77
78int ashmem_unpin_region(int fd, size_t offset, size_t len)
79{
commit-bot@chromium.orge9e62d52014-04-17 17:28:20 +000080 // Skia only calls this when offset=len=0.
81 struct ashmem_pin pin = { static_cast<__u32>(offset),
82 static_cast<__u32>(len) };
83 SkASSERT(pin.offset == offset && pin.len == len);
djsollen@google.com276a2952012-11-19 19:34:23 +000084 return ioctl(fd, ASHMEM_UNPIN, &pin);
85}
86
87int ashmem_get_size_region(int fd)
88{
89 return ioctl(fd, ASHMEM_GET_SIZE, NULL);
90}
commit-bot@chromium.org0c63fcd2013-03-14 17:56:58 +000091
92int ashmem_purge_all_caches(int fd)
93{
94 return ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL);
95}