[PORTCLS]

* Fix some memory leaks.
* Add some comments.
* Fix broken PcNewResourceSublist which ignored the MaximumEntries parameter.
* Add more checks.

svn path=/trunk/; revision=55002
This commit is contained in:
Amine Khaldi 2012-01-17 23:42:13 +00:00
parent 3209e691d6
commit 0102072a69

View file

@ -34,11 +34,9 @@ public:
}
IMP_IResourceList;
#ifdef BUILD_WDK
ULONG NTAPI NumberOfEntries();
#endif
CResourceList(IUnknown * OuterUnknown) : m_OuterUnknown(OuterUnknown), m_PoolType(NonPagedPool), m_TranslatedResourceList(0), m_UntranslatedResourceList(0), m_NumberOfEntries(0) {}
virtual ~CResourceList() {}
CResourceList(IUnknown * OuterUnknown) : m_OuterUnknown(OuterUnknown), m_PoolType(NonPagedPool), m_TranslatedResourceList(0), m_UntranslatedResourceList(0), m_NumberOfEntries(0), m_MaxEntries(0), m_Ref(0) {}
virtual ~CResourceList();
public:
PUNKNOWN m_OuterUnknown;
@ -46,10 +44,24 @@ public:
PCM_RESOURCE_LIST m_TranslatedResourceList;
PCM_RESOURCE_LIST m_UntranslatedResourceList;
ULONG m_NumberOfEntries;
ULONG m_MaxEntries;
LONG m_Ref;
};
CResourceList::~CResourceList()
{
if (m_TranslatedResourceList)
{
/* Free resource list */
FreeItem(m_TranslatedResourceList, TAG_PORTCLASS);
}
if (m_UntranslatedResourceList)
{
/* Free resource list */
FreeItem(m_UntranslatedResourceList, TAG_PORTCLASS);
}
}
NTSTATUS
NTAPI
@ -75,7 +87,7 @@ CResourceList::QueryInterface(
return STATUS_UNSUCCESSFUL;
}
#if 1
ULONG
NTAPI
CResourceList::NumberOfEntries()
@ -84,7 +96,6 @@ CResourceList::NumberOfEntries()
return m_NumberOfEntries;
}
#endif
ULONG
NTAPI
@ -92,39 +103,27 @@ CResourceList::NumberOfEntriesOfType(
IN CM_RESOURCE_TYPE Type)
{
ULONG Index, Count = 0;
PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor, UnPartialDescriptor;
PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
if (!m_TranslatedResourceList)
/* Is there a resource list? */
if (!m_UntranslatedResourceList)
{
// no resource list
/* No resource list provided */
return 0;
}
PC_ASSERT(m_TranslatedResourceList->List[0].PartialResourceList.Count == m_UntranslatedResourceList->List[0].PartialResourceList.Count);
// I guess the translated and untranslated lists will be same length?
for (Index = 0; Index < m_TranslatedResourceList->List[0].PartialResourceList.Count; Index ++ )
for (Index = 0; Index < m_NumberOfEntries; Index ++ )
{
PartialDescriptor = &m_TranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index];
UnPartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index];
DPRINT("Descriptor Type %u\n", PartialDescriptor->Type);
/* Get descriptor */
PartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index];
if (PartialDescriptor->Type == Type)
{
// Yay! Finally found one that matches!
/* Yay! Finally found one that matches! */
Count++;
}
if (PartialDescriptor->Type == CmResourceTypeInterrupt)
{
DPRINT("Index %u TRANS Interrupt Number Affinity %x Level %u Vector %u Flags %x Share %x\n", Index, PartialDescriptor->u.Interrupt.Affinity, PartialDescriptor->u.Interrupt.Level, PartialDescriptor->u.Interrupt.Vector, PartialDescriptor->Flags, PartialDescriptor->ShareDisposition);
DPRINT("Index %u UNTRANS Interrupt Number Affinity %x Level %u Vector %u Flags %x Share %x\\n", Index, UnPartialDescriptor->u.Interrupt.Affinity, UnPartialDescriptor->u.Interrupt.Level, UnPartialDescriptor->u.Interrupt.Vector, UnPartialDescriptor->Flags, UnPartialDescriptor->ShareDisposition);
}
else if (PartialDescriptor->Type == CmResourceTypePort)
{
DPRINT("Index %u TRANS Port Length %u Start %u %u Flags %x Share %x\n", Index, PartialDescriptor->u.Port.Length, PartialDescriptor->u.Port.Start.HighPart, PartialDescriptor->u.Port.Start.LowPart, PartialDescriptor->Flags, PartialDescriptor->ShareDisposition);
DPRINT("Index %u UNTRANS Port Length %u Start %u %u Flags %x Share %x\n", Index, UnPartialDescriptor->u.Port.Length, UnPartialDescriptor->u.Port.Start.HighPart, UnPartialDescriptor->u.Port.Start.LowPart, UnPartialDescriptor->Flags, UnPartialDescriptor->ShareDisposition);
}
}
DPRINT("Found %d type %d\n", Count, Type);
@ -142,27 +141,33 @@ CResourceList::FindTranslatedEntry(
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
/* Is there a resource list? */
if (!m_TranslatedResourceList)
{
// no resource list
/* No resource list */
return NULL;
}
for (DescIndex = 0; DescIndex < m_TranslatedResourceList->List[0].PartialResourceList.Count; DescIndex ++ )
for (DescIndex = 0; DescIndex < m_NumberOfEntries; DescIndex ++ )
{
/* Get descriptor */
PartialDescriptor = &m_TranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[DescIndex];
if (PartialDescriptor->Type == Type)
{
// Yay! Finally found one that matches!
/* Found type, is it the requested index? */
if (Index == Count)
{
/* Found */
return PartialDescriptor;
}
/* Need to continue search */
Count++;
}
}
/* No such descriptor */
return NULL;
}
@ -177,26 +182,34 @@ CResourceList::FindUntranslatedEntry(
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
/* Is there a resource list? */
if (!m_UntranslatedResourceList)
{
// no resource list
/* Empty resource list */
return NULL;
}
for (DescIndex = 0; DescIndex < m_UntranslatedResourceList->List[0].PartialResourceList.Count; DescIndex ++ )
/* Search descriptors */
for (DescIndex = 0; DescIndex < m_NumberOfEntries; DescIndex ++ )
{
/* Get descriptor */
PartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[DescIndex];
if (PartialDescriptor->Type == Type)
{
// Yay! Finally found one that matches!
/* Found type, is it the requested index? */
if (Index == Count)
{
/* Found */
return PartialDescriptor;
}
/* Need to continue search */
Count++;
}
}
/* No such descriptor */
return NULL;
}
@ -206,68 +219,37 @@ CResourceList::AddEntry(
IN PCM_PARTIAL_RESOURCE_DESCRIPTOR Translated,
IN PCM_PARTIAL_RESOURCE_DESCRIPTOR Untranslated)
{
PCM_RESOURCE_LIST NewUntranslatedResources, NewTranslatedResources;
ULONG NewTranslatedSize, NewUntranslatedSize, TranslatedSize, UntranslatedSize, ResourceCount;
PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
/* Sanity check */
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
// calculate translated resource list size
ResourceCount = m_TranslatedResourceList->List[0].PartialResourceList.Count;
#ifdef _MSC_VER
NewTranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount+1]);
TranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]);
#else
NewTranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount+1) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
TranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
#endif
NewTranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(m_PoolType, NewTranslatedSize, TAG_PORTCLASS);
if (!NewTranslatedResources)
return STATUS_INSUFFICIENT_RESOURCES;
// calculate untranslated resouce list size
ResourceCount = m_UntranslatedResourceList->List[0].PartialResourceList.Count;
#ifdef _MSC_VER
NewUntranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount+1]);
UntranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]);
#else
NewUntranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount+1) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
UntranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
#endif
// allocate untranslated resource list size
NewUntranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(m_PoolType, NewUntranslatedSize, TAG_PORTCLASS);
if (!NewUntranslatedResources)
/* Is there still room for another entry */
if (m_NumberOfEntries >= m_MaxEntries)
{
FreeItem(NewTranslatedResources, TAG_PORTCLASS);
/* No more space */
return STATUS_INSUFFICIENT_RESOURCES;
}
// now copy translated resource list
RtlMoveMemory(NewTranslatedResources, m_TranslatedResourceList, TranslatedSize);
RtlMoveMemory((PUCHAR)NewTranslatedResources + TranslatedSize, Translated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
/* Get free descriptor */
PartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[m_NumberOfEntries];
// now copy untranslated resource list
RtlMoveMemory(NewUntranslatedResources, m_UntranslatedResourceList, UntranslatedSize);
RtlMoveMemory((PUCHAR)NewUntranslatedResources + UntranslatedSize, Untranslated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
/* Copy descriptor */
RtlCopyMemory(PartialDescriptor, Untranslated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
// free old lists
FreeItem(m_TranslatedResourceList, TAG_PORTCLASS);
FreeItem(m_UntranslatedResourceList, TAG_PORTCLASS);
/* Get free descriptor */
PartialDescriptor = &m_TranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[m_NumberOfEntries];
// store new lists
m_UntranslatedResourceList = NewUntranslatedResources;
m_TranslatedResourceList = NewTranslatedResources;
/* Copy descriptor */
RtlCopyMemory(PartialDescriptor, Translated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
// increment descriptor count
NewUntranslatedResources->List[0].PartialResourceList.Count++;
NewTranslatedResources->List[0].PartialResourceList.Count++;
// add entry count
/* Add entry count */
m_NumberOfEntries++;
m_UntranslatedResourceList->List[0].PartialResourceList.Count++;
m_TranslatedResourceList->List[0].PartialResourceList.Count++;
/* Done */
return STATUS_SUCCESS;
}
@ -282,16 +264,18 @@ CResourceList::AddEntryFromParent(
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
/* Get entries from parent */
Translated = Parent->FindTranslatedEntry(Type, Index);
Untranslated = Parent->FindUntranslatedEntry(Type, Index);
/* Are both found? */
if (Translated && Untranslated)
{
// add entry from parent
/* Add entry from parent */
return AddEntry(Translated, Untranslated);
}
// entry not found
/* Entry not found */
return STATUS_INVALID_PARAMETER;
}
@ -325,99 +309,92 @@ PcNewResourceList(
IN PCM_RESOURCE_LIST UntranslatedResourceList)
{
PCM_RESOURCE_LIST NewUntranslatedResources, NewTranslatedResources;
ULONG NewTranslatedSize, NewUntranslatedSize, ResourceCount;
ULONG ResourceSize, ResourceCount;
CResourceList* NewList;
NTSTATUS Status;
if (!TranslatedResourceList)
{
//
// if the untranslated resource list is also not provided, it becomes an empty resource list
//
/* If the untranslated resource list is also not provided, it becomes an empty resource list */
if (UntranslatedResourceList)
{
// invalid parameter mix
/* Invalid parameter mix */
return STATUS_INVALID_PARAMETER;
}
}
else
{
//
// if the translated resource list is also not provided, it becomes an empty resource list
//
/* If the translated resource list is also not provided, it becomes an empty resource list */
if (!UntranslatedResourceList)
{
// invalid parameter mix
/* Invalid parameter mix */
return STATUS_INVALID_PARAMETER;
}
}
/* Allocate resource list */
NewList = new(PoolType, TAG_PORTCLASS)CResourceList(OuterUnknown);
if (!NewList)
return STATUS_INSUFFICIENT_RESOURCES;
/* Query resource list */
Status = NewList->QueryInterface(IID_IResourceList, (PVOID*)OutResourceList);
if (!NT_SUCCESS(Status))
{
//
// Ouch, FIX ME
//
/* Ouch, FIX ME */
delete NewList;
return STATUS_INVALID_PARAMETER;
}
/* Is there a resource list */
if (!TranslatedResourceList)
{
//
// empty resource list
//
/* Empty resource list */
return STATUS_SUCCESS;
}
// calculate translated resource list size
ResourceCount = TranslatedResourceList->List[0].PartialResourceList.Count;
#ifdef _MSC_VER
NewTranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]);
#else
NewTranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
#endif
/* Sanity check */
ASSERT(UntranslatedResourceList->List[0].PartialResourceList.Count == TranslatedResourceList->List[0].PartialResourceList.Count);
// store resource count
NewList->m_NumberOfEntries = ResourceCount;
// calculate untranslated resouce list size
/* Get resource count */
ResourceCount = UntranslatedResourceList->List[0].PartialResourceList.Count;
#ifdef _MSC_VER
NewUntranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]);
ResourceSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]);
#else
NewUntranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
ResourceSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
#endif
// allocate translated resource list
NewTranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, NewTranslatedSize, TAG_PORTCLASS);
/* Allocate translated resource list */
NewTranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, ResourceSize, TAG_PORTCLASS);
if (!NewTranslatedResources)
{
/* No memory */
delete NewList;
return STATUS_INSUFFICIENT_RESOURCES;
}
// allocate untranslated resource list
NewUntranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, NewUntranslatedSize, TAG_PORTCLASS);
/* Allocate untranslated resource list */
NewUntranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, ResourceSize, TAG_PORTCLASS);
if (!NewUntranslatedResources)
{
/* No memory */
delete NewList;
FreeItem(NewTranslatedResources, TAG_PORTCLASS);
return STATUS_INSUFFICIENT_RESOURCES;
}
// copy resource lists
RtlCopyMemory(NewTranslatedResources, TranslatedResourceList, NewTranslatedSize);
RtlCopyMemory(NewUntranslatedResources, UntranslatedResourceList, NewUntranslatedSize);
/* Copy resource lists */
RtlCopyMemory(NewTranslatedResources, TranslatedResourceList, ResourceSize);
RtlCopyMemory(NewUntranslatedResources, UntranslatedResourceList, ResourceSize);
// store resource lists
/* Init resource list */
NewList->m_TranslatedResourceList= NewTranslatedResources;
NewList->m_UntranslatedResourceList = NewUntranslatedResources;
NewList->m_NumberOfEntries = ResourceCount;
NewList->m_MaxEntries = ResourceCount;
NewList->m_PoolType = PoolType;
/* Done */
return STATUS_SUCCESS;
}
@ -431,53 +408,60 @@ PcNewResourceSublist(
IN PRESOURCELIST ParentList,
IN ULONG MaximumEntries)
{
CResourceList* NewList, *Parent;
CResourceList* NewList;
ULONG ResourceSize;
if (!OutResourceList || !ParentList || !MaximumEntries)
return STATUS_INVALID_PARAMETER;
Parent = (CResourceList*)ParentList;
if (!Parent->m_TranslatedResourceList->List[0].PartialResourceList.Count ||
!Parent->m_UntranslatedResourceList->List[0].PartialResourceList.Count)
{
// parent list can't be empty
return STATUS_INVALID_PARAMETER;
}
/* Allocate new list */
NewList = new(PoolType, TAG_PORTCLASS) CResourceList(OuterUnknown);
if (!NewList)
return STATUS_INSUFFICIENT_RESOURCES;
/* Get resource size */
#ifdef _MSC_VER
ResourceSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[MaximumEntries]);
#else
ResourceSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (MaximumEntries) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
#endif
/* Allocate resource list */
NewList->m_TranslatedResourceList = (PCM_RESOURCE_LIST)AllocateItem(PoolType, sizeof(CM_RESOURCE_LIST), TAG_PORTCLASS);
if (!NewList->m_TranslatedResourceList)
{
/* No memory */
delete NewList;
return STATUS_INSUFFICIENT_RESOURCES;
}
/* Allocate resource list */
NewList->m_UntranslatedResourceList = (PCM_RESOURCE_LIST)AllocateItem(PoolType, sizeof(CM_RESOURCE_LIST), TAG_PORTCLASS);
if (!NewList->m_UntranslatedResourceList)
{
/* No memory */
delete NewList;
return STATUS_INSUFFICIENT_RESOURCES;
}
RtlCopyMemory(NewList->m_TranslatedResourceList, Parent->m_TranslatedResourceList, sizeof(CM_RESOURCE_LIST));
RtlCopyMemory(NewList->m_UntranslatedResourceList, Parent->m_UntranslatedResourceList, sizeof(CM_RESOURCE_LIST));
/* Copy resource lists */
RtlCopyMemory(NewList->m_TranslatedResourceList, ParentList->TranslatedList(), sizeof(CM_RESOURCE_LIST));
RtlCopyMemory(NewList->m_UntranslatedResourceList, ParentList->UntranslatedList(), sizeof(CM_RESOURCE_LIST));
// mark list as empty
NewList->m_TranslatedResourceList->List[0].PartialResourceList.Count = 0;
/* Resource list is empty */
NewList->m_UntranslatedResourceList->List[0].PartialResourceList.Count = 0;
// store members
NewList->m_TranslatedResourceList->List[0].PartialResourceList.Count = 0;
/* Store members */
NewList->m_OuterUnknown = OuterUnknown;
NewList->m_PoolType = PoolType;
NewList->m_Ref = 1;
NewList->m_NumberOfEntries = 0;
NewList->m_MaxEntries = MaximumEntries;
/* Store result */
*OutResourceList = (IResourceList*)NewList;
DPRINT("PcNewResourceSublist OutResourceList %p OuterUnknown %p ParentList %p\n", *OutResourceList, OuterUnknown, ParentList);
/* Done */
return STATUS_SUCCESS;
}