1 /* 2 * Copyright 2008 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 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 25 #define ASHMEM_DEVICE "/dev/ashmem" 26 27 /* 28 * ashmem_create_region - creates a new ashmem region and returns the file 29 * descriptor, or <0 on error 30 * 31 * `name' is an optional label to give the region (visible in /proc/pid/maps) 32 * `size' is the size of the region, in page-aligned bytes 33 */ 34 int ashmem_create_region(const char *name, size_t size) 35 { 36 int fd, ret; 37 38 fd = open(ASHMEM_DEVICE, O_RDWR); 39 if (fd < 0) 40 return fd; 41 42 if (name) { 43 char buf[ASHMEM_NAME_LEN]; 44 45 strlcpy(buf, name, sizeof(buf)); 46 ret = ioctl(fd, ASHMEM_SET_NAME, buf); 47 if (ret < 0) 48 goto error; 49 } 50 51 ret = ioctl(fd, ASHMEM_SET_SIZE, size); 52 if (ret < 0) 53 goto error; 54 55 return fd; 56 57 error: 58 close(fd); 59 return ret; 60 } 61 62 int ashmem_set_prot_region(int fd, int prot) 63 { 64 return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); 65 } 66 67 int ashmem_pin_region(int fd, size_t offset, size_t len) 68 { 69 struct ashmem_pin pin = { offset, len }; 70 return ioctl(fd, ASHMEM_PIN, &pin); 71 } 72 73 int ashmem_unpin_region(int fd, size_t offset, size_t len) 74 { 75 struct ashmem_pin pin = { offset, len }; 76 return ioctl(fd, ASHMEM_UNPIN, &pin); 77 } 78 79 int ashmem_get_size_region(int fd) 80 { 81 return ioctl(fd, ASHMEM_GET_SIZE, NULL); 82 } 83 84 int ashmem_purge_all_caches(int fd) 85 { 86 return ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL); 87 } 88