1 .\" Id: memcluster.mdoc,v 1.3 2004/03/09 06:30:08 marka Exp
3 .\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4 .\" Copyright (c) 1995-1999 by Internet Software Consortium
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 .\" The following six UNCOMMENTED lines are required.
20 .\"Os OPERATING_SYSTEM [version/release]
22 .\"Dt DOCUMENT_TITLE [section number] [volume]
29 .Nd memory allocation/deallocation system
31 .Fd #include \&<isc/memcluster.h\&>
33 .Fn memget "size_t size"
35 .Fn memput "void *mem" "size_t size"
37 .Fn memstats "FILE *out"
39 These functions access a memory management system which allows callers to not
40 fragment memory to the extent which can ordinarily occur through many random
45 gets a large contiguous chunk of blocks of the requested
47 and parcels out these blocks as requested. The symmetric call is
49 which callers use to return a piece of memory obtained from
51 Statistics about memory usage are returned by
53 which prints a report on the stream
56 Internally, linked lists of free memory blocks are stored in an array.
57 The size of this array is determined by the value
59 currently set to 1100. In general, for any requested blocksize
61 any free blocks will be stored on the linked list at that index.
62 No free lists are managed for blocks greater than or equal to
64 bytes; instead, calls to
70 Since the blocks are actually stored as linked lists, they must at least
71 be large enough to hold a pointer to the next block. This size, which is
72 .Dv SMALL_SIZE_LIMIT ,
73 is currently defined as
74 .Bd -literal -offset indent
75 #define SMALL_SIZE_LIMIT sizeof(struct { void *next; })
82 enforce this limit; for example, any call to
84 requesting a block smaller than
86 bytes will actually be considered to be of size
88 internally. (Such a caller request will be logged for
90 purposes using the caller-requested
94 below, for more information.)
96 Additionally, the requested
98 will be adjusted so that when a large
100 chunk of memory is broken up into a linked list, the blocks will all fall on
101 the correct memory alignment boundaries. Thus, one can conceptualize a call
106 which is used internally.
108 In order to more efficiently allocate memory, there is a
112 It is given by the pre-defined value
114 which is currently 4096 bytes.
115 For any requested block
118 .Xr malloc 3 Ns No -d
119 in order to fill up a block of about
123 For allocations larger than
124 .Dv MEM_TARGET Ns No /2
127 introduced which boosts the target size by 25% of
129 This means that enough memory for two blocks
130 will actually be allocated for any
133 .Pq Dv MEM_TARGET Ns No / 3
135 .Pq Dv MEM_TARGET Ns No *5/8 ,
136 provided that the value of
138 is at least as large as the upper limit shown above.]
140 .Ss FUNCTION DESCRIPTIONS
144 returns a pointer to a block of memory of at least the requested
150 as mentioned above in the
152 subsection, the internal array of free lists is checked.
153 If there is no block of the needed
159 a chunk of memory which is as many times as
161 will fit into the target size. This memory is then turned into a linked list
163 .Va new_size Ns No -sized
164 blocks which are given out as requested; the last such block is the first one
169 is zero or negative, then
177 is larger than or equal to the pre-defined maximum size
179 then only a single block of exactly
182 .Xr malloc 3 Ns No -d
187 call is used to return memory once the caller is finished with it.
194 subsection, above, the block is placed at the head of the free list of
195 .Va new_size Ns -sized
199 is zero or negative, then
207 is larger than or equal to the pre-defined maximum size
209 then the block is immediately
210 .Xr free 3 Ns No -d .
213 It is important that callers give
216 blocks of memory which were previously obtained from
222 bytes in size. Since all blocks will be added to a free list, any block
223 which is not at least
225 bytes long will not be able to hold a pointer to the next block in the
230 function will summarize the number of calls to
234 for any block size from 1 byte up to
235 .Pq Dv MEM_FREECOUNT No - 1
236 bytes, followed by a single line for any calls using a
238 greater than or equal to
240 a brief header with shell-style comment lines prefaces the report and
241 explains the information. The
245 identifies the stream which is used for this report. Currently,
247 reports the number of calls to
251 using the caller-supplied value
253 the percentage of outstanding blocks of a given size (i.e., the percentage
258 are also reported on the line for blocks of the given
260 However, the percent of blocks used is computed using the number of
261 blocks allocated according to the internal parameter
263 it is the percentage of blocks used to those available at a given
265 and is computed using the
271 which map to the internally-computed
279 which has these implications:
280 .Bl -enum -offset indent
285 .Dv SMALL_SIZE_LIMIT ,
288 show statistics for caller requests under
290 but "percent used" information about such blocks will be reported under
291 .Dv SMALL_SIZE_LIMIT Ns No -sized
294 As a general case of point 1, internal statistics are reported on the the
295 line corresponding to
297 so that, for a given caller-supplied
299 the associated internal information will appear on that line or on the next
300 line which shows "percent used" information.
304 If the caller returns blocks of a given
306 and requests others of
308 which map to the same internal
312 to report usage of greater than 100% for blocks of size
314 This should be viewed as A Good Thing.
320 pointer to a block of memory of the requested
326 is invalid (less than or equal to zero) or a
328 of a new block of memory fails. In the former case,
332 in the latter, it is set to
340 .\" This next request is for sections 1, 6, 7 & 8 only
344 .\" This next request is for sections 1, 6, 7 & 8 only
345 .\" (command return values (to shell) and
346 .\" fprintf/stderr type diagnostics)
348 .\" The next request is for sections 2 and 3 error
349 .\" and signal handling only.
353 .Bl -tag -width "ENOMEM " -offset indent
375 Steven J. Richardson and Paul Vixie, Vixie Enterprises.