UEFIStarter
a simple UEFI framework
Macros | Functions | Variables

Memory tracker, will find memory leaks. More...

#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <UEFIStarter/core/memory.h>
#include <UEFIStarter/core/logger.h>

Macros

#define MEMORY_PAGE_LIST_PAGE_COUNT   ((sizeof(memory_page_list_t)-1)/4096+1)
 the number of pages required for each allocation list node
 

Functions

void * allocate_pages_ex (UINTN pages, BOOLEAN track, EFI_ALLOCATE_TYPE type, void *target_address)
 internal: allocates memory pages. More...
 
void print_memory_page_list ()
 Debugging function: prints a human-readable list of tracked memory pages.
 
void reset_memory_tracking ()
 Resets the internal memory tracking. More...
 
static BOOLEAN _get_next_free_entry (memory_page_list_t **page_list, UINTN *index)
 internal: finds next memory page allocation list entry More...
 
static memory_page_list_entry_t_find_page_list_entry (void *address)
 internal: finds a page list entry with the given start address More...
 
void * allocate_pages (UINTN pages)
 Allocate tracked memory pages. More...
 
BOOLEAN free_pages_ex (void *address, UINTN pages, BOOLEAN track)
 internal: frees memory pages. More...
 
BOOLEAN free_pages (void *address, UINTN pages)
 Frees tracked memory pages. More...
 
void track_pool_memory (void *address)
 Starts tracking a pool memory address. More...
 
void print_pool_memory_list ()
 Debugging function: prints a human-readable list of tracked pool memory entries.
 
UINTN free_pool_memory_entries ()
 Frees all currently tracked pool memory entries. More...
 
void init_tracking_memory ()
 Initialize the memory tracker.
 
UINTN stop_tracking_memory ()
 Stops tracking all memory. More...
 

Variables

static memory_page_list_t_memory_page_list
 internal pointer to first memory page allocation list node
 
static pool_memory_list_t _pool_memory_list
 internal pointer to first pool memory allocation list node
 

Detailed Description

Memory tracker, will find memory leaks.

Author
Richard Nusser
License
GPLv3 (see http://www.gnu.org/licenses/)
See also
https://github.com/rinusser/UEFIStarter

Function Documentation

◆ allocate_pages_ex()

void* allocate_pages_ex ( UINTN  pages,
BOOLEAN  track,
EFI_ALLOCATE_TYPE  type,
void *  target_address 
)

internal: allocates memory pages.

This function isn't static - if you declare it with "extern" in a header you can access it directly.

Parameters
pagesthe amount of pages to allocate
trackwhether to track the pages
typesee UEFI's boot services AllocatePages() MemoryType parameter, usually AllocateAnyPages
target_addresssee UEFI's boot services AllocatePages() Memory parameter, usually ignored
Returns
the first page's address, or NULL on error

◆ reset_memory_tracking()

void reset_memory_tracking ( )

Resets the internal memory tracking.

This will forget all currently tracked memory, resulting in untracked memory leaks if you're not freeing those pages later.

◆ _get_next_free_entry()

static BOOLEAN _get_next_free_entry ( memory_page_list_t **  page_list,
UINTN *  index 
)
static

internal: finds next memory page allocation list entry

Parameters
page_listthe page list node to look in
indexthe index variable to write any matches to
Returns
whether the lookup succeeded
Todo:
if there's a next page_list, check that. if not, allocate new page_list and link to it

◆ _find_page_list_entry()

static memory_page_list_entry_t* _find_page_list_entry ( void *  address)
static

internal: finds a page list entry with the given start address

Parameters
addressthe first page's address to look for
Returns
a pointer to the list entry, or NULL if address isn't tracked

◆ allocate_pages()

void* allocate_pages ( UINTN  pages)

Allocate tracked memory pages.

Parameters
pagesthe number of pages to allocate
Returns
the first page's address, or NULL on error

◆ free_pages_ex()

BOOLEAN free_pages_ex ( void *  address,
UINTN  pages,
BOOLEAN  track 
)

internal: frees memory pages.

This function isn't static - if you declare it with "extern" in a header you can access it directly.

Parameters
addressthe first page's address
pagesthe number of pages allocated
trackwhether the pages were tracked
Returns
whether the pages were freed successfully

◆ free_pages()

BOOLEAN free_pages ( void *  address,
UINTN  pages 
)

Frees tracked memory pages.

Parameters
addressthe first page's address
pagesthe number of pages allocated
Returns
whether the pages were freed successfully

◆ track_pool_memory()

void track_pool_memory ( void *  address)

Starts tracking a pool memory address.

Once pool memory is tracked the next call to free_pool_memory_entries() will free all currently tracked pool memory entries. Consider this function call to mean "I don't need this piece of memory anymore, free it whenever".

Parameters
addressthe start of the pool memory
Todo:
implement linked list for pool memory tracking

◆ free_pool_memory_entries()

UINTN free_pool_memory_entries ( )

Frees all currently tracked pool memory entries.

Returns
the number of entries freed.

◆ stop_tracking_memory()

UINTN stop_tracking_memory ( )

Stops tracking all memory.

This will log errors if there are unfreed memory pages.

Returns
the number of errors, 0 if there were no tracked memory pages remaining
Todo:
the free_pool_memory_entries() call was at the end of this function - make sure there aren't any side-effects from calling this before the NULL check below