1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/utils/android/ashmem.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* 1.5 + * Copyright 2008 The Android Open Source Project 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +/* 1.12 + * Implementation of the user-space ashmem API for devices, which have our 1.13 + * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version, 1.14 + * used by the simulator. 1.15 + */ 1.16 + 1.17 +#include <android/ashmem.h> 1.18 + 1.19 +#include <unistd.h> 1.20 +#include <string.h> 1.21 +#include <sys/types.h> 1.22 +#include <sys/stat.h> 1.23 +#include <sys/ioctl.h> 1.24 +#include <fcntl.h> 1.25 + 1.26 +#include <linux/ashmem.h> 1.27 + 1.28 +#define ASHMEM_DEVICE "/dev/ashmem" 1.29 + 1.30 +/* 1.31 + * ashmem_create_region - creates a new ashmem region and returns the file 1.32 + * descriptor, or <0 on error 1.33 + * 1.34 + * `name' is an optional label to give the region (visible in /proc/pid/maps) 1.35 + * `size' is the size of the region, in page-aligned bytes 1.36 + */ 1.37 +int ashmem_create_region(const char *name, size_t size) 1.38 +{ 1.39 + int fd, ret; 1.40 + 1.41 + fd = open(ASHMEM_DEVICE, O_RDWR); 1.42 + if (fd < 0) 1.43 + return fd; 1.44 + 1.45 + if (name) { 1.46 + char buf[ASHMEM_NAME_LEN]; 1.47 + 1.48 + strlcpy(buf, name, sizeof(buf)); 1.49 + ret = ioctl(fd, ASHMEM_SET_NAME, buf); 1.50 + if (ret < 0) 1.51 + goto error; 1.52 + } 1.53 + 1.54 + ret = ioctl(fd, ASHMEM_SET_SIZE, size); 1.55 + if (ret < 0) 1.56 + goto error; 1.57 + 1.58 + return fd; 1.59 + 1.60 +error: 1.61 + close(fd); 1.62 + return ret; 1.63 +} 1.64 + 1.65 +int ashmem_set_prot_region(int fd, int prot) 1.66 +{ 1.67 + return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); 1.68 +} 1.69 + 1.70 +int ashmem_pin_region(int fd, size_t offset, size_t len) 1.71 +{ 1.72 + struct ashmem_pin pin = { offset, len }; 1.73 + return ioctl(fd, ASHMEM_PIN, &pin); 1.74 +} 1.75 + 1.76 +int ashmem_unpin_region(int fd, size_t offset, size_t len) 1.77 +{ 1.78 + struct ashmem_pin pin = { offset, len }; 1.79 + return ioctl(fd, ASHMEM_UNPIN, &pin); 1.80 +} 1.81 + 1.82 +int ashmem_get_size_region(int fd) 1.83 +{ 1.84 + return ioctl(fd, ASHMEM_GET_SIZE, NULL); 1.85 +} 1.86 + 1.87 +int ashmem_purge_all_caches(int fd) 1.88 +{ 1.89 + return ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL); 1.90 +}