对于一个简单的指针增量分配器(它们有正式名称吗?)我正在寻找一种无锁算法。这似乎微不足道,但我想得到一些反馈,我的实现是否正确。
不是线程安全的实现:
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;
}
Run Code Online (Sandbox Code Playgroud)
我对线程安全实现的尝试:
void * Alloc(size_t size)
{
byte * current;
do
{
current = head;
if (end - current < size)
return 0; // allocation failure
} while (CMPXCHG(&head, current+size, current) != current));
return current;
}
Run Code Online (Sandbox Code Playgroud)
哪里 …
是否存在operator new[]用作分配器的STL实现?在我的编译器上,Foo::operator new[]私有化并没有阻止我创建一个vector<Foo>......是什么行为保证了什么?
为什么这段代码没有编译?
#include <cstdlib>
#include <list>
template < typename Type >
class Allocator {
public:
using value_type = Type;
public:
template < typename Other >
struct rebind { using other = Allocator< Other >; };
public:
Type * allocate( std::size_t n ) { return std::malloc( n ); }
void deallocate( Type * p, std::size_t ) throw ( ) { std::free( p ); }
};
int main( void ) {
std::list< void *, Allocator< void * > > list;
return …Run Code Online (Sandbox Code Playgroud) 我正在编写一组分配器,目的是将它们用于非常高性能的环境中,因此需要一些限制使用(由编译器调节,而不是运行时错误)。我一直在阅读有状态分配器的 C++11 语义以及它们如何被符合容器使用。
我在下面粘贴了一个简单的分配器,它只包含分配器对象中的一块内存。在 C++03 中,这是非法的。
template <typename T, unsigned N>
class internal_allocator {
private:
unsigned char storage[N];
std::size_t cursor;
public:
typedef T value_type;
internal_allocator() : cursor(0) {}
~internal_allocator() { }
template <typename U>
internal_allocator(const internal_allocator<U>& other) {
// FIXME: What are the semantics here?
}
T* allocate(std::size_t n) {
T* ret = static_cast<T*>(&storage[cursor]);
cursor += n * sizeof(T);
if (cursor > N)
throw std::bad_alloc("Out of objects");
return ret;
}
void deallocate(T*, std::size_t) {
// Noop!
}
};
Run Code Online (Sandbox Code Playgroud)
在 C++11 …
我正在尝试实现自定义allocator以将内存映射文件存储在std::vector. 执行的文件映射boost::iostreams::mapped_file
文件内存映射的分配器类型:
template<typename T>
class mmap_allocator
{
public:
typedef T value_type;
mmap_allocator(const std::string& filename)
: _mmfile(filename) { }
T* allocate (size_t n)
{
return reinterpret_cast<T*>(_mmfile.data());
}
void deallocate (T* p, size_t n)
{
p = nullptr;
_mmfile.close();
}
private:
boost::iostreams::mapped_file _mmfile;
};
Run Code Online (Sandbox Code Playgroud)
内存映射文件的容器,基于std::vector:
//Get file size
long GetFileSize(std::string filename)
{
FILE *p_file = NULL;
p_file = fopen(filename.c_str(),"rb");
fseek(p_file,0,SEEK_END);
int size = ftell(p_file);
fclose(p_file);
return size;
}
template<typename T>
class mm_vector : public …Run Code Online (Sandbox Code Playgroud) 我还没有看到很多关于如何在C ++ 11中创建自定义分配器的好的示例或教程。我确实看到,自C ++ 03起,它已经发生了变化。但是,那时我也不知道该怎么做。
通过尽可能多的信息,我得出了以下结论:
// Project Includes
#include "IMemoryManager.h"
// Standard Includes
#include <cstddef>
#include <iostream>
#include <memory>
//------------------------------------------------------------------------------
/// <summary>
/// A STL compliant custom allocator
///
/// Based on Dr Dobbs article
/// http://www.drdobbs.com/the-standard-librarian-what-are-allocato/184403759
/// I attempted to make it C++11 compliant.
/// Takes an Interface to a Memory Manager which will implement the custom memory management solution.
/// </summary>
template <class T>
class CustomAllocator
{
std::unique_ptr<IMemoryManager> m_memoryManager;
public:
typedef T value_type;
/// Allocator takes ownership …Run Code Online (Sandbox Code Playgroud) C++ 17引入了std::aligned_alloc对齐感知new,它可以进行过度对齐的分配,但是呢std::allocator?它是否处理过度对齐的类型?
在实现自定义C++分配器时,需要定义:
operator== 对于具有不同的分配器 value_typeoperator!= 对于具有不同的分配器 value_type您可以在Allocator概念文档中看到自定义分配器的示例实现:
#include <cstdlib>
#include <new>
template <class T>
struct Mallocator {
typedef T value_type;
Mallocator() = default;
template <class U> constexpr Mallocator(const Mallocator<U>&) noexcept {}
T* allocate(std::size_t n) {
if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc();
if(auto p = static_cast<T*>(std::malloc(n*sizeof(T)))) return p;
throw std::bad_alloc();
}
void deallocate(T* p, std::size_t) noexcept { std::free(p); }
};
template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) { return true; }
template <class T, …Run Code Online (Sandbox Code Playgroud) new int[0]在C ++中是允许的,但是std::allocator<int>().allocate(0)定义明确?更笼统地说,所有分配器都必须接受0作为分配参数吗?
编辑:阅读我测试了Visual Studio的答案后std::allocator:allocate(0)给nullptr
deallocate(nullptr, anything) 是点头。
因此,使用nullptr是一个很好的建议,但是标准不要求deallocate(nullptr, 0)是nop,请参见是否允许使用C ++ allocator :: deallocate(NULL,1)?
c++ memory-management allocator dynamic-memory-allocation language-lawyer
我采取一个allocator的std::map和std::set在C++ 14.分配器必须提供一次pointer allocate(size_type n)为n项目分配空间的功能.
经过一些测试,我已经看到std::map并且std::set总是allocate(1)在我的平台上做,我还没有看到任何n > 1.如果我考虑内部树表示,对我来说是有意义的.
标准是否保证了这种行为?或者我可以安全地信任n == 1任何特定平台吗?