diff options
author | rmawatson <rmawatson@hotmail.com> | 2017-12-01 18:15:41 +0100 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2017-12-01 09:15:41 -0800 |
commit | 53a897731e23320e67602d3d4c56e09caffb98ca (patch) | |
tree | c6737d0959d8bc4996f9aa146c25ce684d862dff /docs | |
parent | ba08b0ec0266f505bfe06e49e993091157e19c93 (diff) | |
download | flatbuffers-53a897731e23320e67602d3d4c56e09caffb98ca.tar.gz |
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() {}
};
};
Diffstat (limited to 'docs')
-rw-r--r-- | docs/source/CppUsage.md | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/docs/source/CppUsage.md b/docs/source/CppUsage.md index 2ded6110..e858e0a3 100644 --- a/docs/source/CppUsage.md +++ b/docs/source/CppUsage.md @@ -126,6 +126,45 @@ The following attributes are specific to the object-based API code generation: "native_inline", the value specified with this attribute will be included verbatim in the class constructor initializer list for this member. +- `native_custom_alloc`:"custom_allocator" (on a table or struct): When using the + object-based API all generated NativeTables that are allocated when unpacking + your flatbuffer will use "custom allocator". The allocator is also used by + any std::vector that appears in a table defined with `native_custom_alloc`. + This can be used to provide allocation from a pool for example, for faster + unpacking when using the object-based API. + + Minimal Example: + + schema: + + table mytable(native_custom_alloc:"custom_allocator") { + ... + } + + with custom_allocator defined before flatbuffers.h is included, as: + + template <typename T> struct custom_allocator : public std::allocator<T> { + + typedef T *pointer; + + template <class U> + struct rebind { + typedef custom_allocator<U> other; + }; + + pointer allocate(const std::size_t n) { + return std::allocator<T>::allocate(n); + } + + void deallocate(T* ptr, std::size_t n) { + return std::allocator<T>::deallocate(ptr,n); + } + + custom_allocator() throw() {} + template <class U> + custom_allocator(const custom_allocator<U>&) throw() {} + }; + - `native_type`' "type" (on a struct): In some cases, a more optimal C++ data type exists for a given struct. For example, the following schema: |