custom Allocator compilation difficulties 2

I am defining a custom allocator but I need to keep pointers of the allocator as offsets. As I change the pointer definition to a "size_t" type the code doesn't compile again with no apparent clues ! The need is to create a custom allocator where the memory is addressed in a "segment:offset" style and the address of the segment ( object ) is relocatable.

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

Answers


Keep the definitions of pointer and const_pointer the way the standard library expects it to be:

  typedef T* pointer;
  typedef const T* const_pointer;

Add a new typedef:

  typedef std::size_t offset_type;

Use offset_type instead of pointer in suitable places.

Here's a version that compiles and builds but produces Segmentation Fault at run time. I'll leave it to you to figure out that problem.

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

Need Your Help

Apply jquery script to only one class at a time

javascript jquery css-selectors

How can I apply this script to only one class at a time

Delay execution of code following UIAlertView show statement, until the alert is showing

iphone objective-c ios4 uialertview

I'm attempting to create a "Please wait" UIAlertView that will appear while my code is downloading and calculating some data, then disappear when it has finished. Specifically, it is parsing an xml...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.