1libtalloc_bestpractices(3Version) libtalloc_bestpractices(3Version)
2
3
4
6 libtalloc_bestpractices - Chapter 7: Best practises
7
8 The following sections contain several best practices and good manners
9 that were found by the Samba and SSSD developers over the years.
10
11 These will help you to write code which is better, easier to debug and
12 with as few (hopefully none) memory leaks as possible.
13
15 The talloc is a hierarchy memory allocator. The hierarchy nature is
16 what makes the programming more error proof. It makes the memory easier
17 to manage and to free. Therefore, the first thing we should have on our
18 mind is: always project your data structures into the talloc context
19 hierarchy.
20
21 That means if we have a structure, we should always use it as a parent
22 context for its elements. This way we will not encounter any troubles
23 when freeing the structure or when changing its parent. The same rule
24 applies for arrays.
25
26 For example, the structure user from section Hierarchy of talloc
27 context should be created with the context hierarchy illustrated on the
28 next image.
29
31 It is a good practice to create a temporary talloc context at the
32 function beginning and free the context just before the return
33 statement. All the data must be allocated on this context or on its
34 children. This ensures that no memory leaks are created as long as we
35 do not forget to free the temporary context.
36
37 This pattern applies to both situations - when a function does not
38 return any dynamically allocated value and when it does. However, it
39 needs a little extension for the latter case.
40
41 Functions that do not return any dynamically allocated
42 value
43
44 If the function does not return any value created on the heap, we will
45 just obey the aforementioned pattern.
46
47 int bar()
48 {
49 int ret;
50 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
51 if (tmp_ctx == NULL) {
52 ret = ENOMEM;
53 goto done;
54 }
55 /* allocate data on tmp_ctx or on its descendants */
56 ret = EOK;
57 done:
58 talloc_free(tmp_ctx);
59 return ret;
60 }
61
62 Functions returning dynamically allocated values
63 If our function returns any dynamically allocated data, its first
64 parameter should always be the destination talloc context. This context
65 serves as a parent for the output values. But again, we will create the
66 output values as the descendants of the temporary context. If
67 everything goes well, we will change the parent of the output values
68 from the temporary to the destination talloc context.
69
70 This pattern ensures that if an error occurs (e.g. I/O error or
71 insufficient amount of the memory), all allocated data is freed and no
72 garbage appears on the destination context.
73
74 int struct_foo_init(TALLOC_CTX *mem_ctx, struct foo **_foo)
75 {
76 int ret;
77 struct foo *foo = NULL;
78 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
79 if (tmp_ctx == NULL) {
80 ret = ENOMEM;
81 goto done;
82 }
83 foo = talloc_zero(tmp_ctx, struct foo);
84 /* ... */
85 *_foo = talloc_steal(mem_ctx, foo);
86 ret = EOK;
87 done:
88 talloc_free(tmp_ctx);
89 return ret;
90 }
91
93 As it can be seen on the previous listing, instead of allocating the
94 temporary context directly on mem_ctx, we created a new top level
95 context using NULL as the parameter for talloc_new() function. Take a
96 look at the following example:
97
98 char *create_user_filter(TALLOC_CTX *mem_ctx,
99 uid_t uid, const char *username)
100 {
101 char *filter = NULL;
102 char *sanitized_username = NULL;
103 /* tmp_ctx is a child of mem_ctx */
104 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
105 if (tmp_ctx == NULL) {
106 return NULL;
107 }
108
109 sanitized_username = sanitize_string(tmp_ctx, username);
110 if (sanitized_username == NULL) {
111 talloc_free(tmp_ctx);
112 return NULL;
113 }
114
115 filter = talloc_aprintf(tmp_ctx,"(|(uid=%llu)(uname=%s))",
116 uid, sanitized_username);
117 if (filter == NULL) {
118 return NULL; /* tmp_ctx is not freed */ (*@\label{lst:tmp-ctx-3:leak}@*)
119 }
120
121 /* filter becomes a child of mem_ctx */
122 filter = talloc_steal(mem_ctx, filter);
123 talloc_free(tmp_ctx);
124 return filter;
125 }
126
127 We forgot to free tmp_ctx before the return statement in the filter ==
128 NULL condition. However, it is created as a child of mem_ctx context
129 and as such it will be freed as soon as the mem_ctx is freed.
130 Therefore, no detectable memory leak is created.
131
132 On the other hand, we do not have any way to access the allocated data
133 and for all we know mem_ctx may exist for the lifetime of our
134 application. For these reasons this should be considered as a memory
135 leak. How can we detect if it is unreferenced but still attached to its
136 parent context? The only way is to notice the mistake in the source
137 code.
138
139 But if we create the temporary context as a top level context, it will
140 not be freed and memory diagnostic tools (e.g. valgrind) are able to do
141 their job.
142
144 If we want to take the advantage of the talloc pool but also keep to
145 the pattern introduced in the previous section, we are unable to do it
146 directly. The best thing to do is to create a conditional build where
147 we can decide how do we want to create the temporary context. For
148 example, we can create the following macros:
149
150 #ifdef USE_POOL_CONTEXT
151 #define CREATE_POOL_CTX(ctx, size) talloc_pool(ctx, size)
152 #define CREATE_TMP_CTX(ctx) talloc_new(ctx)
153 #else
154 #define CREATE_POOL_CTX(ctx, size) talloc_new(ctx)
155 #define CREATE_TMP_CTX(ctx) talloc_new(NULL)
156 #endif
157
158 Now if our application is under development, we will build it with
159 macro USE_POOL_CONTEXT undefined. This way, we can use memory
160 diagnostic utilities to detect memory leaks.
161
162 The release version will be compiled with the macro defined. This will
163 enable pool contexts and therefore reduce the malloc() calls, which
164 will end up in a little bit faster processing.
165
166 int struct_foo_init(TALLOC_CTX *mem_ctx, struct foo **_foo)
167 {
168 int ret;
169 struct foo *foo = NULL;
170 TALLOC_CTX *tmp_ctx = CREATE_TMP_CTX(mem_ctx);
171 /* ... */
172 }
173
174 errno_t handle_request(TALLOC_CTX mem_ctx)
175 {
176 int ret;
177 struct foo *foo = NULL;
178 TALLOC_CTX *pool_ctx = CREATE_POOL_CTX(NULL, 1024);
179 ret = struct_foo_init(mem_ctx, &foo);
180 /* ... */
181 }
182
183talloc 2.0" libtalloc_bestpractices(3Version)