轉載的一個(gè)小程序,學(xué)習學(xué)習。
align_malloc函數返回的align地址可按任意字節對齊。分配size+align_ment的空間,多分配alignment空間主要是用來(lái)調整指針的地址。調整好以后將mem_ptr指針與tmp指針之間的偏移量保存在*(memptr-1)的內存地址處,回收內存時(shí)用來(lái)找到真正的內存塊地址即tmp指針。
i386中堆的分配是向上增長(cháng)的,而棧則上向下增長(cháng)。
這里有點(diǎn)不明白,為什么要把這個(gè)偏移量保存在*(memptr-1)中,保存在*(memptr-2)或者一個(gè)全局指針中應該都可以吧??
我們可以選擇保存偏移量,也可以選擇保存指向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);也可以按這種方法來(lái)調整指針
/* 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這個(gè)內存地址用來(lái)存儲調整后的mem_ptr與真正的內存分配塊之間的偏移量*/
*(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
減去這個(gè)偏移值,找到真正的內存分配塊地址*/
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;
}
程序的打印來(lái)看,返回的地址的確是按24字節對齊的。