# memory
![Project Status](https://img.shields.io/endpoint?url=https%3A%2F%2Fwww.jonathanmueller.dev%2Fproject%2Fmemory%2Findex.json)
![Build Status](https://github.com/foonathan/memory/workflows/Main%20CI/badge.svg)
[![Code Coverage](https://codecov.io/gh/foonathan/memory/branch/master/graph/badge.svg?token=U6wnInlamY)](https://codecov.io/gh/foonathan/memory)
The C++ STL allocator model has various flaws. For example, they are fixed to a certain type, because they are almost necessarily required to be templates. So you can't easily share a single allocator for multiple types. In addition, you can only get a copy from the containers and not the original allocator object. At least with C++11 they are allowed to be stateful and so can be made object not instance based. But still, the model has many flaws.
Over the course of the years many solutions have been proposed, for example [EASTL]. This library is another. But instead of trying to change the STL, it works with the current implementation.
> |[![](https://www.jonathanmueller.dev/embarcadero-logo.png)](https://www.embarcadero.com/de/products/cbuilder/starter) | Sponsored by [Embarcadero C++Builder](https://www.embarcadero.com/de/products/cbuilder/starter). |
> |-------------------------------------|----------------|
>
> If you like this project, consider [supporting me](https://jonathanmueller.dev/support-me/).
## Features
New allocator concepts:
* a `RawAllocator` that is similar to an `Allocator` but easier to use and write
* a `BlockAllocator` that is an allocator for huge memory blocks
Several implementations:
* `heap_/malloc_/new_allocator`
* virtual memory allocators
* allocator using a static memory block located on the stack
* memory stack, `iteration_allocator`
* different memory pools
* a portable, improved `alloca()` in the form of `temporary_allocator`
* facilities for joint memory allocations: share a big memory block for the object
and all dynamic memory allocations for its members
Adapters, wrappers and storage classes:
* incredible powerful `allocator_traits` allowing `Allocator`s as `RawAllocator`s
* `std_allocator` to make a `RawAllocator` an `Allocator` again
* adapters for the memory resource TS
* `allocator_deleter` classes for smart pointers
* (optionally type-erased) `allocator_reference` and other storage classes
* memory tracking wrapper
In addition:
* container node size debuggers that obtain information about the node size of an STL container at compile-time to specify node sizes for pools
* debugging options for leak checking, double-free checks or buffer overflows
* customizable error handling routines that can work with exceptions disabled
* everything except the STL adapters works on a freestanding environment
## Basic example
```cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <foonathan/memory/container.hpp> // vector, list, list_node_size
#include <foonathan/memory/memory_pool.hpp> // memory_pool
#include <foonathan/memory/smart_ptr.hpp> // allocate_unique
#include <foonathan/memory/static_allocator.hpp> // static_allocator_storage, static_block_allocator
#include <foonathan/memory/temporary_allocator.hpp> // temporary_allocator
// alias namespace foonathan::memory as memory for easier access
#include <foonathan/memory/namespace_alias.hpp>
template <typename BiIter>
void merge_sort(BiIter begin, BiIter end);
int main()
{
using namespace memory::literals;
// a memory pool RawAllocator
// allocates a memory block - initially 4KiB - and splits it into chunks of list_node_size<int>::value big
// list_node_size<int>::value is the size of each node of a std::list
memory::memory_pool<> pool(memory::list_node_size<int>::value, 4_KiB);
// just an alias for std::list<int, memory::std_allocator<int, memory::memory_pool<>>
// a std::list using a memory_pool
// std_allocator stores a reference to a RawAllocator and provides the Allocator interface
memory::list<int, memory::memory_pool<>> list(pool);
list.push_back(3);
list.push_back(2);
list.push_back(1);
for (auto e : list)
std::cout << e << ' ';
std::cout << '\n';
merge_sort(list.begin(), list.end());
for (auto e : list)
std::cout << e << ' ';
std::cout << '\n';
// allocate a std::unique_ptr using the pool
// memory::allocate_shared is also available
auto ptr = memory::allocate_unique<int>(pool, *list.begin());
std::cout << *ptr << '\n';
// static storage of size 4KiB
memory::static_allocator_storage<4096u> storage;
// a memory pool again but this time with a BlockAllocator
// this controls the internal allocations of the pool itself
// we need to specify the first template parameter giving the type of the pool as well
// (node_pool is the default)
// we use a static_block_allocator that uses the static storage above
// all allocations will use a memory block on the stack
using static_pool_t = memory::memory_pool<memory::node_pool, memory::static_block_allocator>;
static_pool_t static_pool(memory::unordered_set_node_size<int>::value, 4096u, storage);
// again, just an alias for std::unordered_set<int, std::hash<int>, std::equal_to<int>, memory::std_allocator<int, static_pool_t>
// see why I wrote these? :D
// now we have a hash set that lives on the stack!
memory::unordered_set<int, static_pool_t> set(static_pool);
set.insert(3);
set.insert(2);
set.insert(3); // running out of stack memory is properly handled, of course
for (auto e : set)
std::cout << e << ' ';
std::cout << '\n';
}
// naive implementation of merge_sort using temporary memory allocator
template <typename BiIter>
void merge_sort(BiIter begin, BiIter end)
{
using value_type = typename std::iterator_traits<BiIter>::value_type;
auto distance = std::distance(begin, end);
if (distance <= 1)
return;
auto mid = begin;
std::advance(mid, distance / 2);
// an allocator for temporary memory
// is similar to alloca() but uses its own stack
// this stack is thread_local and created on the first call to this function
// as soon as the allocator object goes out of scope, everything allocated through it, will be freed
auto alloc = memory::temporary_allocator();
// alias for std::vector<value_type, memory::std_allocator<value_type, memory::temporary_allocator>>
// a std::vector using a temporary_allocator
memory::vector<value_type, memory::temporary_allocator> first(begin, mid, alloc),
second(mid, end, alloc);
merge_sort(first.begin(), first.end());
merge_sort(second.begin(), second.end());
std::merge(first.begin(), first.end(), second.begin(), second.end(), begin);
}
```
See `example/` for more.
## Installation
This library can be used as [CMake] subdirectory.
It is tested on GCC 4.8-5.0, Clang 3.5 and Visual Studio 2013. Newer versions should work too.
1. Fetch it, e.g. using [git submodules] `git submodule add https://github.com/foonathan/memory ext/memory` and `git submodule update --init --recursive`.
2. Call `add_subdirectory(ext/memory)` or whatever your local path is to make it available in CMake.
3. Simply call `target_link_libraries(your_target PUBLIC foonathan_memory)` to link this library and setups the include search path and compilation options.
You can also install the library:
1. Run `cmake -DCMAKE_BUILD_TYPE="buildtype" -DFOONATHAN_MEMORY_BUILD_EXAMPLES=OFF -DFOONATHAN_MEMORY_BUILD_TESTS=OFF .` inside the library sources.
2. Run `cmake --build . -- install` to install the library under `${CMAKE_INSTALL_PREFIX}`.
3. Repeat 1 and 2 for each build type/configuration you want to have (like `Debug`, `RelWithDebInfo`
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Fast DDS动态类型示例程序,DynamicHelloWorldExample源码 (595个子文件)
fast-discovery-server.bat 504B
ros-discovery.bat 466B
fastdds.bat 456B
CMakeDetermineCompilerABI_CXX.bin 50KB
CMakeDetermineCompilerABI_C.bin 50KB
CMakeCCompilerId.c 21KB
cmake.check_cache 86B
CMakeCXXCompiler.cmake 5KB
foonathan_memory-config.cmake 4KB
fastrtps-dynamic-targets.cmake 4KB
fastcdr-dynamic-targets.cmake 3KB
fast-discovery-server-targets.cmake 3KB
cmake_install.cmake 3KB
CMakeCCompiler.cmake 2KB
fastrtps-config.cmake 2KB
fastcdr-config.cmake 2KB
fastcdr-config-version.cmake 2KB
fastrtps-config-version.cmake 2KB
foonathan_memory-config-debug.cmake 1KB
foonathan_memory_vendorConfig-version.cmake 1KB
foonathan_memory-config-version.cmake 1KB
fastrtps-dynamic-targets-debug.cmake 952B
fast-discovery-server-targets-debug.cmake 913B
fastcdr-dynamic-targets-debug.cmake 873B
CMakeSystem.cmake 395B
foonathan_memory_vendorConfig.cmake 322B
CMakeRCCompiler.cmake 212B
CMakeCXXCompilerId.cpp 20KB
HelloWorldPublisher.cpp 6KB
HelloWorldSubscriber.cpp 5KB
HelloWorld_main.cpp 4KB
Browse.VC.db 21.01MB
Solution.VC.db 996KB
generate.stamp.depend 3KB
fastrtpsd-2.4.dll 15.34MB
fastrtpsd-2.4.dll 15.34MB
libcrypto-3-x64.dll 4.89MB
libssl-3-x64.dll 754KB
foonathan_memory-0.7.1-dbg.dll 579KB
foonathan_memory-0.7.1-dbg.dll 579KB
fastcdrd-1.0.dll 287KB
fastcdrd-1.0.dll 287KB
HelloWorld.idl.example 67B
nodesize_dbg.exe 1.62MB
DynamicHelloWorldExample.exe 207KB
fast-discovery-serverd-1.0.0.exe 203KB
CompilerIdC.exe 15KB
CompilerIdCXX.exe 15KB
DynamicHelloWorldExample.vcxproj.filters 911B
ZERO_CHECK.vcxproj.filters 541B
INSTALL.vcxproj.filters 540B
ALL_BUILD.vcxproj.filters 298B
TypeObject.h 186KB
Cdr.h 151KB
FastCdr.h 86KB
TypeIdentifierTypes.h 51KB
AnnotationParameterValue.h 43KB
XMLParser.h 21KB
DynamicData.h 21KB
RTPSParticipantAttributes.h 19KB
RTPSWriter.h 18KB
PDP.h 17KB
StatefulWriter.h 17KB
RTPSReader.h 16KB
ReaderProxy.h 16KB
TypeIdentifier.h 16KB
TypesBase.h 15KB
Time_t.h 14KB
EDP.h 14KB
XMLParserCommon.h 14KB
CacheChange.h 14KB
StatefulReader.h 12KB
Authentication.h 12KB
WriterProxyData.h 12KB
ReaderProxyData.h 12KB
XMLProfileManager.h 11KB
ParticipantGenericMessage.h 11KB
TCPTransportDescriptor.h 11KB
WLP.h 11KB
FastBuffer.h 10KB
Locator.h 10KB
RTPSMessageGroup.h 10KB
EDPSimple.h 10KB
DynamicTypeBuilderFactory.h 10KB
RTPSDomain.h 10KB
SequenceNumber.h 10KB
TypeObjectFactory.h 10KB
SubscriberHistory.h 10KB
IPLocator.h 9KB
StatelessReader.h 9KB
History.h 9KB
Logging.h 9KB
CryptoTransform.h 9KB
RTPSMessageCreator.h 9KB
TransportInterface.h 9KB
RTPSParticipant.h 9KB
CDRMessage.h 9KB
MessageReceiver.h 9KB
ParticipantProxyData.h 8KB
CryptoKeyFactory.h 8KB
共 595 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
咸鱼爱幻想
- 粉丝: 223
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功