GCC Middle and Back End API Reference
ltrans_file_cache Class Reference

#include <lto-ltrans-cache.h>

Collaboration diagram for ltrans_file_cache:

Data Structures

struct  item
 

Public Member Functions

 ltrans_file_cache (const char *dir, const char *prefix, const char *suffix, size_t soft_cache_size)
 
 ~ltrans_file_cache ()
 
void load_cache ()
 
void save_cache ()
 
bool add_to_cache (const char *filename, item *&_item)
 
itemget_item (const char *input)
 
void try_prune ()
 
void cleanup ()
 
 operator bool ()
 

Data Fields

lockfile creation_lock
 
lockfile deletion_lock
 
const char * dir
 

Private Member Functions

void add_item (item *item)
 
itemcreate_item (checksum_t checksum)
 
void prune ()
 
std::string filename_cachedata ()
 

Private Attributes

std::vector< item * > items
 
std::map< checksum_t, item * > map_checksum
 
std::map< std::string, item * > map_input
 
const char * prefix
 
const char * suffix
 
size_t soft_cache_size
 
uint32_t usage_counter
 
std::string cache_prefix
 
uint32_t cache_free_idx
 
char * str_buffer
 

Constructor & Destructor Documentation

◆ ltrans_file_cache()

ltrans_file_cache::ltrans_file_cache ( const char * dir,
const char * prefix,
const char * suffix,
size_t soft_cache_size )
Constructor.  Resulting cache item filenames will be
in format `prefix%d[.ltrans]suffix`.   

References cache_free_idx, cache_prefix, creation_lock, deletion_lock, dir, prefix, str_buffer, and suffix.

◆ ~ltrans_file_cache()

ltrans_file_cache::~ltrans_file_cache ( )
Destructor.   

References cleanup(), free(), and str_buffer.

Member Function Documentation

◆ add_item()

void ltrans_file_cache::add_item ( ltrans_file_cache::item * item)
private

◆ add_to_cache()

bool ltrans_file_cache::add_to_cache ( const char * filename,
item *& _item )
Adds input file into cache.  Cache item with input file identical to
added input file will be returned as _item.
If the file was already cached, `true` is returned, `false` otherwise.
The added input file is deleted (or moved).

Must be called with creation_lock held to prevent data race.   

References add_item(), create_item(), file_checksum(), files_identical(), ltrans_file_cache::item::input, ltrans_file_cache::item::last_used, map_checksum, and usage_counter.

Referenced by run_gcc().

◆ cleanup()

void ltrans_file_cache::cleanup ( )
Clears cache class, as if only constructor was called.   

References items, map_checksum, map_input, and usage_counter.

Referenced by load_cache(), prune(), and ~ltrans_file_cache().

◆ create_item()

ltrans_file_cache::item * ltrans_file_cache::create_item ( checksum_t checksum)
private
Creates new cache item with given checksum.
New input/output files are chosen to not collide with other items.

Must be called with creation_lock held to prevent data race.   

References cache_free_idx, cache_prefix, map_input, str_buffer, suffix, and usage_counter.

Referenced by add_to_cache().

◆ filename_cachedata()

std::string ltrans_file_cache::filename_cachedata ( )
private
Creates cachedata filename for save/load.   

References dir.

Referenced by load_cache(), and save_cache().

◆ get_item()

ltrans_file_cache::item * ltrans_file_cache::get_item ( const char * input)
If exists, returns cache item corresponding to cached input file.   

References map_input, and NULL.

Referenced by run_gcc().

◆ load_cache()

void ltrans_file_cache::load_cache ( )
Loads data about previously cached items from cachedata file.
Sorts items by last_used and remaps last_used to small integers.

Must be called with creation_lock or deletion_lock held to
prevent data race.   

References a, add_item(), b, cleanup(), dir, filename_cachedata(), fopen, i, items, read_cache_item(), and usage_counter.

Referenced by prune(), and run_gcc().

◆ operator bool()

ltrans_file_cache::operator bool ( )
inline

References dir.

◆ prune()

void ltrans_file_cache::prune ( )
private
Prunes oldest unused cache items over limit.
Must be called with deletion_lock held to prevent data race.   

References add_item(), cleanup(), file_exists(), i, ltrans_file_cache::item::input, items, load_cache(), ltrans_file_cache::item::output, save_cache(), and soft_cache_size.

Referenced by try_prune().

◆ save_cache()

void ltrans_file_cache::save_cache ( )
Rewrites data about cache items into cachedata file.

Must be only called when creation_lock or deletion_lock was held since last
call to load_cache.   

References dir, filename_cachedata(), fopen, items, and write_cache_item().

Referenced by prune(), and run_gcc().

◆ try_prune()

void ltrans_file_cache::try_prune ( )
If no other process holds the deletion_lock, prunes oldest unused cache
items over limit.   

References deletion_lock, prune(), lockfile::try_lock_write(), and lockfile::unlock().

Referenced by run_gcc().

Field Documentation

◆ cache_free_idx

uint32_t ltrans_file_cache::cache_free_idx
private

Referenced by create_item(), and ltrans_file_cache().

◆ cache_prefix

std::string ltrans_file_cache::cache_prefix
private

Referenced by create_item(), and ltrans_file_cache().

◆ creation_lock

lockfile ltrans_file_cache::creation_lock

Referenced by ltrans_file_cache(), and run_gcc().

◆ deletion_lock

lockfile ltrans_file_cache::deletion_lock

◆ dir

◆ items

std::vector<item*> ltrans_file_cache::items
private

◆ map_checksum

std::map<checksum_t, item*> ltrans_file_cache::map_checksum
private

Referenced by add_item(), add_to_cache(), and cleanup().

◆ map_input

std::map<std::string, item*> ltrans_file_cache::map_input
private

◆ prefix

const char* ltrans_file_cache::prefix
private

Referenced by ltrans_file_cache().

◆ soft_cache_size

size_t ltrans_file_cache::soft_cache_size
private

Referenced by prune().

◆ str_buffer

char* ltrans_file_cache::str_buffer
private

◆ suffix

const char* ltrans_file_cache::suffix
private

Referenced by create_item(), and ltrans_file_cache().

◆ usage_counter

uint32_t ltrans_file_cache::usage_counter
private

The documentation for this class was generated from the following files: