5 .TH VMALLOC 3 "1 May 1998"
7 vmalloc \- virtual memory allocation
9 .MW "#include <vmalloc.h>"
12 .MW "Vmalloc_t* vmopen(Vmdisc_t* disc, Vmethod_t* meth, int flags);"
13 .MW "int vmclose(Vmalloc_t*);"
14 .MW "int vmclear(Vmalloc_t*);"
15 .MW "int vmcompact(Vmalloc_t* region);"
16 .MW "int vmset(Vmalloc_t* region, int flags, int type);"
17 .MW "Vmalloc_t* Vmheap;"
18 .MW "Vmdisc_t* vmdisc(Vmalloc_t* region, Vmdisc_t* disc);"
19 .MW "Vmalloc_t* vmmopen(char* file, Void_t* base, size_t round);"
20 .MW "Void_t* vmmset(Vmalloc_t* vm, int key, Void_t* data, int set);"
22 .SS "Allocation functions"
24 .MW "Void_t* vmalloc(Vmalloc_t* region, size_t size);"
25 .MW "Void_t* vmalign(Vmalloc_t* region, size_t size, size_t align);"
26 .MW "Void_t* vmresize(Vmalloc_t* region, Void_t* addr, size_t size, int type);"
27 .MW "int vmfree(Vmalloc_t* region, Void_t* addr);"
28 .MW "Void_t* vmnewof(Vmalloc_t* region, Void_t* addr, type, size_t n, size_t x);"
29 .MW "Void_t* vmoldof(Vmalloc_t* region, Void_t* addr, type, size_t n, size_t x);"
30 .MW "Void_t* vmgetmem(Vmalloc_t* region, Void_t* addr, size_t size);"
34 .MW "int vmdebug(int);"
35 .MW "int vmdbcheck(Vmalloc_t* vm);"
36 .MW "int vmdbwatch(Void_t* addr);"
37 .MW "static void vmdbwarn(Vmalloc_t*, char* mesg, int n);"
41 .MW "void vmprofile(Vmalloc_t* vm, int fd);"
43 .SS "Information and statistics"
45 .MW "Vmalloc_t* vmregion(Void_t* addr);"
46 .MW "Void_t* vmsegment(Vmalloc_t* region, Void_t* addr);"
47 .MW "int vmwalk(Vmalloc_t* region, int(*walkf)(Vmalloc_t*, Void_t*, size_t, size_t, Vmdisc_t*, Void_t*), Void_t* handle);"
48 .MW "long vmaddr(Vmalloc_t* region, Void_t* addr);"
49 .MW "long vmsize(Vmalloc_t* region, Void_t* addr);"
50 .MW "int vmstat(Vmalloc_t* vm, Vmstat_t* statb);"
51 .MW "int vmtrace(int fd);"
52 .MW "int vmtrbusy(Vmalloc_t* vm);"
54 .SS "Malloc-compatible functions"
56 .MW "Void_t* malloc(size_t size);"
57 .MW "Void_t* realloc(Void_t* addr, size_t size);"
58 .MW "Void_t* calloc(size_t n_obj, size_t s_obj);"
59 .MW "int cfree(Void_t* addr);"
60 .MW "void free(Void_t* addr);"
61 .MW "Void_t* memalign(size_t align, size_t size);"
62 .MW "Void_t* valloc(size_t size);"
65 These functions for dynamic storage allocation work in
66 \fIregions\fP of memory.
67 Each region has an \fIallocation method\fP
68 for parceling out blocks of storage and a
69 \fImemory discipline\fP for obtaining raw space.
70 Automatic locking prevents interference by reentrant
73 Pointers to space have type \f5Void_t*\fP
74 where \f5Void_t\fP is \f5#define\fPd as \f5void\fP if possible, otherwise \f5char\fP.
75 Space is counted in type \f5size_t\fP.
79 Regions have type \f5Vmalloc_t\fP.
80 Two predefined regions are pointed to by:
83 A general-purpose region, with best-fit
84 allocation, and Unix memory discipline \f5Vmdcsbrk\fP.
86 These functions manipulate regions:
89 creates a region with memory discipline \fIdisc\fP,
90 allocation method \fImeth\fP,
91 and a setting for control \fIflags\fP.
92 It returns a pointer to the region on success and \f5NULL\fP on failure.
93 The flags, represented by bit values or-ed together, are:
96 Disable locking and consistency checks, except under method \f5Vmdebug\fP.
99 Place tracing messages for each allocation event
100 on the tracing file established by \fIvmtrace\fP.
101 This turns off \f5VM_TRUST\fP.
103 \f5VM_DBCHECK\fP, \f5VM_DBABORT\fP
105 See \fBDebugging\fP below.
108 closes a \fIregion\fP and releases all associated memory
109 according to the region's discipline.
110 The first segment obtained from the discipline's
111 \f5memoryf\fP function (see `Disciplines' below) will be the last released.
112 \fIvmclose\fP returns \-1 on failure and a non-negative value otherwise.
115 frees all allocated blocks in \fIregion\fP regardless of methods.
116 It returns \-1 on failure and a non-negative value otherwise.
119 releases as much of a \fIregion\fP's
120 free space to its discipline's \f5memoryf\fP
121 function as possible.
122 It returns a nonnegative value on success and \-1 on failure.
125 adjusts and queries a \fIregion\fP's \fIflags\fP.
126 The indicated flags are turned on if \fItype\fP is nonzero, off if zero.
127 \fIvmset\fP returns the previous value of all flags.
128 Thus, \f5vmset(region,0,0)\fP queries the flags without changing them.
129 In addition to the settable flags, one of
130 \f5VM_MTBEST\fP, \f5VM_MTDEBUG\fP, \f5VM_MTPROFILE\fP,
131 \f5VM_MTPOOL\fP, or \f5VM_MTLAST\fP
132 is returned to indicate the method used in creating the \fIregion\fP.
135 changes the discipline of \fIregion\fP to the given new discipline
136 \fIdisc\fP if \fIdisc\fP is not \f5NULL\fP and its \f5memoryf\fP function
137 is the same as the current discipline. If the current discipline
138 has an \f5exceptf\fP function, it will be called with event \f5VM_DISC\fP.
139 This function always returns the current discipline.
142 creates a region to allocate memory given by the system call \fImmap(2)\fP.
143 The given \fIfile\fP is the backing store for the map.
144 If \fIbase\fP is not \f5NULL\fP, it is the address to map the data to.
145 The \fIround\fP argument asserts that the size of the region is always
146 a multiple of this value. However, note that \fIvmmopen\fP has
147 an internally defined minimum round value (typically 64K) which may be
148 used if the given value is too small.
151 sets a \fIdata\fP value associated with a \fIkey\fP if \f5set\fP is non-zero.
152 In this case, it returns the current data (before setting)
153 if \fIkey\fP is previously set; otherwise, it returns the new \fIdata\fP value.
154 If the argument \fIset\fP is zero,
155 the call returns the current data value associated with \fIkey\fP, if any.
157 .SS "Allocation functions"
159 returns a pointer to a block of the requested \fIsize\fP
160 in a \fIregion\fP, aligned to the \fIstrictest alignment\fP
161 that is suitable for the needs of any basic data type.
162 It returns \f5NULL\fP on failure.
165 works like \fIvmalloc\fP, but returns a block aligned to a common
166 multiple of \fIalign\fP and the \fIstrictest alignment\fP.
169 attempts to change the length of the block pointed to by
170 \fIaddr\fP to the specified \fIsize\fP.
171 If that is impossible and \fItype\fP has
172 at least one of \f5VM_RSMOVE\fP and \f5VM_RSCOPY\fP,
173 a new block is allocated and the old block is freed.
174 The bit \f5VM_RSCOPY\fP also causes
175 the new block to be initialized with
176 as much of the old contents as will fit.
177 When a resized block gets larger, the new space will be cleared
178 if \fItype\fP has the bit \f5VM_RSZERO\fP.
180 returns a pointer to the final block, or \f5NULL\fP on failure.
181 If \fIaddr\fP is \f5NULL\fP, \fIvmresize\fP behaves like \fIvmalloc\fP;
182 otherwise, if \fIsize\fP is 0, it behaves like \fIvmfree\fP.
185 makes the currently allocated block pointed to by
186 \fIaddr\fP available for future allocations in its \fIregion\fP.
187 If \fIaddr\fP is \f5NULL\fP, \fIvmfree\fP does nothing.
188 It returns \-1 on error, and nonnegative otherwise.
191 is a macro function that attempts to change the length of
192 the block pointed to by \fIaddr\fP to the size \f5n*sizeof(type)+x\fP.
193 If the block is moved, new space will be initialized with as much of the
194 old content as will fit.
195 Additional space will be set to zero.
198 is similar to \fIvmnewof\fP but it neither copies data nor clears space.
201 provides a handy function to creat/close regions and allocate/free memory
202 based on chunks of memory obtained from the heap region \fIVmheap\fP.
204 .MW "vmgetmem(0,0,0)"
205 This call opens a new region.
207 .MW "vmgetmem(region, 0, 0)"
208 This call closes the given \f5region\fP.
210 .MW "vmgetmem(region,0,n)"
211 This call allocates a block of length \f5n\fP and clears it to zeros.
213 .MW "vmgetmem(region,p,0)"
214 This call frees the block \f5p\fP.
216 .MW "vmgetmem(region,p,n)"
217 This call resizes the block \f5p\fP to length \f5n\fP
218 and clears the new memory to zeros if the block grows.
219 The block may be moved as deemed necessary by the allocator.
221 .SS "Memory disciplines"
222 Memory disciplines have type \f5Vmdisc_t\fP,
223 a structure with these members:
226 .MW "Void_t* (*memoryf)(Vmalloc_t *region, Void_t* obj,"
228 .MW "size_t csz, size_t nsz, Vmdisc_t *disc);"
229 .MW "int (*exceptf)(Vmalloc_t *region, int type, Void_t* obj, Vmdisc_t *disc);"
235 If this value is positive, all size arguments to the
236 \f5memoryf\fP function will be multiples of it.
239 Points to a function to get or release segments of space for the
243 If this pointer is not \f5NULL\fP,
244 the function it points to is called to announce
245 events in a \fIregion\fP.
247 There are two standard disciplines.
249 \f5round\fP is 0, and \f5exceptf\fP is \f5NULL\fP.
252 A discipline whose \f5memoryf\fP function gets space from \fIsbrk\fP(2).
257 A discipline whose \f5memoryf\fP function gets space from the region \f5Vmheap\fP.
258 A region with \f5Vmdcheap\fP discipline and \f5Vmlast\fP
259 allocation is good for building throwaway data structures.
262 function returns a pointer to a memory segment on success, and \f5NULL\fP on failure.
263 If \fIcsz\fP is 0, the function returns a new segment of size \fInsz\fP.
264 Otherwise, the function attempts to change the length of the segment
265 pointed to by \fIaddr\fP from \fIcsz\fP to \fInsz\fP.
266 If this is successful, \f5memoryf\fP should return \fIaddr\fP (even if \fInsz\fP is 0).
269 function is called for events identified by \fItype\fP, which is coded thus:
272 A new region is being opened.
273 If \fIexceptf\fP returns a zero value, the region opening proceeds normally.
274 A negative return value causes \fIvmopen\fP to terminate with failure.
275 A positive return value indicates that the new region is to manipulate memory
276 already initialized by a previous \fIvmopen\fP call
277 (perhaps by a different process on persistent or shared memory).
278 In this case, the argument \f5(Void_t**)\fP\fIobj\fP should
279 return the initial segment (which is of type \f5(Void_t*)\fP).
280 \fIvmopen\fP will return failure if this segment is not returned or if it
281 has not been properly initialized.
284 The region is being closed.
285 The return value of \f5exceptf\fP is significant as follows.
286 If negative, \fIvmclose\fP immediately returns with failure.
287 If zero, \fIvmclose\fP proceeds normally by calling \f5memoryf\fP to free
288 all allocated memory segments and also freeing the region itself.
289 Finally, if positive, \fIvmclose\fP will only free the region
290 without deallocating the allocated segments.
293 An attempt to extend the region by the amount
294 \f5(size_t)\fP\fIobj\fP failed. The region is unlocked, so the
295 \fIexceptf\fP function may free blocks.
296 If the function returns a positive value the memory
297 request will be repeated.
298 If zero, the allocation method
299 will again invoke \fImemoryf\fP to get space.
300 If negative, the allocation request will fail.
303 Address \fIobj\fP, given to \fIvmfree\fP or \fIvmresize\fP,
304 does not point to an allocated block from the region.
305 The respective call will fail.
308 Announce that a memory allocation request is finished and returning.
311 Announce that a memory freeing request is finished and returning.
314 Announce that a memory resizing request is finished and returning.
316 .SS "Allocation methods"
317 There are five methods, of type \f5Vmethod_t*\fP:
320 An approximately best-fit allocation strategy.
323 A strategy for building structures that are only deleted in whole.
324 Only the latest allocated block can be freed.
325 This means that as soon as a block \f5a\fP is allocated,
326 \fIvmfree\fP calls on blocks other than \c5a\fP are ignored.
329 A strategy for blocks of one size,
330 set by the first \fIvmalloc\fP call after \fIvmopen\fP or \fIvmclear\fP.
333 An allocation strategy with extra-stringent checking and locking
334 regardless of the \f5VM_TRUST\fP flag.
335 It is useful for finding misuses of dynamically allocated
336 memory, such as writing beyond the boundary of a block, or
337 freeing a block twice.
341 An allocation method that records and prints summaries of memory usage.
344 The method \f5Vmdebug\fP is used to debug common memory violation problems.
345 When a problem is found,
346 a warning message is written to file descriptor 2 (standard error).
347 In addition, if flag \f5VM_DBABORT\fP is on,
348 the program is terminated by calling \fIabort\fP(2).
349 Each message is a line of self-explanatory fields separated by colons.
350 The optional flag \f5-DVMFL\fP, if used during compilation,
351 enables recording of file names and line numbers.
352 The following functions work with method \f5Vmdebug\fP.
355 resets the file descriptor to write out warnings to the given argument.
356 By default, this file descriptor is 2, the standard error.
357 \fIvmdebug\fP returns the previous file descriptor.
360 checks a region using \f5Vmdebug\fP or \f5Vmbest\fP for integrity.
361 If \f5Vmdebug\fP, this also checks for block overwriting errors.
362 On errors, \fIvmdbwarn\fP is called.
363 If flag \f5VM_DBCHECK\fP is on,
364 \fIvmdbcheck\fP is called at each invocation of
365 \fIvmalloc\fP, \fIvmfree\fP, or \fIvmresize\fP.
368 causes address \fIaddr\fP
369 to be watched, and reported whenever met in
370 \fIvmalloc\fP, \fIvmresize\fP or \fIvmfree\fP.
371 The watch list has finite size and if it becomes full,
372 watches will be removed in a first-in-first-out fashion.
373 If \fIaddr\fP is \f5NULL\fP,
374 all current watches are canceled.
375 \fIvmdbwatch\fP returns the watch bumped out due to an insertion
376 into a full list or \f5NULL\fP otherwise.
379 is an internal function that processes
380 warning messages for discovered errors.
381 It can't be called from outside the \fIvmalloc\fP package,
382 but is a good place to plant debugger traps because
383 control goes there at every trouble.
386 The method \f5Vmprofile\fP is used to profile memory usage.
387 Profiling data are maintained in private memory of a process so
388 \f5Vmprofile\fP should be avoided from regions manipulating
389 persistent or shared memory.
390 The optional flag \f5-DVMFL\fP, if used during compilation,
391 enables recording of file names and line numbers.
394 prints memory usage summary.
395 The summary is restricted to region \fIvm\fP if \fIvm\fP is not \f5NULL\fP;
396 otherwise, it is for all regions created with \f5Vmprofile\fP.
397 Summary records are written to file descriptor \fIfd\fP as lines with
398 colon-separated fields. Here are some of the fields:
401 Number of allocation and free calls respectively. Note that a resize
402 operation is coded as a free and an allocation.
405 Total amounts allocated and freed. The difference between these numbers
406 is the amount of space not yet freed.
409 These fields are only with the summary record for region.
410 They show the maximum busy space at any time and the extent of the region.
412 .SS "Information and statistics"
414 returns the region to which the block pointed to by
416 This works only in regions that allocate with
417 \f5Vmbest\fP, \f5Vmdebug\fP or \f5Vmprofile\fP.
418 If multiple regions manipulate the same segment of memory,
419 \fIvmregion\fP returns the region that causes the creation that memory segment.
422 finds if some segment of memory in \fIregion\fP
423 contains the address \fIaddr\fP.
424 It returns the address of a found segment or \f5NULL\fP if none found.
427 walks all segments in \fIregion\fP or if \fIregion\fP is \f5NULL\fP,
428 all segments in all regions.
429 At each segment, \fI(*walkf)(vm,addr,size,segno,disc,handle)\fP
430 is called where \fIvm\fP is the region, \fIaddr\fP is the segment,
431 \fIsize\fP is the size of the segment, \fIsegno\fP is the segment ordinal counting from 0,
432 \fIdisc\fP is the region's discipline, and \fIhandle\fP is the user supplied \fIvmwalk\fP handle.
433 If \fIwalkf\fP returns a negative value, the walk stops and returns the same value.
434 On success, \fIvmwalk\fP returns 0; otherwise, it returns \-1.
437 checks whether \fIaddr\fP
438 points to an address within some allocated block of the given region.
439 If not, it returns \-1.
440 If so, it returns the offset from the beginning of the block.
441 The function does not work for a \f5Vmlast\fP region except
442 on the latest allocated block.
445 returns the size of the allocated block pointed to by \fIaddr\fP.
446 It returns \-1 if \fIaddr\fP
447 does not point to a valid block in the region.
448 Sizes may be padded beyond that requested; in
449 particular no block has size 0.
450 The function does not work for a \f5Vmlast\fP region except
451 on the latest allocated block.
454 gathers statistics on the given \fIregion\fP and returns that
455 information in the \f5Vmstat_t\fP structure pointed to by \fIstatb\fP.
456 A \f5Vmstat_t\fP structure has at least these members:
459 .ta \w'\f5size_t \fP'u +\w'\f5extent \fP'u
460 .MW "int n_busy; /* number of busy blocks */
461 .MW "int n_free; /* number of free blocks */
462 .MW "size_t s_busy; /* total busy space */
463 .MW "size_t s_free; /* total free space */
464 .MW "size_t m_busy; /* maximum size of busy block */
465 .MW "size_t m_free; /* maximum size of free block */
466 .MW "int n_seg; /* number of segments in region */
467 .MW "size_t extent; /* total size of the region */
471 Bookeeping overhead is counted in \f5extent\fP,
472 but not in \f5s_busy\fP or \f5s_free\fP.
475 establishes file descriptor \fIfd\fP
476 as the trace file and returns
477 the previous value of the trace file descriptor.
478 The trace descriptor is initially invalid.
479 Output is sent to the trace file by successful allocation
480 events when flag \f5VM_TRACE\fP is on.
482 Tools for analyzing traces are described in \fImtreplay\fP(1).
483 The trace record for an allocation event
484 is a line with colon-separated fields, four numbers and one string.
487 Zero for a fresh allocation;
488 the address argument for freeing and resizing.
492 the address returned by allocation or resizing.
495 The size argument for allocation or resizing;
496 the size freed by freeing.
497 Sizes may differ due to padding for alignment.
500 The address of the affected region.
503 A string that tells the region's method:
504 \f5best\fP, \f5last\fP, \f5pool\fP, \f5profile\fP, or \f5debug\fP.
507 outputs a trace of all currently busy blocks in region \f5vm\fP.
508 This only works with the \f5Vmbest\fP, \f5Vmdebug\fP and \f5Vmprofile\fP methods.
510 .SS "Malloc-compatible functions"
511 Functions in this set provide the behaviors of \fImalloc\fP(3).
513 \fImemalign\fP and \fIvalloc\fP allocate aligned blocks;
514 \fIvalloc\fP further restricts alignment to page boundaries.
516 The \fImalloc\fP functions are instrumented for run-time debugging,
517 profiling and tracing.
518 When these modes are enabled, time and space performance will be affected.
519 For accurate reporting of files and line numbers,
520 code should include \f5vmalloc.h\fP and compile with either \f5-DVMFL\fP
521 or \f5-D_BLD_debug\fP (defined by \fBnmake\fP(1) for \f5-g\fP compilations).
524 environment variable should be set before any memory allocation
525 (e.g., before a process starts). Its value is a space-separated list of
526 [\fBno\fP]\fIname\fP[=\fIvalue\fP] options that control runtime
527 debugging/profiling and other modes:
530 If Vmregion==Vmdebug then VM_DBABORT is set, otherwise _BLD_debug
531 enabled assertions abort() on failure.
534 If Vmregion==Vmbest then the region is checked after every operation.
537 Sets Vmregion=\fImethod\fP if not already set.
539 (\fBVm\fP prefix optional) may be one of:
552 Prefer mmap() over brk() for region allocation.
555 Sets Vmregion=Vmdebug if not already set. If Vmregion==Vmdebug then
556 the region is checked every \fIn\fP operations.
559 Sets Vmregion=Vmprofile if not already set. If Vmregion==Vmprofile then
560 profile info is printed to \fIfile\fP.
563 If Vmregion==Vmbest then region block frees verify
564 that the block belongs to the region.
566 Sets Vmregion=Vmdebug if not already set. If Vmregion==Vmdebug then
567 region checking starts after \fIn\fP operations.
570 Operation trace info is printed to \fIfile\fP.
573 Sets Vmregion=Vmdebug if not already set. If Vmregion==Vmdebug then
574 warnings are printed to \fIfile\fP.
577 Sets Vmregion=Vmdebug if not already set. If Vmregion==Vmdebug then
578 operations on \fIaddress\fP are traced.
580 Output files are created if they don't exist.
584 name the file descriptor
586 which must be open for writing. The pattern
588 in a file name is replaced by the current process ID.
590 combines the features of these previously used env vars {
591 .I "VMDEBUG VMETHOD VMPROFILE VMTRACE"
593 For compatibility, if
595 is not defined, then {
596 .I "VMDEBUG VMETHOD VMPROFILE VMTRACE"
597 } will be checked -- this will be dropped in a future release.
600 \f5Vmlast\fP: allocated blocks are now allowed to be resized (1998-09).
601 \fIVMALLOC_OPTIONS\fP now controls all runtime settings (2010-01).
604 \fImtreplay\fP(1), \fImalloc\fP(3).
607 Kiem-Phong Vo, kpv@research.att.com