26 #include <sys/ioctl.h>
29 #include <sys/types.h>
32 #define ALOGE(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
36 int fd = open(
"/dev/ion", O_RDWR);
38 ALOGE(
"open /dev/ion failed!\n");
50 int ion_ioctl(
int fd,
int req,
void *arg)
52 int ret = ioctl(fd, req, arg);
54 ALOGE(
"ioctl %x failed with code %d: %s\n", req,
55 ret, strerror(errno));
61 int ion_alloc(
int fd,
size_t len,
size_t align,
unsigned int heap_mask,
62 unsigned int flags, ion_user_handle_t *handle)
68 .heap_id_mask = heap_mask,
75 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
78 *handle = data.handle;
82 int ion_free(
int fd, ion_user_handle_t handle)
87 return ion_ioctl(fd, ION_IOC_FREE, &data);
90 int ion_map(
int fd, ion_user_handle_t handle,
size_t length,
int prot,
91 int flags, off_t offset,
unsigned char **ptr,
int *map_fd)
103 ret = ion_ioctl(fd, ION_IOC_MAP, &data);
108 ALOGE(
"map ioctl returned negative fd\n");
111 *ptr = mmap(NULL, length, prot, flags, *map_fd, offset);
112 if (*ptr == MAP_FAILED) {
113 ALOGE(
"mmap failed: %s\n", strerror(errno));
119 int ion_share(
int fd, ion_user_handle_t handle,
int *share_fd)
126 if (share_fd == NULL)
129 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
134 ALOGE(
"share ioctl returned negative fd\n");
140 int ion_alloc_fd(
int fd,
size_t len,
size_t align,
unsigned int heap_mask,
141 unsigned int flags,
int *handle_fd) {
142 ion_user_handle_t handle;
145 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
148 ret = ion_share(fd, handle, handle_fd);
149 ion_free(fd, handle);
153 int ion_import(
int fd,
int share_fd, ion_user_handle_t *handle)
163 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
166 *handle = data.handle;
170 int ion_sync_fd(
int fd,
int handle_fd)
175 return ion_ioctl(fd, ION_IOC_SYNC, &data);