涂 桂旺
/
mbed-os-example-cj
json test
jsoncpp/include/json/allocator.h@0:2ee762ea11b3, 2018-01-26 (annotated)
- Committer:
- tgw
- Date:
- Fri Jan 26 06:05:31 2018 +0000
- Revision:
- 0:2ee762ea11b3
json
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tgw | 0:2ee762ea11b3 | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
tgw | 0:2ee762ea11b3 | 2 | // Distributed under MIT license, or public domain if desired and |
tgw | 0:2ee762ea11b3 | 3 | // recognized in your jurisdiction. |
tgw | 0:2ee762ea11b3 | 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
tgw | 0:2ee762ea11b3 | 5 | |
tgw | 0:2ee762ea11b3 | 6 | #ifndef CPPTL_JSON_ALLOCATOR_H_INCLUDED |
tgw | 0:2ee762ea11b3 | 7 | #define CPPTL_JSON_ALLOCATOR_H_INCLUDED |
tgw | 0:2ee762ea11b3 | 8 | |
tgw | 0:2ee762ea11b3 | 9 | #include <cstring> |
tgw | 0:2ee762ea11b3 | 10 | #include <memory> |
tgw | 0:2ee762ea11b3 | 11 | |
tgw | 0:2ee762ea11b3 | 12 | #pragma pack(push, 8) |
tgw | 0:2ee762ea11b3 | 13 | |
tgw | 0:2ee762ea11b3 | 14 | namespace Json { |
tgw | 0:2ee762ea11b3 | 15 | template<typename T> |
tgw | 0:2ee762ea11b3 | 16 | class SecureAllocator { |
tgw | 0:2ee762ea11b3 | 17 | public: |
tgw | 0:2ee762ea11b3 | 18 | // Type definitions |
tgw | 0:2ee762ea11b3 | 19 | using value_type = T; |
tgw | 0:2ee762ea11b3 | 20 | using pointer = T*; |
tgw | 0:2ee762ea11b3 | 21 | using const_pointer = const T*; |
tgw | 0:2ee762ea11b3 | 22 | using reference = T&; |
tgw | 0:2ee762ea11b3 | 23 | using const_reference = const T&; |
tgw | 0:2ee762ea11b3 | 24 | using size_type = std::size_t; |
tgw | 0:2ee762ea11b3 | 25 | using difference_type = std::ptrdiff_t; |
tgw | 0:2ee762ea11b3 | 26 | |
tgw | 0:2ee762ea11b3 | 27 | /** |
tgw | 0:2ee762ea11b3 | 28 | * Allocate memory for N items using the standard allocator. |
tgw | 0:2ee762ea11b3 | 29 | */ |
tgw | 0:2ee762ea11b3 | 30 | pointer allocate(size_type n) { |
tgw | 0:2ee762ea11b3 | 31 | // allocate using "global operator new" |
tgw | 0:2ee762ea11b3 | 32 | return static_cast<pointer>(::operator new(n * sizeof(T))); |
tgw | 0:2ee762ea11b3 | 33 | } |
tgw | 0:2ee762ea11b3 | 34 | |
tgw | 0:2ee762ea11b3 | 35 | /** |
tgw | 0:2ee762ea11b3 | 36 | * Release memory which was allocated for N items at pointer P. |
tgw | 0:2ee762ea11b3 | 37 | * |
tgw | 0:2ee762ea11b3 | 38 | * The memory block is filled with zeroes before being released. |
tgw | 0:2ee762ea11b3 | 39 | * The pointer argument is tagged as "volatile" to prevent the |
tgw | 0:2ee762ea11b3 | 40 | * compiler optimizing out this critical step. |
tgw | 0:2ee762ea11b3 | 41 | */ |
tgw | 0:2ee762ea11b3 | 42 | void deallocate(volatile pointer p, size_type n) { |
tgw | 0:2ee762ea11b3 | 43 | std::memset(p, 0, n * sizeof(T)); |
tgw | 0:2ee762ea11b3 | 44 | // free using "global operator delete" |
tgw | 0:2ee762ea11b3 | 45 | ::operator delete(p); |
tgw | 0:2ee762ea11b3 | 46 | } |
tgw | 0:2ee762ea11b3 | 47 | |
tgw | 0:2ee762ea11b3 | 48 | /** |
tgw | 0:2ee762ea11b3 | 49 | * Construct an item in-place at pointer P. |
tgw | 0:2ee762ea11b3 | 50 | */ |
tgw | 0:2ee762ea11b3 | 51 | template<typename... Args> |
tgw | 0:2ee762ea11b3 | 52 | void construct(pointer p, Args&&... args) { |
tgw | 0:2ee762ea11b3 | 53 | // construct using "placement new" and "perfect forwarding" |
tgw | 0:2ee762ea11b3 | 54 | ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...); |
tgw | 0:2ee762ea11b3 | 55 | } |
tgw | 0:2ee762ea11b3 | 56 | |
tgw | 0:2ee762ea11b3 | 57 | size_type max_size() const { |
tgw | 0:2ee762ea11b3 | 58 | return size_t(-1) / sizeof(T); |
tgw | 0:2ee762ea11b3 | 59 | } |
tgw | 0:2ee762ea11b3 | 60 | |
tgw | 0:2ee762ea11b3 | 61 | pointer address( reference x ) const { |
tgw | 0:2ee762ea11b3 | 62 | return std::addressof(x); |
tgw | 0:2ee762ea11b3 | 63 | } |
tgw | 0:2ee762ea11b3 | 64 | |
tgw | 0:2ee762ea11b3 | 65 | const_pointer address( const_reference x ) const { |
tgw | 0:2ee762ea11b3 | 66 | return std::addressof(x); |
tgw | 0:2ee762ea11b3 | 67 | } |
tgw | 0:2ee762ea11b3 | 68 | |
tgw | 0:2ee762ea11b3 | 69 | /** |
tgw | 0:2ee762ea11b3 | 70 | * Destroy an item in-place at pointer P. |
tgw | 0:2ee762ea11b3 | 71 | */ |
tgw | 0:2ee762ea11b3 | 72 | void destroy(pointer p) { |
tgw | 0:2ee762ea11b3 | 73 | // destroy using "explicit destructor" |
tgw | 0:2ee762ea11b3 | 74 | p->~T(); |
tgw | 0:2ee762ea11b3 | 75 | } |
tgw | 0:2ee762ea11b3 | 76 | |
tgw | 0:2ee762ea11b3 | 77 | // Boilerplate |
tgw | 0:2ee762ea11b3 | 78 | SecureAllocator() {} |
tgw | 0:2ee762ea11b3 | 79 | template<typename U> SecureAllocator(const SecureAllocator<U>&) {} |
tgw | 0:2ee762ea11b3 | 80 | template<typename U> struct rebind { using other = SecureAllocator<U>; }; |
tgw | 0:2ee762ea11b3 | 81 | }; |
tgw | 0:2ee762ea11b3 | 82 | |
tgw | 0:2ee762ea11b3 | 83 | |
tgw | 0:2ee762ea11b3 | 84 | template<typename T, typename U> |
tgw | 0:2ee762ea11b3 | 85 | bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) { |
tgw | 0:2ee762ea11b3 | 86 | return true; |
tgw | 0:2ee762ea11b3 | 87 | } |
tgw | 0:2ee762ea11b3 | 88 | |
tgw | 0:2ee762ea11b3 | 89 | template<typename T, typename U> |
tgw | 0:2ee762ea11b3 | 90 | bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) { |
tgw | 0:2ee762ea11b3 | 91 | return false; |
tgw | 0:2ee762ea11b3 | 92 | } |
tgw | 0:2ee762ea11b3 | 93 | |
tgw | 0:2ee762ea11b3 | 94 | } //namespace Json |
tgw | 0:2ee762ea11b3 | 95 | |
tgw | 0:2ee762ea11b3 | 96 | #pragma pack(pop) |
tgw | 0:2ee762ea11b3 | 97 | |
tgw | 0:2ee762ea11b3 | 98 | #endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED |