BaseTools/CommonLib: Consume MemoryAllocationLib

This commit is contained in:
Marvin Häuser 2023-03-29 21:04:28 +02:00
parent 7506a40c12
commit c382e9c571
4 changed files with 76 additions and 79 deletions

View File

@ -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;
INTN
BtCompareGuid (
IN EFI_GUID *Guid1,
@ -551,102 +557,70 @@ Returns:
}
VOID *
InternalAllocatePool (
UINTN AllocationSize
EFI_STATUS
EFIAPI
PhaseAllocatePages (
IN EFI_ALLOCATE_TYPE Type,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages,
IN OUT EFI_PHYSICAL_ADDRESS *Memory
)
{
VOID * Memory;
VOID *Buffer;
UINTN BufferSize;
Memory = malloc(AllocationSize);
ASSERT(Memory != NULL);
return Memory;
}
ASSERT (Type == AllocateAnyPages);
BufferSize = EFI_PAGES_TO_SIZE (Pages);
VOID *
InternalReallocatePool (
UINTN OldSize,
UINTN NewSize,
VOID *OldBuffer OPTIONAL
)
{
VOID *NewBuffer;
#ifndef _WIN32
Buffer = aligned_alloc (EFI_PAGE_SIZE, BufferSize);
#else
Buffer = _aligned_malloc (BufferSize, EFI_PAGE_SIZE);
#endif
NewBuffer = AllocateZeroPool (NewSize);
if (NewBuffer != NULL && OldBuffer != NULL) {
memcpy (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
free(OldBuffer);
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;
#ifndef _WIN32
free (Buffer);
#else
_aligned_free (Buffer);
#endif
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 malloc (AllocationSize);
}
VOID
EFIAPI
FreePool (
PhaseFreePool (
IN VOID *Buffer
)
{

View File

@ -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

View File

@ -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

View File

@ -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$@