简而言之,我的问题是:如果您有class,则MyClass<T>如何更改类定义以支持您拥有的情况MyClass<T, Alloc>,类似于STL vector提供的方式。
我需要此功能来支持共享内存的分配器。具体来说,我正在尝试在共享内存中实现环形缓冲区。当前它具有以下ctor:
template<typename ItemType>
SharedMemoryBuffer<ItemType>::SharedMemoryBuffer( unsigned long capacity, std::string name )
Run Code Online (Sandbox Code Playgroud)
在哪里ItemType是要放置在缓冲区的每个插槽中的数据的类型。
现在,当我从主程序创建缓冲区时,效果如此出色
SharedMemoryBuffer<int>* sb;
sb = new SharedMemoryBuffer<int>(BUFFER_CAPACITY + 1, sharedMemoryName);
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,缓冲区本身不会在共享内存中创建,因此其他进程无法访问该缓冲区。我想做的是能够做类似的事情
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
typedef SharedMemoryBuffer<int, ShmemAllocator> MyBuffer;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShmemAllocator alloc_inst (segment.get_segment_manager());
MyBuffer *mybuf = segment.construct<MyBuffer>("MyBuffer")(alloc_inst);
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何向类模板添加显式分配器。
可能重复:
在特定的内存地址创建新的C++对象?
我正在编写本质上是一个对象池分配器,它将分配一个类.我正在分配足够的内存来适应我需要的对象,我正在传递指向内部空间的指针.
现在我的问题是这样的:一旦我在我的池中得到一个指针,我该如何在那里构建一个对象?
首先,我认为不是.但是,我在调试模式下观察到了MSVC 10.0的这种行为.我正在使用一个自定义allocator类,它依赖于用户只传递在同一个实例上分配的指针deallocate.但是,在发布模式下,我的代码正在运行.
这是一个错误还是我弄错了?
我对一些基本的字符串实现有点困惑.我一直在寻找内心的工作和学习新事物.我无法完全掌握如何管理内存.
只是来自基本字符串实现的一些花絮
原始分配器用于char类型
typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
Run Code Online (Sandbox Code Playgroud)...然后,当分配Rep放置在分配的缓冲区内时,__size计算出也适合字符
size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
_Rep *__p = new (__place) _Rep;
Run Code Online (Sandbox Code Playgroud)这是从_Rep缓冲区中获取字符数据的方式
_CharT* _M_refdata() throw()
{
return reinterpret_cast<_CharT*>(this + 1);
}
Run Code Online (Sandbox Code Playgroud)设置角色 - 用于一种方式
_M_assign(__p->_M_refdata(), __n, __c);
Run Code Online (Sandbox Code Playgroud)困扰我的是原始分配器是char类型,但分配的内存可能包含_Rep对象,加上字符数据(不必是char类型)
另外,为什么(或者更确切地说)如何调用以_M_refdata知道字符数据的开始(或结束)在缓冲区内(即this+1)
编辑:this+1只是将内部指针推到_Rep对象后面的下一个位置?
我对内存对齐和转换有基本的了解,但这似乎超出了我所读过的内容.
任何人都可以帮忙,或者给我指出更多信息丰富的阅读材料?
我最近试图跟踪我们项目中lua的内存使用情况,并且遇到了使用lua_Alloc自定义分配器执行此任务的想法。好吧,分配器代码看起来很简单,而且看起来工作正常。
但是很快,这个小函数遇到了两个挑战:
1.它输出的结果与collectgarbage('count')给出的值完全不同;
2.假设当前的内存使用量为M个字节,然后我们输入一些nil引用并调用gc,则内存使用量将大于M个字节。例如:A返回,B返回,C返回,...,collectgarbage()
好吧,我听说如果您正确使用lua,就不会有内存泄漏,因此我认为在计算内存使用时我做错了什么。请帮我弄清楚。提前致谢。
随附的可编译代码如下:
extern "C"
{
#include "lua.h"
#include <lauxlib.h>
#include <lualib.h>
};
#include <cstdio>
#include <string>
#include <cstdlib>
using namespace std;
struct Tracker
{
size_t m_usage;
}g_tracker;
void*
Allocator(void* ud, void* ptr, size_t osize, size_t nsize)
{
Tracker* pTracker = (Tracker*)ud;
void* pRet = NULL;
if( nsize == 0 )
{
pTracker->m_usage -= osize;
//printf("Free %d bytes; ", osize);
free(ptr);
}
else
{
pTracker->m_usage -= osize;
//printf("first Free %d bytes; ", osize);
pTracker->m_usage += nsize;
//printf("then …Run Code Online (Sandbox Code Playgroud) 我有一个关于STL类和分配器的问题,这些问题似乎不容易在网上找到.有谁知道在嵌套的STL类中使用了哪个分配器?例如:
typedef std::vector<int> myvect;
Run Code Online (Sandbox Code Playgroud)
//按照后续回复/评论的指示编辑了以下行
typedef std::map<int, myvect, std::less<int>, A> mymap; //uses my custom allocator for map creation
Run Code Online (Sandbox Code Playgroud)
让我们调用默认的分配器D,并假设我有一个自定义分配器A.
如果我做了以下事情将会发生什么:
创建地图:
mymap mapInstance;
Run Code Online (Sandbox Code Playgroud)现在,假设存在一个条目mapInstance[0],假设我将一个值推入向量:
mapInstance[0].push_back(999);
Run Code Online (Sandbox Code Playgroud)什么分配器用于向量的动态内存mapInstance[0]?
到目前为止,我的理解D是使用了默认分配器,但我想确认A传入映射的自定义分配器不会被使用.(据我所知,只有我使用某种嵌套分配选项才会发生这种情况.)
当然,我理解mapInstance[0]使用自定义分配器分配元数据/标头信息A.我关心的是动态内存部分,即之后的部分d_dataBegin.
我有一个Matrix类是Rows 的集合.数据类型定义如下:
行:
template <typename Index>
class Row {
public:
Row()
{
_index_vector = std::vector<Index, aligned_allocator<Index> > ();
}
Row& operator=( const Row& source )
{
//some copy logic here
return *this;
}
private:
std::vector<Index, aligned_allocator<Index> > _index_vector;
};
Run Code Online (Sandbox Code Playgroud)
矩阵:
template <typename Index>
class Matrix {
public:
typedef std::vector<Row<Index> > Rep;
Matrix () : _m (0), _n (0)
{}
Matrix (size_t n, size_t m) :
_m (m),
_n (n)
{
_A = Rep (n);
}
private: …Run Code Online (Sandbox Code Playgroud) 我喜欢创建一个包含进程间容器的类的boost进程间向量.以下代码一直有效,直到resize函数调用,当然因为我的类没有默认构造函数.我该如何解决这个问题?该示例基于容器的boost 容器示例
谢谢马库斯
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
using namespace boost::interprocess;
//Typedefs of allocators and containers
typedef managed_shared_memory::segment_manager segment_manager_t;
typedef allocator<void, segment_manager_t> void_allocator;
typedef allocator<int, segment_manager_t> int_allocator;
typedef vector<int, int_allocator> int_vector;
typedef allocator<char, segment_manager_t> char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator> char_string;
class complex_data
{
public:
int id_;
char_string char_string_;
int_vector int_vector_;
//Since void_allocator is convertible to any other allocator<T>, we can simplify
//the initialization taking just one allocator for all inner containers.
complex_data(const …Run Code Online (Sandbox Code Playgroud) 提供STL容器(例如,std :: vector)与分配器作为模板参数之间的区别是什么,例如:
std::vector<int, std::allocator<int>> some_ints;
Run Code Online (Sandbox Code Playgroud)
并提供一个分配器作为构造函数参数,例如:
std::allocator<int> temp;
std::vector<int> some_ints(temp);
Run Code Online (Sandbox Code Playgroud)
鉴于它们不是同一个东西(即一个提供类型,另一个是类型实例)并且可以彼此分开使用,那么两者的优点是什么?
该分配器的概念和标准:: allocator_traits不说什么allocate时候分配失败,会做-将它返回nullptr或抛出?
当我使用标准分配器API编写容器时,我应该
检查返回值并捕获noexcept版本成员函数中的异常(例如push_back,resize...);
检查返回值,如果在抛出异常中失败则抛出
所以无论它是否抛出,我都会得到正确的行为.