feisty meow concerns codebase  2.140
structures::memory_limiter Class Reference

Tracks memory currently in use by memory manager objects. More...

#include <memory_limiter.h>

Public Member Functions

 memory_limiter (int overall_limit, int individual_limit)
 creates a limiter that allows "overall_limit" bytes to be in use. More...
 
virtual ~memory_limiter ()
 
 DEFINE_CLASS_NAME ("memory_limiter")
 
int overall_limit () const
 returns the current overall limit. More...
 
int individual_limit () const
 returns the current individual limit. More...
 
int overall_usage () const
 returns the size used by all managed memory. More...
 
int overall_space_left () const
 returns the overall space left for allocation. More...
 
int individual_usage (int individual) const
 returns the amount of memory used by "individual". More...
 
int individual_space_left (int individual) const
 returns the space left for the individual specified. More...
 
basis::astring text_form (int indent=0) const
 returns a string that lists out the overall plus individual limits. More...
 
bool okay_allocation (int individual, int memory_desired)
 returns true if "individual" may allocate "memory_desired" bytes. More...
 
bool record_deletion (int individual, int memory_deleted)
 acknowledges that the "individual" freed "memory_deleted" bytes. More...
 
void reset ()
 returns the object to a pristine state. More...
 
const structures::int_setindividuals_listed () const
 reports the current set of individuals using memory. More...
 

Detailed Description

Tracks memory currently in use by memory manager objects.

The manager is given the ability to control overall memory usage as well as to track memory usage per each user of the memory (assuming that the memory is granted to unique users indexable via an integer).

Definition at line 35 of file memory_limiter.h.

Constructor & Destructor Documentation

◆ memory_limiter()

structures::memory_limiter::memory_limiter ( int  overall_limit,
int  individual_limit 
)

creates a limiter that allows "overall_limit" bytes to be in use.

any attempts to add new memory after that limit is reached will be rejected. if "overall_limit" is zero, then no limit is enforced on the amount of memory that can be used in total. the "individual_limit" specifies per-user limits, where each user of memory is identified by a unique integer and where all users are granted equal rights to allocate memory. "individual_limit" can also be given as zero, meaning no limit is enforced per individual. note that "overall_limit" should usually be many multiples of the "individual_limit", as appropriate to how many users are expected.

Definition at line 47 of file memory_limiter.cpp.

◆ ~memory_limiter()

structures::memory_limiter::~memory_limiter ( )
virtual

Definition at line 55 of file memory_limiter.cpp.

References basis::WHACK().

Member Function Documentation

◆ DEFINE_CLASS_NAME()

structures::memory_limiter::DEFINE_CLASS_NAME ( "memory_limiter"  )

◆ individual_limit()

int structures::memory_limiter::individual_limit ( ) const
inline

returns the current individual limit.

Definition at line 56 of file memory_limiter.h.

Referenced by individual_space_left(), and text_form().

◆ individual_space_left()

int structures::memory_limiter::individual_space_left ( int  individual) const

returns the space left for the individual specified.

Definition at line 84 of file memory_limiter.cpp.

References individual_limit(), and individual_usage().

Referenced by text_form().

◆ individual_usage()

int structures::memory_limiter::individual_usage ( int  individual) const

returns the amount of memory used by "individual".

Definition at line 77 of file memory_limiter.cpp.

Referenced by individual_space_left(), and text_form().

◆ individuals_listed()

const int_set & structures::memory_limiter::individuals_listed ( ) const

reports the current set of individuals using memory.

to know whether one is using this class appropriately, check the returned set. if one does not think there should be any memory in use, then the set should be empty and overall_usage() should return zero. if the overall_usage() is zero, but there are members in the set, then there is an implementation error in memory_limiter. otherwise, if the set is non-empty, then deleted memory has not been recorded.

Definition at line 66 of file memory_limiter.cpp.

◆ okay_allocation()

bool structures::memory_limiter::okay_allocation ( int  individual,
int  memory_desired 
)

returns true if "individual" may allocate "memory_desired" bytes.

false indicates that this memory must not be allocated if the limits are to be adhered to, either because there is already too much used in the system at large or because this user is already using their limit.

Definition at line 127 of file memory_limiter.cpp.

References FUNCDEF, and LOG.

◆ overall_limit()

int structures::memory_limiter::overall_limit ( ) const
inline

returns the current overall limit.

Definition at line 54 of file memory_limiter.h.

Referenced by overall_space_left(), and text_form().

◆ overall_space_left()

int structures::memory_limiter::overall_space_left ( ) const
inline

returns the overall space left for allocation.

Definition at line 62 of file memory_limiter.h.

References overall_limit(), and overall_usage().

Referenced by text_form().

◆ overall_usage()

int structures::memory_limiter::overall_usage ( ) const
inline

returns the size used by all managed memory.

Definition at line 59 of file memory_limiter.h.

Referenced by overall_space_left(), and text_form().

◆ record_deletion()

bool structures::memory_limiter::record_deletion ( int  individual,
int  memory_deleted 
)

acknowledges that the "individual" freed "memory_deleted" bytes.

returns true if the "individual" is known and if "memory_deleted" could be subtracted from that object's usage count. failure of this method indicates that this class is not being used properly; if memory was okayed to be granted in okay_allocation, then the paired record_deletion will always succeed (in any arbitrary order where the okay_allocation succeeds and proceeds the matching record_deletion). if there are no remaining allocations for this individual, then its record is removed.

Definition at line 153 of file memory_limiter.cpp.

◆ reset()

void structures::memory_limiter::reset ( )

returns the object to a pristine state.

Definition at line 60 of file memory_limiter.cpp.

◆ text_form()

astring structures::memory_limiter::text_form ( int  indent = 0) const

returns a string that lists out the overall plus individual limits.

"indent" is used for spacing the printed rows of information.

Definition at line 90 of file memory_limiter.cpp.

References structures::set< contents >::elements(), individual_limit(), individual_space_left(), individual_usage(), basis::KILOBYTE, overall_limit(), overall_space_left(), overall_usage(), and basis::astring::s().


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