allocator

Why is deleted memory unable to be reused

霸气de小男生 提交于 2019-12-06 01:37:11
问题 I am using C++ on Windows 7 with MSVC 9.0, and have also been able to test and reproduce on Windows XP SP3 with MSVC 9.0. If I allocate 1 GB of 0.5 MB sized objects, when I delete them, everything is ok and behaves as expected. However if I allocate 1 GB of 0.25 MB sized objects when I delete them, the memory remains reserved (yellow in Address Space Monitor) and from then on will only be able to be used for allocations smaller than 0.25 MB. This simple code will let you test both scenarios

Copy stateful allocator: standard library allocator semantics and internal memory

穿精又带淫゛_ 提交于 2019-12-05 16:54:39
I am writing a collection of allocators, with the intention that they're to be used in very high performance environments, so a little bit of restricted usage (mediated by the compiler, not runtime errors) is desirable. I've been reading into the C++11 semantics of stateful allocators and how they're expected to be used by conforming containers. I've pasted a simple allocator below which just contains a block of memory within the allocator object. In C++03, this was illegal. template <typename T, unsigned N> class internal_allocator { private: unsigned char storage[N]; std::size_t cursor;

suggestions for improving an allocator algorithm implementation

流过昼夜 提交于 2019-12-05 06:42:35
I have a Visual Studio 2008 C++ application where I'm using a custom allocator for standard containers such that their memory comes from a Memory Mapped File rather than the heap. This allocator is used for 4 different use cases: 104-byte fixed size structure std::vector< SomeType, MyAllocator< SomeType > > foo; 200-byte fixed size structure 304-byte fixed size structure n-byte strings std::basic_string< char, std::char_traits< char >, MyAllocator< char > > strn; I need to be able to allocate roughly 32MB total for each of these. The allocator tracks memory usage using a std::map of pointers

Does an allocator.construct loop equal std::uninitialized_copy?

拈花ヽ惹草 提交于 2019-12-05 06:00:07
In this context T is a certain type and allocator is an allocator object for that type. By default it is std::allocator<T> but this is not necessarily true. I have a chunk of memory acquired by allocator.allocate(n) . I also have a container con of T objects (say, a std::vector<T> ). I want to initialize that chunk of memory with the T object(s). The location of the chunk of memory is stored in T* data . Are these two code examples always identical? #include <memory> // example 1 std::uninitialized_copy(con.begin(), con.end(), data) // example 2 std::vector<T>::const_iterator in = con.begin();

Is libstdc++ support for std::unordered_map incomplete?

非 Y 不嫁゛ 提交于 2019-12-05 05:34:43
Related to this question on CodeReview, I tried to use std::unordered_map with a custom allocator but apparently this does not work with gcc/clang and libstdc++. The error can be generated from initializing an empty hash map with a std::allocator #include <unordered_map> int main() { typedef std::allocator<std::pair<const int, int>> A; typedef std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, A> H; auto h = H{A()}; // ERROR, cannot find constructor H::H(const A&) } Live Example . Question : is libstdc++ support for constructing std::unordered_map with a single allocator as

Type-erased allocators in modern C++

你说的曾经没有我的故事 提交于 2019-12-05 03:37:04
The "classic" STL containers such as std::vector and std::map take their allocator types as a template argument. This means that std::vector<T, std::allocator<T>> and std::vector<T, MyAllocator> for example are considered completely separate types. Some newer allocator-aware classes like std::shared_ptr and std::tuple on the other hand use type-erasure to "hide" information about the allocator, so it does not form part of the type signature. However, std::unordered_map (which is of a similar vintage to shared_ptr ) maintains the classic approach of taking an extra defaulted template parameter.

Contiguous memory allocation for several small std::vectors?

房东的猫 提交于 2019-12-05 03:22:22
I would like to find a way to store several std::vectors , each of a different but known and reasonably small size, in contiguous memory. I realize I could write my own class, say with a very large array and with pointers to the start of each subsection of the array within the larger array treated like a separate entity, but it seems like there should be a smarter way to do this. Is there a way to use allocators , for example, to create contiguous std::vectors ? I'd like not to reinvent the wheel just because I want this memory-locality of otherwise normal std::vectors I don't know how to even

Rebinding in a custom STL allocator with pre-allocated block

点点圈 提交于 2019-12-04 17:25:56
问题 I'm going to build a custom allocator , preallocating a big block (array) for storing N elements of some class T , and then just increase an index inside the array to service allocation requests. Since I don't want any initialization for the elements in the pre-allocated block, something like this won't work: T buffer[N]; because in this case T 's constructor will be called for the N elements of the block. Since my understanding is that std::aligned_storage doesn't call T 's constructor, I

lock free arena allocator implementation - correct?

荒凉一梦 提交于 2019-12-04 12:48:51
for a simple pointer-increment allocator (do they have an official name?) I am looking for a lock-free algorithm. It seems trivial, but I'd like to get soem feedback whether my implementaiton is correct. not threadsafe implementation: byte * head; // current head of remaining buffer byte * end; // end of remaining buffer void * Alloc(size_t size) { if (end-head < size) return 0; // allocation failure void * result = head; head += size; return head; } My attempt at a thread safe implementation: void * Alloc(size_t size) { byte * current; do { current = head; if (end - current < size) return 0;

How is allocator-aware container assignment implemented?

只愿长相守 提交于 2019-12-04 10:16:34
For example, from std::deque::operator = in C++ Reference: (1) Copy Assignment (const std::deque &other) Replaces the contents with a copy of the contents of other. If std::allocator_traits::propagate_on_container_copy_assignment() is true, the target allocator is replaced by a copy of the source allocator. If the target and the source allocators do not compare equal, the target (*this) allocator is used to deallocate the memory, then other's allocator is used to allocate it before copying the elements. If this->get_allocator() == other.get_allocator() , I can simply destroy and deallocate