Skip to content

Commit 8d14078

Browse files
committed
Create README.md
1 parent 7f9d562 commit 8d14078

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

‎README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
### shared_allocator
2+
SharedAllocator is simple allocator that allocates memory inside one shared library and can be used to make it safe
3+
to share STL containers between several shared libraries.
4+
SharedAllocator interface is similar to `std::allocator` interface
5+
so `std::allocator` can be easily replaced by `salloc::shared_allocator` (or by other `salloc::` allocators).
6+
7+
### adding shared_allocator to your project with CMake
8+
1. Copy files include/shared_allocator/*.* to your shared_allocator directory inside your include directory
9+
(for example, include/third_party/shared_allocator or just include/shared_allocator).
10+
2. Copy files source/*.* to your sources directory (for example, source/shared_allocator).
11+
3. Include source/shared_allocator/CMakeLists.txt into your main CMakeLists.txt: `add_subdirectory(source/shared_allocator)`
12+
4. Add path to shared_allocator header files into additional include directories: `include_directories(include/third_party)`
13+
to make it possible for your compiler to find includes like `#include "shared_allocator/shared_allocator.hpp"`.
14+
5. Add link to shared_allocator library into every project's CMakeLists.txt that would be using shared_allocator<T>
15+
(or shared_allocate function and so on): `target_link_libraries( ${PROJECT_NAME} shared_allocator)`
16+
17+
### example code
18+
```
19+
#include "shared_allocator/shared_allocator.hpp"
20+
#include <vector>
21+
22+
int main()
23+
{
24+
// This vector can be safely shared among several shared libraries.
25+
std::vector<int, salloc::shared_allocator<int> > shared_vector;
26+
27+
shared_vector.reserve(100);
28+
for (int i = 0; i < 100; ++i)
29+
{
30+
shared_vector.push_back(i);
31+
}
32+
33+
return 0;
34+
}
35+
```
36+
37+
### allocators
38+
- **salloc::shared_allocator** is simple allocator that allocates and deallocates memory using imported functions `shared_allocate`
39+
and `shared_deallocate`
40+
- **salloc::cached_allocator** is allocator that caches deallocated memory (stores pointers to previously allocated memory for further usage).
41+
So if you allocating and deallocating memory buffers **of the same size** rapidly you will have a very small number
42+
of real memory allocations, because once allocated, this memory buffer will be reused for many times.
43+
It is best suited for containers that allocating and deallocating memory buffers of the same size
44+
(for example, `std::list`, `std::set`, `std::map`). It is not so good for `std::vector` because it needs memory allocations of
45+
different size, but it should not impact performance if you will use `salloc::cached_allocator` instead of `salloc::shared_allocator`
46+
for `std::vector`.
47+
- **salloc::size_cached_allocator** works like `salloc::cached_allocator` but it have an individual cache for each size of memory buffer
48+
(pointers for deallocated memory buffers of each size are stored in individual lists. For example, 3 pointers of size 100 will be
49+
stored in one list and 5 pointers of size 60 will be stored in another list).
50+
It is not recommended to use this allocator with individual containers, it is better to share this allocator between several
51+
containers at once.

0 commit comments

Comments
 (0)