aligned memory: fixes in _aligned_offset_recalloc

* _aligned_offset_recalloc did ignore the nmemb element
  therefore only *one* element was allocated
* in case memblock was NULL the allocated memory
  wasn't zeroed
* restructure realloc and recalloc to check if the memory was initially
  created aligned before allocating a new segment
This commit is contained in:
Bernhard Miklautz 2015-03-30 14:21:11 +02:00
parent 879ed36a3c
commit b8e41b843d
1 changed files with 24 additions and 17 deletions

View File

@ -110,6 +110,13 @@ void* _aligned_offset_realloc(void* memblock, size_t size, size_t alignment, siz
if (!memblock) if (!memblock)
return _aligned_offset_malloc(size, alignment, offset); return _aligned_offset_malloc(size, alignment, offset);
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
{
WLog_ERR(TAG, "_aligned_offset_realloc: memory block was not allocated by _aligned_malloc!");
return NULL;
}
if (size == 0) if (size == 0)
{ {
_aligned_free(memblock); _aligned_free(memblock);
@ -117,19 +124,11 @@ void* _aligned_offset_realloc(void* memblock, size_t size, size_t alignment, siz
} }
newMemblock = _aligned_offset_malloc(size, alignment, offset); newMemblock = _aligned_offset_malloc(size, alignment, offset);
if (!newMemblock) if (!newMemblock)
return NULL; return NULL;
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock); pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
{
WLog_ERR(TAG, "_aligned_offset_realloc: memory block was not allocated by _aligned_malloc!");
return NULL;
}
copySize = (pNewMem->size < pMem->size) ? pNewMem->size : pMem->size; copySize = (pNewMem->size < pMem->size) ? pNewMem->size : pMem->size;
CopyMemory(newMemblock, memblock, copySize); CopyMemory(newMemblock, memblock, copySize);
_aligned_free(memblock); _aligned_free(memblock);
@ -143,7 +142,22 @@ void* _aligned_offset_recalloc(void* memblock, size_t num, size_t size, size_t a
WINPR_ALIGNED_MEM* pNewMem; WINPR_ALIGNED_MEM* pNewMem;
if (!memblock) if (!memblock)
return _aligned_offset_malloc(size, alignment, offset); {
newMemblock = _aligned_offset_malloc(size * num, alignment, offset);
if (newMemblock)
{
pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);
ZeroMemory(newMemblock, pNewMem->size);
}
return memblock;
}
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
{
WLog_ERR(TAG, "_aligned_offset_recalloc: memory block was not allocated by _aligned_malloc!");
return NULL;
}
if (size == 0) if (size == 0)
{ {
@ -151,20 +165,13 @@ void* _aligned_offset_recalloc(void* memblock, size_t num, size_t size, size_t a
return NULL; return NULL;
} }
newMemblock = _aligned_offset_malloc(size, alignment, offset); newMemblock = _aligned_offset_malloc(size * num, alignment, offset);
if (!newMemblock) if (!newMemblock)
return NULL; return NULL;
pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);
pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock); pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);
if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)
{
WLog_ERR(TAG, "_aligned_offset_recalloc: memory block was not allocated by _aligned_malloc!");
return NULL;
}
ZeroMemory(newMemblock, pNewMem->size); ZeroMemory(newMemblock, pNewMem->size);
_aligned_free(memblock); _aligned_free(memblock);
return newMemblock; return newMemblock;