/*++\r
\r
-Copyright (c) 2006, Intel Corporation \r
-All rights reserved. This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution. The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php \r
- \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
\r
Module Name:\r
\r
EhciMem.c\r
- \r
-Abstract: \r
- \r
+\r
+Abstract:\r
+\r
\r
Revision History\r
--*/\r
HcDev - USB2_HC_DEV\r
MemoryHeader - MEMORY_MANAGE_HEADER to output\r
MemoryBlockSizeInPages - MemoryBlockSizeInPages\r
- \r
+\r
Returns:\r
\r
EFI_SUCCESS Success\r
EFI_OUT_OF_RESOURCES Fail for no resources\r
EFI_UNSUPPORTED Unsupported currently\r
- \r
+\r
--*/\r
{\r
EFI_STATUS Status;\r
//\r
// each bit in Bit Array will manage 32 bytes memory in memory block\r
//\r
- (*MemoryHeader)->BitArraySizeInBytes = ((*MemoryHeader)->MemoryBlockSizeInBytes / 32) / 8;\r
+ (*MemoryHeader)->BitArraySizeInBytes = ((*MemoryHeader)->MemoryBlockSizeInBytes / MEM_UNIT_SIZE) / 8;\r
\r
//\r
// Allocate memory for BitArray\r
gBS->FreePool (*MemoryHeader);\r
return EFI_OUT_OF_RESOURCES;\r
}\r
- \r
+\r
//\r
// Memory Block uses MemoryBlockSizeInPages pages,\r
// and it is allocated as common buffer use.\r
&Mapping\r
);\r
//\r
- // If returned Mapped size is less than the size \r
+ // If returned Mapped size is less than the size\r
// we request,do not support.\r
//\r
if (EFI_ERROR (Status) || (MemoryBlockSizeInBytes != EFI_PAGES_TO_SIZE (MemoryBlockSizeInPages))) {\r
gBS->FreePool (*MemoryHeader);\r
return EFI_UNSUPPORTED;\r
}\r
- \r
+\r
//\r
- // Data structure involved by host controller \r
+ // Data structure involved by host controller\r
// should be restricted into the same 4G\r
//\r
if (HcDev->Is64BitCapable != 0) {\r
return EFI_UNSUPPORTED;\r
}\r
}\r
- \r
+\r
//\r
// Set Memory block initial address\r
//\r
ASSERT (MemoryHeader != NULL);\r
\r
OldTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY + 1);\r
- \r
+\r
//\r
// allocate unit is 32 bytes (align on 32 byte)\r
//\r
- if (AllocSize & 0x1F) {\r
- RealAllocSize = (AllocSize / 32 + 1) * 32;\r
+ if (AllocSize & (MEM_UNIT_SIZE - 1)) {\r
+ RealAllocSize = (AllocSize / MEM_UNIT_SIZE + 1) * MEM_UNIT_SIZE;\r
} else {\r
RealAllocSize = AllocSize;\r
}\r
- \r
+\r
//\r
// There may be linked MemoryHeaders.\r
// To allocate a free pool in Memory blocks,\r
Status = AllocMemInMemoryBlock (\r
TempHeaderPtr,\r
(VOID **) Pool,\r
- RealAllocSize / 32\r
+ RealAllocSize / MEM_UNIT_SIZE\r
);\r
if (!EFI_ERROR (Status)) {\r
- ZeroMem (*Pool, AllocSize);\r
- gBS->RestoreTPL (OldTpl);\r
- return EFI_SUCCESS;\r
+ break;\r
}\r
}\r
\r
gBS->RestoreTPL (OldTpl);\r
- \r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ ZeroMem (*Pool, AllocSize);\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+\r
//\r
// There is no enough memory,\r
// Create a new Memory Block\r
//\r
- \r
+\r
//\r
// if pool size is larger than NORMAL_MEMORY_BLOCK_UNIT_IN_PAGES,\r
// just allocate a large enough memory block.\r
Status = AllocMemInMemoryBlock (\r
NewMemoryHeader,\r
(VOID **) Pool,\r
- RealAllocSize / 32\r
+ RealAllocSize / MEM_UNIT_SIZE\r
);\r
+\r
+ gBS->RestoreTPL (OldTpl);\r
+\r
if (!EFI_ERROR (Status)) {\r
ZeroMem (*Pool, AllocSize);\r
}\r
\r
- gBS->RestoreTPL (OldTpl);\r
return Status;\r
}\r
\r
//\r
// allocate unit is 32 byte (align on 32 byte)\r
//\r
- if (AllocSize & 0x1F) {\r
- RealAllocSize = (AllocSize / 32 + 1) * 32;\r
+ if (AllocSize & (MEM_UNIT_SIZE - 1)) {\r
+ RealAllocSize = (AllocSize / MEM_UNIT_SIZE + 1) * MEM_UNIT_SIZE;\r
} else {\r
RealAllocSize = AllocSize;\r
}\r
- \r
+\r
//\r
// scan the memory header linked list for\r
// the asigned memory to free.\r
// Pool is in the Memory Block area,\r
// find the start byte and bit in the bit array\r
//\r
- StartBytePos = ((Pool - TempHeaderPtr->MemoryBlockPtr) / 32) / 8;\r
- StartBitPos = (UINT8) (((Pool - TempHeaderPtr->MemoryBlockPtr) / 32) & 0x7);\r
+ StartBytePos = ((Pool - TempHeaderPtr->MemoryBlockPtr) / MEM_UNIT_SIZE) / 8;\r
+ StartBitPos = (UINT8) (((Pool - TempHeaderPtr->MemoryBlockPtr) / MEM_UNIT_SIZE) & 0x7);\r
\r
//\r
// reset associated bits in bit arry\r
//\r
- for (Index = StartBytePos, Index2 = StartBitPos, Count = 0; Count < (RealAllocSize / 32); Count++) {\r
- TempHeaderPtr->BitArrayPtr[Index] = (UINT8) (TempHeaderPtr->BitArrayPtr[Index] ^ (bit (Index2)));\r
+ for (Index = StartBytePos, Index2 = StartBitPos, Count = 0; Count < (RealAllocSize / MEM_UNIT_SIZE); Count++) {\r
+ ASSERT ((TempHeaderPtr->BitArrayPtr[Index] & bit (Index2) )== bit (Index2));\r
+\r
+ TempHeaderPtr->BitArrayPtr[Index] ^= (UINT8) (bit (Index2));\r
Index2++;\r
if (Index2 == 8) {\r
Index += 1;\r
break;\r
}\r
}\r
- \r
+\r
//\r
// Release emptied memory blocks (only if the memory block is not\r
// the first one in the memory header list\r
Returns:\r
\r
EFI_SUCCESS Success\r
- EFI_NOT_FOUND Can't find the free memory \r
+ EFI_NOT_FOUND Can't find the free memory\r
\r
--*/\r
{\r
//\r
// right shift the byte\r
//\r
- ByteValue /= 2;\r
+ ByteValue = ByteValue >> 1;\r
\r
if (BitValue == 0) {\r
//\r
if (NumberOfZeros < NumberOfMemoryUnit) {\r
return EFI_NOT_FOUND;\r
}\r
- \r
+\r
//\r
// Found enough free space.\r
//\r
- \r
+\r
//\r
// The values recorded in (FoundBytePos,FoundBitPos) have two conditions:\r
// 1)(FoundBytePos,FoundBitPos) record the position\r
if ((MemoryHeader->BitArrayPtr[FoundBytePos] & bit (FoundBitPos)) != 0) {\r
FoundBitPos += 1;\r
}\r
- \r
+\r
//\r
// Have the (FoundBytePos,FoundBitPos) make sense.\r
//\r
FoundBytePos += 1;\r
FoundBitPos -= 8;\r
}\r
- \r
+\r
//\r
// Set the memory as allocated\r
//\r
for (TempBytePos = FoundBytePos, Index = FoundBitPos, Count = 0; Count < NumberOfMemoryUnit; Count++) {\r
\r
- MemoryHeader->BitArrayPtr[TempBytePos] = (UINT8) (MemoryHeader->BitArrayPtr[TempBytePos] | (bit (Index)));\r
+ ASSERT ((MemoryHeader->BitArrayPtr[TempBytePos] & bit (Index) )== 0);\r
+ MemoryHeader->BitArrayPtr[TempBytePos] |= bit (Index);\r
Index++;\r
if (Index == 8) {\r
TempBytePos += 1;\r
}\r
}\r
\r
- *Pool = MemoryHeader->MemoryBlockPtr + (FoundBytePos * 8 + FoundBitPos) * 32;\r
+ *Pool = MemoryHeader->MemoryBlockPtr + (FoundBytePos * 8 + FoundBitPos) * MEM_UNIT_SIZE;\r
\r
return EFI_SUCCESS;\r
}\r
Returns:\r
\r
TRUE Empty\r
- FALSE Not Empty \r
+ FALSE Not Empty\r
\r
--*/\r
{\r
// Link the before and after\r
//\r
TempHeaderPtr->Next = NeedFreeMemoryHeader->Next;\r
+ NeedFreeMemoryHeader->Next = NULL;\r
break;\r
}\r
}\r
\r
EFI_SUCCESS Success\r
EFI_DEVICE_ERROR Fail\r
- \r
+\r
--*/\r
{\r
EFI_STATUS Status;\r
\r
EFI_SUCCESS Success\r
EFI_DEVICE_ERROR Fail\r
- \r
+\r
--*/\r
{\r
MEMORY_MANAGE_HEADER *TempHeaderPtr;\r