轉(zhuǎn)載的一個小程序,學(xué)習(xí)學(xué)習(xí)。
align_malloc函數(shù)返回的align地址可按任意字節(jié)對齊。分配size+align_ment的空間,多分配alignment空間主要是用來調(diào)整指針的地址。調(diào)整好以后將mem_ptr指針與tmp指針之間的偏移量保存在*(memptr-1)的內(nèi)存地址處,回收內(nèi)存時用來找到真正的內(nèi)存塊地址即tmp指針。
i386中堆的分配是向上增長的,而棧則上向下增長。
這里有點不明白,為什么要把這個偏移量保存在*(memptr-1)中,保存在*(memptr-2)或者一個全局指針中應(yīng)該都可以吧??
我們可以選擇保存偏移量,也可以選擇保存指向tmp指針的指針。
#include <stdio.h>
#include <string.h> #include <stdlib.h> void * align_malloc(unsigned int size, unsigned int alignment)
{ unsigned char * mem_ptr; unsigned char * tmp; if(!alignment) alignment=4; //至少按4對齊
/* Allocate the required size memory + alignment so we * can realign the data if necessary */ if ((tmp = (unsigned char *) malloc(size + alignment)) != NULL){ /* Align the tmp pointer */
mem_ptr = (unsigned char *) ((unsigned int) (tmp + alignment - 1) & (~(unsigned int) (alignment - 1))); /////////mem_ptr= (unsigned char*)tmp + (alignment - (unsigned long)(tmp) % alignment);也可以按這種方法來調(diào)整指針
/* 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這個內(nèi)存地址用來存儲調(diào)整后的mem_ptr與真正的內(nèi)存分配塊之間的偏移量*/ *(mem_ptr - 1) = (unsigned char) (mem_ptr - tmp); //PRT("Alloc mem addr: 0x%08x, size:% 8d, file:%s <line:%d>, ", tmp, size, file, line); /**//* Return the aligned pointer */ printf("memptr address is %p\n",mem_ptr); printf("tmp address is %p\n",tmp); printf("offset is %d\n",*(mem_ptr -1)); return ((void *)mem_ptr); } return(NULL); } /**//*****************************************************************************
* align_free * * Free a previously 'xvid_malloc' allocated block. Does not free NULL * references. * * Returned value : None. * ****************************************************************************/ void align_free(void *mem_ptr)
{ unsigned char *ptr; if (mem_ptr == NULL) return; /**//* Aligned pointer */
ptr = (unsigned char *)mem_ptr; /* *(ptr - 1) holds the offset to the real allocated block
* we sub that offset os we free the real pointer 減去這個偏移值,找到真正的內(nèi)存分配塊地址*/ ptr -= *(ptr - 1); /**//* Free the memory */
free(ptr); } int main()
{ int *ptr; ptr = (int *)align_malloc(256,24); printf("address is %p\n",ptr); printf("value is %d \n",(unsigned int)ptr % 24); align_free(ptr); return 0; } 程序的打印來看,返回的地址的確是按24字節(jié)對齊的。
|
|
來自: tom and jerry > 《C語言》