?? align.c
字號:
/***
*align.c - Aligned allocation, reallocation or freeing of memory in the heap
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* Defines the _aligned_malloc(),
* _aligned_realloc(),
* _aligned_recalloc(),
* _aligned_offset_malloc(),
* _aligned_offset_realloc(),
* _aligned_offset_recalloc(),
* _aligned_free(),
* _aligned_msize() functions.
*
*******************************************************************************/
#include <dbgint.h>
#include <crtdbg.h>
#include <errno.h>
#include <string.h>
#include <malloc.h>
#include <stddef.h>
#include <stdlib.h>
#include <internal.h>
#define IS_2_POW_N(X) (((X)&(X-1)) == 0)
#define PTR_SZ sizeof(void *)
/***
*
* |1|___6___|2|3|4|_________5__________|_6_|
*
* 1 -> Pointer to start of the block allocated by malloc.
* 2 -> Value of 1.
* 3 -> Gap used to get 1 aligned on sizeof(void *).
* 4 -> Pointer to the start of data block.
* 4+5 -> Data block.
* 6 -> Wasted memory at rear of data block.
* 6 -> Wasted memory.
*
*******************************************************************************/
/***
* void *_aligned_malloc_base(size_t size, size_t alignment)
* - Get a block of aligned memory from the heap.
*
* Purpose:
* Allocate of block of aligned memory aligned on the alignment of at least
* size bytes from the heap and return a pointer to it.
*
* Entry:
* size_t size - size of block requested
* size_t alignment - alignment of memory (needs to be a power of 2)
*
* Exit:
* Success: Pointer to memory block
* Failure: Null, errno is set
*
* Exceptions:
* Input parameters are validated. Refer to the validation section of the function.
*
*******************************************************************************/
void * __cdecl _aligned_malloc_base(
size_t size,
size_t alignment
)
{
return _aligned_offset_malloc_base(size, alignment, 0);
}
/***
* void *_aligned_offset_malloc_base(size_t size, size_t alignment, int offset)
* - Get a block of memory from the heap.
*
* Purpose:
* Allocate a block of memory which is shifted by offset from alignment of
* at least size bytes from the heap and return a pointer to it.
*
* Entry:
* size_t size - size of block of memory
* size_t alignment - alignment of memory (needs to be a power of 2)
* size_t offset - offset of memory from the alignment
*
* Exit:
* Success: Pointer to memory block
* Failure: Null, errno is set
*
* Exceptions:
* Input parameters are validated. Refer to the validation section of the function.
*
*******************************************************************************/
void * __cdecl _aligned_offset_malloc_base(
size_t size,
size_t align,
size_t offset
)
{
uintptr_t ptr, retptr, gap;
size_t nonuser_size,block_size;
/* validation section */
_VALIDATE_RETURN(IS_2_POW_N(align), EINVAL, NULL);
_VALIDATE_RETURN(offset == 0 || offset < size, EINVAL, NULL);
align = (align > PTR_SZ ? align : PTR_SZ) -1;
/* gap = number of bytes needed to round up offset to align with PTR_SZ*/
gap = (0 - offset)&(PTR_SZ -1);
nonuser_size = PTR_SZ +gap +align;
block_size = nonuser_size + size;
_VALIDATE_RETURN_NOEXC(size <= block_size, ENOMEM, NULL)
if ( (ptr =(uintptr_t)malloc(block_size)) == (uintptr_t)NULL)
return NULL;
retptr =((ptr +nonuser_size+offset)&~align)- offset;
((uintptr_t *)(retptr - gap))[-1] = ptr;
return (void *)retptr;
}
/***
*
* void *_aligned_realloc_base(void * memblock, size_t size, size_t alignment)
* - Reallocate a block of aligned memory from the heap.
*
* Purpose:
* Reallocates of block of aligned memory aligned on the alignment of at
* least size bytes from the heap and return a pointer to it. Size can be
* either greater or less than the original size of the block.
* The reallocation may result in moving the block as well as changing the
* size.
*
* Entry:
* void *memblock - pointer to block in the heap previously allocated by
* call to _aligned_malloc(), _aligned_offset_malloc(),
* _aligned_realloc() or _aligned_offset_realloc().
* size_t size - size of block requested
* size_t alignment - alignment of memory
*
* Exit:
* Success: Pointer to re-allocated memory block
* Failure: Null, errno is set
*
* Exceptions:
* Input parameters are validated. Refer to the validation section of the function.
*
*******************************************************************************/
void * __cdecl _aligned_realloc_base(
void *memblock,
size_t size,
size_t alignment
)
{
return _aligned_offset_realloc_base(memblock, size, alignment, 0);
}
/***
*
* void *_aligned_recalloc_base(void * memblock, size_t count, size_t size, size_t alignment)
* - Reallocate a block of aligned memory from the heap.
*
* Purpose:
* Reallocates of block of aligned memory aligned on the alignment of at
* least size bytes from the heap and return a pointer to it. Size can be
* either greater or less than the original size of the block.
* The reallocation may result in moving the block as well as changing the
* size.
*
* Entry:
* void *memblock - pointer to block in the heap previously allocated by
* call to _aligned_malloc(), _aligned_offset_malloc(),
* _aligned_realloc() or _aligned_offset_realloc().
* size_t count - count of items
* size_t size - size of item
* size_t alignment - alignment of memory
*
* Exit:
* Success: Pointer to re-allocated memory block
* Failure: Null, errno is set
*
* Exceptions:
* Input parameters are validated. Refer to the validation section of the function.
*
*******************************************************************************/
void * __cdecl _aligned_recalloc_base(
void *memblock,
size_t count,
size_t size,
size_t alignment
)
{
return _aligned_offset_recalloc_base(memblock, count, size, alignment, 0);
}
/***
*
* void *_aligned_offset_realloc_base (void * memblock, size_t size,
* size_t alignment, int offset)
* - Reallocate a block of memory from the heap.
*
* Purpose:
* Reallocates a block of memory which is shifted by offset from
* alignment of at least size bytes from the heap and return a pointer
* to it. Size can be either greater or less than the original size of the
* block.
*
* Entry:
* void *memblock - pointer to block in the heap previously allocated by
* call to _aligned_malloc(), _aligned_offset_malloc(),
* _aligned_realloc() or _aligned_offset_realloc().
* size_t size - size of block of memory
* size_t alignment - alignment of memory
* size_t offset - offset of memory from the alignment
*
* Exit:
* Success: Pointer to re-allocated memory block
* Failure: Null, errno is set
*
* Exceptions:
* Input parameters are validated. Refer to the validation section of the function.
*
*******************************************************************************/
void * __cdecl _aligned_offset_realloc_base(
void *memblock,
size_t size,
size_t align,
size_t offset
)
{
uintptr_t ptr, retptr, gap, stptr, diff;
uintptr_t movsz, reqsz;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -