d3dx8 [6/7] Implement D3DXMatrixStack_Push

H. Verbeet hverbeet at gmail.com
Wed Apr 9 01:39:40 CDT 2008

On 08/04/2008, David Adam <david.adam.cnrs at gmail.com> wrote:
> +    This->current = This->current +1;
> +    HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->matrix, (This->current +1) * sizeof(D3DXMATRIX) );
> +    if ( This->matrix == NULL ) return E_OUTOFMEMORY;

Aside from being a bit suboptimal (doing a realloc on every push),
this probably doesn't do what you want. Consider what happens to the
size of the array when you do something like push/pop/push/pop/...etc.

It would be better to keep track of the current stack size, and then
grow it by a factor (eg. 2 or 1.5) if a push would overflow the
current stack. You could also consider shrinking the array again if a
pop would make it use less than a certain percentage of the current
size (eg. a third).

You should also assign the result of HeapReAlloc() to This->matrix
again. Although it's quite possible for HeapReAlloc to just grow the
current block without changing its location, there's no guarantee it
will. The NULL check is useless this way.

More information about the wine-devel mailing list