1libtalloc_destructors(3Version)                libtalloc_destructors(3Version)
2
3
4

NAME

6       libtalloc_destructors - Chapter 4: Using destructors
7

Using destructors

9       Destructors are well known methods in the world of object oriented
10       programming. A destructor is a method of an object that is
11       automatically run when the object is destroyed. It is usually used to
12       return resources taken by the object back to the system (e.g. closing
13       file descriptors, terminating connection to a database, deallocating
14       memory).
15
16       With talloc we can take the advantage of destructors even in C. We can
17       easily attach our own destructor to a talloc context. When the context
18       is freed, the destructor will run automatically.
19
20       To attach/detach a destructor to a talloc context use:
21       talloc_set_destructor().
22

Example

24       Imagine that we have a dynamically created linked list. Before we
25       deallocate an element of the list, we need to make sure that we have
26       successfully removed it from the list. Normally, this would be done by
27       two commands in the exact order: remove it from the list and then free
28       the element. With talloc, we can do this at once by setting a
29       destructor on the element which will remove it from the list and
30       talloc_free() will do the rest.
31
32       The destructor would be:
33
34       int list_remove(void *ctx)
35       {
36           struct list_el *el = NULL;
37           el = talloc_get_type_abort(ctx, struct list_el);
38           /* remove element from the list */
39       }
40
41       GCC version 3 and newer can check for the types during the compilation.
42       So if it is our major compiler, we can use a more advanced destructor:
43
44       int list_remove(struct list_el *el)
45       {
46           /* remove element from the list */
47       }
48
49       Now we will assign the destructor to the list element. We can do this
50       directly in the function that inserts it.
51
52       struct list_el* list_insert(TALLOC_CTX *mem_ctx,
53                                   struct list_el *where,
54                                   void *ptr)
55       {
56         struct list_el *el = talloc(mem_ctx, struct list_el);
57         el->data = ptr;
58         /* insert into list */
59
60         talloc_set_destructor(el, list_remove);
61         return el;
62       }
63
64       Because talloc is a hierarchical memory allocator, we can go a step
65       further and free the data with the element as well:
66
67       struct list_el* list_insert_free(TALLOC_CTX *mem_ctx,
68                                        struct list_el *where,
69                                        void *ptr)
70       {
71         struct list_el *el = NULL;
72         el = list_insert(mem_ctx, where, ptr);
73
74         talloc_steal(el, ptr);
75
76         return el;
77       }
78
79talloc                               2.0"      libtalloc_destructors(3Version)
Impressum