Data Ownership¶
The Node class provides two ways to hold data, the data is either owned or externally described:
If a Node owns data, the Node allocated the memory holding the data and is responsible or deallocating it.
If a Node externally describes data, the Node holds a pointer to the memory where the data resides and is not responsible for deallocating it.
set vs set_external¶
The Node::set methods support creating owned data and copying data values in both the owned and externally described cases. The Node::set_external methods allow you to create externally described data:
set(…): Makes a copy of the data passed into the Node. This will trigger an allocation if the current data type of the Node is incompatible with what was passed. The Node assignment operators use their respective set variants, so they follow the same copy semantics.
set_external(…): Sets up the Node to describe data passed and access the data externally. Does not copy the data.
C++ Example:
int vsize = 5;
std::vector<float64> vals(vsize,0.0);
for(int i=0;i<vsize;i++)
{
vals[i] = 3.1415 * i;
}
Node n;
n["v_owned"] = vals;
n["v_external"].set_external(vals);
n.info().print();
n.print();
vals[1] = -1 * vals[1];
n.print();
Output:
mem_spaces:
0x7fddedc0a890:
path: "v_owned"
type: "allocated"
bytes: 40
allocator_id: 0
0x7fddedc0a460:
path: "v_external"
type: "external"
total_bytes_allocated: 40
total_bytes_mmaped: 0
total_bytes_compact: 80
total_strided_bytes: 80
v_owned: [0.0, 3.1415, 6.283, 9.4245, 12.566]
v_external: [0.0, 3.1415, 6.283, 9.4245, 12.566]
v_owned: [0.0, 3.1415, 6.283, 9.4245, 12.566]
v_external: [0.0, -3.1415, 6.283, 9.4245, 12.566]