The talloc context is the most important part of this library and is responsible for every single feature of this memory allocator. It is a logical unit which represents a memory space managed by talloc.
From the programmer's point of view, the talloc context is completely equivalent to a pointer that would be returned by the memory routines from the C standard library. This means that every context that is returned from the talloc library can be used directly in functions that do not use talloc internally. For example we can do the following:
char *str1 = strdup("I am NOT a talloc context"); char *str2 = talloc_strdup(NULL, "I AM a talloc context"); printf("%d, strcmp(str1, str2) == 0); free(str1); talloc_free(str2); /* we can not use free() on str2 */
This is possible because the context is internally handled as a special fixed-length structure called talloc chunk. Each chunk stores context metadata followed by the memory space requested by the programmer. When a talloc function returns a context (pointer), it will in fact return a pointer to the user space portion of the talloc chunk. If we to manipulate this context using talloc functions, the talloc library transforms the user-space pointer back to the starting address of the chunk. This is also the reason why we were unable to use free(str2) in the previous example - because str2 does not point at the beginning of the allocated block of memory. This is illustrated on the next image:
The type TALLOC_CTX is defined in talloc.h to identify a talloc context in function parameters. However, this type is just an alias for void and exists only for semantical reasons - thus we can differentiate between void * (arbitrary data) and TALLOC_CTX * (talloc context).
Every talloc context contains information about its parent and children. Talloc uses this information to create a hierarchical model of memory or to be more precise, it creates an n-ary tree where each node represents a single talloc context. The root node of the tree is referred to as a top level context - a context without any parent.
This approach has several advantages:
struct user { uid_t uid; char *username; size_t num_groups; char **groups; };
We will allocate this structure using talloc. The result will be the following context tree:
/* create new top level context */ struct user *user = talloc(NULL, struct user); user->uid = 1000; user->num_groups = N; /* make user the parent of following contexts */ user->username = talloc_strdup(user, "Test user"); user->groups = talloc_array(user, char*, user->num_groups); for (i = 0; i < user->num_groups; i++) { /* make user->groups the parent of following context */ user->groups[i] = talloc_asprintf(user->groups, "Test group %d", i); }
This way, we have gained a lot of additional capabilities, one of which is very simple deallocation of the structure and all of its elements.
With the C standard library we need first to iterate over the array of groups and free every element separately. Then we must deallocate the array that stores them. Next we deallocate the username and as the last step free the structure itself. But with talloc, the only operation we need to execute is freeing the structure context. Its descendants will be freed automatically.
talloc_free(user);
The talloc is a hierarchy memory allocator. The hierarchy nature is what makes the programming more error proof. It makes the memory easier to manage and to free. Therefore, the first thing we should have on our mind is: always project our data structures into the talloc context hierarchy.
That means if we have a structure, we should always use it as a parent context for its elements. This way we will not encounter any troubles when freeing this structure or when changing its parent. The same rule applies for arrays.
Here are the most important functions that create a new talloc context.
The name of the context is automatically set to the name of the data type which is used to simulate a dynamic type system.
struct user *user = talloc(ctx, struct user); /* initialize to default values */ user->uid = 0; user->name = NULL; user->num_groups = 0; user->groups = NULL; /* or we can achieve the same result with */ struct user *user_zero = talloc_zero(ctx, struct user);
TALLOC_CTX *tmp_ctx = NULL; struct foo *foo = NULL; struct bar *bar = NULL; /* new zero-length top level context */ tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { return ENOMEM; } foo = talloc(tmp_ctx, struct foo); bar = talloc(tmp_ctx, struct bar); /* free everything at once */ talloc_free(tmp_ctx);