自定义分配器编译困难

时间:2014-06-29 19:13:10

标签: c++ c++11 allocator

我正在尝试创建自定义分配器,但存在编译问题。 通过更改

的定义值
#define _GLIBCXX_FULLY_DYNAMIC_STRING 0

#define _GLIBCXX_FULLY_DYNAMIC_STRING 1

我设法从无法编译切换到能够编译,但应该是这样吗?不应该有点简单吗?

是否有任何机构都有这方面的经验,并且已经知道如何解决这些编译问题。

所需的最低代码:

#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:
    static const size_t heapSize=0x1000;
    static size_t freePos;
    static Token freeT;
    static byte m[heapSize];

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

    typedef T value_type;

    typedef value_type* pointer;typedef const value_type* 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;};

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

    //template<typename U>
  //inline explicit Allocator(Allocator<U> const&) {}

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

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

    static inline void deallocate(pointer p, size_type){
        ::operator delete(p);
/*  pointer deallocate(pointer p,size_type n){
        size_t sz=sizeof(T)*n;
        *(size_t*)(m+p)=sz;
    }
*/
    }

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

/*
  void              construct(pointer p, const T& val) 
                    { new ((T*) p) T(val); }
*/
  inline void destroy(pointer p) { p->~T(); }
//   void              destroy(pointer p) { ((T*)m[p])->~T(); }

  inline bool operator==(Allocator const&) {return true;}
  inline bool operator!=(Allocator const& a) {return false;}
};

#endif


using namespace std;

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

int main(){
String s("Nice-the-data-goes-in-my-memory");
return 0;
}

1 个答案:

答案 0 :(得分:1)

如果使比较运算符自由起作用,它可以正常工作。通常,关系运算符应该是自由函数,因此它们可以双方隐式转换:

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; }