1libtalloc_pools(3)                  talloc                  libtalloc_pools(3)
2
3
4

NAME

6       libtalloc_pools - Chapter 5: Memory pools
7
8

Memory pools

10       Allocation of a new memory is an expensive operation and large programs
11       can contain thousands of calls of malloc() for a single computation,
12       where every call allocates only a very small amount of the memory. This
13       can result in an undesirable slowdown of the application. We can avoid
14       this slowdown by decreasing the number of malloc() calls by using a
15       memory pool.
16
17       A memory pool is a preallocated memory space with a fixed size. If we
18       need to allocate new data we will take the desired amount of the memory
19       from the pool instead of requesting a new memory from the system. This
20       is done by creating a pointer that points inside the preallocated
21       memory. Such a pool must not be reallocated as it would change its
22       location - pointers that were pointing inside the pool would become
23       invalid. Therefore, a memory pool requires a very good estimate of the
24       required memory space.
25
26       The talloc library contains its own implementation of a memory pool. It
27       is highly transparent for the programmer. The only thing that needs to
28       be done is an initialization of a new pool context using talloc_pool()
29       - which can be used in the same way as any other context.
30
31       Refactoring of existing code (that uses talloc) to take the advantage
32       of a memory pool is quite simple due to the following properties of the
33       pool context:
34
35       · if we are allocating data on a pool context, it takes the desired
36         amount of memory from the pool,
37
38       · if the context is a descendant of the pool context, it takes the
39         space from the pool as well,
40
41       · if the pool does not have sufficient portion of memory left, it will
42         create a new non-pool context, leaving the pool intact
43
44       /* allocate 1KiB in a pool */
45       TALLOC_CTX *pool_ctx = talloc_pool(NULL, 1024);
46
47       /* Take 512B from the pool, 512B is left there */
48       void *ptr = talloc_size(pool_ctx, 512);
49
50       /* 1024B > 512B, this will create new talloc chunk outside
51          the pool */
52       void *ptr2 = talloc_size(ptr, 1024);
53
54       /* The pool still contains 512 free bytes
55        * this will take 200B from them. */
56       void *ptr3 = talloc_size(ptr, 200);
57
58       /* This will destroy context 'ptr3' but the memory
59        * is not freed, the available space in the pool
60        * will increase to 512B. */
61       talloc_free(ptr3);
62
63       /* This will free memory taken by 'pool_ctx'
64        * and 'ptr2' as well. */
65       talloc_free(pool_ctx);
66
67       The above given is very convenient, but there is one big issue to be
68       kept in mind. If the parent of a talloc pool child is changed to a
69       parent that is outside of this pool, the whole pool memory will not be
70       freed until the child is freed. For this reason we must be very careful
71       when stealing a descendant of a pool context.
72
73       TALLOC_CTX *mem_ctx = talloc_new(NULL);
74       TALLOC_CTX *pool_ctx = talloc_pool(NULL, 1024);
75       struct foo *foo = talloc(pool_ctx, struct foo);
76
77       /* mem_ctx is not in the pool */
78       talloc_steal(mem_ctx, foo);
79
80       /* pool_ctx is marked as freed but the memory is not
81          deallocated, accessing the pool_ctx again will cause
82          an error */
83       talloc_free(pool_ctx);
84
85       /* This deallocates the pool_ctx. */
86       talloc_free(mem_ctx);
87
88       It may often be better to copy the memory we want instead of stealing
89       it to avoid this problem. If we do not need to retain the context name
90       (to keep the type information), we can use talloc_memdup() to do this.
91
92       Copying the memory out of the pool may, however, discard all the
93       performance boost given by the pool, depending on the size of the
94       copied memory. Therefore, the code should be well profiled before
95       taking this path. In general, the golden rule is: if we need to steal
96       from the pool context, we should not use a pool context.
97
98
99
100Version 2.0                     Tue Oct 30 2018             libtalloc_pools(3)
Impressum