ION设计的目标
为了避免内存碎片化,或者为一些有着特殊内存需求的硬件,比如GPUs、display controller以及camera等,在系统启动的时候,会为他们预留一些memory pools,这些memory pools就由ION来管理。通过ION就可以在硬件以及user space之间实现zero-copy的内存share。
ION的实现
ION通过ION heaps来展示presents它对应的memory pools。不同的Android硬件可能会要求不同的ION heaps实现,默认的ION驱动会提供如下三种不同的ION heaps实现:ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc_user()
ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kzalloc
. ION_HEAP_TYPE_CARVEOUT: carveout memory is physically contiguous and set aside at boot.
开发者可以自己实现更多的ION heaps。比如NVIDIA就提交了一种ION_HEAP_TYPE_IOMMU的heap,这种heap带有IOMMU功能。
不管哪一种ION heaps实现,他们都必须实现如下接口:struct ion_heap_ops { int (*allocate) (struct ion_heap *heap, struct ion_buffer *buffer, unsigned long len, unsigned long align, unsigned long flags); void (*free) (struct ion_buffer *buffer); int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
ion_phys_addr_t *addr, size_t *len); struct scatterlist *(*map_dma) (struct ion_heap *heap, struct ion_buffer *buffer); void (*unmap_dma) (struct ion_heap *heap,
struct ion_buffer *buffer); void * (*map_kernel) (struct ion_heap *heap,
struct ion_buffer *buffer); void (*unmap_kernel) (struct ion_heap *heap,
struct ion_buffer *buffer); int (*map_user) (struct ion_heap *heap, struct ion_buffer *buffer, struct vm