1libtalloc_dts(3)                    talloc                    libtalloc_dts(3)
2
3
4

NAME

6       libtalloc_dts - Chapter 3: Dynamic type system
7

Dynamic type system

9       Generic programming in the C language is very difficult. There is no
10       inheritance nor templates known from object oriented languages. There
11       is no dynamic type system. Therefore, generic programming in this
12       language is usually done by type-casting a variable to void* and
13       transferring it through a generic function to a specialized callback as
14       illustrated on the next listing.
15
16       void generic_function(callback_fn cb, void *pvt)
17       {
18         /* do some stuff and call the callback */
19         cb(pvt);
20       }
21
22       void specific_callback(void *pvt)
23       {
24         struct specific_struct *data;
25         data = (struct specific_struct*)pvt;
26         /* ... */
27       }
28
29       void specific_function()
30       {
31         struct specific_struct data;
32         generic_function(callback, &data);
33       }
34
35       Unfortunately, the type information is lost as a result of this type
36       cast. The compiler cannot check the type during the compilation nor are
37       we able to do it at runtime. Providing an invalid data type to the
38       callback will result in unexpected behaviour (not necessarily a crash)
39       of the application. This mistake is usually hard to detect because it
40       is not the first thing which comes the mind.
41
42       As we already know, every talloc context contains a name. This name is
43       available at any time and it can be used to determine the type of a
44       context even if we lose the type of a variable.
45
46       Although the name of the context can be set to any arbitrary string,
47       the best way of using it to simulate the dynamic type system is to set
48       it directly to the type of the variable.
49
50       It is recommended to use one of talloc() and talloc_array() (or its
51       variants) to create the context as they set its name to the name of the
52       given type automatically.
53
54       If we have a context with such as a name, we can use two similar
55       functions that do both the type check and the type cast for us:
56
57       · talloc_get_type()
58
59       · talloc_get_type_abort()
60

Examples

62       The following example will show how generic programming with talloc is
63       handled - if we provide invalid data to the callback, the program will
64       be aborted. This is a sufficient reaction for such an error in most
65       applications.
66
67       void foo_callback(void *pvt)
68       {
69         struct foo *data = talloc_get_type_abort(pvt, struct foo);
70         /* ... */
71       }
72
73       int do_foo()
74       {
75         struct foo *data = talloc_zero(NULL, struct foo);
76         /* ... */
77         return generic_function(foo_callback, data);
78       }
79
80       But what if we are creating a service application that should be
81       running for the uptime of a server, we may want to abort the
82       application during the development process (to make sure the error is
83       not overlooked) and try to recover from the error in the customer
84       release. This can be achieved by creating a custom abort function with
85       a conditional build.
86
87       void my_abort(const char *reason)
88       {
89         fprintf(stderr, 'talloc abort: %s0, reason);
90       #ifdef ABORT_ON_TYPE_MISMATCH
91         abort();
92       #endif
93       }
94
95       The usage of talloc_get_type_abort() would be then:
96
97       talloc_set_abort_fn(my_abort);
98
99       TALLOC_CTX *ctx = talloc_new(NULL);
100       char *str = talloc_get_type_abort(ctx, char);
101       if (str == NULL) {
102         /* recovery code */
103       }
104       /* talloc abort: ../src/main.c:25: Type mismatch:
105          name[talloc_new: ../src/main.c:24] expected[char] */
106
107Version 2.0                       12 Apr 2016                 libtalloc_dts(3)
Impressum