djsollen@google.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 1 | /* |
reed@google.com | dd335ae | 2012-12-13 19:24:05 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
djsollen@google.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 3 | * |
reed@google.com | dd335ae | 2012-12-13 19:24:05 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
djsollen@google.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 6 | */ |
| 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.org | e9e62d5 | 2014-04-17 17:28:20 +0000 | [diff] [blame^] | 25 | #include <SkTypes.h> // SkASSERT |
| 26 | |
djsollen@google.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 27 | #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 | */ |
| 36 | int 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 | |
| 59 | error: |
| 60 | close(fd); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | int ashmem_set_prot_region(int fd, int prot) |
| 65 | { |
| 66 | return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); |
| 67 | } |
| 68 | |
| 69 | int ashmem_pin_region(int fd, size_t offset, size_t len) |
| 70 | { |
commit-bot@chromium.org | e9e62d5 | 2014-04-17 17:28:20 +0000 | [diff] [blame^] | 71 | // 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.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 75 | return ioctl(fd, ASHMEM_PIN, &pin); |
| 76 | } |
| 77 | |
| 78 | int ashmem_unpin_region(int fd, size_t offset, size_t len) |
| 79 | { |
commit-bot@chromium.org | e9e62d5 | 2014-04-17 17:28:20 +0000 | [diff] [blame^] | 80 | // 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.com | 276a295 | 2012-11-19 19:34:23 +0000 | [diff] [blame] | 84 | return ioctl(fd, ASHMEM_UNPIN, &pin); |
| 85 | } |
| 86 | |
| 87 | int ashmem_get_size_region(int fd) |
| 88 | { |
| 89 | return ioctl(fd, ASHMEM_GET_SIZE, NULL); |
| 90 | } |
commit-bot@chromium.org | 0c63fcd | 2013-03-14 17:56:58 +0000 | [diff] [blame] | 91 | |
| 92 | int ashmem_purge_all_caches(int fd) |
| 93 | { |
| 94 | return ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL); |
| 95 | } |