?? mem_align.c
字號:
#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) { size += CACHE_LINE; //reserved_size; /* 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; *mem_ptr = 1; /* 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 */ size += CACHE_LINE; //reserved_size; 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; uint8_t *virt_ret; uint32_t tsize; if (!alignment) { tsize = size + 9 + CACHE_LINE; if (dma_malloc(tsize, phy, (void **)&virt_mem_ptr, (void **)&virt_ret) == 0) { /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-9) */ *virt_mem_ptr ++ = 9; /* Store (malloc size) in *(mem_ptr-8) ~ *(mem_ptr-5)*/ *virt_mem_ptr ++ = (tsize >> 24) & 0xFF; *virt_mem_ptr ++ = (tsize >> 16) & 0xFF; *virt_mem_ptr ++ = (tsize >> 8) & 0xFF; *virt_mem_ptr ++ = (tsize >> 0) & 0xFF; /* Store (virt_ret at consistent_alloc) in *(mem_ptr-4) ~ *(mem_ptr-1)*/ *virt_mem_ptr ++ = ((uint32_t)virt_ret >> 24) & 0xFF; *virt_mem_ptr ++ = ((uint32_t)virt_ret >> 16) & 0xFF; *virt_mem_ptr ++ = ((uint32_t)virt_ret >> 8) & 0xFF; *virt_mem_ptr ++ = ((uint32_t)virt_ret >> 0) & 0xFF; *(uint32_t *)phy += 9; /* 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 + 8 + CACHE_LINE; if (dma_malloc(tsize, phy, (void **)&tmp, (void **)&virt_ret) == 0) { /* Align the tmp pointer */ virt_mem_ptr = (uint8_t *) ((ptr_t) (tmp + 8 + 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) <= 8) 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 */ /* Store (mem_ptr - "real allocated memory") in *(mem_ptr-5) */ *(virt_mem_ptr - 9) = (uint8_t) (virt_mem_ptr - tmp); /* Store (malloc size) in *(mem_ptr-8) ~ *(mem_ptr-5)*/ *(virt_mem_ptr - 8) = (tsize >> 24) & 0xFF; *(virt_mem_ptr - 7) = (tsize >> 16) & 0xFF; *(virt_mem_ptr - 6) = (tsize >> 8) & 0xFF; *(virt_mem_ptr - 5) = (tsize >> 0) & 0xFF; /* Store (virt_ret at consistent_alloc) in *(mem_ptr-4) ~ *(mem_ptr-1)*/ *(virt_mem_ptr - 4) = ((uint32_t)virt_ret >> 24) & 0xFF; *(virt_mem_ptr - 3) = ((uint32_t)virt_ret >> 16) & 0xFF; *(virt_mem_ptr - 2) = ((uint32_t)virt_ret >> 8) & 0xFF; *(virt_mem_ptr - 1) = ((uint32_t)virt_ret >> 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; uint8_t *virt_ret; uint8_t offset; /* *(virt_mem_ptr - 1) give us the offset to the real malloc block */ if (virt_mem_ptr) { offset = *(uint8_t *) (virt_mem_ptr - 9); tsize = (*(uint8_t *)(virt_mem_ptr - 8) << 24) + (*(uint8_t *)(virt_mem_ptr - 7) << 16) + (*(uint8_t *)(virt_mem_ptr - 6) << 8) + (*(uint8_t *)(virt_mem_ptr - 5) << 0); virt_ret = (uint8_t*)((*(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) phy_ptr - offset), (void *)((uint32_t) virt_mem_ptr - offset), (void *)virt_ret); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -