Giter VIP home page Giter VIP logo

static_exception's Introduction

Introduction

Avoiding dynamic memory allocations is ciritical in safety relevant systems, but throwing an exception dynamically allocates memory in GCC. This header only library replaces this allocation scheme with a thread safe memory pool.

Usage

To use this library you need to include it and then link to it:

add_subdirectory(static_exception)
add_executable(my_exe ...)
target_link_libraries(my_exe static_exception ...)

Configuration

The resource limits of memory pool can be configured using compiler defines:

# Set custom memory pool limits:
add_definitions(-EXCEPTION_MEMORY__CXX_MAX_EXCEPTION_SIZE 1024)
add_definitions(-EXCEPTION_MEMORY__CXX_POOL_SIZE 64*128)
add_definitions(-EXCEPTION_MEMORY__CXX_POOL_ALIGNMENT 8)

Errors can be handled by overwriting error specific callback functions. By default these call std::terminate:

/** Overridable function to specify behaviour if the exception memory pool is exhausted. By default
 *  this function calls std::terminate.
 *  \param thrown_size The requested memory size.
 *  \return A pointer to some additional memory.
 */
extern "C" void* exception_memory_pool_exhausted(const size_t thrown_size) {
  std::terminate();
  return nullptr;
}

/** Overridable function to specify behaviour if the thrown exception is too large for the
 *  exception memory pool. By default this function calls std::terminate.
 *  \param thrown_size The requested memory size.
 *  \return A pointer to some additional memory.
 */
extern "C" void* exception_too_large(const size_t thrown_size) {
  std::terminate();
  return nullptr;
}

/** Overridable function to specify behaviour if the memory pool detects an memory leak. By
 *  default this function calls std::terminate.
 */
extern "C" void exception_memory_pool_leak() {
  std::terminate();
}

Running the Tests

Note that you can omit the cmake parameter in the instructions below on ubuntu as the gtest source dirctory is automatically set.

  1. Install gtest: sudo apt-get install libgtest-dev.
  2. Create and enter build folder: cd static_exception && mkdir build && cd build.
  3. Build the tests: cmake ../static_exception -DGTEST_SOURCE_DIR:STRING="pathToGtestInstallation" .. && make.
  4. Run the tests: test/static_exception_test.

Limitations

  • Exceptions thrown during library initalization might still be allocated dynamically. This is usually not a problem as static memory is only required during steady time.

  • Standard exceptions such std::runtime_error will still allocate memory for their internal error string if thrown. This library does not solve this issue.

static_exception's People

Contributors

deeplearningrobotics avatar evan-flynn-apexai avatar flynneva avatar kylemarcey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

static_exception's Issues

undefined reference to `exception_memory::__cxx::cxa_free_exception(void*)'

I had the following issue when trying to compile the static_exception library:
CMake Warning at /usr/src/gtest/CMakeLists.txt:54 (project):
VERSION keyword not followed by a value or was followed by a value that
expanded to nothing.

static_exception/test/static_exception_test.cpp:37:20: warning: inline function ‘void exception_memory::__cxx::cxa_free_exception(void*)’ used but never defined
37 | extern inline void cxa_free_exception(void vptr) noexcept;
| ^~~~~~~~~~~~~~~~~~
static_exception/test/static_exception_test.cpp:36:20: warning: inline function ‘void exception_memory::__cxx::cxa_free_dependent_exception(void
)’ used but never defined
36 | extern inline void cxa_free_dependent_exception (void vptr) noexcept;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: CMakeFiles/static_exception_test.dir/static_exception_test.cpp.o: in function StaticExceptions_MemoryLeak_Test::TestBody()': static_exception/test/static_exception_test.cpp:193: undefined reference to exception_memory::__cxx::cxa_free_exception(void
)'
/usr/bin/ld: static_exception/test/static_exception_test.cpp:194: undefined reference to `exception_memory::__cxx::cxa_free_dependent_exception(void*)'
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/static_exception_test.dir/build.make:90: test/static_exception_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:153: test/CMakeFiles/static_exception_test.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

I used colcon build to build it.
gcc/g++ version are: 9.4.0.

Do you have any idea on how to solve this?
Thank you!

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.