UEFIStarter
a simple UEFI framework
Data Structures | Macros | Functions

Memory allocation tracker: use this to prevent memory leaks. More...

Go to the source code of this file.

Data Structures

struct  memory_page_list_entry_t
 type for a single tracked allocation of memory pages More...
 
struct  memory_page_list_t
 type for a list node of tracked memory page allocations More...
 
struct  pool_memory_list_t
 type for a list node of tracked pool memory More...
 

Macros

#define MEMORY_PAGE_LIST_MAX_ENTRY_COUNT   510
 maximum number of entries per memory page list node More...
 
#define POOL_MEMORY_LIST_MAX_ENTRY_COUNT   1022
 like MEMORY_PAGE_LIST_MAX_ENTRY_COUNT, but for pool_memory_list_t
 

Functions

void reset_memory_tracking ()
 Resets the internal memory tracking. More...
 
void * allocate_pages (UINTN pages)
 Allocate tracked memory pages. More...
 
BOOLEAN free_pages (void *address, UINTN pages)
 Frees tracked memory pages. More...
 
void * allocate_pages_ex (UINTN pages, BOOLEAN track, EFI_ALLOCATE_TYPE type, void *target_address)
 internal: allocates memory pages. More...
 
BOOLEAN free_pages_ex (void *address, UINTN pages, BOOLEAN track)
 internal: frees memory pages. More...
 
void track_pool_memory (void *address)
 Starts tracking a pool memory address. More...
 
UINTN free_pool_memory_entries ()
 Frees all currently tracked pool memory entries. More...
 
void print_memory_page_list ()
 Debugging function: prints a human-readable list of tracked memory pages.
 
void print_pool_memory_list ()
 Debugging function: prints a human-readable list of tracked pool memory entries.
 
void init_tracking_memory ()
 Initialize the memory tracker.
 
UINTN stop_tracking_memory ()
 Stops tracking all memory. More...
 

Detailed Description

Memory allocation tracker: use this to prevent memory leaks.

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

Macro Definition Documentation

◆ MEMORY_PAGE_LIST_MAX_ENTRY_COUNT

#define MEMORY_PAGE_LIST_MAX_ENTRY_COUNT   510

maximum number of entries per memory page list node

Keep this as high as possible without breaking page size: sizeof(memory_page_list_t) should be as close to n*4096 as possible without going over it.

As of right now 510 will result in 2 pages for 64 bit systems and 1 page for 32 bit systems.

Function Documentation

◆ 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.

◆ 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()

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

◆ 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

◆ 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

◆ 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