午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

動態(tài)內(nèi)存分配,地址對齊

 tom and jerry 2010-06-13
轉(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é)對齊的。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多