-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Am 2013-08-19 14:34, schrieb Henri Verbeet:
On 19 August 2013 14:20, Stefan Dösinger stefandoesinger@gmail.com wrote:
Am 2013-08-19 13:53, schrieb Henri Verbeet:
That's actually a bug in what I posted.
What's your suggested fix? Allocating size + align + alignment, and adding alignment to mem if mem happens to be already aligned?
I think it needs to be "align = max(sizeof(*p), alignment);", but that's actually for the aligned + 1 case.
With some inspiration from _aligned_offset_malloc in msvcrt I came up with this:
void *wined3d_alloc_resource_mem(SIZE_T size) { void **p; SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p); void *mem;
if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + align))) return NULL;
p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1; *p = mem;
return ++p; }
void wined3d_free_resource_mem(void *mem) { void **p = mem;
if (!mem) return;
HeapFree(GetProcessHeap(), 0, *(--p)); }
I'm not entirely sure about the SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p); line though. Msvcrt doesn't have the - 1, and allocating size + RESOURCE_ALIGNMENT bytes might be enough. msvcrt allocates the equivalent of size + RESOURCE_ALIGNMENT + sizeof(void *).