void* _aligned_offset_recalloc(void* memblock, size_t num, size_t size, size_t alignment, size_t offset) { size_t copySize; void* newMemblock; WINPR_ALIGNED_MEM* pMem; WINPR_ALIGNED_MEM* pNewMem; if (!memblock) return _aligned_offset_malloc(size, alignment, offset); if (size == 0) { _aligned_free(memblock); return NULL; } newMemblock = _aligned_offset_malloc(size, alignment, offset); if (!newMemblock) return NULL; pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock); pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock); if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE) { fprintf(stderr, "_aligned_offset_recalloc: memory block was not allocated by _aligned_malloc!\n"); return NULL; } ZeroMemory(newMemblock, pNewMem->size); _aligned_free(memblock); return newMemblock; }
void* _aligned_offset_realloc(void* memblock, size_t size, size_t alignment, size_t offset) { size_t copySize; void* newMemblock; WINPR_ALIGNED_MEM* pMem; WINPR_ALIGNED_MEM* pNewMem; if (!memblock) 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) { _aligned_free(memblock); return NULL; } newMemblock = _aligned_offset_malloc(size, alignment, offset); if (!newMemblock) return NULL; pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock); copySize = (pNewMem->size < pMem->size) ? pNewMem->size : pMem->size; CopyMemory(newMemblock, memblock, copySize); _aligned_free(memblock); return newMemblock; }
void* _aligned_offset_malloc(size_t size, size_t alignment, size_t offset) { void* base; void* memblock; WINPR_ALIGNED_MEM* pMem; /* alignment must be a power of 2 */ if (alignment % 2 == 1) return NULL; /* offset must be less than size */ if (offset >= size) return NULL; /* minimum alignment is pointer size */ if (alignment < sizeof(void*)) alignment = sizeof(void*); /* malloc size + alignment to make sure we can align afterwards */ base = malloc(size + alignment + sizeof(WINPR_ALIGNED_MEM)); if (!base) return NULL; memblock = (void*)((((size_t)(((BYTE*) base) + alignment + offset + sizeof(WINPR_ALIGNED_MEM)) & ~(alignment - 1)) - offset)); pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock); pMem->sig = WINPR_ALIGNED_MEM_SIGNATURE; pMem->base_addr = base; pMem->size = size; return memblock; }
void* _aligned_offset_recalloc(void* memblock, size_t num, size_t size, size_t alignment, size_t offset) { void* newMemblock; WINPR_ALIGNED_MEM* pMem; WINPR_ALIGNED_MEM* pNewMem; if (!memblock) { 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) { _aligned_free(memblock); return NULL; } newMemblock = _aligned_offset_malloc(size * num, alignment, offset); if (!newMemblock) return NULL; pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock); ZeroMemory(newMemblock, pNewMem->size); _aligned_free(memblock); return newMemblock; }
void* _aligned_offset_malloc(size_t size, size_t alignment, size_t offset) { size_t header, alignsize; uintptr_t basesize; void* base; void* memblock; WINPR_ALIGNED_MEM* pMem; /* alignment must be a power of 2 */ if (alignment % 2 == 1) return NULL; /* offset must be less than size */ if (offset >= size) return NULL; /* minimum alignment is pointer size */ if (alignment < sizeof(void*)) alignment = sizeof(void*); if (alignment > SIZE_MAX - sizeof(WINPR_ALIGNED_MEM)) return NULL; header = sizeof(WINPR_ALIGNED_MEM) + alignment; if (size > SIZE_MAX - header) return NULL; alignsize = size + header; /* malloc size + alignment to make sure we can align afterwards */ base = malloc(alignsize); if (!base) return NULL; basesize = (uintptr_t)base; if ((offset > UINTPTR_MAX) || (header > UINTPTR_MAX - offset) || (basesize > UINTPTR_MAX - header - offset)) { free(base); return NULL; } memblock = (void*)(((basesize + header + offset) & ~(alignment - 1)) - offset); pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock); pMem->sig = WINPR_ALIGNED_MEM_SIGNATURE; pMem->base_addr = base; pMem->size = size; return memblock; }
void _aligned_free(void* memblock) { WINPR_ALIGNED_MEM* pMem; if (!memblock) return; pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock); if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE) { fprintf(stderr, "_aligned_free: memory block was not allocated by _aligned_malloc!\n"); return; } free(pMem->base_addr); }
size_t _aligned_msize(void* memblock, size_t alignment, size_t offset) { WINPR_ALIGNED_MEM* pMem; if (!memblock) return 0; pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock); if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE) { fprintf(stderr, "_aligned_msize: memory block was not allocated by _aligned_malloc!\n"); return 0; } return pMem->size; }