| 260 | |
| 261 | template <class Trait> |
| 262 | dt_byte* TensorStorage<Trait>::apply_lazy_and_get_ptr() { |
| 263 | check_comp_node_valid(); |
| 264 | if (m_size > m_capacity) { |
| 265 | mgb_assert(m_allow_realloc && !m_offset); |
| 266 | m_data.reset(); // free old ptr |
| 267 | m_capacity = 0; // to be exception safe |
| 268 | auto ptr = static_cast<dt_byte*>(Trait::alloc(m_comp_node, m_size)); |
| 269 | mgb_throw_if(!ptr, SystemError, "failed to allocate memory"); |
| 270 | CompNode cn = m_comp_node; |
| 271 | m_data.reset(ptr, [cn](void* p) { Trait::free(cn, p); }); |
| 272 | m_ref_ptr = std::make_shared<void*>(static_cast<void*>(nullptr)); |
| 273 | m_capacity = m_size; |
| 274 | } |
| 275 | *m_ref_ptr = static_cast<void*>(m_data.get()); |
| 276 | return m_data.get() + m_offset; |
| 277 | } |
| 278 | |
| 279 | template <class Trait> |
| 280 | TensorStorage<Trait>& TensorStorage<Trait>::comp_node( |