Skip to content

Commit d4c7dc7

Browse files
committed
EmbeddedPkg/PrePiMemoryAllocationLib: Add ReallocatePool
Add implementation of ReallocatePool which is defined in the MemoryAllocationLib header file to allow components to not need special handling for PrePi module types. Jira TEGRAUEFI-3105 Signed-off-by: Jeff Brasen <[email protected]> Change-Id: Ifbc8f74de3855b1a60e14ad3014becdd577cde22
1 parent eccdab6 commit d4c7dc7

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,60 @@ FreePool (
269269
{
270270
// Not implemented yet
271271
}
272+
273+
/**
274+
Reallocates a buffer of type EfiBootServicesData.
275+
276+
Allocates and zeros the number bytes specified by NewSize from memory of type
277+
EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
278+
NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
279+
OldBuffer is freed. A pointer to the newly allocated buffer is returned.
280+
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
281+
enough memory remaining to satisfy the request, then NULL is returned.
282+
283+
If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
284+
is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
285+
286+
@param OldSize The size, in bytes, of OldBuffer.
287+
@param NewSize The size, in bytes, of the buffer to reallocate.
288+
@param OldBuffer The buffer to copy to the allocated buffer. This is an optional
289+
parameter that may be NULL.
290+
291+
@return A pointer to the allocated buffer or NULL if allocation fails.
292+
293+
**/
294+
VOID *
295+
EFIAPI
296+
ReallocatePool (
297+
IN UINTN OldSize,
298+
IN UINTN NewSize,
299+
IN VOID *OldBuffer OPTIONAL
300+
)
301+
{
302+
VOID *NewBuffer;
303+
304+
// Validate the OldBuffer is HobAllocated.
305+
DEBUG_CODE_BEGIN ();
306+
EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
307+
308+
if (OldBuffer != NULL) {
309+
HandOffHob = GetHobList ();
310+
ASSERT (((EFI_PHYSICAL_ADDRESS)OldBuffer >= HandOffHob->EfiMemoryBottom));
311+
ASSERT (((EFI_PHYSICAL_ADDRESS)(OldBuffer + OldSize) <= HandOffHob->EfiFreeMemoryBottom));
312+
}
313+
314+
DEBUG_CODE_END ();
315+
316+
// If new buffer would be smaller just return old buffer as FreePool isn't supported.
317+
if ((OldBuffer != NULL) && (OldSize >= NewSize)) {
318+
return OldBuffer;
319+
}
320+
321+
NewBuffer = AllocateZeroPool (NewSize);
322+
if ((NewBuffer != NULL) && (OldBuffer != NULL)) {
323+
CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
324+
FreePool (OldBuffer);
325+
}
326+
327+
return NewBuffer;
328+
}

0 commit comments

Comments
 (0)