dpnet: Convert Address components to use an array.

This commit is contained in:
Alistair Leslie-Hughes 2015-03-11 16:38:59 +11:00 committed by Alexandre Julliard
parent 0516d969eb
commit f2aa42de09
2 changed files with 75 additions and 16 deletions

View File

@ -44,6 +44,11 @@ static inline void *heap_alloc(size_t len)
return HeapAlloc(GetProcessHeap(), 0, len); return HeapAlloc(GetProcessHeap(), 0, len);
} }
static inline void *heap_realloc(void *mem, size_t len)
{
return HeapReAlloc( GetProcessHeap(), 0, mem, len);
}
static inline BOOL heap_free(void *mem) static inline BOOL heap_free(void *mem)
{ {
return HeapFree(GetProcessHeap(), 0, mem); return HeapFree(GetProcessHeap(), 0, mem);
@ -74,6 +79,28 @@ static char *heap_strdupA( const char *str )
return ret; return ret;
} }
static BOOL add_component(IDirectPlay8AddressImpl *This, struct component *item)
{
if(This->comp_count == This->comp_array_size)
{
struct component **temp;
temp = heap_realloc(This->components, sizeof(*temp) * This->comp_array_size * 2 );
if(!temp)
{
return FALSE;
}
This->comp_array_size *= 2;
This->components = temp;
}
This->components[This->comp_count] = item;
This->comp_count++;
return TRUE;
}
static inline IDirectPlay8AddressImpl *impl_from_IDirectPlay8Address(IDirectPlay8Address *iface) static inline IDirectPlay8AddressImpl *impl_from_IDirectPlay8Address(IDirectPlay8Address *iface)
{ {
return CONTAINING_RECORD(iface, IDirectPlay8AddressImpl, IDirectPlay8Address_iface); return CONTAINING_RECORD(iface, IDirectPlay8AddressImpl, IDirectPlay8Address_iface);
@ -112,10 +139,13 @@ static ULONG WINAPI IDirectPlay8AddressImpl_Release(IDirectPlay8Address *iface)
if (!ref) if (!ref)
{ {
struct component *entry, *entry2; struct component *entry;
DWORD i;
LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->components, struct component, entry) for(i=0; i < This->comp_count; i++)
{ {
entry = This->components[i];
switch(entry->type) switch(entry->type)
{ {
case DPNA_DATATYPE_STRING: case DPNA_DATATYPE_STRING:
@ -129,10 +159,11 @@ static ULONG WINAPI IDirectPlay8AddressImpl_Release(IDirectPlay8Address *iface)
break; break;
} }
HeapFree(GetProcessHeap(), 0, entry); heap_free(entry);
} }
HeapFree(GetProcessHeap(), 0, This); heap_free(This->components);
heap_free(This);
} }
return ref; return ref;
} }
@ -192,13 +223,15 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_Duplicate(IDirectPlay8Address *ifa
if(hr == S_OK) if(hr == S_OK)
{ {
IDirectPlay8AddressImpl *DupThis = impl_from_IDirectPlay8Address(dup); IDirectPlay8AddressImpl *DupThis = impl_from_IDirectPlay8Address(dup);
struct component *entry; DWORD i;
DupThis->SP_guid = This->SP_guid; DupThis->SP_guid = This->SP_guid;
DupThis->init = This->init; DupThis->init = This->init;
LIST_FOR_EACH_ENTRY(entry, &This->components, struct component, entry) for(i=0; i < This->comp_count; i++)
{ {
struct component *entry = This->components[i];
hr = IDirectPlay8Address_AddComponent(dup, entry->name, &entry->data, entry->size, entry->type); hr = IDirectPlay8Address_AddComponent(dup, entry->name, &entry->data, entry->size, entry->type);
if(hr != S_OK) if(hr != S_OK)
ERR("Failed to copy component: %s - 0x%08x\n", debugstr_w(entry->name), hr); ERR("Failed to copy component: %s - 0x%08x\n", debugstr_w(entry->name), hr);
@ -308,7 +341,7 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_GetNumComponents(IDirectPlay8Addre
if(!pdwNumComponents) if(!pdwNumComponents)
return DPNERR_INVALIDPOINTER; return DPNERR_INVALIDPOINTER;
*pdwNumComponents = list_count(&This->components); *pdwNumComponents = This->comp_count;
return DPN_OK; return DPN_OK;
} }
@ -318,14 +351,17 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_GetComponentByName(IDirectPlay8Add
{ {
IDirectPlay8AddressImpl *This = impl_from_IDirectPlay8Address(iface); IDirectPlay8AddressImpl *This = impl_from_IDirectPlay8Address(iface);
struct component *entry; struct component *entry;
DWORD i;
TRACE("(%p)->(%p %p %p %p)\n", This, pwszName, pvBuffer, pdwBufferSize, pdwDataType); TRACE("(%p)->(%p %p %p %p)\n", This, pwszName, pvBuffer, pdwBufferSize, pdwDataType);
if(!pwszName || !pdwBufferSize || !pdwDataType || (!pvBuffer && pdwBufferSize)) if(!pwszName || !pdwBufferSize || !pdwDataType || (!pvBuffer && pdwBufferSize))
return E_POINTER; return E_POINTER;
LIST_FOR_EACH_ENTRY(entry, &This->components, struct component, entry) for(i=0; i < This->comp_count; i++)
{ {
entry = This->components[i];
if (lstrcmpW(pwszName, entry->name) == 0) if (lstrcmpW(pwszName, entry->name) == 0)
{ {
TRACE("Found %s\n", debugstr_w(pwszName)); TRACE("Found %s\n", debugstr_w(pwszName));
@ -381,6 +417,7 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_AddComponent(IDirectPlay8Address *
IDirectPlay8AddressImpl *This = impl_from_IDirectPlay8Address(iface); IDirectPlay8AddressImpl *This = impl_from_IDirectPlay8Address(iface);
struct component *entry; struct component *entry;
BOOL found = FALSE; BOOL found = FALSE;
DWORD i;
TRACE("(%p, %s, %p, %u, %x)\n", This, debugstr_w(pwszName), lpvData, dwDataSize, dwDataType); TRACE("(%p, %s, %p, %u, %x)\n", This, debugstr_w(pwszName), lpvData, dwDataSize, dwDataType);
@ -419,15 +456,17 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_AddComponent(IDirectPlay8Address *
break; break;
} }
LIST_FOR_EACH_ENTRY(entry, &This->components, struct component, entry) for(i=0; i < This->comp_count; i++)
{ {
entry = This->components[i];
if (lstrcmpW(pwszName, entry->name) == 0) if (lstrcmpW(pwszName, entry->name) == 0)
{ {
TRACE("Found %s\n", debugstr_w(pwszName)); TRACE("Found %s\n", debugstr_w(pwszName));
found = TRUE; found = TRUE;
if(entry->type == DPNA_DATATYPE_STRING_ANSI) if(entry->type == DPNA_DATATYPE_STRING_ANSI)
heap_free(entry->data.ansi); heap_free(entry->data.ansi);
else if(entry->type == DPNA_DATATYPE_STRING) else if(entry->type == DPNA_DATATYPE_STRING)
heap_free(entry->data.string); heap_free(entry->data.string);
else if(entry->type == DPNA_DATATYPE_BINARY) else if(entry->type == DPNA_DATATYPE_BINARY)
@ -441,9 +480,22 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_AddComponent(IDirectPlay8Address *
{ {
/* Create a new one */ /* Create a new one */
entry = heap_alloc(sizeof(struct component)); entry = heap_alloc(sizeof(struct component));
entry->name = heap_strdupW(pwszName); if(!entry)
return E_OUTOFMEMORY;
list_add_tail(&This->components, &entry->entry); entry->name = heap_strdupW(pwszName);
if(!entry->name)
{
heap_free(entry);
return E_OUTOFMEMORY;
}
if(!add_component(This, entry))
{
heap_free(entry->name);
heap_free(entry);
return E_OUTOFMEMORY;
}
} }
switch (dwDataType) switch (dwDataType)
@ -532,7 +584,7 @@ HRESULT DPNET_CreateDirectPlay8Address(IClassFactory *iface, IUnknown *pUnkOuter
TRACE("(%p, %s, %p)\n", pUnkOuter, debugstr_guid(riid), ppobj); TRACE("(%p, %s, %p)\n", pUnkOuter, debugstr_guid(riid), ppobj);
*ppobj = NULL; *ppobj = NULL;
client = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectPlay8AddressImpl)); client = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectPlay8AddressImpl));
if (!client) if (!client)
@ -540,8 +592,13 @@ HRESULT DPNET_CreateDirectPlay8Address(IClassFactory *iface, IUnknown *pUnkOuter
client->IDirectPlay8Address_iface.lpVtbl = &DirectPlay8Address_Vtbl; client->IDirectPlay8Address_iface.lpVtbl = &DirectPlay8Address_Vtbl;
client->ref = 1; client->ref = 1;
client->comp_array_size = 4;
list_init(&client->components); client->components = heap_alloc( sizeof(*client->components) * client->comp_array_size );
if(!client->components)
{
heap_free(client);
return E_OUTOFMEMORY;
}
ret = IDirectPlay8AddressImpl_QueryInterface(&client->IDirectPlay8Address_iface, riid, ppobj); ret = IDirectPlay8AddressImpl_QueryInterface(&client->IDirectPlay8Address_iface, riid, ppobj);
IDirectPlay8AddressImpl_Release(&client->IDirectPlay8Address_iface); IDirectPlay8AddressImpl_Release(&client->IDirectPlay8Address_iface);

View File

@ -91,7 +91,9 @@ struct IDirectPlay8AddressImpl
GUID SP_guid; GUID SP_guid;
BOOL init; BOOL init;
struct list components; struct component **components;
DWORD comp_count;
DWORD comp_array_size;
}; };
/***************************************************************************** /*****************************************************************************