michael@0: /* michael@0: * Copyright 2008 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: /* michael@0: * Implementation of the user-space ashmem API for devices, which have our michael@0: * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version, michael@0: * used by the simulator. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #define ASHMEM_DEVICE "/dev/ashmem" michael@0: michael@0: /* michael@0: * ashmem_create_region - creates a new ashmem region and returns the file michael@0: * descriptor, or <0 on error michael@0: * michael@0: * `name' is an optional label to give the region (visible in /proc/pid/maps) michael@0: * `size' is the size of the region, in page-aligned bytes michael@0: */ michael@0: int ashmem_create_region(const char *name, size_t size) michael@0: { michael@0: int fd, ret; michael@0: michael@0: fd = open(ASHMEM_DEVICE, O_RDWR); michael@0: if (fd < 0) michael@0: return fd; michael@0: michael@0: if (name) { michael@0: char buf[ASHMEM_NAME_LEN]; michael@0: michael@0: strlcpy(buf, name, sizeof(buf)); michael@0: ret = ioctl(fd, ASHMEM_SET_NAME, buf); michael@0: if (ret < 0) michael@0: goto error; michael@0: } michael@0: michael@0: ret = ioctl(fd, ASHMEM_SET_SIZE, size); michael@0: if (ret < 0) michael@0: goto error; michael@0: michael@0: return fd; michael@0: michael@0: error: michael@0: close(fd); michael@0: return ret; michael@0: } michael@0: michael@0: int ashmem_set_prot_region(int fd, int prot) michael@0: { michael@0: return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); michael@0: } michael@0: michael@0: int ashmem_pin_region(int fd, size_t offset, size_t len) michael@0: { michael@0: struct ashmem_pin pin = { offset, len }; michael@0: return ioctl(fd, ASHMEM_PIN, &pin); michael@0: } michael@0: michael@0: int ashmem_unpin_region(int fd, size_t offset, size_t len) michael@0: { michael@0: struct ashmem_pin pin = { offset, len }; michael@0: return ioctl(fd, ASHMEM_UNPIN, &pin); michael@0: } michael@0: michael@0: int ashmem_get_size_region(int fd) michael@0: { michael@0: return ioctl(fd, ASHMEM_GET_SIZE, NULL); michael@0: } michael@0: michael@0: int ashmem_purge_all_caches(int fd) michael@0: { michael@0: return ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL); michael@0: }