gpt4 book ai didi

c++ - 自定义Allocator编译难点2

转载 作者:行者123 更新时间:2023-11-28 06:48:35 25 4
gpt4 key购买 nike

我正在定义一个自定义分配器,但我需要将分配器的指针保留为偏移量。当我将指针定义更改为“size_t”类型时,代码不会在没有明显线索的情况下再次编译!需要创建一个自定义分配器,其中内存以“段:偏移”样式寻址并且段(对象)的地址是可重定位的。

#include <bits/c++config.h>
#define _GLIBCXX_FULLY_DYNAMIC_STRING 0

#include <stdint.h>
#include <stddef.h>

#include <memory>
#include <string>
#include <limits>


typedef int32_t Token;
typedef unsigned char byte;

using namespace std;
template <typename T>
struct Allocator {
public:

// http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement

typedef T value_type;

typedef std::size_t pointer;
typedef const std::size_t const_pointer;


typedef value_type& reference;typedef const value_type& const_reference;

typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

template<typename U> struct rebind {typedef Allocator<U> other;};

static const size_t heapSize=0x1000;
static pointer freePos;
static Token freeT;
static byte m[heapSize];

inline explicit Allocator() {freeT=0;freePos=0;}
inline ~Allocator() {}
inline Allocator(Allocator const&) {} // with explicit it doesn't compile

inline pointer address(reference r) {return &r;}
inline const_pointer address(const_reference r) {return &r;}

//static inline pointer allocate(size_type n, typename std::allocator<void>::const_pointer hint = 0){
// pointer t=freePos;freePos+=n*sizeof(T);return t;
static inline pointer allocate(size_type n){pointer t=freePos;freePos+=n*sizeof(T);return t;}


static void deallocate(pointer p, size_type n){
*(pointer*)((byte*)m+p)=(pointer)(n*sizeof(T));
}

static inline void deallocate(T* p,size_type n){deallocate((pointer)((byte*)p-m),n);}
static inline void deallocate(const T* p,size_type n){deallocate((pointer)((byte*)p-m),n);}

inline size_type max_size() const{
return std::numeric_limits<size_type>::max() / sizeof(T);
}
inline void construct(pointer p, const T& t) {}
inline void destroy(pointer p) {}

};

template <typename T>
bool operator==(Allocator<T> const &, Allocator<T> const &) { return true; }

template <typename T>
bool operator!=(Allocator<T> const &, Allocator<T> const &) { return false; }


using namespace std;

typedef std::basic_string< char,std::char_traits<char>,Allocator<char> > String;

int main(){
String s("nice");
String t("very nice");
String u("good");
return 0;
}

最佳答案

保持 pointerconst_pointer 的定义符合标准库的预期:

  typedef T* pointer;
typedef const T* const_pointer;

添加一个新的typedef:

  typedef std::size_t offset_type;

在合适的地方使用offset_type代替pointer

这是一个编译和构建但在运行时产生段错误的版本。我会留给你来解决这个问题。

#include <bits/c++config.h>
#define _GLIBCXX_FULLY_DYNAMIC_STRING 0

#include <stdint.h>
#include <stddef.h>

#include <memory>
#include <string>
#include <limits>


typedef int32_t Token;
typedef unsigned char byte;

using namespace std;
template <typename T>
struct Allocator {
public:

// http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement

typedef T value_type;

typedef T* pointer;
typedef const T* const_pointer;

typedef std::size_t offset_type;


typedef value_type& reference;typedef const value_type& const_reference;

typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

template<typename U> struct rebind {typedef Allocator<U> other;};

static const size_t heapSize=0x1000;
static offset_type freePos;
static Token freeT;
static byte m[heapSize];

inline explicit Allocator() {freeT=0;freePos=0;}
inline ~Allocator() {}
inline Allocator(Allocator const&) {} // with explicit it doesn't compile

inline offset_type address(reference r) {return &r;}
inline const_pointer address(const_reference r) {return &r;}

//static inline offset_type allocate(size_type n, typename std::allocator<void>::const_pointer hint = 0)
// offset_type t=freePos;freePos+=n*sizeof(T);return t;
static inline pointer allocate(size_type n){offset_type t=freePos;freePos+=n*sizeof(T);return (pointer)t;}


static void deallocate(offset_type p, size_type n){
*(offset_type*)((byte*)m+p)=(offset_type)(n*sizeof(T));
}

static inline void deallocate(pointer p,size_type n){deallocate((offset_type)((byte*)p-m),n);}
static inline void deallocate(const_pointer* p,size_type n){deallocate((offset_type)((byte*)p-m),n);}

inline size_type max_size() const{
return std::numeric_limits<size_type>::max() / sizeof(T);
}
inline void construct(offset_type p, const T& t) {}
inline void destroy(offset_type p) {}

};

template <typename T>
typename Allocator<T>::offset_type Allocator<T>::freePos;

template <typename T>
Token Allocator<T>::freeT;

template <typename T>
byte Allocator<T>::m[Allocator::heapSize];

template <typename T>
bool operator==(Allocator<T> const &, Allocator<T> const &) { return true; }

template <typename T>
bool operator!=(Allocator<T> const &, Allocator<T> const &) { return false; }


using namespace std;

typedef std::basic_string< char,std::char_traits<char>,Allocator<char> > String;

int main(){
String s("nice");
String t("very nice");
String u("good");
return 0;
}

关于c++ - 自定义Allocator编译难点2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com