Browse Source

fix static function

master
Odilitime 5 years ago
parent
commit
996a7d62ca
  1. 126
      src/TLSFAlloc.h

126
src/TLSFAlloc.h

@ -7,78 +7,90 @@ @@ -7,78 +7,90 @@
#include "tlsf.h"
namespace ntr {
template <class T>
class TLSFAlloc {
public:
typedef T value_type;
typedef T* ptr;
typedef const T* const_ptr;
typedef T& ref;
typedef const T& const_ref;
typedef std::size_t size_type;
typedef std::ptrdiff_t diff_type;
template <class T>
class TLSFAlloc {
public:
typedef T value_type;
typedef T* ptr;
typedef const T* const_ptr;
typedef T& ref;
typedef const T& const_ref;
typedef std::size_t size_type;
typedef std::ptrdiff_t diff_type;
template <class U>
struct rebind {
typedef TLSFAlloc<U> other;
};
template <class U>
struct rebind {
typedef TLSFAlloc<U> other;
};
ptr address (ref value) const {
return &value;
}
const_ptr address (const_ref value) const {
return &value;
}
ptr address (ref value) const {
return &value;
}
const_ptr address (const_ref value) const {
return &value;
}
// these are the constructors for same: had I not ported sbrk(2) to winnt,
// the constructor would have generated a memory pool from above.
TLSFAlloc() throw() {}
TLSFAlloc(const TLSFAlloc&) throw() {}
template <class U> TLSFAlloc(const TLSFAlloc<U>&) throw() {}
~TLSFAlloc() throw() {}
// these are the constructors for same: had I not ported sbrk(2) to winnt,
// the constructor would have generated a memory pool from above.
TLSFAlloc() throw() {}
TLSFAlloc(const TLSFAlloc&) throw() {}
template <class U> TLSFAlloc(const TLSFAlloc<U>&) throw() {}
~TLSFAlloc() throw() {}
// return maximum number of elements that can be allocated
size_type max_size() const throw() {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
// placement new
void* operator new (std::size_t size, void*) {
// because this is a static, it can't access this->allocate
// so I've jsut copied allocate here
#ifdef DEBUG_TLSF_CPP
// print message and allocate memory with 2LSF
std::cerr << "allocate " << size << " element(s)" << " of size " << sizeof(T) << std::endl;
ptr ret = (ptr)(tlsf_malloc(size * sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl;
#else
ptr ret = static_cast<ptr>(tlsf_malloc(size * sizeof(T)));
#endif
return ret;
}
// destroy elements of initialized storage p
void static destroy (ptr p) {
p->~T();
}
// return maximum number of elements that can be allocated
size_type max_size() const throw() {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
// allocate but don't initialize num elements of type T
ptr allocate(size_type num, const void* = 0) {
// allocate but don't initialize num elements of type T
ptr allocate(size_type num, const void* = 0) {
#ifdef DEBUG_TLSF_CPP
// print message and allocate memory with 2LSF
std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl;
ptr ret = (ptr)(tlsf_malloc(num*sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl; return ret;
// print message and allocate memory with 2LSF
std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl;
ptr ret = (ptr)(tlsf_malloc(num*sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl; return ret;
#else
ptr ret = static_cast<ptr>(tlsf_malloc(num*sizeof(T))); return ret;
ptr ret = static_cast<ptr>(tlsf_malloc(num*sizeof(T))); return ret;
#endif
}
// initialize elements of allocated storage p with value value
// using placement new (defined here)
void construct (ptr p, const T& value) {
new (reinterpret_cast<void*>(p))T(value);
}
// placement new
void* operator new (std::size_t size, void*){
return allocate(size);
}
}
// initialize elements of allocated storage p with value value
// using placement new (defined here)
void construct (ptr p, const T& value) {
new (reinterpret_cast<void*>(p))T(value);
}
// destroy elements of initialized storage p
void destroy (ptr p) {
p->~T();
}
// deallocate storage p of deleted elements
void deallocate(ptr p, size_type num) {
// deallocate storage p of deleted elements
void deallocate(ptr p, size_type num) {
#ifdef DEBUG_TLSF_CPP
// print message and deallocate memory with global delete
std::cerr << "deallocate " << num << " element(s)" << " of size " << sizeof(T) << " at: " << (void*)p << std::endl;
#endif
tlsf_free(reinterpret_cast<void*>(p));
}
};
tlsf_free(reinterpret_cast<void*>(p));
}
};
// return that all specializations of this allocator are interchangeable
template <class T1, class T2> bool operator== (const TLSFAlloc<T1>&,const TLSFAlloc<T2>&) throw() { return true; }
template <class T1, class T2> bool operator!= (const TLSFAlloc<T1>&, const TLSFAlloc<T2>&) throw() { return false; }
}
}

Loading…
Cancel
Save