# FeatStd

# FeatStd Overview

#### <a class="anchor" id="bkmrk-"></a>Introduction

FeatStd is the short form for FEAT Standard library. It's purpose is to provide common basic functionalities, like Datatypes, Classes, Macros, Platform/OS abstraction, etc. for various FEAT products. FeatStd is also configurable to the needs of a software product, having feature flags to enable self-contained functionalities, like IPC support, UnitTest framework (based on google test), logging, etc. resp. overall settings like thread-safety support, fractional number handling, etc.

Historically FeatStd was introduced, when the need came up to have a common root for software products in CGI Studio context, which basically need not depend on each other. Precisely it was founded as the request for having CGI Studio [Courier](http://dev.doc.cgistudio.at/APILINK/namespace_courier.html) running without CGI Studio [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") came at a time where [Courier](http://dev.doc.cgistudio.at/APILINK/namespace_courier.html) had many dependencies to [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") like Datatypes, Container classes, a.s.o.

FeatStd itself has the requirement to have no CGI Studio context. Generally spoken it shall not have product context at all. It shall be a conglomeration of real common functionalities, which can be re-used in any FEAT software product, which is based on C++.

#### <a class="anchor" id="bkmrk--0"></a>FeatStd build system

Due to the fact that FeatStd shall have no product context at all, the build system itself was based on plain CMake, including tool-chain sample files to cross-compile for various target platforms (to be found in featstd/cmake/toolchain-files)

As mentioned above FeatStd is configurable and is completely dependent on the configuration file generated via the CMake pass (FeatStd/Config.h in the binary dir). This file is a must-include for any and every file using FeatStd, in any product, as it provides whole configuration for FeatStd. The benefit for this approach is that there is not handicraft work needed to set compiler defines to configure FeatStd, but simple set the include path to the generated source directory (typically called &lt;binary-dir&gt;/gensrc) in the binary output folder. Additionally to the include path to the output directory also a include path to the source directory has to be set. More on that later.

And finally the last and very important setting is the FEATSTD\_BUILD\_MODE, which unfortunately must be compiler flag dependent on the build configuration. CMake supports four different built-in build configurations, which are *Debug*, *Release*, *MinSizeRel* and *RelWithDebInfo*. For all these four build configuration CMake provides compiler variables called CMAKE\_CXX\_FLAGS\_&lt;build-mode&gt; (e.g. CMAKE\_CXX\_FLAGS\_MINSIZEREL), which all have to be decorated with the FEATSTD\_BUILD\_MODE define. FeatStd's CMakeLists already provides a macro just for decorating the compiler flags, and can either be called or used as template by the product/application.

See here how it is called and what is does:

```
# ------------------------------------------------------------------------------
# Adds FEATSTD_BUILD_MODE setting to compiler flags 
# ------------------------------------------------------------------------------
macro(FeatStdAddBuildModeDefine)
    set(definitionsFlag "-D")
    if (MSVC) 
        string(REPLACE - / definitionsFlag ${definitionsFlag})
    endif()

    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#gae285977b338939b4703752080c87ff66">FEATSTD_BUILD_MODE_DEBUG</a>")
    set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga060a652e61dbe0a256db83820b52fbee">FEATSTD_BUILD_MODE_MINSIZEREL</a>")
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#gaf68165ec34b53093a94d0f1e70a25341">FEATSTD_BUILD_MODE_RELEASE</a>")   
    set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga87c69c1bfbfc33c30e23c8eccb698127">FEATSTD_BUILD_MODE_RELWITHDEBINFO</a>")

    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#gae285977b338939b4703752080c87ff66">FEATSTD_BUILD_MODE_DEBUG</a>")
    set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga060a652e61dbe0a256db83820b52fbee">FEATSTD_BUILD_MODE_MINSIZEREL</a>")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#gaf68165ec34b53093a94d0f1e70a25341">FEATSTD_BUILD_MODE_RELEASE</a>")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${definitionsFlag}FEATSTD_BUILD_MODE=<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga87c69c1bfbfc33c30e23c8eccb698127">FEATSTD_BUILD_MODE_RELWITHDEBINFO</a>")
endmacro()
```

In every CMake run FeatStd produces a pretty informative output, describing under which conditions it was build, which features are enabled and which CMake variables are exported for further use in the product using FeatStd. The latter is quite of interest, as the caller (the software component which is binding FeatStd), might re-use the exported CMake settings produced by FeatStd. All FeatStd exported CMake variables are put into the internal CMake cache and therefore available in the buildsystem after FeatStd was added to the product (using CMake's add\_subdirectory()).

Above was mentioned that it is enough to simply add the include directories, which basically are the FeatStd source directory and the generated source directory, but it may happen that FeatStd binds itself a component (e.g. google test), where also the include directory has to be propagated. Additionally FeatStd may add link libraries which have to be bound to the overall software product. To ease this fact of configuration it is recommended to simply propagate values of the *exported FeatStd CMake variables* for include directories(FEATSTD\_EXPORTED\_INCLUDE\_DIRECTORIES), definitions(FEATSTD\_EXPORTED\_DEFINITIONS) and link libraries(FEATSTD\_EXPORTED\_LINK\_LIBRARIES) to the whole software product, when FeatStd is in the game. All other exported FeatStd variables are ready to be used, in case this information is needed.

For a typical output of a CMake configuration run of FeatStd please refer to the device specific documentation:

<div class="contents" id="bkmrk-device-specific-docu"><div class="textblock">- <span style="color:rgb(230,126,35);">[Device Specific Documentation](https://doc316en.candera.eu/shelves/7-devices)</span>

</div></div>

# FeatStd Features

<div class="contents" id="bkmrk-cmake-feature-switch"><div class="contents"><div class="textblock"><dl class="warning"><dt></dt><dd><p class="callout warning">CMake feature switches have been renamed for V3.x releases, see <span style="color:rgb(230,126,35);">[Change Log](https://doc316en.candera.eu/books/cgi-studio-300/page/candera-v300-migration-guide)</span></p>

</dd></dl></div></div></div>#### <a class="anchor" id="bkmrk-"></a>Select FeatStd Features

The FeatStd library feature set is specified by the following CMake settings:

<div class="contents" id="bkmrk-name-description-pos"><div class="contents"><div class="textblock"><table style="width:800px;"><tbody><tr style="background-color:#d4d4d4;"><th>Name</th><th>Description</th><th>Possible values</th></tr><tr><td>FEATSTD\_LOG\_ENABLED</td><td>Whether or not CGI-Studio logging system shall be enabled.</td><td>ON/OFF</td></tr><tr><td>FEATSTD\_SYSTEM\_MEMORY\_STATISTIC\_ENABLED</td><td>Whether or not CGI-Studio shall collect memory allocation statistics.</td><td>ON/OFF</td></tr><tr><td>FEATSTD\_SYSTEM\_MEMORY\_STATISTIC\_FILE  
\_AND\_LINE\_TRACKING\_ENABLED</td><td>If FEATSTD\_SYSTEM\_MEMORY\_STATISTIC\_ENABLED is enabled, this option can be enabled to generate file and line number output for memory allocation statistics.</td><td>ON/OFF</td></tr><tr><td>FEATSTD\_FRACTIONAL\_NUMBER\_TYPE</td><td>Configures the build system to replace the 32 bit fractional number representation (designated in e.g. [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") by the symbol Float), with one of 3 options:

- **Float** floating point representation
- **Fixed16p16** fixed point implementation with 16 bit integer part and 16 bit fractional part.
- **Fixed24p12** fixed point implementation with 24 bit integer part and 12 bit fractional part.

The fixed point implementation used is `FixedPointPlatform::FixedPoint` and the mathematical functions are defined in `FixedPointPlatform::Math`.

</td><td>Float  
Fixed16p16  
Fixed24p12</td></tr><tr><td>FEATSTD\_MONITOR\_ENABLED</td><td>Enable CGI Analyzer compliant performance tracing (compile time switch).

<dl class="note"><dt></dt><dd><p class="callout info">Monitor is an optional module only available upon special request. To enable this feature, CGI Analyzer tool chain which is a separate add-on component to CGI Studio, is required.</p>

</dd></dl></td><td>ON/OFF</td></tr><tr><td>FEATSTD\_GLERRORS\_AND\_EGLERRORS\_ENABLED</td><td>Enables calls to glGetError and eglGetError.</td><td>ON/OFF</td></tr></tbody></table>

</div></div></div>

# Interfaces & Namespaces

#### <a class="anchor" id="bkmrk-"></a>Interfaces

The Feat-Standard public interface is covered by the namespaces FeatStd and various subnamespaces of namespace FeatStd. All namespaces containing either **Internal** or **Private** in the name cover private namespaces, which are not recommended to be used in any application, as it is not guaranteed that these interfaces remain stable respectively exist in any future release. Changes in any of these private namespaces can be applied without any (prior) notifications.

#### <a class="anchor" id="bkmrk--0"></a>Namespace FeatStd

<div class="contents" id="bkmrk-container-utilities"><div class="contents"><div class="textblock">- [Container](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_t_a_i_n_e_r.html)
- [Utilities](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html)

</div></div></div>#### <a class="anchor" id="bkmrk--1"></a>Namespace FeatStd::Diagnostics

<div class="contents" id="bkmrk-diagnostics"><div class="contents"><div class="textblock">- [Diagnostics](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html)

</div></div></div>#### <a class="anchor" id="bkmrk--2"></a>Namespace FeatStd::MemoryManagement

<div class="contents" id="bkmrk-memory-management"><div class="contents"><div class="textblock">- [Memory Management](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html)

</div></div></div>#### <a class="anchor" id="bkmrk--3"></a>Namespace FeatStd::PerfMon

<div class="contents" id="bkmrk-monitorperformancere"><div class="contents"><div class="textblock">- MonitorPerformanceReporting

</div></div></div>#### <a class="anchor" id="bkmrk--4"></a>Namespace FeatStd::Platform

This interface defines the access to platform dependent objects. The implementation of this platform dependent code is not part of FeatStd, though there are a few reference platforms, like SimulationPlatform (implemented basically for Win32) or GenericPlatform (providing generic respectively stub implementations), which can used as templates for the customer.

This platform interface can be seen as an official porting interface of FeatStd to any customer specific platform setup, which is the reason for outlining it in the separate namespace FeatStd::Platform.

<div class="contents" id="bkmrk-platform"><div class="textblock">- [Platform](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___p_l_a_t_f_o_r_m.html)

</div></div>

# Tutorials

A set of tutorials is demonstrating the use of FeatStd library.  
  
The FeatStd API documentation is added to provide full traceability between the tutorials and the used FeatStd API - refer to the sub page 'API Documentation'.

# Diagnostics

#### <a class="anchor" id="bkmrk-"></a>Description

This tutorial describes several aspects of diagnostic support during development and post-development phases.

#### **Assertions** 

For using assertions include *FeatStd/Diagnostics/Debug.h* in your program.

##### <a class="anchor" id="bkmrk--1"></a>Compile Time Checks

Best time to catch an error is at compile time before the program starts. If the assertion is a constant expression, then the compiler is able to check it.

Use

<div class="contents" id="bkmrk-featstd_compiletime_"><div class="contents"><div class="textblock">- [FEATSTD\_COMPILETIME\_ASSERT(assertion)](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#gaeee202e9a6abec1fa3a162c6a1324500)

</div></div></div>##### <a class="anchor" id="bkmrk--2"></a>Runtime Checks

These kinds of assertions should be used to document logically impossible situations and discover programming errors.

Use

<div class="contents" id="bkmrk-featstd_debug_assert"><div class="contents"><div class="textblock">- FEATSTD\_DEBUG\_ASSERT(condition)
- FEATSTD\_DEBUG\_FAIL()
- FEATSTD\_DEBUG\_REENTRANCE\_GUARD()
- FEATSTD\_DEBUG\_BREAK()

</div></div></div>FEATSTD\_DEBUG\_ASSERT() and FEATSTD\_DEBUG\_FAIL() allow the user to check **pre-conditions** and also **post-conditions** as well as **invariants**, sometimes known as "embedded testing".

```
bool CgiApp::LoadAsset(const Char* filePath)
{
    FEATSTD_DEBUG_ASSERT(filePath != 0);

    ClearLoadedAsset();
    m_assetConfig.AddFileRepository(filePath);
```

```
                if ((*tempName >= '0') && (*tempName <= '9')) {
                    hash += (<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#gae299c392c2a207dfa2036cb528451eb7">ToUInt32</a>(*tempName) - static_cast<UInt32>('0'));
                }
                else if ((*tempName >= 'A') && (*tempName <= 'F')) {
                    hash += (<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#gae299c392c2a207dfa2036cb528451eb7">ToUInt32</a>(*tempName) - (static_cast<UInt32>('A') - 10U));
                }
                else if ((*tempName >= 'a') && (*tempName <= 'f')) {
                    hash += (<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#gae299c392c2a207dfa2036cb528451eb7">ToUInt32</a>(*tempName) - (static_cast<UInt32>('a') - 10U));
                }
                else {
                    // failure
                    FEATSTD_DEBUG_FAIL();
                }
```

FEATSTD\_DEBUG\_REENTRANCE\_GUARD() allows to check that a part of code isn't entered twice:

```
void Appender::Append(const LogEvent& logEvent)
{
    FEATSTD_DEBUG_REENTRANCE_GUARD();
```

  
Be aware that all FEATSTD\_**DEBUG**\_... macros are expanded in Debug-Versions only. This avoids codesize and performance penalty in Release-Versions.

For a distinction between ErrorHandling and Assertions please see [http://en.wikipedia.org/wiki/Assertion\_(computing)](http://en.wikipedia.org/wiki/Assertion_(computing))

##### <a class="anchor" id="bkmrk--3"></a>Customizing

The behavior can be adapted by implementing

<div class="contents" id="bkmrk-either-a-platform-sp"><div class="contents"><div class="textblock">- either a platform specific FeatStd::Platform::Diagnostic class
- or a **DebugControlHook** class derived from FeatStd::Diagnostics::IDebugControlHook, activated via FeatStd::Diagnostics::DebugControl::SetHook.

</div></div></div>  
See UnitTests\\Diagnostics\\DebugControlTest.cpp and the FeatStd::UnitTestHelper::DebugOutputChecker implementation for details.

#### **Logging** 

##### <a class="anchor" id="bkmrk--4"></a>Description

Logging is an important feature while programming (debugging), but also during the post-development phase to detect the reasons for system and software crashes.

In contrast to classical "printf" debugging, the FeatStd implementation is "two-dimensionally" configurable.

<div class="contents" id="bkmrk-first%2C-the-framework"><div class="contents"><div class="textblock">- First, the framework is structured in various **realms**.
- Second, for every realm the **level of detail** (FeatStd::Diagnostics::LogLevel::Enum, Candera::Diagnostics::LogLevel::Enum) can be chosen separately.

</div></div></div>Free configurable **appenders** decide which channel(s) should be used to output the log messages.   
See [FeatStd::Diagnostics::Appender](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_appender.html) (and subclasses) for details.

#####   
**Configure Log Output** 

##### <a class="anchor" id="bkmrk--6"></a>Default Log Output

By default, [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") produces logging output with LogLevel "Warning" for all realms on the console. This chapter explains how to adapt those default logging configuration in an application.

To configure logging support include *Log.h* in your program.

##### <a class="anchor" id="bkmrk--7"></a>Set LogRealm and LogLevel

To modify LogLevel for one, more, or even all realms, use

<div class="contents" id="bkmrk-featstd%3A%3Adiagnostics"><div class="contents"><div class="textblock">- [FeatStd::Diagnostics::LogControl::SetLogLevel&lt;LogRealm&gt;()](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_log_control.html#aad6be2658a44e5710d395b3a0d4b1649).
- The convenience function [FeatStd::Diagnostics::LogControl::SetLogLevel()](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_log_control.html#aad6be2658a44e5710d395b3a0d4b1649) sets the requested level for all realms.

</div></div></div>For a list of all LogRealms supported by FeatStd, refer to *FeatStd/Diagnostics/LogRealm.h*:

```
FEATSTD_LOG_DECLARE_REALM(FeatStdSystem);
FEATSTD_LOG_DECLARE_REALM(FeatStdPlatform);
FEATSTD_LOG_DECLARE_REALM(FeatStdIO);
FEATSTD_LOG_DECLARE_REALM(FeatStdMemoryManagement);
FEATSTD_LOG_DECLARE_REALM(FeatStdMonitor);
FEATSTD_LOG_DECLARE_REALM(FeatStdAsync);
```

Use the macro FEATSTD\_LOG\_SET\_REALM to activate a realm (see also chapter <span style="color: rgb(230, 126, 35);">[Custom Log Output](#bkmrk-custom-log-output%C2%A0)</span>), e.g. FeatStd class FileAppender will produce logging output under the realm "FeatStdSystem", because of the following declaration:

```
class FileAppender : public Appender
{
    typedef Appender Base;

    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ga1c81a9c5191e30fa4d7119772b6edb47">FEATSTD_LOG_SET_REALM</a>(Diagnostics::LogRealm::FeatStdSystem);
```

For each realm different log levels can be set, see log levels defined in FeatStd/Diagnostics/Loglevel.h.

```
enum Enum
{
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0ab1ef5245ddb4e605054fc70baa0d1f96" title="All.">All</a> = 0,        
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0af5dfc3720ca935220d7c86cf1bb2aa27" title="Debug.">Debug</a> = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0ab1ef5245ddb4e605054fc70baa0d1f96" title="All.">All</a>,    
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0a4022bc360aafc7b5692c92639c5d975b" title="Info.">Info</a> = 1,       
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0a87cb3271087cc2730a2062f0e5c990e6" title="Warning = Default.">Warning</a> = 2,    
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0a0fd982e4d51e3751a94e4fd79188c009" title="Error.">Error</a> = 3,      
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0ad18182830c1767d5dd8a8ad2f72af949" title="Fatal.">Fatal</a> = 4,      
    Off = 5         
};
```

Use [FeatStd::Diagnostics::LogControl::SetLogLevel&lt;LogRealm&gt;()](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_log_control.html#aad6be2658a44e5710d395b3a0d4b1649) to set the desired log level.

```
    LogControl::SetAppender(&CgiAppLogAppender::GetCgiAppLogAppenderInstance());
    LogControl::SetLogLevel<CgiAppLog>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0ab1ef5245ddb4e605054fc70baa0d1f96" title="All.">LogLevel::All</a>);
```

<div class="contents" id="bkmrk-the-default-log-leve"><div class="contents"><div class="textblock"><div class="fragment">  
</div><dl class="note"><dd><p class="callout info">The default log level is *Warning*.</p>

</dd></dl></div></div></div>##### <a class="anchor" id="bkmrk--8"></a>Output Channel - Log Appenders

Logging output is handled by instances of [FeatStd::Diagnostics::Appender](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_appender.html) - for this purpose, all [FeatStd::Diagnostics::Logger](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_logger.html) instances share a list of appenders.

FeatStd provides following appenders:

<div class="contents" id="bkmrk-featstd%3A%3Adiagnostics-0"><div class="contents"><div class="textblock">- [FeatStd::Diagnostics::ConsoleAppender](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_console_appender.html)
- [FeatStd::Diagnostics::FileAppender](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_file_appender.html "[FEATSTD_LOG_SET_REALM_H]")
- [FeatStd::UnitTestHelper::MemorizeAppender](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_unit_test_helper_1_1_memorize_appender.html)

</div></div></div>By default, a ConsoleAppender is configured, means once an application doesn't configure anything regarding Logging, log messages from [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") will be sent to the console.

The list of appenders can be modified by [FeatStd::Diagnostics::LogControl::SetAppender](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_log_control.html#a517c5f22e2a3c97d45f15193eb4f5262), AppendAppender and RemoveAppender.

##### <a class="anchor" id="bkmrk--9"></a>Implement a Custom Appender

A custom appender class has to be derived from [FeatStd::Diagnostics::Appender](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_appender.html) and implement the abstract method [FeatStd::Diagnostics::Appender.DoAppend()](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_appender.html#a46e866a6eff038a3a691ec2ee6c4326e).

A custom **CgiAppLogAppender** has been created for modifying the LogLevel::Info output.

```
void CgiAppLogAppender::DoAppend(const LogEvent& logEvent)
{
    // Info traces will be outputted as is, for all others keep ConsoleAppender implementation
    if (logEvent.mLogLevel <= <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0a4022bc360aafc7b5692c92639c5d975b" title="Info.">LogLevel::Info</a>) {
        FeatStd::Internal::Diagnostic::ConsoleOut("%s\n", logEvent.mMessage);
        FeatStd::Internal::Diagnostic::DebuggerOut("%s\n", logEvent.mMessage);
    } else {
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_console_appender.html#a5e13c2d5b02c6e57c228efc31b6fa7a3">FeatStd::Diagnostics::ConsoleAppender::DoAppend</a>(logEvent);
    }
}
```

This custom appender is set to LogControl and activated for all log levels.

```
    LogControl::SetAppender(&CgiAppLogAppender::GetCgiAppLogAppenderInstance());
    LogControl::SetLogLevel<CgiAppLog>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0ab1ef5245ddb4e605054fc70baa0d1f96" title="All.">LogLevel::All</a>);
```

##### **Custom Log Output** 

##### <a class="anchor" id="bkmrk--10"></a>Player Logging Support

This chapter explains how to create logging output from a custom application.

Include *Log.h* in your program.

##### <a class="anchor" id="bkmrk--11"></a>Default Application Realm

FeatStd predefines a default application realm:

```
FEATSTD_LOG_DECLARE_REALM(User);
```

To activate this realm for an application, use the macro [FEATSTD\_LOG\_SET\_REALM()](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ga1c81a9c5191e30fa4d7119772b6edb47) with the default application realm as parameter:

```
<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ga1c81a9c5191e30fa4d7119772b6edb47">FEATSTD_LOG_SET_REALM</a>(LogRealm::User);
```

##### <a class="anchor" id="bkmrk--12"></a>Produce Logging Output

Output itself is done by calling level specific **FEATSTD\_LOG\_&lt;level&gt;** macros in your code:

<div class="contents" id="bkmrk-featstd_log_debug%28.."><div class="contents"><div class="textblock">- FEATSTD\_LOG\_DEBUG(...)
- FEATSTD\_LOG\_INFO(...)
- FEATSTD\_LOG\_WARN(...)
- FEATSTD\_LOG\_ERROR(...)
- FEATSTD\_LOG\_FATAL(...)

</div></div></div>FEATSTD\_LOG\_&lt;level&gt; macros provide a **printf-like** interface, which means that a format string has to be given as first argument, followed by a variable number of arguments (referenced in the format string).

  
**FEATSTD\_LOG\_INFO()** example when loading a Scene.

```
bool CgiApp::LoadScene(const Char* sceneName) {
    FEATSTD_LOG_INFO(" -> Loading scene '%s'", sceneName);
```

**FEATSTD\_LOG\_ERROR()** example when loading a asset configuration fails.

```
    bool result =  LoadAssetConfig();
    if (!result) {
        FEATSTD_LOG_ERROR("\n%s\n", m_loadError);
        m_assetConfig.ClearRepositoryList();
    }
```

<div class="contents" id="bkmrk-the-length-of-the-as"><div class="contents"><div class="textblock"><div class="fragment">  
</div><dl class="note"><dt></dt><dd><p class="callout info">The length of the assembled information **must be** less than 511 characters!</p>

</dd></dl></div></div></div>##### <a class="anchor" id="bkmrk--13"></a>Define Custom Realms

For simple applications the usage of the predefined realm **LogRealm::User** may suffice but more sophisticated applications possibly want to structure the output by further realms.

To do so, first define a [LogRealm](http://dev.doc.cgistudio.at/APILINK/namespace_log_realm.html "[COURIER_LogRealm_Declare]") class by using the FEATSTD\_LOG\_DECLARE\_REALM(name\_of\_the\_realm) macro.

```
#include <FeatStd/FeatStd.h>

namespace CgiApplication {

FEATSTD_LOG_DECLARE_REALM(CgiAppLog);
FEATSTD_LOG_DECLARE_REALM(TutorialWidgets);
```

See LightPlayer/CgiAppLogRealm.\[h|cpp\] for details.

<div class="contents" id="bkmrk-the-new-realm-doesn%27"><div class="contents"><div class="textblock"><dl class="note"><dt></dt><dd><p class="callout info">The new realm doesn't take part in any iteration functionality (e.g. [FeatStd::Diagnostics::LogControl::SetLogLevel()](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_diagnostics_1_1_log_control.html#aad6be2658a44e5710d395b3a0d4b1649)) until the realm (Logger) is used a first time (instantiated).</p>

</dd></dl></div></div></div>So it is good practice to implement an initialization function (using FEATSTD\_LOG\_DEFINE\_REALM(name\_of\_the\_realm))

```
void CgiApp_LoggerInitialization()
{
    FEATSTD_LOG_DEFINE_REALM(CgiAppLog);
    FEATSTD_LOG_DEFINE_REALM(TutorialWidgets);
}
```

and to call it at an early point in program execution.

Finally, the new custom realms must be set for the application, which shall log on that realm:

```
<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ga1c81a9c5191e30fa4d7119772b6edb47">FEATSTD_LOG_SET_REALM</a>(CgiAppLog);
```

Use an appropriate **FEATSTD\_LOG\_&lt;level&gt;** macro to output the message.

# MemoryPool

#### <a class="anchor" id="bkmrk--3"></a>Description

FeatStd::MemoryPool is a dynamic memory management infrastructure with following high level features:

<div class="contents" id="bkmrk-provide-replacement-"><div class="contents"><div class="textblock">- Provide replacement for Standard C library heap
- Multi instance support 
    - Dedicated MemoryPool instances for specific components
- Flexible operation modes 
    - Separation of transient and permanent allocations
    - Enforce permanent allocations only (no fragmentation)
- Minimal runtime and resource overhead
- Multi threading support
- Debugging facilities 
    - Memory guards to detect memory overwrites
    - Leak detection (dump currently allocated blocks with context information)
- Analysis support 
    - Logging of heap operations
    - Statistics

</div></div></div>#### **Build Setup and MemoryPool Features** 

##### <a class="anchor" id="bkmrk--5"></a>Build Setup

MemoryPool source code is located under src/FeatStd/MemoryPool. MemoryPool is an optional feature of FeatStd and can be enabled with the CMake flag FEATSTD\_MEMORYPOOL\_ENABLED.

##### <a class="anchor" id="bkmrk--6"></a>MemoryPool Features

MemoryPool supports a number of diagnostic and debugging facilities. The facilities can be enabled or disabled at compile time. Following CMake variables can be used to configure MemoryPool features for specific build modes:

<div class="contents" id="bkmrk-featstd_memorypool_f"><div class="contents"><div class="textblock">- [FEATSTD\_MEMORYPOOL\_FEATURES\_DEBUG](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga7b37600de43a8962eb4f86b0f21ac7fe)
- [FEATSTD\_MEMORYPOOL\_FEATURES\_RELEASE](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga6eb3fc57a40f023b20eb274f1dd2a55d)
- [FEATSTD\_MEMORYPOOL\_FEATURES\_MINSIZEREL](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#gab1fe66064b9475ef957d4112b323144b)
- [FEATSTD\_MEMORYPOOL\_FEATURES\_RELWITHDEBINFO](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga78929ec25b1dd332c882edbd0fa6801d)

</div></div></div>Each of these variables can be set to enable one or more of the following features:

<div class="contents" id="bkmrk-featstd_memorypool_l"><div class="contents"><div class="textblock">- [FEATSTD\_MEMORYPOOL\_LEAK\_DETECTION](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaa25045f0ddce6abba2edabb06be2b94d)
- [FEATSTD\_MEMORYPOOL\_STATISTICS](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga6e2ced08d04b9c686b0b832bf0f0936d)
- [FEATSTD\_MEMORYPOOL\_MEMGUARDS](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gacd4045fb9315bdb3d40bc0970d3a8150)
- [FEATSTD\_MEMORYPOOL\_ERASE\_BUFFERS](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaae2a2dac88b65886936199eb92454cd7)

</div></div></div>A reasonable feature setup is:

<div class="contents" id="bkmrk-featstd_memorypool_f-0"><div class="textblock">- [FEATSTD\_MEMORYPOOL\_FEATURES\_DEBUG](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga7b37600de43a8962eb4f86b0f21ac7fe) = (FEATSTD\_MEMORYPOOL\_LEAK\_DETECTION | FEATSTD\_MEMORYPOOL\_STATISTICS | FEATSTD\_MEMORYPOOL\_MEMGUARDS | FEATSTD\_MEMORYPOOL\_ERASE\_BUFFERS)
- [FEATSTD\_MEMORYPOOL\_FEATURES\_RELEASE](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga6eb3fc57a40f023b20eb274f1dd2a55d) = FEATSTD\_MEMORYPOOL\_MEMGUARDS
- [FEATSTD\_MEMORYPOOL\_FEATURES\_MINSIZEREL](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#gab1fe66064b9475ef957d4112b323144b) =
- [FEATSTD\_MEMORYPOOL\_FEATURES\_RELWITHDEBINFO](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___c_o_n_f_i_g.html#ga78929ec25b1dd332c882edbd0fa6801d) = FEATSTD\_MEMORYPOOL\_MEMGUARDS

</div></div>####   
**Using MemoryPoo**l 

##### <a class="anchor" id="bkmrk--7"></a>Main MemoryPool Interfaces

##### <a class="anchor" id="bkmrk--8"></a>Allocate And Deallocate Memory

The main interfaces to MemoryPool are following macros.

<div class="contents" id="bkmrk-featstd_memorypool_a"><div class="contents"><div class="textblock">- [FEATSTD\_MEMORYPOOL\_ALLOC](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga39620312d035076ed361e10bff45120b)
- [FEATSTD\_MEMORYPOOL\_TRANSIENT\_ALLOC](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf5fb87570fc930b1483ff87ce9c584bd)
- [FEATSTD\_MEMORYPOOL\_PERMANENT\_ALLOC](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga438cdd64c50492d159eb19f214936ab2)
- [FEATSTD\_MEMORYPOOL\_REALLOC](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga5498eb96f3a1e3356c72c43321e6a1d6)
- [FEATSTD\_MEMORYPOOL\_GLOBAL\_REALLOC](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga709460dbba22f0efbba792314d2c0465)
- [FEATSTD\_MEMORYPOOL\_FREE](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gacb75dbf105ef52745caeaac305e67bc8)
- [FEATSTD\_MEMORYPOOL\_GLOBALFREE](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga0c0855b41b6ce4ae00a081c3717b7907)
- [FEATSTD\_MEMORYPOOL\_NEW](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga834b142630d8d2f3c5a628ca343d854c)
- [FEATSTD\_MEMORYPOOL\_TRANSIENT\_NEW](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga8ecc45d1102edfae79d6f95f2411b12f)
- [FEATSTD\_MEMORYPOOL\_PERMANENT\_NEW](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gacb81a451865cf969b951e9780665be68)
- [FEATSTD\_MEMORYPOOL\_DELETE](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga4318d34872b8385f1bb1d72653d27175)
- [FEATSTD\_MEMORYPOOL\_ARRAY\_NEW](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga3c96cc3c7a66acc87cb2d584f5e5e6aa)
- [FEATSTD\_MEMORYPOOL\_ARRAY\_NEW\_WITH\_PRESET](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12c99fb19fe3895347b487458ca620c9)
- [FEATSTD\_MEMORYPOOL\_ARRAY\_DELETE](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gacf13ace7de81b15a73e78aba4c9ff631)

</div></div></div>The first parameter to most macros is MemRealm. As MemoryPool supports multiple instances (heaps), allocation requests always have to specify the pool the allocation should be performed from. MemRealm is the "name" of a specific MemoryPool that is defined in the system.

##### <a class="anchor" id="bkmrk--9"></a>MemoryPool Declaration

To define a memory pool, following macros can be used:

<div class="contents" id="bkmrk-featstd_memorypool_d"><div class="contents"><div class="textblock">- [FEATSTD\_MEMORYPOOL\_DEFINE\_MANAGED\_MEMORYPOOL](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gac3f664163c8c312c9d58c7650edded2c)
- [FEATSTD\_MEMORYPOOL\_DEFINE\_MEMORYPOOL](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gac210b01392bc436f906528b33bab3f2e)
- [FEATSTD\_DEFINE\_MEMORYPOOLHEAP](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gae025fc330708fade2409bc1291387839)

</div></div></div>##### <a class="anchor" id="bkmrk--10"></a>MemoryPool Configuration

Following macros can be used to create a MemoryPool configuration:

<div class="contents" id="bkmrk-featstd_memorypool_c"><div class="contents"><div class="textblock">- [FEATSTD\_MEMORYPOOL\_CONFIG\_INITIALIZER](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22)
- [FEATSTD\_MEMORYPOOL\_CONFIG](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf63785bfbe0fb16dcb8b37ebc7e1124e)
- [FEATSTD\_MEMORYPOOL\_BIN\_SPECIFICATION](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8)
- [FEATSTD\_MEMORYPOOL\_BUFFER\_PRESET](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828)

</div></div></div>##### <a class="anchor" id="bkmrk--11"></a>Usage Sample

Following code illustrates how to transient allocate 20 bytes of memory from a MemoryPool called MallocBackingHeapPool.

```
    void *p = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga39620312d035076ed361e10bff45120b">FEATSTD_MEMORYPOOL_ALLOC</a>(MallocBackingHeapPool, MemAttrib::Transient, 20);
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gacb75dbf105ef52745caeaac305e67bc8">FEATSTD_MEMORYPOOL_FREE</a>(MallocBackingHeapPool, p);
```

<div class="contents" id="bkmrk-please-refer-to-late"><div class="textblock"><div class="fragment">  
</div><dl class="note"><dt></dt><dd><p class="callout info">please refer to later tutorial chapters (<span style="color: rgb(230, 126, 35);">[MemoryPool Sample Application](#bkmrk-memorypool-sample-ap)</span>) for instructions how to use these macros.</p>

</dd></dl></div></div>####   
**MemoryPool Sample Application** 

##### <a class="anchor" id="bkmrk--12"></a>Introduction

The MemoryPool sample application shows several use cases and aspects of MemoryPool. Please refer to the sub sections below for more detail.

The sample application can be found in folder *featstd/src/MemoryPoolSample*.

##### **Configuration of FeatStd::DefaultMemoryPool** 

##### <a class="anchor" id="bkmrk--14"></a>Introduction

Once FeatStd::MemoryPool feature has been enabled in the build system, it is mandatory to initialize FeatStd::DefaultMemoryPool accordingly. Enabling FeatStd::MemoryPool feature without any further action will lead to an unresolved FeatStd::InitDefaultMemoryPool symbol error in the link step of the software build.

FeatStd::DefaultMemoryPool is the memory pool which FeatStd library components use to allocate memory from. Memory allocated with FEATSTD\_NEW, FEATSTD\_NEW\_ARRAY, FEATSTD\_NEW\_PRESET\_ARRAY, FEATSTD\_ALLOC, and FEATSTD\_REALLOC will use FeatStd::DefaultMemoryPool to allocate the memory. Same applies to the allocation interfaces in class [FeatStd::MemoryManagement::PlatformHeap](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_platform_heap.html "This heap policy delegates heap creation to the platform interface MemoryPlatform.h. The memory platform functions defined there are used for any heap requests.").

##### <a class="anchor" id="bkmrk--15"></a>FeatStd::InitDefaultMemoryPool

The sample code shows how to implement FeatStd::InitDefaultMemoryPool and to configure FeatStd::DefaultMemoryPool. The code referenced in this tutorial can be found in FeatStd/src/MemoryPoolSample/FeatStdDefaultMemoryPool.cpp

Initializing a MemoryPool is in most cases a two stage process

<div class="contents" id="bkmrk-initialize-the-backi"><div class="contents"><div class="textblock">- initialize the backing heap for the memory pool
- initialize the memory pool

</div></div></div>First have a look at FeatStd::InitDefaultMemoryPool sample implementation:

```
namespace FeatStd {

    // ------------------------------------------------------------------------
    bool <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga9b6dbeb64ef3200d5bf1f8fa0ca1f167">InitDefaultMemoryPool</a>()
    {
        // FeatStd will invoke this function before main function is entered
        // initialize FeatStd::DefaultMemoryPool

        using namespace MemoryManagement;
        // IntrinsicBackingHeap defines a BackingHeap with an intrinsic memory buffer that
        // will be used by this heap. The size of the memory buffer is specified as template
        // parameter (here 1MB)
        typedef IntrinsicBackingHeap<DefaultMemoryPool, 1 * 1024 * 1024> Heap;

        // define the configuration for DefaultMemoryPool.
        static const MemoryPoolConfig config = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
            config,                             // name of the variable receiving the configuration
            "FeatStdDefaultMemoryPool",         // the name of the MemoryPool
            <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolFlags::PermanentOnly</a>,     // Flags that control the memory pool behavior
            128,                                // all bins up to 128 bytes are small bins
            cPoolBinSpecs,                      // the bin configurations
            cBufferPresets,                     // preset allocations
            Heap,                               // type name of the backing heap
            &SampleApp::OnOutOfMemory,          // optional the out of memory function
            &OnDestroy                          // optional OnDestroy function
        );

        // initialize first the backing heap and then the memory pool associated with the backing heap
        return Heap::Init() && DefaultMemoryPool::Init(&config);
    }
}
```

The FeatStd::InitDefaultMemoryPool has to be implemented in namespace FeatStd. The function will be either invoked when FeatStd::DefaultMemoryPool receives the first allocation request or latest before main function is entered. This typically ensures that the function is executed in a single threaded environment, which in fact is a requirement for every MemoryPool initialization.

Memory allocations before main function may happen in the progress of C++ static objects construction.

Likewise FeatStd::DefaultMemoryPool will be initialized automatically, it will also be destroyed automatically at the latest possible point of time but not before main function has exit.

Back to FeatStd::InitDefaultMemoryPool sample implementation. First initialization step is to setup a Backing Heap. The sample implementation uses a [FeatStd::MemoryManagement::IntrinsicBackingHeap](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_intrinsic_backing_heap.html). This Backing Heap implementation defines its own intrinsic static buffer which will be used as heap. The heap is configured with 1MB RAM and "personalized" for FeatStd::DefaultMemoryPool.

The second step is to create the FeatStd::InitDefaultMemoryPool configuration. A FeatStd::MemoryPool is configured at runtime with a [FeatStd::MemoryManagement::MemoryPoolConfig](http://dev.doc.cgistudio.at/APILINK/struct_feat_std_1_1_memory_management_1_1_memory_pool_config.html) structure. The FEATSTD\_MEMORYPOOL\_CONFIG\_INITIALIZER or FEATSTD\_MEMORYPOOL\_CONFIG macros should be used to initialize this structure correctly.

The final step of FeatStd::InitDefaultMemoryPool implementation is to execute the [FeatStd::MemoryManagement::IntrinsicBackingHeap](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_intrinsic_backing_heap.html) and FeatStd::DefaultMemoryPool init functions.

#####   
**MemoryPool Configuration** 

In order to configure MemoryPools, the allocation footprint of the application needs to be analysed. Various MemoryPool diagnostic tools and statistics support in this process. A rough outline of the process is

<div class="contents" id="bkmrk-instrumentation-of-t"><div class="contents"><div class="textblock">- <span style="color: rgb(230, 126, 35);">[Instrumentation of the application with StatisticsFileDumper and PermanentBlockFileDumper](#bkmrk-instrumentation-of-t-0)</span>
- Adaptation of StatisticsFileDumper and PermanentBlockFileDumper to the platform capabilities (e.g. if no file system is available)
- <span style="color: rgb(230, 126, 35);">[Stage 1 - permanent block analysis with a minimal configuration](#bkmrk-stage-1---permanent-)</span>
- Use MemoryPoolStatisticsAnalysis.xlsm to analyse application allocation footprint
- <span style="color: rgb(230, 126, 35);">[Stage 2 - Create bin configuration according to results of the analysis](#bkmrk-stage-2---create-bin)</span>

</div></div></div>The single steps of the configuration process can also be seen from MemoryPoolSample/Configuration.cpp

<div class="contents" id="bkmrk-see-also%3A-how-does-t"><div class="contents"><div class="textblock"><dl class="see"><dt>**See also:**</dt><dd><span style="color: rgb(230, 126, 35);">[How does the number of bins influence system performance?](#bkmrk-how-does-the-number-)</span></dd></dl></div></div></div>##### <a class="anchor" id="bkmrk--16"></a>Instrumentation of the application with StatisticsFileDumper and PermanentBlockFileDumper

MemoryPoolSample application not only shows the usage of MemoryPool, but also includes utility code that can be re-used in applications.

The classes PermanentBlockFileDumper and StatisticsFileDumper can be easily incorporated in any application

```
class PermanentBlockFileDumper {
    public:
        ~PermanentBlockFileDumper();

        template<typename MemRealm> static bool Dump(const FeatStd::Char *fileName) {
            PermanentBlockFileDumper pbd;
            <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_concrete_pool_mgmt_object.html">FeatStd::MemoryManagement::ConcretePoolMgmtObject<MemRealm></a> pmo;
            return pbd.Init(fileName) &&
                   pmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_pool_mgmt_object.html#a2faf79c497767bfd6e733761a67d191d">VisitBins</a>(&BinVisitor, &pbd);
        }

        static bool Dump(const FeatStd::Char *fileName);

    private:
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___p_l_a_t_f_o_r_m.html#ga9bae1466e761591337f63a866fb8d7d1" title="Typedef for the file handle. Implemented as a pointer to a FILE struct for now.">FeatStd::FileHandle</a> mFileHdl;

        PermanentBlockFileDumper();

        bool <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a>(const FeatStd::Char *fileName);

        static bool PoolVisitor(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_pool_mgmt_object.html">FeatStd::MemoryManagement::PoolMgmtObject</a> &pmo, void *userData);
        static bool BinVisitor(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_bin_mgmt_object.html">FeatStd::MemoryManagement::BinMgmtObject</a> &bmo, void *userData);
        static bool BlockVisitor(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> &bmo, void *userData);

        bool DoDump(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> &bmo);

        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#ga7183fcdda489f336a03c35283aff2db1">FEATSTD_MAKE_CLASS_UNCOPYABLE</a>(PermanentBlockFileDumper);
};
```

```
class StatisticsFileDumper {
    public:
        ~StatisticsFileDumper();

        static bool Dump(const FeatStd::Char *poolStatsFileName, const FeatStd::Char *binStatsFileName) {
            StatisticsFileDumper sfd;
            return sfd.Init(poolStatsFileName, binStatsFileName) &&
                   <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_memory_pool_manager.html#af428957681a720867543e10e31600987">FeatStd::MemoryManagement::MemoryPoolManager::DumpStatistics</a>(&DumpPoolStatistics, &DumpBinStatistics, &sfd);
        }

        template<typename MemRealm> static bool Dump(const FeatStd::Char *poolStatsFileName, const FeatStd::Char *binStatsFileName) {
            StatisticsFileDumper sfd;
            return sfd.Init(poolStatsFileName, binStatsFileName) &&
                   FeatStd::MemoryManagement::MemoryPoolManager::DumpStatistics<MemRealm>(&DumpPoolStatistics, &DumpBinStatistics, &sfd);
        }

    private:
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___p_l_a_t_f_o_r_m.html#ga9bae1466e761591337f63a866fb8d7d1" title="Typedef for the file handle. Implemented as a pointer to a FILE struct for now.">FeatStd::FileHandle</a> mHdlPoolStats;          
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___p_l_a_t_f_o_r_m.html#ga9bae1466e761591337f63a866fb8d7d1" title="Typedef for the file handle. Implemented as a pointer to a FILE struct for now.">FeatStd::FileHandle</a> mHdlBinStats;           

        StatisticsFileDumper();

        bool <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a>(const FeatStd::Char *poolStatsFileName, const FeatStd::Char *binStatsFileName);

        static bool DumpPoolStatistics(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_pool_mgmt_object.html">FeatStd::MemoryManagement::PoolMgmtObject</a> &pmo, void *userData);

        static bool DumpBinStatistics(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_bin_mgmt_object.html">FeatStd::MemoryManagement::BinMgmtObject</a> &bmo, void *userData);

        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#ga7183fcdda489f336a03c35283aff2db1">FEATSTD_MAKE_CLASS_UNCOPYABLE</a>(StatisticsFileDumper);
};
```

Both classes will write diagnostic and statistic information in CSV format to text files. if the target platform does not support file system, the classes need to be adapted. It is also possible to run the analysis in host environment with certain limitations (differing platform abstraction layers, potential size differences of complex types due to different tool chains).

PermanentBlockFileDumper and StatisticsFileDumper use MemoryPool functionality that is only present when FEATSTD\_MEMORYPOOL\_STATISTICS\_ENABLED and FEATSTD\_MEMORYPOOL\_LEAK\_DETECTION\_ENABLED is enabled.

##### <a class="anchor" id="bkmrk--17"></a>Stage 1 - permanent block analysis with a minimal configuration

The first stage in MemoryPool configuration is to start off with a very simple configuration.

```
static const BinSpecification cStageOneBinSpecs[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 4),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1024),

    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 0)
};

// define the configuration for DefaultMemoryPool.
static const MemoryPoolConfig cStageOneConfig = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
    cStageOneConfig,                    // name of the variable receiving the configuration
    "ConfigSampleStageOneMemoryPool",   // the name of the MemoryPool
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolFlags::PermanentOnly</a>,     // Flags that control the memory pool behavior
    4,                                  // all bins up to 128 bytes are small bins
    cStageOneBinSpecs,                  // the bin configurations
    0,                                  // preset allocations
    ConfigSampleHeap,                   // type name of the backing heap
    0,                                  // optional the out of memory function
    0                                   // no OnDestroy function
);
```

The goal of the permanent block analysis is to get an allocation profile of the application. Thus the configuration sets FeatStd::MemoryManagement::MemoryPoolFlags::PermanentOnly. This will enforce that allocation requests will be permanent allocations. Thus memory will never be returned to the backing heap, but blocks will be maintained in the bin free lists. MemoryPool supports to dump all permanent blocks of a memoryPool (this is exactly the main task of PermanentBlockFileDumper).

Thus dumping the permanent blocks after running the application will give a good overview on the allocation profile of the application.

<div class="contents" id="bkmrk-each-memorypool-conf"><div class="contents"><div class="textblock"><dl class="note"><dt></dt><dd><p class="callout info">each MemoryPool configuration must have at least one small block and one large block bin. A limit of 255 bins per MemoryPool applies.</p>

</dd></dl></div></div></div>##### <a class="anchor" id="bkmrk--18"></a>Use MemoryPoolStatisticsAnalysis.xlsm to analyse applicaton allocation footprint

PermanentBlockFileDumper and StatisticsFileDumper dump data to three CSV formatted files.

```
    // use StatisticFileDumper to dump the statistics to files
    StatisticsFileDumper::Dump("MemoryPoolStatistics.csv", "MemoryPoolBinStatistics.csv");
```

```
    // dump permanent block list
    PermanentBlockFileDumper::Dump("PermanentBlockList.csv");
```

<div class="contents" id="bkmrk-memorypoolstatistics"><div class="contents"><div class="textblock"><div class="fragment">  
</div>- MemoryPoolStatistics.csv contains the overall MemoryPool statistics
- MemoryPoolBinStatistics.csv contains the statistics of the bins
- PermanentBlockList.csv contains the list of permanent blocks

</div></div></div>The CSV formatted files now can be imported to MemoryPoolStatisticsAnalysis.xlsm (can be found in supplement/MemoryPool folder)

Open the Excel document (enable external content to enable external data sources and macros). The Excel document contains 4 sheets

<div class="contents" id="bkmrk-poolanalysis-poolbin"><div class="contents"><div class="textblock">1. PoolAnalysis
2. PoolBinAnalysis
3. PermanentBlockAnalysis
4. ResourceReq

</div></div></div>The first three sheets contain update buttons that will import data and update the Pivot tables.

<div class="contents" id="bkmrk-memorypoolstatistics-0"><div class="contents"><div class="textblock">- MemoryPoolStatistics.csv gets imported to sheet PoolAnalysis
- MemoryPoolBinStatistics.csv gets imported to sheet PoolBinAnalysis
- PermanentBlockList.csv gets imported to sheet PermanentBlockAnalysis

</div><div class="textblock"><dl class="note"><dd><p class="callout info">the numbers shown in this tutorial origin from MemoryPoolSample. Numbers might vary from actual program output.</p>

</dd></dl></div></div></div>After importing the data sheet PermanentBlockAnalysis will show following Pivot table data. The Pivot table will group the permanent block list by BinIndex and BufferSize (and context, which is currently out of interest). Actually the list already represents an "optimal" configuration of bin sizes (Bin configuration).

<div class="contents" id="bkmrk-buffersize-blockcoun"><div class="contents"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><th>BufferSize</th><th>BlockCount</th><th>BufferSize</th><th>BlocksSize/Diag</th><th>Overhead/Diag</th><th>BlockSize</th><th>Overhead</th><th>Allocated</th></tr><tr><td>4</td><td>7</td><td>28</td><td>56</td><td>28</td><td>140</td><td>112</td><td>7</td></tr><tr><td>12</td><td>5</td><td>60</td><td>100</td><td>40</td><td>160</td><td>100</td><td>5</td></tr><tr><td>16</td><td>4</td><td>64</td><td>96</td><td>32</td><td>144</td><td>80</td><td>4</td></tr><tr><td>20</td><td>7</td><td>140</td><td>196</td><td>56</td><td>280</td><td>140</td><td>7</td></tr><tr><td>24</td><td>4</td><td>96</td><td>128</td><td>32</td><td>176</td><td>80</td><td>4</td></tr><tr><td>28</td><td>8</td><td>224</td><td>288</td><td>64</td><td>384</td><td>160</td><td>8</td></tr><tr><td>32</td><td>8</td><td>256</td><td>320</td><td>64</td><td>416</td><td>160</td><td>8</td></tr><tr><td>36</td><td>8</td><td>288</td><td>352</td><td>64</td><td>448</td><td>160</td><td>8</td></tr><tr><td>40</td><td>6</td><td>240</td><td>288</td><td>48</td><td>360</td><td>120</td><td>6</td></tr><tr><td>44</td><td>7</td><td>308</td><td>364</td><td>56</td><td>448</td><td>140</td><td>7</td></tr><tr><td>48</td><td>2</td><td>96</td><td>112</td><td>16</td><td>136</td><td>40</td><td>2</td></tr><tr><td>52</td><td>9</td><td>468</td><td>540</td><td>72</td><td>648</td><td>180</td><td>9</td></tr><tr><td>56</td><td>6</td><td>336</td><td>384</td><td>48</td><td>456</td><td>120</td><td>6</td></tr><tr><td>60</td><td>3</td><td>180</td><td>204</td><td>24</td><td>240</td><td>60</td><td>3</td></tr><tr><td>64</td><td>11</td><td>704</td><td>792</td><td>88</td><td>924</td><td>220</td><td>11</td></tr><tr><td>68</td><td>2</td><td>136</td><td>152</td><td>16</td><td>176</td><td>40</td><td>2</td></tr><tr><td>88</td><td>1</td><td>88</td><td>96</td><td>8</td><td>108</td><td>20</td><td>1</td></tr><tr><td>108</td><td>1</td><td>108</td><td>116</td><td>8</td><td>128</td><td>20</td><td>1</td></tr><tr><td>116</td><td>2</td><td>232</td><td>248</td><td>16</td><td>272</td><td>40</td><td>2</td></tr><tr><td>120</td><td>1</td><td>120</td><td>128</td><td>8</td><td>140</td><td>20</td><td>1</td></tr><tr><td>124</td><td>1</td><td>124</td><td>132</td><td>8</td><td>144</td><td>20</td><td>1</td></tr><tr><td>200</td><td>1</td><td>200</td><td>208</td><td>8</td><td>220</td><td>20</td><td>1</td></tr><tr><td>244</td><td>1</td><td>244</td><td>252</td><td>8</td><td>264</td><td>20</td><td>1</td></tr><tr><td>276</td><td>1</td><td>276</td><td>284</td><td>8</td><td>296</td><td>20</td><td>1</td></tr><tr><td>284</td><td>2</td><td>568</td><td>584</td><td>16</td><td>608</td><td>40</td><td>2</td></tr><tr><td>296</td><td>1</td><td>296</td><td>304</td><td>8</td><td>316</td><td>20</td><td>1</td></tr><tr><td>300</td><td>1</td><td>300</td><td>308</td><td>8</td><td>320</td><td>20</td><td>1</td></tr><tr><td>312</td><td>1</td><td>312</td><td>320</td><td>8</td><td>332</td><td>20</td><td>1</td></tr><tr><td>340</td><td>1</td><td>340</td><td>348</td><td>8</td><td>360</td><td>20</td><td>1</td></tr><tr><td>344</td><td>1</td><td>344</td><td>352</td><td>8</td><td>364</td><td>20</td><td>1</td></tr><tr><td>352</td><td>2</td><td>704</td><td>720</td><td>16</td><td>744</td><td>40</td><td>2</td></tr><tr><td>356</td><td>1</td><td>356</td><td>364</td><td>8</td><td>376</td><td>20</td><td>1</td></tr><tr><td>380</td><td>1</td><td>380</td><td>388</td><td>8</td><td>400</td><td>20</td><td>1</td></tr><tr><td>392</td><td>1</td><td>392</td><td>400</td><td>8</td><td>412</td><td>20</td><td>1</td></tr><tr><td>396</td><td>1</td><td>396</td><td>404</td><td>8</td><td>416</td><td>20</td><td>1</td></tr><tr><td>404</td><td>1</td><td>404</td><td>412</td><td>8</td><td>424</td><td>20</td><td>1</td></tr><tr><td>420</td><td>1</td><td>420</td><td>428</td><td>8</td><td>440</td><td>20</td><td>1</td></tr><tr><td>472</td><td>1</td><td>472</td><td>480</td><td>8</td><td>492</td><td>20</td><td>1</td></tr><tr><td>496</td><td>1</td><td>496</td><td>504</td><td>8</td><td>516</td><td>20</td><td>1</td></tr><tr><td>504</td><td>1</td><td>504</td><td>512</td><td>8</td><td>524</td><td>20</td><td>1</td></tr><tr><td>508</td><td>1</td><td>508</td><td>516</td><td>8</td><td>528</td><td>20</td><td>1</td></tr><tr><td>1024</td><td>1</td><td>1024</td><td>1032</td><td>8</td><td>1044</td><td>20</td><td>1</td></tr><tr><td>1056</td><td>1</td><td>1056</td><td>1064</td><td>8</td><td>1076</td><td>20</td><td>1</td></tr><tr><td>1632</td><td>1</td><td>1632</td><td>1640</td><td>8</td><td>1652</td><td>20</td><td>1</td></tr></tbody></table>

</div></div></div></div>##### <a class="anchor" id="bkmrk--19"></a>Stage 2 - Create bin configuration according to results of the analysis

##### <a class="anchor" id="bkmrk--20"></a>Transfer Excel data to source file

The first two columns of the table can be easily copied to source code with the clipboard. Visual Studio find &amp; replace can be used to generate a Bin configuration from table of numbers.

```
// these are the first two columns of the Excel sheet PermanentBlockAnalysis (copied with Clipboard)
// use Visual Studio Find&Replace with Regex to convert to bin configuration.
// Search string (regex): "(\d+)\t(\d+)"
// Replace string: "FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, $1), // $2"
static const BinSpecification cStageOneBinSpecs[] = {
4    7
12    5
16    4
20    7
24    4
28    8
32    8
36    8
40    6
44    7
48    2
52    9
56    6
60    3
64    11
68    2
88    1
108    1
116    2
120    1
124    1
200    1
244    1
276    1
284    2
296    1
300    1
312    1
340    1
344    1
352    2
356    1
380    1
392    1
396    1
404    1
420    1
472    1
496    1
504    1
508    1
1024    1
1056    1
1632    1
};
```

After applying regex search &amp; replace, following bin configuration has been created:

```
static const BinSpecification cStageOneBinSpecs[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 4), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 12), // 5
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 16), // 4
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 20), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 24), // 4
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 28), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 32), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 36), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 40), // 6
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 44), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 48), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 52), // 9
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 56), // 6
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 60), // 3
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 64), // 11
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 68), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 88), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 108), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 116), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 120), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 124), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 200), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 244), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 276), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 284), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 296), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 300), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 312), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 340), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 344), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 352), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 356), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 380), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 392), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 396), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 404), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 420), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 472), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 496), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 504), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 508), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1024), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1056), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1632), // 1
};
```

##### <a class="anchor" id="bkmrk--21"></a>Optimize bin configuration

The bin configuration contains now more than 40 bins. As each bin has a memory penalty of 12 bytes (see Excel sheet ResourceReq), the next step is to minimize the number of bins.

For this bins with a low number of buffers are identified. This is the reason to copy the BufferCount column from the Excel sheet too. After applying the regex search &amp; replace, the number of buffers is in the comment after each FEATSTD\_MEMORYPOOL\_BIN\_SPECIFICATION line. The bin with buffer size 48 has two blocks. The next larger bin has buffer size 52. Eliminating the bin with buffer size 48 would implies that the buffers would be allocated from the bin with buffer size 52. The additional overhead for the two 48 bytes buffers are 8 (2 \* 4) bytes. This means that eliminating the bin with buffer size 48 will save 4 bytes of overall memory consumption (12 bytes bin overhead saved minus 8 bytes overhead for the two buffers that will move to the bin with buffer size 52).

The result of bin reduction can be seen below (eliminated bins are commented out).

```
static const BinSpecification cStageTwoBinSpecs[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 4), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 12), // 5
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 16), // 4
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 20), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 24), // 4
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 28), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 32), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 36), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 40), // 6
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 44), // 7
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 48), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 52), // 9
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 56), // 6
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 60), // 3
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 64), // 11
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 68), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 88), // 1
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 108), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 116), // 2
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 120), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 124), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 200), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 244), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 276), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 284), // 2
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 296), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 300), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 312), // 1
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 340), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 344), // 1
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 352), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 356), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 380), // 1
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 392), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 396), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 404), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 420), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 472), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 496), // 1
//    FEATSTD_MEMORYPOOL_BIN_SPECIFICATION(MemoryPoolBinFlags::None, 504), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 508), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1024), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1056), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1632), // 1

    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 0)
};

// define the configuration for DefaultMemoryPool.
static const MemoryPoolConfig cStageTwoConfig = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
    cStageTwoConfig,                    // name of the variable receiving the configuration
    "ConfigSampleStageTwoMemoryPool",   // the name of the MemoryPool
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolFlags::PermanentOnly</a>,     // Flags that control the memory pool behavior
    1056,                               // all bins up to 128 bytes are small bins
    cStageTwoBinSpecs,                  // the bin configurations
    0,                                  // preset allocations
    ConfigSampleHeap,                   // type name of the backing heap
    0,                                  // optional the out of memory function
    0                                   // no OnDestroy function
);
```

##### <a class="anchor" id="bkmrk--22"></a>Create Buffer Presets

The next step is optional and configures the buffer presets. With buffer presets permanent blocks can added to the bin free lists at MemoryPool initialization time. With buffer presets a specific buffer free list layout can be enforced.

Assume the application requires at system startup for whatever reason a 2040 byte buffer. This buffer will be freed as soon the system is setup. Later the application will allocate a 2048 byte buffer. The existing 2040 byte buffer is not suitable for the later 2048 byte allocation request and a new 2048 bytes buffer must be allocated from the BackingHeap. With buffer presets the allocation of a 2048 bytes buffer from BackingHeap can be enforced prior the allocation of the 2040 bytes buffer. Thus the first allocation will use the preset 2048 bytes buffer, and the same buffer can be used for the succeeding later allocations.

After re-running the application with the optimized bin configuration and import of the new PermanentBlockList.csv into PermanentBlockAnalysis sheet, the first two columns (BufferSize and BufferCount) can be copied again to source code. Like in the prior steps, regex search &amp; replace can be used to generate from this table the [FeatStd::MemoryManagement::BufferPreset](http://dev.doc.cgistudio.at/APILINK/struct_feat_std_1_1_memory_management_1_1_buffer_preset.html) table. The table is then input to the final configuration.

```
static const BinSpecification cStageThreeBinSpecs[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 4), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 12), // 5
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 16), // 4
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 20), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 24), // 4
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 28), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 32), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 36), // 8
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 40), // 6
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 44), // 7
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 52), // 9
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 56), // 6
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 64), // 11
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 68), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 88), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 116), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 124), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 200), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 244), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 276), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 284), // 2
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 300), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 312), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 344), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 356), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 380), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 396), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 404), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 420), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 472), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 496), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 508), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1024), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1056), // 1
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1632), // 1

    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 0)
};

// based on re-imported PermanentBlockList.csv
// BufferSize / BufferCount columns
const BufferPreset cStageThreeBufferPresets[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(4, 7),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(12, 5),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(16, 4),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(20, 7),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(24, 4),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(28, 8),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(32, 8),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(36, 8),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(40, 6),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(44, 7),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(52, 11),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(56, 6),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(64, 14),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(68, 2),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(88, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(116, 3),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(124, 2),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(200, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(244, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(276, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(284, 2),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(300, 2),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(312, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(344, 2),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(356, 3),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(380, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(396, 2),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(404, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(420, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(472, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(496, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(508, 2),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(1024, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(1056, 1),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(1632, 1),

    // mandatory last entry
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(0, 0)
};

// define the configuration for DefaultMemoryPool.
static const MemoryPoolConfig cStageThreeConfig = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
    cStageThreeConfig,                  // name of the variable receiving the configuration
    "ConfigSampleStageTwoMemoryPool",   // the name of the MemoryPool
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolFlags::PermanentOnly</a>,     // Flags that control the memory pool behavior
    1056,                               // all bins up to 128 bytes are small bins
    cStageThreeBinSpecs,                // the bin configurations
    cStageThreeBufferPresets,           // preset allocations
    ConfigSampleHeap,                   // type name of the backing heap
    0,                                  // optional the out of memory function
    0                                   // no OnDestroy function
);
```

##### **Application Specific MemoryPool** 

##### <a class="anchor" id="bkmrk--23"></a>MemoryPool declaration

One central feature of MemoryPool is the ability to define multiple MemoryPool instances. The instances are separated by "names" - the MemRealm.

Following statement defines a specific MemoryPool with the MemRealm "AppMemoryPool".

```
extern bool InitAppMemoryPool();

// define an application specific memory pool with realm AppMemoryPool
<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gac3f664163c8c312c9d58c7650edded2c">FEATSTD_MEMORYPOOL_DEFINE_MANAGED_MEMORYPOOL</a>(AppMemoryPool, &InitAppMemoryPool);
```

The InitAppMemoryPool function is invoked when the first allocation on this pool is done or latest before main function will be executed.

This is the whole setup of an application specific MemoryPool. Macros as listed below can ease the use of the specific MemoryPool.

```
// macros to allocate and free memory from AppMemoryPool
#define APP_PERMANENT_ALLOC(nBytes) FEATSTD_MEMORYPOOL_PERMANENT_ALLOC(AppMemoryPool, nBytes)
#define APP_TRANSIENT_ALLOC(nBytes) FEATSTD_MEMORYPOOL_TRANSIENT_ALLOC(AppMemoryPool, nBytes)
#define APP_REALLOC(p, newSize) FEATSTD_MEMORYPOOL_REALLOC(AppMemoryPool, p, newSize)
#define APP_FREE(p) FEATSTD_MEMORYPOOL_GLOBALFREE(p)

// scalar new and delete macros
#define APP_PERMANENT_NEW(type) FEATSTD_MEMORYPOOL_PERMANENT_NEW(AppMemoryPool, type)
#define APP_TRANSIENT_NEW(type) FEATSTD_MEMORYPOOL_TRANSIENT_NEW(AppMemoryPool, type)
#define APP_DELETE(p) FEATSTD_MEMORYPOOL_DELETE(p)

// array new and delete macros
#define APP_PERMANENT_ARRAY_NEW(type, itemCount) \
        FEATSTD_MEMORYPOOL_ARRAY_NEW(AppMemoryPool, FeatStd::MemoryManagement::MemAttrib::Permanent, type, (itemCount))

#define APP_TRANSIENT_ARRAY_NEW(type, itemCount) \
        FEATSTD_MEMORYPOOL_ARRAY_NEW(AppMemoryPool, FeatStd::MemoryManagement::MemAttrib::Transient, type, (itemCount))

#define APP_PERMANENT_ARRAY_INIT_NEW(type, itemCount, preset) \
        FEATSTD_MEMORYPOOL_ARRAY_NEW_WITH_PRESET(AppMemoryPool, FeatStd::MemoryManagement::MemAttrib::Permanent, type, (itemCount), (preset))

#define APP_TRANSIENT_ARRAY_INIT_NEW(type, itemCount, preset) \
    FEATSTD_MEMORYPOOL_ARRAY_NEW_WITH_PRESET(AppMemoryPool, FeatStd::MemoryManagement::MemAttrib::Transient, type, (itemCount), (preset))

#define APP_ARRAY_DELETE(p) FEATSTD_MEMORYPOOL_ARRAY_DELETE(p)
```

##### <a class="anchor" id="bkmrk--24"></a>MemoryPool Initialization

```
static const BinSpecification cPoolBinSpecs[] = {
    // PermanentOnly will force all allocations from these pools to be permanent.
    // This means that the buffer will be added to the bin free list and not
    // returned to the backing heap.
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 8),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 16),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 32),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 64),

    // An OverlowBarrier will stop bins with smaller buffer sizes to allocate
    // from the pools beyond the OverflowBarrier bin.
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72ad9fc04576a26c4e913b0ea6c5f1ee729">MemoryPoolBinFlags::OverflowBarrier</a> | <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 128),

    // GenuineOnly stops temporary allocations to cannibalize permanent buffers
    // in default allocation mode, a transient allocation request can be satisfied
    // by allocating a permanent buffer. With GenuineOnly, permanent buffers will
    // not be used for transient allocations.
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72af34caca275f85110d4cab95eabbb276e">MemoryPoolBinFlags::GenuineOnly</a>, 256),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72af34caca275f85110d4cab95eabbb276e">MemoryPoolBinFlags::GenuineOnly</a>, 512),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72af34caca275f85110d4cab95eabbb276e">MemoryPoolBinFlags::GenuineOnly</a>, 1024),

    // mandatory final entry
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(0, 0)
};

static const BufferPreset cBufferPresets[] = {
    // all bins up to 128 bytes are small bins. Thus buffer size
    // will always round up to bin buffer size.
    //                                  bufferSize    bufferCount
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(       8,            16),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(      16,            16),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(      32,            16),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(      64,            16),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(     128,            16),

    // large bin buffers will have exactly the size requested
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(     300,             1),

    // mandatory final entry
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga12862c40d07ba776d0bd5bc7a17b4828">FEATSTD_MEMORYPOOL_BUFFER_PRESET</a>(0, 0)
};



// ------------------------------------------------------------------------
bool InitAppMemoryPool()
{
    // initialize the memory pool of this sample application

    using namespace FeatStd::MemoryManagement;
    // IntrinsicBackingHeap defines a BackingHeap with an intrinsic memory buffer that
    // will be used by this heap. The size of the memory buffer is specified as template
    // parameter
    typedef <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_intrinsic_backing_heap.html">IntrinsicBackingHeap<AppMemoryPool, 1 * 64 * 1024></a> Heap;

    // define the configuration for DefaultMemoryPool.
    static const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/struct_feat_std_1_1_memory_management_1_1_memory_pool_config.html">MemoryPoolConfig</a> config = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
        config,                             // name of the variable receiving the configuration
        "AppMemoryPool",                    // the name of the MemoryPool
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolFlags::None</a>,              // Flags that control the memory pool behavior
        128,                                // all bins up to 128 bytes are small bins
        cPoolBinSpecs,                      // the bin configurations
        cBufferPresets,                     // preset allocations
        Heap,                               // type name of the backing heap
        &SampleApp::OnOutOfMemory,          // optional the out of memory function
        0                                   // no OnDestroy function
    );

    // initialize first the backing heap and then the memory pool associated with the backing heap
    return Heap::Init() && AppMemoryPool::Init(&config);
}

```

##### **Diagnostic Utilities** 

In <span style="color: rgb(230, 126, 35);">[MemoryPool Configuration](#bkmrk-memorypool-configura)</span> StatisticsFileDumper and PermanentBlockFileDumper have been already briefly discussed. The two utility classes use [FeatStd::MemoryManagement::MemoryPoolManager](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_memory_pool_manager.html) visitor facilities to dump MemoryPool statistics and permanent block list to a file.

The generated files then can be imported to Excel (CSV format) as outlined in <span style="color: rgb(230, 126, 35);">[MemoryPool Configuration](#bkmrk-memorypool-configura)</span>.

The diagnostic utility classes can be incorporated in any application to analyse the application allocation profile.

##### <a class="anchor" id="bkmrk--25"></a>StatisticsFileDumper

StatisticsFileDumper generates two CSV files

<div class="contents" id="bkmrk-one-containing-the-o"><div class="contents"><div class="textblock">- one containing the overall MemoryPool statistics (equivalent to the data that can be found in [FeatStd::MemoryManagement::PoolStatisticsData](http://dev.doc.cgistudio.at/APILINK/struct_feat_std_1_1_memory_management_1_1_pool_statistics_data.html) )
- one containing the bin statistics of all MemoryPools (equivalent to the data that can be found in [FeatStd::MemoryManagement::BinStatisticsData](http://dev.doc.cgistudio.at/APILINK/struct_feat_std_1_1_memory_management_1_1_bin_statistics_data.html) )

</div></div></div><div class="contents" id="bkmrk-all-memorypools-mean"><div class="contents"><div class="contents"><div class="textblock"><dl class="note"><dd><p class="callout info">All MemoryPools means all MemoryPool instances registered at MemoryPoolManager at the time of the dump.</p>

</dd></dl><div class="fragment">  
</div></div></div></div></div>```
class StatisticsFileDumper {
    public:
        ~StatisticsFileDumper();

        static bool Dump(const FeatStd::Char *poolStatsFileName, const FeatStd::Char *binStatsFileName) {
            StatisticsFileDumper sfd;
            return sfd.Init(poolStatsFileName, binStatsFileName) &&
                   <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_memory_pool_manager.html#af428957681a720867543e10e31600987">FeatStd::MemoryManagement::MemoryPoolManager::DumpStatistics</a>(&DumpPoolStatistics, &DumpBinStatistics, &sfd);
        }

        template<typename MemRealm> static bool Dump(const FeatStd::Char *poolStatsFileName, const FeatStd::Char *binStatsFileName) {
            StatisticsFileDumper sfd;
            return sfd.Init(poolStatsFileName, binStatsFileName) &&
                   FeatStd::MemoryManagement::MemoryPoolManager::DumpStatistics<MemRealm>(&DumpPoolStatistics, &DumpBinStatistics, &sfd);
        }

    private:
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___p_l_a_t_f_o_r_m.html#ga9bae1466e761591337f63a866fb8d7d1" title="Typedef for the file handle. Implemented as a pointer to a FILE struct for now.">FeatStd::FileHandle</a> mHdlPoolStats;          
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___p_l_a_t_f_o_r_m.html#ga9bae1466e761591337f63a866fb8d7d1" title="Typedef for the file handle. Implemented as a pointer to a FILE struct for now.">FeatStd::FileHandle</a> mHdlBinStats;           

        StatisticsFileDumper();

        bool <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a>(const FeatStd::Char *poolStatsFileName, const FeatStd::Char *binStatsFileName);

        static bool DumpPoolStatistics(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_pool_mgmt_object.html">FeatStd::MemoryManagement::PoolMgmtObject</a> &pmo, void *userData);

        static bool DumpBinStatistics(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_bin_mgmt_object.html">FeatStd::MemoryManagement::BinMgmtObject</a> &bmo, void *userData);

        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#ga7183fcdda489f336a03c35283aff2db1">FEATSTD_MAKE_CLASS_UNCOPYABLE</a>(StatisticsFileDumper);
};
```

##### <a class="anchor" id="bkmrk--26"></a>PermanentBlockFileDumper

PermanentBlockFileDumper uses [FeatStd::MemoryManagement::BinMgmtObject::VisitPermanentBlocks](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_bin_mgmt_object.html#ac468e690b0e112a41fcb003af47397f2) function to dump the permanent blocks of all MemoryPools to a CSV formatted file.

```
class PermanentBlockFileDumper {
    public:
        ~PermanentBlockFileDumper();

        template<typename MemRealm> static bool Dump(const FeatStd::Char *fileName) {
            PermanentBlockFileDumper pbd;
            <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_concrete_pool_mgmt_object.html">FeatStd::MemoryManagement::ConcretePoolMgmtObject<MemRealm></a> pmo;
            return pbd.Init(fileName) &&
                   pmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_pool_mgmt_object.html#a2faf79c497767bfd6e733761a67d191d">VisitBins</a>(&BinVisitor, &pbd);
        }

        static bool Dump(const FeatStd::Char *fileName);

    private:
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___p_l_a_t_f_o_r_m.html#ga9bae1466e761591337f63a866fb8d7d1" title="Typedef for the file handle. Implemented as a pointer to a FILE struct for now.">FeatStd::FileHandle</a> mFileHdl;

        PermanentBlockFileDumper();

        bool <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_courier.html#a2e6f80d7ef7c7a120595f3d56e6f4246">Init</a>(const FeatStd::Char *fileName);

        static bool PoolVisitor(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_pool_mgmt_object.html">FeatStd::MemoryManagement::PoolMgmtObject</a> &pmo, void *userData);
        static bool BinVisitor(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_bin_mgmt_object.html">FeatStd::MemoryManagement::BinMgmtObject</a> &bmo, void *userData);
        static bool BlockVisitor(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> &bmo, void *userData);

        bool DoDump(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> &bmo);

        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#ga7183fcdda489f336a03c35283aff2db1">FEATSTD_MAKE_CLASS_UNCOPYABLE</a>(PermanentBlockFileDumper);
};
```

<div class="contents" id="bkmrk-memorypool-in-normal"><div class="textblock"><div class="fragment">  
</div><dl class="note"><dt></dt><dd><p class="callout info">MemoryPool in normal operation on tracks free blocks. FEATSTD\_MEMORYPOOL\_LEAK\_DETECTION\_ENABLED must be enabled to include also allocated blocks into the permanent block dump.</p>

</dd></dl></div></div>##### **MallocBackingHeap** 

[FeatStd::MemoryManagement::MallocBackingHeap](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_malloc_backing_heap.html) is a specific backing heap implementation that uses Standard C library malloc, realloc, and free to allocate memory.

Intended use scenarios:

<div class="contents" id="bkmrk-host-development-pro"><div class="contents"><div class="textblock">- Host development
- Prototyping
- Debugging on target to utilize toolchain specific heap analysis functions (e.g. Greenhills heap analyzer)

<div class="fragment">  
</div></div></div></div>```
// ========================================================================

bool InitMallocBackingHeapPool();

<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gac3f664163c8c312c9d58c7650edded2c">FEATSTD_MEMORYPOOL_DEFINE_MANAGED_MEMORYPOOL</a>(MallocBackingHeapPool, &InitMallocBackingHeapPool);

// ========================================================================


static const BinSpecification cPoolBinSpecs[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 8),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 16),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 32),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 64),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72ad9fc04576a26c4e913b0ea6c5f1ee729">MemoryPoolBinFlags::OverflowBarrier</a> | <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolBinFlags::PermanentOnly</a>, 128),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72af34caca275f85110d4cab95eabbb276e">MemoryPoolBinFlags::GenuineOnly</a>, 256),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72af34caca275f85110d4cab95eabbb276e">MemoryPoolBinFlags::GenuineOnly</a>, 512),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72af34caca275f85110d4cab95eabbb276e">MemoryPoolBinFlags::GenuineOnly</a>, 1024),

    // mandatory final entry
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(0, 0)
};

// ------------------------------------------------------------------------
bool InitMallocBackingHeapPool()
{
    using namespace FeatStd::MemoryManagement;

#if defined(FEATSTD_PLATFORM_OS_WIN32)
    // for host development use MallocBackingHeap. MallocBackingHeap will
    // use C Standard Library Heap (malloc, realloc, free). Thus is
    // especially usefull if compiling for SCHost to avoid.
    typedef <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_malloc_backing_heap.html">MallocBackingHeap<MallocBackingHeapPool></a> Heap;
#else
    // for other builds use a fixed size heap
    typedef <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_intrinsic_backing_heap.html">IntrinsicBackingHeap<MallocBackingHeapPool, 1 * 1024></a> Heap;
#endif

    // define the configuration for DefaultMemoryPool.
    static const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/struct_feat_std_1_1_memory_management_1_1_memory_pool_config.html">MemoryPoolConfig</a> config = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
        config,                             // name of the variable receiving the configuration
        "MallocBackingHeapPool",            // the name of the MemoryPool
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolFlags::None</a>,              // Flags that control the memory pool behavior
        128,                                // all bins up to 128 bytes are small bins
        cPoolBinSpecs,                      // the bin configurations
        0,                                  // preset allocations
        Heap,                               // type name of the backing heap
        &SampleApp::OnOutOfMemory,          // optional the out of memory function
        0                                   // no OnDestroy function
    );

    // initialize first the backing heap and then the memory pool associated with the backing heap
    return Heap::Init() && MallocBackingHeapPool::Init(&config);
}

// ------------------------------------------------------------------------
void MallocBackingHeapSample()
{
    void *p = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga39620312d035076ed361e10bff45120b">FEATSTD_MEMORYPOOL_ALLOC</a>(MallocBackingHeapPool, MemAttrib::Transient, 20);
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gacb75dbf105ef52745caeaac305e67bc8">FEATSTD_MEMORYPOOL_FREE</a>(MallocBackingHeapPool, p);
}

```

##### **Memory Leak Analysis** 

MemoryPool has a highly capable memory leak analysis facility. At any time, the application can request to visit the allocated block list. An allocated block list is maintained for each MemoryPool bin.

<div class="contents" id="bkmrk-featstd_memorypool_l-0"><div class="contents"><div class="textblock"><dl class="note"><dt></dt><dd><p class="callout info">FEATSTD\_MEMORYPOOL\_LEAK\_DETECTION\_ENABLED must be enabled to visit the allocated blocks list</p>

</dd></dl></div></div></div>Typically the allocated block list will be dumped in [FeatStd::MemoryManagement::MemoryPoolConfig::mOnDestroy](http://dev.doc.cgistudio.at/APILINK/struct_feat_std_1_1_memory_management_1_1_memory_pool_config.html#a51cdfa6655d89ddc47dc663e97277490 "invoked before the memory gets destroyed") callback function. This ensures that all non-leaked memory has been de-allocated and the remaining allocated blocks may indicate memory leaks.

<div class="contents" id="bkmrk-it-might-be-a-design"><div class="contents"><div class="textblock"><dl class="note"><dt></dt><dd><p class="callout info">It might be a design decision not to de-allocate all memory at system shutdown. Technically a memory leak is given when no references to the allocated buffer exist any more.</p>

</dd></dl></div></div></div>Central classes for MemoryPool analysis are:

<div class="contents" id="bkmrk-featstd%3A%3Amemorymanag-0"><div class="contents"><div class="textblock">- [FeatStd::MemoryManagement::MemoryPoolManager](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_memory_pool_manager.html)
- [FeatStd::MemoryManagement::BlockMgmtObject](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html)
- [FeatStd::MemoryManagement::BinMgmtObject](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_bin_mgmt_object.html)
- [FeatStd::MemoryManagement::PoolMgmtObject](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_pool_mgmt_object.html)
- [FeatStd::MemoryManagement::ConcretePoolMgmtObject](http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_concrete_pool_mgmt_object.html)

</div><div class="textblock"><dl class="note"><dd><p class="callout info">Visiting the allocated block list requires locking of the lists. Thus no memory allocations can be done during visit.</p>

</dd></dl></div></div></div>The following code shows how to trigger an allocated block dump.

```
// ------------------------------------------------------------------------
void MemoryLeakAnalysisSample()
{
#if defined(FEATSTD_MEMORYPOOL_LEAK_DETECTION_ENABLED)
    using FeatStd::Internal::Diagnostic;

    // allocate some memory and fill with data
    Char *str = static_cast<Char*>(APP_TRANSIENT_ALLOC(30));
    if (str == 0) {
        FEATSTD_LOG_FATAL("out of memory");
    }

    FeatStd::Internal::String::Copy(str, "this is a memory leak");

    // now dump the allocated blocks of AppMemoryPool - output will
    // go to Diagnostics::DebuggerOut.  this is the default implementation
    // provided by MemoryPool.
    FeatStd::MemoryManagement::MemoryPoolManager::DumpAllocatedBlocks<AppMemoryPool>();

    // VisualStudio Output pane should have now following output
    // <...>\MemLeakAnalysis.cpp(23): 0018AC28[32+16] AppMemoryPool::2
    //      data < 74 68 69 73 20 69 73 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 00 cd cd > this.is.a.memory.leak...

    // repeat dump of allocated blocks, this time with a user defined visitor function
    LogAppMemoryPoolAllocatedBlocks();

    // BlockMgmtObject can be used to get information on each MemoryPool allocated buffer
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> bmo(str);
    FeatStd::Internal::Diagnostic::DebuggerOut("str has been allocated from pool %s at: %s\n", bmo.GetAssociatedPoolName(), bmo.GetAllocContext());

    // release memory
    APP_FREE(str);
#endif
}
```

Executing the code above in MS Visual Studio will result in following output in Debug Output Pane:

```
s:\featstd\src\MemoryPoolSample\MemLeakAnalysis.cpp(121): 00443DB0[32+16] AppMemoryPool::2
    data < 74 68 69 73 20 69 73 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 00 cd cd > this.is.a.memory.leak...
```

A customized version of an allocated blocks visitor can be seen below.

```
// ------------------------------------------------------------------------
static void LogBlockManagementObject(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> &bmo)
{
    using FeatStd::Internal::Diagnostic;

    // dump information to Diagnostic::DebuggerOut
    Diagnostic::DebuggerOut("%s: %p[%p] %s::%u %s %s [%u+%u]\n",
                            bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a6a86c535895a8dd09eb5109bcbb15cd5">GetAllocContext</a>(),
                            bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a7cf01996c5e321cf692ab78f9155c80b">GetBlockAddress</a>(),
                            bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#afbbd37797928192167fe1e3b25eed403">GetBuffer</a>(),
                            bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a3fe816c50885fd57cdf82b4c06386a30">GetAssociatedPoolName</a>(),
                            bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a442c1733ec18607c8f085f663d572633">GetAssociatedBinIndex</a>(),
                            (bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#aee77051d5939453a1502ebbf39fb0412">IsLargeBlock</a>() ? "large" : "small"),
                            (bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a619cb9e5072d542358f8bd0fdb1b4e93">IsPermanentBlock</a>() ? "permanent" : "transient"),
                            bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#ad184da20e9086e17a869ce5a2b43b65f">GetBufferSize</a>(),
                            bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a1f2397174b6382a69fab06880befdebe">GetBlockSize</a>() - bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#ad184da20e9086e17a869ce5a2b43b65f">GetBufferSize</a>());
}

namespace {
    // Accumulation of information abount allocated blocks
    struct AllocatedBlockData {
        AllocatedBlockData() {
            mBlockCount = 0;
            mAccumulatedBlockSize = 0;
            mAccumulatedBufferSize = 0;
        }

        UInt32 mBlockCount;
        UInt32 mAccumulatedBlockSize;
        UInt32 mAccumulatedBufferSize;
    };
}

#if defined(FEATSTD_MEMORYPOOL_LEAK_DETECTION_ENABLED)
// ------------------------------------------------------------------------
static bool VisitAllocatedBlock(const <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> &bmo, void *userData)
{
    FEATSTD_DEBUG_ASSERT(userData != 0);

    // userData refers to an AllocatedBlockData
    // cast and update AllocatedBlockData with the current block data
    AllocatedBlockData *abd = static_cast<AllocatedBlockData*>(userData);
    ++abd->mBlockCount;
    abd->mAccumulatedBlockSize += bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a1f2397174b6382a69fab06880befdebe">GetBlockSize</a>();
    abd->mAccumulatedBufferSize += bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#ad184da20e9086e17a869ce5a2b43b65f">GetBufferSize</a>();

    // finally log the block info
    LogBlockManagementObject(bmo);

    // always return true (returning false abandons the visit)
    return true;
}
#endif

// ------------------------------------------------------------------------
void LogAppMemoryPoolAllocatedBlocks()
{
    using FeatStd::Internal::Diagnostic;

#if defined(FEATSTD_MEMORYPOOL_LEAK_DETECTION_ENABLED)
    // VisitAllocatedBlock expects a AllocatedBlockData passed with userData
    AllocatedBlockData abd;
    FeatStd::MemoryManagement::MemoryPoolManager::DumpAllocatedBlocks<AppMemoryPool>(&VisitAllocatedBlock, &abd);

    if (abd.mBlockCount > 0) {
        // if allocated blocks have been found, dump summary information
        Diagnostic::DebuggerOut("%u allocated block(s) found with %u+%u bytes\n",
                                abd.mBlockCount,
                                abd.mAccumulatedBufferSize,
                                abd.mAccumulatedBlockSize - abd.mAccumulatedBufferSize);
    }
    else {
        Diagnostic::DebuggerOut("no allocated blocks found\n");
    }
#endif
}
```

Executing the code above in MS Visual Studio will result in following output in Debug Output Pane:

```
S:\featstd\src\MemoryPoolSample\MemLeakAnalysis.cpp(121): 00443DA0[00443DB0] AppMemoryPool::2 small permanent [32+16]
1 allocated block(s) found with 32+16 bytes
```

##### **STL (Standard Template Library) Sample** 

STL sample shows how to use MemoryPool with Standard Template Library container classes. STL container classes accept an allocator template argument. The allocator type has standardized interface to allocate and free memory.

The StdAllocator class shows how to implement a STL allocator with MemoryPool:

```
template<typename T, typename Pool, UInt8 attrib = FeatStd::MemoryManagement::MemAttrib::Transient>
struct StdAllocator {
    typedef T value_type;
    typedef T* pointer;
    typedef T& reference;
    typedef const T* const_pointer;
    typedef const T& const_reference;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;

    inline StdAllocator() throw() { }
    inline StdAllocator(const StdAllocator&) throw() { }
    template<class U> inline StdAllocator(const StdAllocator<U, Pool, attrib> &) throw() { }
    ~StdAllocator() { }

    inline pointer address ( reference x ) const { return &x; }
    inline const_pointer address ( const_reference x ) const { return &x; }

    inline pointer allocate(size_type n, const void *hint = 0) const {
        FEATSTD_UNUSED(hint);
        return static_cast<pointer>(Pool::Alloc(FEATSTD_MEMORYPOOL_CALL_CONTEXT(sizeof(T) * n), attrib));
    }

    inline void deallocate(pointer p, size_type n) const {
        FEATSTD_UNUSED(n);
        Pool::Free(p);
    }

    inline size_type max_size() const throw() {
        return UInt32(0xffffffff) / sizeof(T);
    }

    inline void construct(pointer p, const_reference val) const {
        (void) FeatStd::MemoryManagement::ConstructObject<T>(p, val);
    }

    inline void destroy(pointer p) const {
        FeatStd::MemoryManagement::DestructObject<T>(p);
    }

    // MS specific (not mentioned in C++ standard)
    template<typename U> struct rebind {
       typedef StdAllocator<U, Pool, attrib> other;
    };
};

```

<div class="contents" id="bkmrk-the-stdallocator%3A%3Are"><div class="contents"><div class="textblock"><div class="fragment">  
</div><dl class="note"><dt></dt><dd><p class="callout info">The StdAllocator::rebind type is not part of C++ standard but required by MS Visual Studio flavor STL implementation.</p>

</dd></dl></div></div></div>The following code shows how to use StdAllocator with STL vector class.

```
#include <vector>
#include "SampleApp.h"
#include "StdAllocator.h"

<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ga1c81a9c5191e30fa4d7119772b6edb47">FEATSTD_LOG_SET_REALM</a>(SampleAppLogRealm);

// ------------------------------------------------------------------------
void StlSample()
{
    using namespace FeatStd::MemoryManagement;

    SampleApp::EnableMemoryPoolLogging();

    {
        // define an allocator that uses AppMemoryPool and does permanent allocations
        typedef StdAllocator<UInt32, AppMemoryPool, MemAttrib::Permanent> AppMemPoolAllocator;

        // std::vector using AppMemoryPool for allocations
        std::vector<UInt32, AppMemPoolAllocator> v;
        // reserve space for 10 items
        v.reserve(10);
        // fill the items with data
        for (UInt32 i = 0; i < v.capacity(); ++i) {
            v.push_back(i);
        }
    }

    SampleApp::EnableMemoryPoolLogging(false);
}

```

The code above will trigger following log output:

```
	S:\featstd\src\FeatStd\MemoryPool\MemoryPoolState.cpp(113)
0000000.250 [0x00001fcc] DEBUG FeatStdMemoryManagement S:\featstd\src\memorypoolsample\StdAllocator.h(51): AppMemoryPool(0) alloc 004442B0 8(24:P)
 {LogAllocSuccess}
	S:\featstd\src\FeatStd\MemoryPool\MemoryPoolState.cpp(113)
0000000.250 [0x00001fcc] DEBUG FeatStdMemoryManagement S:\featstd\src\memorypoolsample\StdAllocator.h(51): AppMemoryPool(3) alloc 004438B0 40(80:P)
 {LogAllocSuccess}
	S:\featstd\src\FeatStd\MemoryPool\MemoryPoolState.cpp(140)
0000000.250 [0x00001fcc] DEBUG FeatStdMemoryManagement S:\featstd\src\memorypoolsample\StdAllocator.h(51): AppMemoryPool(3) free 004438B0(80)
 {LogFree}
	S:\featstd\src\FeatStd\MemoryPool\MemoryPoolState.cpp(140)
0000000.250 [0x00001fcc] DEBUG FeatStdMemoryManagement S:\featstd\src\memorypoolsample\StdAllocator.h(51): AppMemoryPool(0) free 004442B0(24)
 {LogFree}
```

##### **Unmanaged MemoryPool** 

The lifetime of managed MemoryPools is handled by FeatStd library facilities. Unmanaged MemoryPool lifetime is completely in control of the application.

##### <a class="anchor" id="bkmrk--27"></a>Managed MemoryPools

<div class="contents" id="bkmrk-initialization-lates"><div class="contents"><div class="textblock">- Initialization 
    - latest before main function will be invoked
    - at event of first allocation request
- Destruction 
    - at the latest possible time, but earliest before main function has been executed.

</div></div></div>##### <a class="anchor" id="bkmrk--28"></a>Unmanaged MemoryPools

Initialization and destruction of the pool is controlled by the application.

Following code shows how to declare an Unmanaged MemoryPool:

```
// define an application specific memory pool with realm UnmanagedPool
// in contradiction to a managed memory pool, the application needs to take
// care to initialize the pool correctly before first use
// The pool can be - as any other MemoryPool - explicitly destroyed, or
// MemoryPoolManager will destroy the pool after main function has exit.
<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gac210b01392bc436f906528b33bab3f2e">FEATSTD_MEMORYPOOL_DEFINE_MEMORYPOOL</a>(UnmanagedPool);
```

Following code shows some sample use cases for Unmanaged MemoryPools

```
// ========================================================================

static const BinSpecification cPoolBinSpecs[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 8),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 16),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 32),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 64),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72ad9fc04576a26c4e913b0ea6c5f1ee729">MemoryPoolBinFlags::OverflowBarrier</a>, 128),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 256),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(0, 0)
};

namespace {

    // define the heap to be used for UnmanagedPool
    typedef BackingHeap<UnmanagedPool> Heap;

    // define the configuration for DefaultMemoryPool.
    static const MemoryPoolConfig cUnmanagedPoolConfig = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
        cUnmanagedPoolConfig,               // name of the variable receiving the configuration
        "UnmanagedPool",                    // the name of the MemoryPool
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolFlags::None</a>,              // Flags that control the memory pool behavior
        128,                                // all bins up to 128 bytes are small bins
        cPoolBinSpecs,                      // the bin configurations
        0,                                  // no presets
        Heap,                               // type name of the backing heap
        &SampleApp::OnOutOfMemory,          // optional the out of memory function
        0                                   // no OnDestroy function
    );

}

// ------------------------------------------------------------------------
static bool InitUnmanagedPool()
{
    using namespace FeatStd::MemoryManagement;

    // the size of the backing heap buffer (buffer managed by BackingHeap)
    static const UInt32 cHeapSize = 2048;
    // define the heap buffer (never use UInt8 to define this buffers as
    // the linker might locate a UInt8 to an unaligned address)
    static UInt32 sHeapBuffer[(cHeapSize + 3) / 4];

    // initialize first the backing heap and then the memory pool associated with the backing heap
    // The BackingHeap::Init expects a pointer to a buffer and the buffer size
    // The buffer can be - like in this example - a static declared memory buffer,
    // but also a fixed address (e.g. video ram area of a GPU).
    return Heap::Init(sHeapBuffer, cHeapSize) && UnmanagedPool::Init(&cUnmanagedPoolConfig);
}

static void *sUnmanagedHeapBuffer;

// ------------------------------------------------------------------------
static bool InitDynamicUnmanagedPool(UInt32 heapSize)
{
    using namespace FeatStd::MemoryManagement;
    using FeatStd::Internal::Diagnostic;

    if (sUnmanagedHeapBuffer != 0) {
        return true;
    }

    // in this sample the heap buffer is allocated from the application heap
    // thus a MemoryPool within a MemeoryPool is created (nested MemoryPool)
    sUnmanagedHeapBuffer = APP_TRANSIENT_ALLOC(heapSize);

    // if heap buffer allocation succeeded, initialize the Heap with the
    // allocated buffer
    if (sUnmanagedHeapBuffer == 0 || !Heap::Init(sUnmanagedHeapBuffer, heapSize)) {
        return false;
    }

    Diagnostic::DebuggerOut("allocated %u bytes for backing heap, %u bytes available for allocations\n",
                            heapSize,
                            Heap::GetFreeAreaSize());

    // log the buffer attributes
    LogBufferAttributes(sUnmanagedHeapBuffer);

    // now initialize UnmanagedMemoryPool
    return UnmanagedPool::Init(&cUnmanagedPoolConfig);
}

// ------------------------------------------------------------------------
static void DestroyDynamicUnmanagedPool()
{
    using namespace FeatStd::MemoryManagement;

    if (sUnmanagedHeapBuffer != 0 && UnmanagedPool::IsInitialized()) {
        // if sanity checks passed, destroy UnmanagedPool, destroy Heap, and free
        // heap memory buffer to AppMemoryPool
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9a13c73c3aa67c954a10dc64b4a0557d21">UnmanagedPool::Destroy</a>();
        <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9a13c73c3aa67c954a10dc64b4a0557d21">Heap::Destroy</a>();
        APP_FREE(sUnmanagedHeapBuffer);
        sUnmanagedHeapBuffer = 0;
    }

}

// ------------------------------------------------------------------------
void UnmanagedPoolSample()
{
    if (!InitUnmanagedPool()) {
        FEATSTD_LOG_FATAL("could not initialize UnmanagedPool");
    }

    void *p = UnmanagedPool::Alloc(FEATSTD_MEMORYPOOL_CALL_CONTEXT(32), FeatStd::MemoryManagement::MemAttrib::Permanent);
    if (p != 0) {
        UnmanagedPool::Free(p);
    }

    // explicit destruction of the pool && Heap
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9a13c73c3aa67c954a10dc64b4a0557d21">UnmanagedPool::Destroy</a>();
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9a13c73c3aa67c954a10dc64b4a0557d21">Heap::Destroy</a>();

    // create UnmanagedPool with a 1024 bytes buffer
    if (InitDynamicUnmanagedPool(1024)) {
        p = UnmanagedPool::Alloc(FEATSTD_MEMORYPOOL_CALL_CONTEXT(32), FeatStd::MemoryManagement::MemAttrib::Permanent);
        if (p != 0) {
            UnmanagedPool::Free(p);
        }
        DestroyDynamicUnmanagedPool();
    }
}
```

#### **Creating A Fragment Free Memory Pool Confuration** 

MemoryPool allows a broad range of operation modes. MemoryPool implementation and design itself already tries to reduce the risk of memory fragmentation by separating transient and permanent allocations. Nevertheless fragmentation still can happen as long transient allocations are done.

<div class="contents" id="bkmrk-to-be-more-precise--"><div class="contents"><div class="textblock"><dl class="note"><dt></dt><dd><p class="callout info">To be more precise - also a system with transient allocations can be fragment free. The application "just" has to ensure that functions are side effect free in terms of dynamic memory allocations. E.g. each function will free all dynamic memory it has allocated before returning to the calling function.</p>

</dd></dl></div></div></div>Permanent allocations do not cause memory fragmentation, thus the easiest and most straight forward way to achieve a fragment free system. With MemoryPool this can be easily achieved by defining FeatStd::MemoryManagement::MemoryPoolFlags::PermanentOnly.

Once set in the MemoryPool configuration, this flag will overrule the application requests for transient memory and the pool will only perform permanent memory allocations.

Following code sample shows how to configure a MemoryPool for permanent allocations only:

```
static const BinSpecification cStageOneBinSpecs[] = {
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 4),
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 1024),

    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#ga66c95b4820a360afb11f0347b03b61a8">FEATSTD_MEMORYPOOL_BIN_SPECIFICATION</a>(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___layout.html#gga143e31a12d442971ea2cbcda11cf35e3a6325ea46e8f4c69da2da6ccae1f7c4a6" title="The image will not be stretched; it will be rendered in its original size. If the available space is ...">MemoryPoolBinFlags::None</a>, 0)
};

// define the configuration for DefaultMemoryPool.
static const MemoryPoolConfig cStageOneConfig = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf05c0cba0b04c2190941573cbf222b22">FEATSTD_MEMORYPOOL_CONFIG_INITIALIZER</a>(
    cStageOneConfig,                    // name of the variable receiving the configuration
    "ConfigSampleStageOneMemoryPool",   // the name of the MemoryPool
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gga565d1641a4dce4dd5b9a6bd104e75e72aeb52b6c7a447f37badaf1a182479205f">MemoryPoolFlags::PermanentOnly</a>,     // Flags that control the memory pool behavior
    4,                                  // all bins up to 128 bytes are small bins
    cStageOneBinSpecs,                  // the bin configurations
    0,                                  // preset allocations
    ConfigSampleHeap,                   // type name of the backing heap
    0,                                  // optional the out of memory function
    0                                   // no OnDestroy function
);
```

Of course this may lead to accessive memory use if the application has a very high buffer size variance in its allocations. <span style="color: rgb(230, 126, 35);">[MemoryPool Configuration](#bkmrk-memorypool-configura)</span> shows how to optimize the system towards specific application memory allocation footprints.

The optimization of MemoryPool configuration can be an exhaustive process. Using the MemoryPool overflow mechanism strategically to re-use buffers in different use cases can save significant amount of memory, but requires in-depth analysis of allocations over time and ties application dynamics tightly to the MemoryPool configuration - something definitely to avoid in application development phase.

#### **Frequently Asked Questions and Tips** 

<div class="contents" id="bkmrk-what-is-the-differen"><div class="textblock">- <span style="color: rgb(230, 126, 35);">[What is the difference between small and large blocks?](#bkmrk-what-is-the-differen-0)</span>
- <span style="color: rgb(230, 126, 35);">[Is it possible to determine from which MemoryPool a buffer has been allocated from?](#bkmrk-is-it-possible-to-de)</span>
- <span style="color: rgb(230, 126, 35);">[My application reports memory leaks, but the allocation happens in a generic class used in multiple places](#bkmrk-my-application-repor)</span>
- <span style="color: rgb(230, 126, 35);">[What is the sequence of block allocations?](#bkmrk-what-is-the-sequence)</span>
- <span style="color: rgb(230, 126, 35);">[How to log all allocations and deallocations?](#bkmrk-how-to-log-all-alloc)</span>
- <span style="color: rgb(230, 126, 35);">[How does the number of bins influence system performance?](#bkmrk-how-does-the-number-)</span>

</div></div>##### **What is the difference between small and large blocks?** 

Small blocks bear less overhead - both in RAM and runtime - then large blocks. As all blocks in a small block bin have the same size, the size of the block must not be stored with the block. Thus a small block has only 4 bytes overhead. In contrary large blocks differ in size and therefore the size must be stored with the block. This results in 8 bytes administrative overhead for a large block.

Small blocks have also less runtime overhead. As all blocks in small block bin have the same size, the free list does not require sorting. Adding a block to the free list - which is a simple single linked list - has O(1) run time.

Large block bin free list is a balanced binary tree (Red Black Tree). Thus inserting and removing has O(log n) run time.

##### **Is it possible to determine from which MemoryPool a buffer has been allocated from?** 

Yes, the class [FeatStd::MemoryManagement::BlockMgmtObject](http://dev.doc.cgistudio.at/APILINK/class_feat_std_1_1_memory_management_1_1_block_mgmt_object.html) does the trick. An instance of [FeatStd::MemoryManagement::BlockMgmtObject](http://dev.doc.cgistudio.at/APILINK/class_feat_std_1_1_memory_management_1_1_block_mgmt_object.html) can be created with a valid pointer to a buffer allocated from a MemoryPool.

In the following code [FeatStd::MemoryManagement::BlockMgmtObject](http://dev.doc.cgistudio.at/APILINK/class_feat_std_1_1_memory_management_1_1_block_mgmt_object.html) will be used to determine the name of the MemoryPool a certain buffer has been allocated from.

```
// ------------------------------------------------------------------------
void MemoryLeakAnalysisSample()
{
#if defined(FEATSTD_MEMORYPOOL_LEAK_DETECTION_ENABLED)
    using FeatStd::Internal::Diagnostic;

    // allocate some memory and fill with data
    Char *str = static_cast<Char*>(APP_TRANSIENT_ALLOC(30));
    if (str == 0) {
        FEATSTD_LOG_FATAL("out of memory");
    }

    FeatStd::Internal::String::Copy(str, "this is a memory leak");

    // now dump the allocated blocks of AppMemoryPool - output will
    // go to Diagnostics::DebuggerOut.  this is the default implementation
    // provided by MemoryPool.
    FeatStd::MemoryManagement::MemoryPoolManager::DumpAllocatedBlocks<AppMemoryPool>();

    // VisualStudio Output pane should have now following output
    // <...>\MemLeakAnalysis.cpp(23): 0018AC28[32+16] AppMemoryPool::2
    //      data < 74 68 69 73 20 69 73 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 00 cd cd > this.is.a.memory.leak...

    // repeat dump of allocated blocks, this time with a user defined visitor function
    LogAppMemoryPoolAllocatedBlocks();

    // BlockMgmtObject can be used to get information on each MemoryPool allocated buffer
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html">FeatStd::MemoryManagement::BlockMgmtObject</a> bmo(str);
    FeatStd::Internal::Diagnostic::DebuggerOut("str has been allocated from pool %s at: %s\n", bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a3fe816c50885fd57cdf82b4c06386a30">GetAssociatedPoolName</a>(), bmo.<a class="code" href="http://dev.doc.cgistudio.at/APILINK/namespace_feat_std_1_1_memory_management_1_1_block_mgmt_object.html#a6a86c535895a8dd09eb5109bcbb15cd5">GetAllocContext</a>());

    // release memory
    APP_FREE(str);
#endif
}
```

##### **My application reports memory leaks, but the allocation happens in a generic class used in multiple places** 

Generic classes like FeatStd::Internal::Vector that allocate memory are used in various places. Thus the allocation context of the leaked block will tell only that the allocation happened in FeatStd::Internal::Vector class, but will not tell from which of the many places FeatStd::Internal::Vector class is used the leak stems from. If the leak is reproducible in MS Visual Studio host environment, the origin of the leak can be easily determined.

<div class="contents" id="bkmrk-create-a-breakpoint-"><div class="textblock">- Create a breakpoint the line after the allocation context of the leaked block is referring to. In this example this will be inside FeatStd::Internal::Vector::Reserve method.
- Once created, open the breakpoint context menu and select "When hit ...". In the "When Breakpoint Is Hit" dialog select print a message.
- Now add the name of the variable that received the address of the allocated block in curly braces (in case of FeatStd::Internal::Vector::Reserve add {newMemory}). This will print the value of the variable to the Debug output pane of Visual Studio when the application is executed.
- Now add also $CALLER (or $CALLSTACK) the the message. This will print the calling function / the complete callstack too.
- Run the program in VS debug mode
- Go to to the end of the debug output pane and get the address of the leaked block from the memory leak report.
- Search the address from bottom to top in the debug output pane.
- This will bring you to the output generated from the breakpoint. It will contain the address of the leaked block and the caller / callstack that invoked the function.

</div></div>##### **What is the sequence of block allocations?** 

The default behavior of MemoryPool for allocations is:

<div class="contents" id="bkmrk-determine-the-bin-to"><div class="contents"><div class="textblock">1. Determine the bin to allocate from according the the requested buffer size
2. Check the free list of the identified bin for suitable blocks 
    - Trivial for small block bins
    - Best fit search for large block bins
3. If no suitable block could be found in the free list, try to allocate an appropriate buffer from the backing heap.
4. If the allocation from the backing heap fails, try to allocate from the next larger bins (overflow).

</div></div></div>The default behavior can be influenced with flags FeatStd::MemoryManagement::MemoryPoolFlags::Enum and FeatStd::MemoryManagement::MemoryPoolBinFlags::Enum.

<div class="contents" id="bkmrk-if-the-memorypool-co"><div class="textblock">- If the MemoryPool configuration specifies FeatStd::MemoryManagement::MemoryPoolFlags::Enum::NoOverflow, the MemoryPool will not execute step 4.
- If FeatStd::MemoryManagement::MemoryPoolFlags::Enum::GenuineOnly is specified, transient blocks will not be allocated from the bin free list (the MemoryPool will skip step 2).
- If the bin defines FeatStd::MemoryManagement::MemoryPoolBinFlags::Enum::OverflowPreferred, the MemoryPool will exchange step 3 and 4 (first try to overflow and then allocate from the backing heap).

</div></div>##### **How to log all allocations and deallocations?** 

MemoryPool logs all requests with FeatStd logging facilities. To see the allocation logs, logging facilities need to be configured accordingly:

```
        static inline void EnableMemoryPoolLogging(bool enable = true) {
            using namespace FeatStd::Diagnostics;

            // get the logger and set log level to all (MemoryPool logs allocs and free at log
            // log level Info
            if (enable) {
                LogControl::GetLogger<LogRealm::FeatStdMemoryManagement>().SetLevel(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___d_i_a_g_n_o_s_t_i_c_s.html#ggad2cbc908487f2a3b4340020c0f69bda0ab1ef5245ddb4e605054fc70baa0d1f96" title="All.">LogLevel::All</a>);
            }
            else {
                LogControl::GetLogger<LogRealm::FeatStdMemoryManagement>().SetLevel(LogControl::GetLogger<LogRealm::FeatStdMemoryManagement>().GetDefaultLogLevel());
            }

        }
```

##### **How does the number of bins influence system performance?** 

The number of bins have both impact on RAM and system performance. Typically 12 bytes must be accounted for each bin object.

##### <a class="anchor" id="bkmrk--29"></a>RAM

The Excel file MemoryPoolStatisticsAnalysis.xlsm contains a sheet labeled ResourceReq. MemoryPool configuration can be specified and the overall RAM consumption of the MemoryPool will be estimated.

##### <a class="anchor" id="bkmrk--30"></a>Runtime Performance

The number of bins have two impacts on runtime performance.

<div class="contents" id="bkmrk-bin-lookup-locking-%28"><div class="contents"><div class="textblock">- bin lookup
- locking (in terms of multithreading)

</div></div></div>##### <a class="anchor" id="bkmrk--31"></a>Bin Lookup

Bin lookup is done with binary search - thus it has a runtime complexity of O(log n). Therefore bin count has marginal impact on bin lookup time.

##### <a class="anchor" id="bkmrk--32"></a>Locking

MemoryPool implementation is thread safe, therefore shared resource access must be synchronized. MemoryPool locking is done on bin level. This means that two threads allocating memory from two different bins of the same MemoryPool will not lock each other. Thus the number of bins can have positive impact on the number of thread context switches.

MemoryPool uses FeatStd::Internal::Spinlock for thread synchronization.

##### <a class="anchor" id="bkmrk--33"></a>Summary

<div class="contents" id="bkmrk-low-bin-count-high-b"><div class="textblock"><table class="doxtable"><tbody><tr><th style="background-color: rgb(35, 111, 161);">  
</th><th style="background-color: rgb(35, 111, 161);"><span style="color: rgb(255, 255, 255);">**Low Bin Count**</span></th><th style="background-color: rgb(35, 111, 161);"><span style="color: rgb(255, 255, 255);">**High Bin Count**</span></th></tr><tr><th style="background-color: rgb(35, 111, 161);"><span style="color: rgb(255, 255, 255);">**Runtime**</span></th><td>increased number of locking conflicts may decrease overall performance</td><td>reduced risk of locking conflicts</td></tr><tr><th style="background-color: rgb(35, 111, 161);"><span style="color: rgb(255, 255, 255);">**RAM**</span></th><td>marginal (12 bytes per bin)</td><td>marginal (12 bytes per bin)</td></tr><tr><th style="background-color: rgb(35, 111, 161);"><span style="color: rgb(255, 255, 255);">**Overhead per allocation**</span></th><td>low bin count may be an indicator of higher overhead per allocation</td><td>high bin count is an indicator of well configured bins and lower overhead per allocation</td></tr></tbody></table>

</div></div><div class="contents" id="bkmrk-the-number-of-bins-t"><div class="textblock"><dl class="attention"><dd><p class="callout warning">The number of bins to configure is heavily related to variance of buffer sizes actually allocated. Many different buffer sizes lead to a high number of bins (otherwise the overhead per allocation increases), low buffer size variance typically results also in a low bin count.</p>

</dd></dl></div></div>#### **MemoryPool Excel Analysis** 

To ease the analysis of MemoryPool statistics and diagnostics output, the Excel file supplement/MemoryPool/MemoryPoolStatisticsAnalysis.xltm is supplied. The Excel can import data generated during runtime of an application using MemoryPool (CSV files). The data import format is defined by re-useable code snippets in MemoryPoolSample application (<span style="color: rgb(230, 126, 35);">[Diagnostic Utilities](#bkmrk-diagnostic-utilities)</span>)

Incorporate and adapt the code snippets from MemoryPoolSample in your application. Once the application has been run, a list of CSV files will be generated:

<div class="contents" id="bkmrk-memorypoolstatistics-1"><div class="contents"><div class="textblock">- MemoryPoolStatistics.csv contains the overall MemoryPool statistics
- MemoryPoolBinStatistics.csv contains the Bin statistics of each MemoryPool
- PermanentBlockList.csv contains the information on all permanent blocks in all MemoryPool instances

</div></div></div>Note that features FEATSTD\_MEMORYPOOL\_LEAK\_DETECTION and FEATSTD\_MEMORYPOOL\_STATISTICS must be enabled to generate the CSV files.

##### <a class="anchor" id="bkmrk--34"></a>PoolAnalysis Sheet

The pool analysis sheet can be used to visualize the overall MemoryPool statistics of each MemoryPool in the system. Press the update button and select the file MemoryPoolStatistics.csv that has been generated by the application. The data from the file will be read and the tables will be updated.

##### <a class="anchor" id="bkmrk--35"></a>PoolBinAnalysis

The PoolBinAnalysis sheet can be used to analyse the bin statistics of each MemoryPool. Press the update button and select the file MemoryPoolBinStatistics.csv which has been generated by the application.

In the field PoolName the MemoryPool to analyse can be selected.

##### <a class="anchor" id="bkmrk--36"></a>PermanentBlockAnalysis

Can be used to analyse all permanent blocks MemoryPool instances. <span style="color: rgb(230, 126, 35);">[MemoryPool Configuration](#bkmrk-memorypool-configura)</span> how to use the permanent block list.

##### <a class="anchor" id="bkmrk--37"></a>ResourceReq

Can be used to estimate the RAM resource requirements of MemoryPool.