blob: b659d90e8703f9c4ce7a4a504ff11579b90135c0 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Naseer Ahmed29a26812012-06-14 00:56:20 -07002 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#include <unistd.h>
30#include <fcntl.h>
31#include <sys/mman.h>
32#include <stdlib.h>
33#include <cutils/log.h>
34#include <linux/ashmem.h>
35#include <cutils/ashmem.h>
36#include <errno.h>
37#include "ashmemalloc.h"
38
39using gralloc::AshmemAlloc;
40int AshmemAlloc::alloc_buffer(alloc_data& data)
41{
42 int err = 0;
43 int fd = -1;
44 void* base = 0;
45 int offset = 0;
46 char name[ASHMEM_NAME_LEN];
47 snprintf(name, ASHMEM_NAME_LEN, "gralloc-buffer-%x", data.pHandle);
48 int prot = PROT_READ | PROT_WRITE;
49 fd = ashmem_create_region(name, data.size);
50 if (fd < 0) {
51 ALOGE("couldn't create ashmem (%s)", strerror(errno));
52 err = -errno;
53 } else {
54 if (ashmem_set_prot_region(fd, prot) < 0) {
55 ALOGE("ashmem_set_prot_region(fd=%d, prot=%x) failed (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -070056 fd, prot, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070057 close(fd);
58 err = -errno;
59 } else {
60 base = mmap(0, data.size, prot, MAP_SHARED|MAP_POPULATE|MAP_LOCKED, fd, 0);
61 if (base == MAP_FAILED) {
62 ALOGE("alloc mmap(fd=%d, size=%d, prot=%x) failed (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -070063 fd, data.size, prot, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070064 close(fd);
65 err = -errno;
66 } else {
67 memset((char*)base + offset, 0, data.size);
68 }
69 }
70 }
71 if(err == 0) {
72 data.fd = fd;
73 data.base = base;
74 data.offset = offset;
75 clean_buffer(base, data.size, offset, fd);
76 ALOGD("ashmem: Allocated buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -070077 base, data.size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070078
79 }
80 return err;
81
82}
83
84int AshmemAlloc::free_buffer(void* base, size_t size, int offset, int fd)
85{
86 ALOGD("ashmem: Freeing buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -070087 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070088 int err = 0;
89
90 if(!base) {
91 ALOGE("Invalid free");
92 return -EINVAL;
93 }
94 err = unmap_buffer(base, size, offset);
95 close(fd);
96 return err;
97}
98
99int AshmemAlloc::map_buffer(void **pBase, size_t size, int offset, int fd)
100{
101 int err = 0;
102 void *base = 0;
103
104 base = mmap(0, size, PROT_READ| PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700105 MAP_SHARED|MAP_POPULATE, fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700106 *pBase = base;
107 if(base == MAP_FAILED) {
108 ALOGE("ashmem: Failed to map memory in the client: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700109 strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700110 err = -errno;
111 } else {
112 ALOGD("ashmem: Mapped buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700114 }
115 return err;
116}
117
118int AshmemAlloc::unmap_buffer(void *base, size_t size, int offset)
119{
120 ALOGD("ashmem: Unmapping buffer base: %p size: %d", base, size);
121 int err = munmap(base, size);
122 if(err) {
123 ALOGE("ashmem: Failed to unmap memory at %p: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700124 base, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700125 }
126 return err;
127
128}
129int AshmemAlloc::clean_buffer(void *base, size_t size, int offset, int fd)
130{
131 int err = 0;
132 if (ioctl(fd, ASHMEM_CACHE_FLUSH_RANGE, NULL)) {
133 ALOGE("ashmem: ASHMEM_CACHE_FLUSH_RANGE failed fd = %d", fd);
134 }
135
136 return err;
137}
138