xref: /aosp_15_r20/external/pytorch/c10/core/impl/COW.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #pragma once
2 
3 #include <c10/macros/Macros.h>
4 #include <c10/util/intrusive_ptr.h>
5 
6 namespace c10 {
7 struct StorageImpl;
8 class DataPtr;
9 }; // namespace c10
10 
11 namespace c10::impl::cow {
12 
13 // Creates a Copy-on-write (COW) clone of the given storage. This will also
14 // convert the given storage into a COW storage if it is not COW already.
15 //
16 // Converting the storage into a COW storage will not be successful if the
17 // storage's DataPtr has some context (`DataPtr::get_context()`) which is not
18 // equal to the data pointer (`DataPtr::get()`). In this case, a nullptr is
19 // returned.
20 C10_API c10::intrusive_ptr<StorageImpl> lazy_clone_storage(
21     StorageImpl& storage);
22 
23 // Check if a storage has a simple DataPtr with no abnormal context
24 C10_API bool has_simple_data_ptr(const c10::StorageImpl& storage);
25 
26 // Check if a DataPtr is COW
27 C10_API bool is_cow_data_ptr(const c10::DataPtr& data_ptr);
28 
29 // Eagerly copies a COW storage's data, turning it into a non-COW storage.
30 C10_API void materialize_cow_storage(StorageImpl& storage);
31 
32 } // namespace c10::impl::cow
33