diff --git a/BaseTools/Source/C/Common/CommonLib.c b/BaseTools/Source/C/Common/CommonLib.c index 3f022fba46..80b62897ca 100644 --- a/BaseTools/Source/C/Common/CommonLib.c +++ b/BaseTools/Source/C/Common/CommonLib.c @@ -18,6 +18,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "CommonLib.h" #include "EfiUtilityMsgs.h" +#include +#include + +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData; +GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode; + /** Compares to GUIDs @@ -483,107 +489,113 @@ LongFilePath ( } -VOID * -InternalAllocatePool ( - UINTN AllocationSize +static +void * +InternalAlignedAlloc ( + size_t Alignment, + size_t Size ) { - VOID * Memory; - - Memory = malloc(AllocationSize); - ASSERT(Memory != NULL); - return Memory; +#ifndef _WIN32 + return aligned_alloc (Alignment, Size); +#else + return _aligned_malloc (Size, Alignment); +#endif } - -VOID * -InternalReallocatePool ( - UINTN OldSize, - UINTN NewSize, - VOID *OldBuffer OPTIONAL +static +void +InternalAlignedFree ( + void *Ptr ) { - VOID *NewBuffer; +#ifndef _WIN32 + free (Ptr); +#else + _aligned_free (Ptr); +#endif +} - NewBuffer = AllocateZeroPool (NewSize); - if (NewBuffer != NULL && OldBuffer != NULL) { - memcpy (NewBuffer, OldBuffer, MIN (OldSize, NewSize)); - free(OldBuffer); +EFI_STATUS +EFIAPI +PhaseAllocatePages ( + IN EFI_ALLOCATE_TYPE Type, + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN OUT EFI_PHYSICAL_ADDRESS *Memory + ) +{ + VOID *Buffer; + UINTN BufferSize; + + ASSERT (Type == AllocateAnyPages); + + BufferSize = EFI_PAGES_TO_SIZE (Pages); + Buffer = InternalAlignedAlloc (EFI_PAGE_SIZE, BufferSize); + if (Buffer == NULL) { + return EFI_OUT_OF_RESOURCES; } - return NewBuffer; + + *Memory = (UINTN)Buffer; + + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +PhaseFreePages ( + IN EFI_PHYSICAL_ADDRESS Memory, + IN UINTN Pages + ) +{ + VOID *Buffer; + + Buffer = (VOID *)(UINTN)Memory; + InternalAlignedFree (Buffer); + + return EFI_SUCCESS; } VOID * EFIAPI -ReallocatePool ( - UINTN OldSize, - UINTN NewSize, - VOID *OldBuffer OPTIONAL +PhaseAllocatePool ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN AllocationSize ) { - return InternalReallocatePool (OldSize, NewSize, OldBuffer); -} - -VOID * -InternalAllocateCopyPool ( - UINTN AllocationSize, - CONST VOID *Buffer - ) -{ - VOID *Memory; - - ASSERT (Buffer != NULL); - - Memory = malloc (AllocationSize); - if (Memory != NULL) { - Memory = memcpy (Memory, Buffer, AllocationSize); - } - return Memory; -} - -VOID * -EFIAPI -AllocateCopyPool ( - UINTN AllocationSize, - CONST VOID *Buffer - ) -{ - return InternalAllocateCopyPool (AllocationSize, Buffer); -} - -VOID * -EFIAPI -AllocateZeroPool ( - UINTN AllocationSize - ) -{ - VOID * Memory; - Memory = malloc(AllocationSize); - ASSERT (Memory != NULL); - if (Memory == NULL) { - fprintf(stderr, "Not memory for malloc\n"); - } - memset(Memory, 0, AllocationSize); - return Memory; -} - -VOID * -EFIAPI -AllocatePool ( - UINTN AllocationSize - ) -{ - return InternalAllocatePool (AllocationSize); + return InternalAlignedAlloc (8, AllocationSize); } VOID EFIAPI -FreePool ( +PhaseFreePool ( IN VOID *Buffer ) { ASSERT (Buffer != NULL); - free (Buffer); + InternalAlignedFree (Buffer); +} + +VOID * +InternalAllocateAlignedPages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment + ) +{ + UINTN BufferSize; + + BufferSize = EFI_PAGES_TO_SIZE (Pages); + return InternalAlignedAlloc (MAX (Alignment, EFI_PAGE_SIZE), BufferSize); +} + +VOID +InternalFreeAlignedPages ( + IN VOID *Buffer, + IN UINTN Pages + ) +{ + InternalAlignedFree (Buffer); } VOID diff --git a/BaseTools/Source/C/Common/GNUmakefile b/BaseTools/Source/C/Common/GNUmakefile index 17e166d62e..c5a49ffe2f 100644 --- a/BaseTools/Source/C/Common/GNUmakefile +++ b/BaseTools/Source/C/Common/GNUmakefile @@ -92,4 +92,11 @@ OBJECTS += \ $(EDK2_OBJPATH)/MdePkg/Library/BasePrintLib/PrintLib.o \ $(EDK2_OBJPATH)/MdePkg/Library/BasePrintLib/PrintLibInternal.o +OBJECTS += \ + $(EDK2_OBJPATH)/MdeModulePkg/Library/BaseMemoryProfileLibNull/BaseMemoryProfileLibNull.o + +OBJECTS += \ + $(EDK2_OBJPATH)/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.o \ + $(EDK2_OBJPATH)/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLibEx.o + include $(MAKEROOT)/Makefiles/lib.makefile diff --git a/BaseTools/Source/C/Common/Makefile b/BaseTools/Source/C/Common/Makefile index c2abbca39b..e4982cbff4 100644 --- a/BaseTools/Source/C/Common/Makefile +++ b/BaseTools/Source/C/Common/Makefile @@ -91,6 +91,13 @@ OBJECTS = $(OBJECTS) \ $(EDK2_OBJPATH)\MdePkg\Library\BasePrintLib\PrintLib.obj \ $(EDK2_OBJPATH)\MdePkg\Library\BasePrintLib\PrintLibInternal.obj +OBJECTS = $(OBJECTS) \ + $(EDK2_OBJPATH)\MdeModulePkg\Library\BaseMemoryProfileLibNull\BaseMemoryProfileLibNull.obj + +OBJECTS = $(OBJECTS) \ + $(EDK2_OBJPATH)\MdeModulePkg\Library\CommonMemoryAllocationLib\CommonMemoryAllocationLib.obj \ + $(EDK2_OBJPATH)\MdeModulePkg\Library\CommonMemoryAllocationLib\CommonMemoryAllocationLibEx.obj + !INCLUDE ..\Makefiles\ms.lib diff --git a/BaseTools/Source/C/Makefiles/ms.rule b/BaseTools/Source/C/Makefiles/ms.rule index a7ea626592..97f43126ad 100644 --- a/BaseTools/Source/C/Makefiles/ms.rule +++ b/BaseTools/Source/C/Makefiles/ms.rule @@ -27,6 +27,15 @@ -@if not exist $(@D)\ mkdir $(@D) $(CC) -c $(CFLAGS) $(EDK2_INC) $< -Fo$@ +{$(EDK2_PATH)\MdeModulePkg\Library\BaseMemoryProfileLibNull\}.c{$(EDK2_OBJPATH)\MdeModulePkg\Library\BaseMemoryProfileLibNull\}.obj : + -@if not exist $(@D)\ mkdir $(@D) + $(CC) -c $(CFLAGS) $(EDK2_INC) $< -Fo$@ + +{$(EDK2_PATH)\MdeModulePkg\Library\CommonMemoryAllocationLib\}.c{$(EDK2_OBJPATH)\MdeModulePkg\Library\CommonMemoryAllocationLib\}.obj : + -@if not exist $(@D)\ mkdir $(@D) + $(CC) -c $(CFLAGS) $(EDK2_INC) $< -Fo$@ + + .c.obj : $(CC) -c $(CFLAGS) $(INC) $< -Fo$@ diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.c b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.c new file mode 100644 index 0000000000..47203089e3 --- /dev/null +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.c @@ -0,0 +1,84 @@ +/** @file + Common code for the Memory Allocation Library aligned page allocation code. + + Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include + +#include +#include + +#include "AlignedPages.h" + +/** + Allocates one or more 4KB pages of a certain memory type at a specified alignment. + + Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment + specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. + If there is not enough memory at the specified alignment remaining to satisfy the request, then + NULL is returned. + If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). + + @param MemoryType The type of memory to allocate. + @param Pages The number of 4 KB pages to allocate. + @param Alignment The requested alignment of the allocation. Must be a power of two. + If Alignment is zero, then byte alignment is used. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +InternalAllocateAlignedPages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment + ) +{ + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + + Status = AllocateAlignedPagesEx ( + AllocateAnyPages, + MemoryType, + Pages, + Alignment, + &Memory + ); + if (EFI_ERROR (Status)) { + return NULL; + } + + return (VOID *)(UINTN)Memory; +} + +/** + Frees one or more 4KB pages that were previously allocated with one of the aligned page + allocation functions in the Memory Allocation Library. + + Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer + must have been allocated on a previous call to the aligned page allocation services of the Memory + Allocation Library. If it is not possible to free allocated pages, then this function will + perform no actions. + + If Buffer was not allocated with an aligned page allocation function in the Memory Allocation + Library, then ASSERT(). + If Pages is zero, then ASSERT(). + + @param Buffer Pointer to the buffer of pages to free. + @param Pages The number of 4 KB pages to free. + +**/ +VOID +InternalFreeAlignedPages ( + IN VOID *Buffer, + IN UINTN Pages + ) +{ + FreePages (Buffer, Pages); +} diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.h b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.h new file mode 100644 index 0000000000..d5aa547b30 --- /dev/null +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/AlignedPages.h @@ -0,0 +1,60 @@ +/** @file + Common code for the Memory Allocation Library aligned page allocation code. + + Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef ALIGNED_PAGES_H_ +#define ALIGNED_PAGES_H_ + +/** + Allocates one or more 4KB pages of a certain memory type at a specified alignment. + + Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment + specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. + If there is not enough memory at the specified alignment remaining to satisfy the request, then + NULL is returned. + If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). + + @param MemoryType The type of memory to allocate. + @param Pages The number of 4 KB pages to allocate. + @param Alignment The requested alignment of the allocation. Must be a power of two. + If Alignment is zero, then byte alignment is used. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +InternalAllocateAlignedPages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment + ); + +/** + Frees one or more 4KB pages that were previously allocated with one of the aligned page + allocation functions in the Memory Allocation Library. + + Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer + must have been allocated on a previous call to the aligned page allocation services of the Memory + Allocation Library. If it is not possible to free allocated pages, then this function will + perform no actions. + + If Buffer was not allocated with an aligned page allocation function in the Memory Allocation + Library, then ASSERT(). + If Pages is zero, then ASSERT(). + + @param Buffer Pointer to the buffer of pages to free. + @param Pages The number of 4 KB pages to free. + +**/ +VOID +InternalFreeAlignedPages ( + IN VOID *Buffer, + IN UINTN Pages + ); + +#endif // ALIGNED_PAGES_H_ diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c index 18365a70b9..7e02718a2b 100644 --- a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.c @@ -7,7 +7,9 @@ **/ -#include +#include +#include +#include #include #include @@ -17,6 +19,8 @@ #include +#include "AlignedPages.h" + /** Allocates one or more 4KB pages of type EfiBootServicesData. @@ -203,49 +207,6 @@ FreePages ( ASSERT_EFI_ERROR (Status); } -/** - Allocates one or more 4KB pages of a certain memory type at a specified alignment. - - Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment - specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. - If there is not enough memory at the specified alignment remaining to satisfy the request, then - NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). - - @param MemoryType The type of memory to allocate. - @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -STATIC -VOID * -InternalAllocateAlignedPages ( - IN EFI_MEMORY_TYPE MemoryType, - IN UINTN Pages, - IN UINTN Alignment - ) -{ - EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Memory; - - Status = AllocateAlignedPagesEx ( - AllocateAnyPages, - MemoryType, - Pages, - Alignment, - &Memory - ); - if (EFI_ERROR (Status)) { - return NULL; - } - - return (VOID *)(UINTN)Memory; -} - /** Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment. @@ -406,7 +367,7 @@ FreeAlignedPages ( IN UINTN Pages ) { - FreePages (Buffer, Pages); + InternalFreeAlignedPages (Buffer, Pages); } /** diff --git a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf index 8c806dc642..6bbecffbb0 100644 --- a/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf +++ b/MdeModulePkg/Library/CommonMemoryAllocationLib/CommonMemoryAllocationLib.inf @@ -23,6 +23,8 @@ # [Sources] + AlignedPages.h + AlignedPages.c CommonMemoryAllocationLib.c CommonMemoryAllocationLibEx.c