4 \chapter{Bacula Memory Management}
6 \index{Management!Bacula Memory}
7 \index{Bacula Memory Management}
8 \addcontentsline{toc}{section}{Bacula Memory Management}
12 \addcontentsline{toc}{subsection}{General}
14 This document describes the memory management routines that are used in Bacula
15 and is meant to be a technical discussion for developers rather than part of
18 Since Bacula may be called upon to handle filenames of varying and more or
19 less arbitrary length, special attention needs to be used in the code to
20 ensure that memory buffers are sufficiently large. There are four
21 possibilities for memory usage within {\bf Bacula}. Each will be described in
25 \item Statically allocated memory.
26 \item Dynamically allocated memory using malloc() and free().
27 \item Non-pooled memory.
31 \subsection{Statically Allocated Memory}
32 \index{Statically Allocated Memory}
33 \index{Memory!Statically Allocated}
34 \addcontentsline{toc}{subsubsection}{Statically Allocated Memory}
36 Statically allocated memory is of the form:
40 char buffer[MAXSTRING];
44 The use of this kind of memory is discouraged except when you are 100\% sure
45 that the strings to be used will be of a fixed length. One example of where
46 this is appropriate is for {\bf Bacula} resource names, which are currently
47 limited to 127 characters (MAX\_NAME\_LENGTH). Although this maximum size may
48 change, particularly to accommodate Unicode, it will remain a relatively small
51 \subsection{Dynamically Allocated Memory}
52 \index{Dynamically Allocated Memory}
53 \index{Memory!Dynamically Allocated}
54 \addcontentsline{toc}{subsubsection}{Dynamically Allocated Memory}
56 Dynamically allocated memory is obtained using the standard malloc() routines.
66 This kind of memory can be released with:
74 It is recommended to use this kind of memory only when you are sure that you
75 know the memory size needed and the memory will be used for short periods of
76 time -- that is it would not be appropriate to use statically allocated
77 memory. An example might be to obtain a large memory buffer for reading and
78 writing files. When {\bf SmartAlloc} is enabled, the memory obtained by
79 malloc() will automatically be checked for buffer overwrite (overflow) during
80 the free() call, and all malloc'ed memory that is not released prior to
81 termination of the program will be reported as Orphaned memory.
83 \subsection{Pooled and Non-pooled Memory}
84 \index{Memory!Pooled and Non-pooled}
85 \index{Pooled and Non-pooled Memory}
86 \addcontentsline{toc}{subsubsection}{Pooled and Non-pooled Memory}
88 In order to facility the handling of arbitrary length filenames and to
89 efficiently handle a high volume of dynamic memory usage, we have implemented
90 routines between the C code and the malloc routines. The first is called
91 ``Pooled'' memory, and is memory, which once allocated and then released, is
92 not returned to the system memory pool, but rather retained in a Bacula memory
93 pool. The next request to acquire pooled memory will return any free memory
94 block. In addition, each memory block has its current size associated with the
95 block allowing for easy checking if the buffer is of sufficient size. This
96 kind of memory would normally be used in high volume situations (lots of
97 malloc()s and free()s) where the buffer length may have to frequently change
98 to adapt to varying filename lengths.
100 The non-pooled memory is handled by routines similar to those used for pooled
101 memory, allowing for easy size checking. However, non-pooled memory is
102 returned to the system rather than being saved in the Bacula pool. This kind
103 of memory would normally be used in low volume situations (few malloc()s and
104 free()s), but where the size of the buffer might have to be adjusted
107 \paragraph*{Types of Memory Pool:}
109 Currently there are three memory pool types:
112 \item PM\_NOPOOL -- non-pooled memory.
113 \item PM\_FNAME -- a filename pool.
114 \item PM\_MESSAGE -- a message buffer pool.
115 \item PM\_EMSG -- error message buffer pool.
118 \paragraph*{Getting Memory:}
120 To get memory, one uses:
124 void *get_pool_memory(pool);
128 where {\bf pool} is one of the above mentioned pool names. The size of the
129 memory returned will be determined by the system to be most appropriate for
132 If you wish non-pooled memory, you may alternatively call:
136 void *get_memory(size_t size);
140 The buffer length will be set to the size specified, and it will be assigned
141 to the PM\_NOPOOL pool (no pooling).
143 \paragraph*{Releasing Memory:}
145 To free memory acquired by either of the above two calls, use:
149 void free_pool_memory(void *buffer);
153 where buffer is the memory buffer returned when the memory was acquired. If
154 the memory was originally allocated as type PM\_NOPOOL, it will be released to
155 the system, otherwise, it will be placed on the appropriate Bacula memory pool
156 free chain to be used in a subsequent call for memory from that pool.
158 \paragraph*{Determining the Memory Size:}
160 To determine the memory buffer size, use:
164 size_t sizeof_pool_memory(void *buffer);
168 \paragraph*{Resizing Pool Memory:}
170 To resize pool memory, use:
174 void *realloc_pool_memory(void *buffer);
178 The buffer will be reallocated, and the contents of the original buffer will
179 be preserved, but the address of the buffer may change.
181 \paragraph*{Automatic Size Adjustment:}
183 To have the system check and if necessary adjust the size of your pooled
188 void *check_pool_memory_size(void *buffer, size_t new-size);
192 where {\bf new-size} is the buffer length needed. Note, if the buffer is
193 already equal to or larger than {\bf new-size} no buffer size change will
194 occur. However, if a buffer size change is needed, the original contents of
195 the buffer will be preserved, but the buffer address may change. Many of the
196 low level Bacula subroutines expect to be passed a pool memory buffer and use
197 this call to ensure the buffer they use is sufficiently large.
199 \paragraph*{Releasing All Pooled Memory:}
201 In order to avoid orphaned buffer error messages when terminating the program,
206 void close_memory_pool();
210 to free all unused memory retained in the Bacula memory pool. Note, any memory
211 not returned to the pool via free\_pool\_memory() will not be released by this
214 \paragraph*{Pooled Memory Statistics:}
216 For debugging purposes and performance tuning, the following call will print
217 the current memory pool statistics:
221 void print_memory_pool_stats();
225 an example output is:
229 Pool Maxsize Maxused Inuse