[Careercup] 13.9 Aligned Malloc and free function write a pair of application and release memory functions

Source: Internet
Author: User

13.9 Write a aligned malloc and free function This supports allocating memory such that the memory address returned is DI Visible by a specific power of.
EXAMPLE
Align_malloc (1000,128) would return a memory address that's a multiple of a and that points to memory of size S.
Aligned_free () would free memory allocated by Align_malloc

This problem lets us write a pair of application and release memory functions, and requires us to apply for the memory block of the starting address to be divisible by multiples of 2. For example, let's apply 1000 bytes of memory space, and the starting address should be divisible by 128. We know that when we use malloc to apply for memory, we can't control which memory in the heap is being applied, and if we want the first address to be divisible by 128, then we need a little bit of skill: we need to apply for 127 byte size so that our first address can not be evenly divisible by 128. We can also adjust to the divisible position, and the memory space behind is 1000 bytes, you can use the following code to apply:

 void  * Aligned_malloc (size_t required_ Bytes, size_t alignment) { int  offset = alignment-1  ;  void  *p = (void  *) malloc  (required_bytes + offset);  void  *q = (void  *) (((size_t) (p1) +    Offset) & ~ (Alignment-1   return   Q;}  

We apply for additional space alignment-1, and then adjust the position of the first address through and ~ (alignment-1) and, to get the address divisible by alignment, below we will see how to release memory, because we have applied for offset memory more, We finally need to release the memory, we need a pointer to record the entire memory block start position, because the new pointer, do not forget to also give the pointer to request space, so we need to apply the space size of alignment-1 + sizeof (void*), see the following code:

classSolution { Public:    void*Aligned_malloc (size_t required_bytes, size_t alignment) {void*P1; void**P2; intoffset = alignment-1+sizeof(void*); if((P1 = (void*)malloc(required_bytes + offset)) ==NULL) {            returnNULL; } P2= (void* *) (((size_t) (p1) + offset) & ~ (Alignment-1)); p2[-1] =P1; returnP2; }    voidAligned_free (void*p2) {        void*P1 = ((void* *) p2) [-1];  Free(p1); }};

We define a double pointer p2, in the position of the-1 to save the P1, that is, the entire application of memory block of the starting address, in 0 of the location can be alignment divisible by the location, the release of memory from P2 extract P1, p1 released.

[Careercup] 13.9 Aligned Malloc and free function write a pair of application and release memory functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.