Code Monkey home page Code Monkey logo

memorysentinel's Introduction

 ╔╦╗┌─┐┌┬┐┌─┐┬─┐┬ ┬  ╔═╗┌─┐┌┐┌┌┬┐┬┌┐┌┌─┐┬
 ║║║├┤ ││││ │├┬┘└┬┘  ╚═╗├┤ │││ │ ││││├┤ │
 ╩ ╩└─┘┴ ┴└─┘┴└─ ┴   ╚═╝└─┘┘└┘ ┴ ┴┘└┘└─┘┴─┘                            

A utility to detect memory allocation and de-allocation in a given scope. Meant to be used for testing environments.

The MemorySentinel hijacks the all the system's variants of new & delete as well malloc & free. When unarmed, it quietly monitors the memory allocation landscape without intervening (quiet infiltration). When armed, and as soon as a "transgression" is detected, the MemorySentinel will become active, and either:

  • Throw a std::bad_alloc
  • Log to console (while still allocating normally)
  • Register the event silently (status can be queried)

MemorySentinel is very useful in Unit Tests to:

  • Monitor dynamic memory allocation during certain function calls / operations
  • Simulate out-of-memory conditions (e.g. branch coverage metrics)

Allocation Quota

The sentinel's strict zero-allocation-tolerance can be softened by calling setAllocationQuota(int numBytes). If this scenario the sentinel will log allocation until the quota is reached and then behave as usual.

Usage

MemorySentinel& sentinel = MemorySentinel::getInstance();
sentinel.setTransgressionBehaviour(MemorySentinel::TransgressionBehaviour::THROW_EXCEPTION);
sentinel.setArmed(true);

float* heapObject = new float[32]; // will throw an exception

// After use, disarm Sentinel
sentinel.setArmed(false);
sentinel.clearTransgressions();

Scoped usage

{
  ScopedMemorySentinel sentinel;	
  float* heapObject = new float[32];  
} 
// will assert upon exiting scope

Requirements / Compatibility

  • C++14
  • STL
  • tested with GCC, Clang and MSVC
  • tested with Catch2 Test Framework
  • tested on macos, windows & linux

Status

Sidelobe Build Matrix Linux Sidelobe Build Matrix macos Sidelobe Build Matrix Windows

MemorySentinel Quality Gate Status Coverage Bugs Vulnerabilities Lines of Code Maintainability Rating Reliability Rating Security Rating Technical Debt

memorysentinel's People

Contributors

sidelobe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.