1libtalloc_stealing(3)               talloc               libtalloc_stealing(3)
2
3
4

NAME

6       libtalloc_stealing - Chapter 2: Stealing a context
7

Stealing a context

9       Talloc has the ability to change the parent of a talloc context to
10       another one. This operation is commonly referred to as stealing and it
11       is one of the most important actions performed with talloc contexts.
12
13       Stealing a context is necessary if we want the pointer to outlive the
14       context it is created on. This has many possible use cases, for
15       instance stealing a result of a database search to an in-memory cache
16       context, changing the parent of a field of a generic structure to a
17       more specific one or vice-versa. The most common scenario, at least in
18       Samba, is to steal output data from a function-specific context to the
19       output context given as an argument of that function.
20
21       struct foo {
22           char *a1;
23           char *a2;
24           char *a3;
25       };
26
27       struct bar {
28           char *wurst;
29           struct foo *foo;
30       };
31
32       struct foo *foo = talloc_zero(ctx, struct foo);
33       foo->a1 = talloc_strdup(foo, 'a1');
34       foo->a2 = talloc_strdup(foo, 'a2');
35       foo->a3 = talloc_strdup(foo, 'a3');
36
37       struct bar *bar = talloc_zero(NULL, struct bar);
38       /* change parent of foo from ctx to bar */
39       bar->foo = talloc_steal(bar, foo);
40
41       /* or do the same but assign foo = NULL */
42       bar->foo = talloc_move(bar, &foo);
43
44       The talloc_move() function is similar to the talloc_steal() function
45       but additionally sets the source pointer to NULL.
46
47       In general, the source pointer itself is not changed (it only replaces
48       the parent in the meta data). But the common usage is that the result
49       is assigned to another variable, thus further accessing the pointer
50       from the original variable should be avoided unless it is necessary. In
51       this case talloc_move() is the preferred way of stealing a context.
52       Additionally sets the source pointer to NULL, thus.protects the pointer
53       from being accidentally freed and accessed using the old variable after
54       its parent has been changed.
55
56Version 2.0                       12 Apr 2016            libtalloc_stealing(3)
Impressum