《Windows核心编程》---C运行库的内存对齐函数

来源:互联网 发布:逆火采集软件 编辑:程序博客网 时间:2024/05/22 23:51

C运行库提供了一系列函数用于分配对齐过的内存:

1_aligned_malloc函数的功能是分配一块对齐过的内存:

void * _aligned_malloc(

    size_t size,  //要分配的字节数

    size_t alignment //要对齐到的字节边界,传给alignment的值必须是2的整数幂次方

);

 

2_aligned_offset_malloc函数用于在指定的内存对齐边界上分配内存:

void * _aligned_offset_malloc(

   size_t size,  //要分配的字节数

   size_t alignment,  //要对齐到的字节边界,传给alignment的值必须是2的整数幂次方

   size_t offset //为达到内存对齐所指定的偏移量

);

 

3_aligned_realloc函数和_aligned_offset_realloc函数用于改变由_aligned_malloc或者_aligned_offset_malloc分配的内存的大小:

void * _aligned_realloc(

   void *memblock, //要改变大小的内存地址

   size_t size, //新分配的字节数

   size_t alignment //要对齐到的字节边界,传给alignment的值必须是2的整数幂次方

);

 

void * _aligned_offset_realloc(

   void *memblock,

   size_t size,

   size_t alignment,

   size_t offset

);

 

4_aligned_free函数用于释放由上述函数申请的内存资源:

void _aligned_free (

   void *memblock

);

 

实例代码如下:

#include <iostream>

#include <malloc.h>

#include <stdio.h>

 

using namespace std;

 

int main()

{

    void *ptr;

    size_t alignment;

    size_t offset;

   

    //注意alignment必须是2的整数幂次方

    alignment = 16;

    offset = 5;

    //使用_aligned_malloc

    ptr = _aligned_malloc(100, alignment);

    if(ptr == NULL)

    {

        printf_s("Error allocation aligned memory/n");

        return -1;      

    }

    if(((int)ptr % alignment) == 0)

    {

        printf_s("This pointer, %d, is aligned on %d/n",

                      ptr, alignment);            

    }

    else

    {

        printf_s("This pointer, %d, is not aligned on %d/n",

                       ptr, alignment);

    }

   

    //使用_aligned_realloc

    ptr = _aligned_realloc(ptr, 200, alignment);

    if(((int)ptr % alignment) == 0)

    {

        printf_s("This pointer, %d, is aligned on %d/n",

                       ptr, alignment);            

    }

    else

    {

        printf_s("This pointer, %d, is not aligned on %d/n",

                       ptr, alignment);   

    }

   

    //释放

    _aligned_free(ptr);

   

    //使用_aligned_offset_malloc

    ptr = _aligned_offset_malloc(200, alignment, offset);

    if(ptr == NULL)

    {

        printf_s("Error allocation aligned offset memory/n");

        return -1;      

    }

    if((((int)ptr + offset) % alignment) == 0)

    {

        printf_s("This pointer, %d, is offset by %d on alignment of %d/n",

                       ptr, offset, alignment);             

    }

    else

    {

        printf_s("This pointer, %d, does not satisfy offset %d and alignment %d/n",

                       ptr, offset, alignment);    

    }

   

    //使用_aligned_offset_realloc

    ptr = _aligned_offset_realloc(ptr, 200, alignment, offset);

    if(ptr == NULL)

    {

        printf_s("Error reallocation aligned offset memory/n");      

        return -1;

    }

    if((((int)ptr + offset) % alignment) == 0)

    {

        printf_s("This pointer, %d, is offset by %d on alignment of %d/n",

                       ptr, offset, alignment);             

    }

    else

    {

        printf_s("This pointer, %d, does not satisfy offset %d and alignment %d/n",

                       ptr, offset, alignment);    

    }

    

    //_aligned_free同时与_aligned_malloc_aligned_realloc配合使用

    _aligned_free(ptr);

   

    system("pause");

    return 0;   

}

 

原创粉丝点击