]> git.ozlabs.org Git - ccan/blob - ccan/talloc_link/talloc_link.h
tdb2: copy tdb1's changed expansion logic.
[ccan] / ccan / talloc_link / talloc_link.h
1 /* Licensed under GPLv2+ - see LICENSE file for details */
2 #ifndef TALLOC_LINK_H
3 #define TALLOC_LINK_H
4 #include <ccan/talloc/talloc.h>
5
6 /**
7  * talloc_linked - set up an object with an initial link.
8  * @ctx - the context to initially link to
9  * @newobj - the newly allocated object (with a NULL parent)
10  *
11  * The object will be freed when @ctx is freed (or talloc_delink(ctx,
12  * newobj) is called), unless more links are added using
13  * talloc_link().
14  *
15  * For convenient chaining, it returns @newobj on success, or frees
16  * @newobj and returns NULL.
17  */
18 #define talloc_linked(ctx, newobj) \
19         ((_TALLOC_TYPEOF(newobj))_talloc_linked((ctx), (newobj)))
20
21 /**
22  * talloc_link - add another link to a linkable object.
23  * @ctx - the context to link to
24  * @obj - the object previously made linkable with talloc_linked().
25  *
26  * The @obj will only be freed when all contexts linked to it are
27  * freed (or talloc_delink()ed).
28  *
29  * Returns @obj, or NULL on failure (out of memory).
30  */
31 #define talloc_link(ctx, obj) \
32         ((_TALLOC_TYPEOF(obj))_talloc_link((ctx), (obj)))
33
34 /**
35  * talloc_delink - explicitly remove a link from a linkable object.
36  * @ctx - the context previously used for talloc_link/talloc_linked
37  * @obj - the object previously used for talloc_link/talloc_linked
38  *
39  * Explicitly remove a link: normally it is implied by freeing @ctx.
40  * Removing the last link frees the object.
41  */
42 void talloc_delink(const void *ctx, const void *linked);
43
44 /* Internal helpers. */
45 void *_talloc_link(const void *ctx, const void *linked);
46 void *_talloc_linked(const void *ctx, const void *linked);
47
48 #endif /* TALLOC_LINK_H */