如何分配预定义堆类型的ION内存?

时间:2019-02-06 10:43:58

标签: c linux ion

我找不到有关ION分配器的文档。 我不知道如何使用ion分配所选堆类型的连续内存。

我尝试使用以下代码分配内存:

    struct ion_allocation_data arg_alloc;

    arg_alloc.len = len;
    arg_alloc.heap_mask = heap_mask;
    arg_alloc.flags = flags;
    arg_alloc.fd = 0;

    ret = ioctl(client, ION_IOC_ALLOC_V1, &arg_alloc);

我不明白逻辑,应该将哪个值放在 heap_mask 变量中。

1 个答案:

答案 0 :(得分:0)

ION的驱动程序包含ioctl的命令“ ION_IOC_HEAP_QUERY”参数,该参数可用于获取有关在具体平台上启用的堆的信息(名称,类型,ID等)。 在以下link上找到了实现示例:

int ion_query_heap_cnt(int fd, int* cnt) {
    int ret;
    struct ion_heap_query query;
    memset(&query, 0, sizeof(query));
    ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
    if (ret < 0) return ret;
    *cnt = query.cnt;
    return ret;
}

int ion_query_get_heaps(int fd, int cnt, void* buffers) {
    int ret;
    struct ion_heap_query query = {
        .cnt = cnt, .heaps = (uintptr_t)buffers,
    };
    ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
    return ret;
}

找到该API的使用示例here

static int find_ion_heap_id(int ion_client, char* name)
{
    int i, ret, cnt, heap_id = -1;
    struct ion_heap_data *data;
    ret = ion_query_heap_cnt(ion_client, &cnt);
    if (ret)
    {
        AERR("ion count query failed with %s", strerror(errno));
        return -1;
    }
    data = (struct ion_heap_data *)malloc(cnt * sizeof(*data));
    if (!data)
    {
        AERR("Error allocating data %s\n", strerror(errno));
        return -1;
    }
    ret = ion_query_get_heaps(ion_client, cnt, data);
    if (ret)
    {
        AERR("Error querying heaps from ion %s", strerror(errno));
    }
    else
    {
        for (i = 0; i < cnt; i++) {
            struct ion_heap_data *dat = (struct ion_heap_data *)data;
            if (strcmp(dat[i].name, name) == 0) {
                heap_id = dat[i].heap_id;
                break;
            }
        }
        if (i > cnt)
        {
            AERR("No System Heap Found amongst %d heaps\n", cnt);
            heap_id = -1;
        }
    }
    free(data);
    return heap_id;
}

这就是获取heap_id的方式。 要获取heap_mask,我们需要:

heap_mask = (1 << heap_id);