[PATCH 2/5] wined3d: Wrap resource->heapMemory into a structure

Henri Verbeet hverbeet at gmail.com
Mon Aug 19 05:29:24 CDT 2013


On 19 August 2013 10:52, Stefan Dösinger <stefan at codeweavers.com> wrote:
> +struct wined3d_heap_memory *wined3d_alloc_resource_mem(UINT size)
> +{
> +    struct wined3d_heap_memory *ret;
> +
> +    ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
> +    if (!ret)
> +        return NULL;
> +
> +    ret->alloc_ptr = HeapAlloc(GetProcessHeap(), 0, size + RESOURCE_ALIGNMENT);
> +    if (!ret->alloc_ptr)
> +    {
> +        HeapFree(GetProcessHeap(), 0, ret);
> +        return NULL;
> +    }
> +    ret->ptr = (BYTE *)(((ULONG_PTR)ret->alloc_ptr
> +            + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
> +    return ret;
> +}
> +

I don't think this is a good idea. How about something like the
following? The corresponding free function would be trivial.

void *wined3d_memalign(SIZE_T alignment, SIZE_T size)
{
    SIZE_T align;
    void *mem;
    void **p;

    align = max(sizeof(*p), alignment - 1);
    if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + align)))
        return NULL;

    p = (void **)(((ULONG_PTR)mem + align) & ~(alignment - 1)) - 1;
    *p = mem;

    return ++p;
}

(Untested, there's probably some room for improvement.)



More information about the wine-devel mailing list