| 102 | } |
| 103 | |
| 104 | TfLiteStatus SimpleMemoryArena::Commit(TfLiteContext* context) { |
| 105 | size_t required_size = RequiredBufferSize(); |
| 106 | if (required_size > underlying_buffer_size_) { |
| 107 | char* new_alloc = new char[required_size]; |
| 108 | char* new_underlying_buffer_aligned_ptr = reinterpret_cast<char*>( |
| 109 | AlignTo(arena_alignment_, reinterpret_cast<intptr_t>(new_alloc))); |
| 110 | |
| 111 | // If the arena had been previously allocated, copy over the old memory. |
| 112 | // Since Alloc pointers are offset based, they will remain valid in the new |
| 113 | // memory block. |
| 114 | if (high_water_mark_ > 0 && underlying_buffer_size_ > 0) { |
| 115 | size_t copy_amount = std::min( |
| 116 | underlying_buffer_.get() + underlying_buffer_size_ - |
| 117 | underlying_buffer_aligned_ptr_, |
| 118 | new_alloc + required_size - new_underlying_buffer_aligned_ptr); |
| 119 | memcpy(new_underlying_buffer_aligned_ptr, underlying_buffer_aligned_ptr_, |
| 120 | copy_amount); |
| 121 | } |
| 122 | |
| 123 | underlying_buffer_.reset(new_alloc); |
| 124 | underlying_buffer_size_ = required_size; |
| 125 | underlying_buffer_aligned_ptr_ = new_underlying_buffer_aligned_ptr; |
| 126 | } |
| 127 | committed_ = true; |
| 128 | return underlying_buffer_ != nullptr ? kTfLiteOk : kTfLiteError; |
| 129 | } |
| 130 | |
| 131 | TfLiteStatus SimpleMemoryArena::ResolveAlloc(TfLiteContext* context, |
| 132 | const ArenaAlloc& alloc, |