📄
字号:
#include <stdlib.h>#include <stdio.h>#include "mem_align.h"#include "../../faraday_mpeg4_common/dev_mem.h"void *xvid_malloc(size_t size, uint8_t alignment){ uint8_t *mem_ptr; if (!alignment) { /* We have not to satisfy any alignment */ if ((mem_ptr = (uint8_t *) malloc(size + 1)) != NULL) { /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-1) */ *mem_ptr = 0; /* Return the mem_ptr pointer */ return (void *) ++mem_ptr; } } else { uint8_t *tmp; /* * Allocate the required size memory + alignment so we * can realign the data if necessary */ if ((tmp = (uint8_t *) malloc(size + alignment)) != NULL) { /* Align the tmp pointer */ mem_ptr = (uint8_t *) ((ptr_t) (tmp + alignment - 1) & (~(ptr_t) (alignment - 1))); /* * Special case where malloc have already satisfied the alignment * We must add alignment to mem_ptr because we must store * (mem_ptr - tmp) in *(mem_ptr-1) * If we do not add alignment to mem_ptr then *(mem_ptr-1) points * to a forbidden memory space */ if (mem_ptr == tmp) mem_ptr += alignment; /* * (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve * the real malloc block allocated and free it in xvid_free */ *(mem_ptr - 1) = (uint8_t) (mem_ptr - tmp); /* Return the aligned pointer */ return (void *) mem_ptr; } } return NULL;}/***************************************************************************** * xvid_free * * Free a previously 'xvid_malloc' allocated block. Does not free NULL * references. * * Returned value : None. * ****************************************************************************/voidxvid_free(void *mem_ptr){ /* *(mem_ptr - 1) give us the offset to the real malloc block */ if (mem_ptr) free((uint8_t *) mem_ptr - *((uint8_t *) mem_ptr - 1));}void *xvid_dma_malloc(size_t size, uint8_t alignment, void ** phy){ uint8_t *virt_mem_ptr; uint32_t tsize; if (!alignment) { tsize = size + 5 + CACHE_LINE; if (dma_malloc(tsize, phy, (void **)&virt_mem_ptr) == 0) { /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-5) */ *virt_mem_ptr ++ = 5; /* Store (malloc size) in *(mem_ptr-4) ~ *(mem_ptr-1)*/ *virt_mem_ptr ++ = (tsize >> 24) & 0xFF; *virt_mem_ptr ++ = (tsize >> 16) & 0xFF; *virt_mem_ptr ++ = (tsize >> 8) & 0xFF; *virt_mem_ptr ++ = (tsize >> 0) & 0xFF; *(uint32_t *)phy += 5; /* Return the mem_ptr pointer */ return (void *) virt_mem_ptr; } } else { uint8_t *tmp; /* * Allocate the required size memory + alignment so we * can realign the data if necessary */ tsize = size + alignment + 4 + CACHE_LINE; if (dma_malloc(size + alignment + CACHE_LINE, phy, (void **)&tmp) == 0) { /* Align the tmp pointer */ virt_mem_ptr = (uint8_t *) ((ptr_t) (tmp + 4 + alignment - 1) & (~(ptr_t) (alignment - 1))); /* * Special case where malloc have already satisfied the alignment * We must add alignment to mem_ptr because we must store * (mem_ptr - tmp) in *(mem_ptr-1) * If we do not add alignment to mem_ptr then *(mem_ptr-1) points * to a forbidden memory space */ while ((virt_mem_ptr - tmp) < 4) virt_mem_ptr += alignment; *phy += (virt_mem_ptr - tmp); /* * (mem_ptr - tmp) is stored in *(mem_ptr-1) so we are able to retrieve * the real malloc block allocated and free it in xvid_free */ *(virt_mem_ptr - 5) = (uint8_t) (virt_mem_ptr - tmp); *(virt_mem_ptr - 4) = (tsize >> 24) & 0xFF; *(virt_mem_ptr - 3) = (tsize >> 16) & 0xFF; *(virt_mem_ptr - 2) = (tsize >> 8) & 0xFF; *(virt_mem_ptr - 1) = (tsize >> 0) & 0xFF; /* Return the aligned pointer */ return (void *) virt_mem_ptr; } } return NULL;}/***************************************************************************** * xvid_free * * Free a previously 'xvid_malloc' allocated block. Does not free NULL * references. * * Returned value : None. * ****************************************************************************/voidxvid_dma_free(void *virt_mem_ptr, void * phy_ptr){ uint32_t tsize; /* *(virt_mem_ptr - 1) give us the offset to the real malloc block */ if (virt_mem_ptr) { tsize = (*(uint8_t *)(virt_mem_ptr - 4) << 24) + (*(uint8_t *)(virt_mem_ptr - 3) << 16) + (*(uint8_t *)(virt_mem_ptr - 2) << 8) + (*(uint8_t *)(virt_mem_ptr - 1) << 0); dma_free(tsize, (void *)((uint32_t) virt_mem_ptr - *((uint8_t *) virt_mem_ptr - 5)), phy_ptr); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -