mirror of https://github.com/acidanthera/audk.git
BaseTools/CommonLib: Consume MemoryAllocationLib
This commit is contained in:
parent
af9f4495e9
commit
c14c33e76a
|
@ -18,6 +18,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
|
|||
#include "CommonLib.h"
|
||||
#include "EfiUtilityMsgs.h"
|
||||
|
||||
#include <Uefi/UefiSpec.h>
|
||||
#include <Library/PhaseMemoryAllocationLib.h>
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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$@
|
||||
|
||||
|
|
|
@ -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.<BR>
|
||||
SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||
|
||||
**/
|
||||
|
||||
#include <Base.h>
|
||||
#include <Uefi/UefiBaseType.h>
|
||||
#include <Uefi/UefiSpec.h>
|
||||
|
||||
#include <Library/MemoryAllocationLib.h>
|
||||
#include <Library/MemoryAllocationLibEx.h>
|
||||
|
||||
#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);
|
||||
}
|
|
@ -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.<BR>
|
||||
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_
|
|
@ -7,7 +7,9 @@
|
|||
|
||||
**/
|
||||
|
||||
#include <PiDxe.h>
|
||||
#include <Base.h>
|
||||
#include <Uefi/UefiBaseType.h>
|
||||
#include <Uefi/UefiSpec.h>
|
||||
|
||||
#include <Library/MemoryAllocationLib.h>
|
||||
#include <Library/MemoryAllocationLibEx.h>
|
||||
|
@ -17,6 +19,8 @@
|
|||
|
||||
#include <Library/MemoryProfileLib.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
#
|
||||
|
||||
[Sources]
|
||||
AlignedPages.h
|
||||
AlignedPages.c
|
||||
CommonMemoryAllocationLib.c
|
||||
CommonMemoryAllocationLibEx.c
|
||||
|
||||
|
|
Loading…
Reference in New Issue