this is allow custom allocator for obj-api structs/tables. (#4520)

added "native_custom_alloc" attribute to tables/structs, eg.

table parent_table( native_custom_alloc:"custom_alloc_name" ) {
...
}

with a custom allocator defined as

template <typename T> class custom_alloc_name : public std::allocator<T> {
public:

 typedef T*       pointer;

 template <class U>
 struct rebind {
  typedef custom_alloc_name<U> other;
 };

 pointer allocate(const std::size_t n) {
      return ....;
 }

 void deallocate(T* ptr, std::size_t n) {
    ...
 }

 custom_alloc_name() throw() {}
 template <class U> custom_alloc_name(const custom_alloc_name<U>&) throw() {}
};
};
This commit is contained in:
rmawatson
2017-12-01 18:15:41 +01:00
committed by Wouter van Oortmerssen
parent ba08b0ec02
commit 53a897731e
7 changed files with 77 additions and 15 deletions

View File

@@ -656,10 +656,10 @@ inline voffset_t FieldIndexToOffset(voffset_t field_id) {
return static_cast<voffset_t>((field_id + fixed_fields) * sizeof(voffset_t));
}
template <typename T> const T* data(const std::vector<T> &v) {
template <typename T, typename Alloc> const T* data(const std::vector<T, Alloc> &v) {
return v.empty() ? nullptr : &v.front();
}
template <typename T> T* data(std::vector<T> &v) {
template <typename T, typename Alloc> T* data(std::vector<T, Alloc> &v) {
return v.empty() ? nullptr : &v.front();
}
@@ -1283,8 +1283,8 @@ class FlatBufferBuilder
/// serialize into the buffer as a `vector`.
/// @return Returns a typed `Offset` into the serialized data indicating
/// where the vector is stored.
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
const std::vector<T> &v) {
template<typename T, typename Alloc> Offset<Vector<const T *>> CreateVectorOfStructs(
const std::vector<T, Alloc> &v) {
return CreateVectorOfStructs(data(v), v.size());
}

View File

@@ -526,6 +526,7 @@ class Parser : public ParserState {
known_attributes_["cpp_ptr_type"] = true;
known_attributes_["cpp_str_type"] = true;
known_attributes_["native_inline"] = true;
known_attributes_["native_custom_alloc"] = true;
known_attributes_["native_type"] = true;
known_attributes_["native_default"] = true;
known_attributes_["flexbuffer"] = true;