📄 libmpatrol.3
字号:
functions will then be called as soon as the mpatrol library is initialised,which can be useful if the initialisation occurs before \fBmain\fP is called.These functions must accept no arguments and must return no value. Similarbehaviour exists for global functions whose names begin with \fB__mp_fini_\fP,except that such functions will be executed when the mpatrol library terminates.Note that this feature will have no effect if the symbol table is stripped fromthe executable file or shared library before the program is run, and the orderin which such functions will be called if there are more than one isunspecified..PPOn UNIX platforms, the \fBfork\fP function can cause problems if it is usedto make a copy of the parent process without immediately calling one of the\fBexec\fP family of functions. This is because the child process inheritsall of the memory allocations of the parent process, but also inherits the log,profile and trace files as well. If both the parent and child processes makesubsequent memory allocations there will be multiple entries with the sameallocation indices written to the log, profile or trace files. This can bemost confusing when processing these files afterwards! As a workaround, thempatrol library will always check the current process identifier every time oneof its functions is called if the \fBCHECKFORK\fP option is used and will opennew log, profile or trace files if it has determined that the process has beenforked. If the \fBCHECKFORK\fP option is not used then a call to\fB__mp_reinit\fP should be added as the first function call in the childprocess in order to duplicate the behaviour of the \fBCHECKFORK\fP option..PPMemory allocation profiling is supported, with statistics about every memoryallocation and deallocation that was made during the execution of a programbeing written to a file at program termination if the \fBPROF\fP option isused. The information stored in this file can then be used by the \fBmprof\fPcommand to display various tables summarising the memory allocation behaviourof the program that produced it. Memory allocation tracing is also supported,where a trace of all memory allocations, reallocations and deallocations can bewritten to a tracing output file in a concise encoded format for laterprocessing by the \fBmptrace\fP command. This is controlled with the\fBTRACE\fP option..SH FUNCTIONSThe following 19 functions are available as replacements for existing C libraryfunctions. To use these you must include \fImpatrol.h\fP before all otherheader files, although on UNIX and Windows platforms (and AmigaOS when using\fBgcc\fP) they will be used anyway, albeit with slightly less tracinginformation. If \fBalloca\fP is being used and \fIalloca.h\fP is included then\fImpatrol.h\fP must appear after \fIalloca.h\fP otherwise the debugging versionof \fBalloca\fP will not be used:.TP\fBmalloc\fPAllocates \fIsize\fP uninitialised bytes from the heap and returns a pointer tothe first byte of the allocation. The pointer returned will be suitablyaligned for casting to any type and can be used to store data of up to\fIsize\fP bytes in length. If \fIsize\fP is \fI0\fP then the memory allocatedwill be implicitly rounded up to \fI1\fP byte. If there is not enough space inthe heap then the null pointer will be returned and \fBerrno\fP will be set to\fBENOMEM\fP. The allocated memory must be deallocated with \fBfree\fP orreallocated with \fBrealloc\fP..TP\fBcalloc\fPAllocates \fInelem\fP elements of \fIsize\fP zero-initialised bytes from theheap and returns a pointer to the first byte of the allocation. The pointerreturned will be suitably aligned for casting to any type and can be used tostore data of up to \fInelem * size\fP bytes in length. If \fInelem * size\fPis \fI0\fP then the amount of memory allocated will be implicitly rounded up to\fI1\fP byte. If there is not enough space in the heap then the null pointerwill be returned and \fBerrno\fP will be set to \fBENOMEM\fP. The allocatedmemory must be deallocated with \fBfree\fP or reallocated with \fBrealloc\fP..TP\fBmemalign\fPAllocates \fIsize\fP uninitialised bytes from the heap and returns a pointer tothe first byte of the allocation. The pointer returned will be aligned to\fIalign\fP bytes and can be used to store data of up to \fIsize\fP bytes inlength. If \fIalign\fP is zero then the default system alignment will be used.If \fIalign\fP is not a power of two then it will be rounded up to the nearestpower of two. If \fIalign\fP is greater than the system page size then it willbe truncated to that value. If \fIsize\fP is \fI0\fP then the memory allocatedwill be implicitly rounded up to \fI1\fP byte. If there is not enough space inthe heap then the null pointer will be returned and \fBerrno\fP will be set to\fBENOMEM\fP. The allocated memory must be deallocated with \fBfree\fP orreallocated with \fBrealloc\fP, although the latter will not guarantee thepreservation of alignment..TP\fBvalloc\fPAllocates \fIsize\fP uninitialised bytes from the heap and returns a pointer tothe first byte of the allocation. The pointer returned will be aligned to thesystem page size and can be used to store data of up to \fIsize\fP bytes inlength. If \fIsize\fP is \fI0\fP then the memory allocated will be implicitlyrounded up to \fI1\fP byte. If there is not enough space in the heap then thenull pointer will be returned and \fBerrno\fP will be set to \fBENOMEM\fP. Theallocated memory must be deallocated with \fBfree\fP or reallocated with\fBrealloc\fP, although the latter will not guarantee the preservation ofalignment..TP\fBpvalloc\fPAllocates \fIsize\fP uninitialised bytes from the heap and returns a pointer tothe first byte of the allocation. The pointer returned will be aligned to thesystem page size and can be used to store data of up to \fIsize\fP bytes inlength. If \fIsize\fP is \fI0\fP then the memory allocated will be implicitlyrounded up to \fI1\fP page, otherwise \fIsize\fP will be implicitly rounded upto a multiple of the system page size. If there is not enough space in the heapthen the null pointer will be returned and \fBerrno\fP will be set to\fBENOMEM\fP. The allocated memory must be deallocated with \fBfree\fP orreallocated with \fBrealloc\fP, although the latter will not guarantee thepreservation of alignment..TP\fBalloca\fPAllocates \fIsize\fP temporary uninitialised bytes from the heap and returns apointer to the first byte of the allocation. The pointer returned will besuitably aligned for casting to any type and can be used to store data of up to\fIsize\fP bytes in length. If \fIsize\fP is \fI0\fP then the memory allocatedwill be implicitly rounded up to \fI1\fP byte. If there is not enough space inthe heap then the program will be terminated and the \fIOUTMEM\fP error will begiven. The \fBalloca\fP function normally allocates its memory from the stack,with the result that all such allocations will be freed when the functionreturns. This version of \fBalloca\fP allocates its memory from the heap inorder to provide better debugging, but the allocations may not necessarily befreed immediately when the function returns. The allocated memory can bedeallocated explicitly with \fBdealloca\fP, but may not be reallocated ordeallocated in any other way. This function is available for backwardscompatibility with older C source code and should not be used in new code..TP\fBstrdup\fPAllocates exactly enough memory from the heap to duplicate \fIstr\fP (includingthe terminating nul character) and returns a pointer to the first byte of theallocation after copying \fIstr\fP to the newly-allocated memory. The pointerreturned will have no alignment constraints and can be used to store characterdata up to the length of \fIstr\fP. If \fIstr\fP is \fBNULL\fP then an errorwill be given and the null pointer will be returned. If there is not enoughspace in the heap then the null pointer will be returned and \fBerrno\fP willbe set to \fBENOMEM\fP. The allocated memory must be deallocated with\fBfree\fP or reallocated with \fBrealloc\fP..TP\fBstrndup\fPAllocates exactly enough memory from the heap to duplicate \fIstr\fP (includingthe terminating nul character) and returns a pointer to the first byte of theallocation after copying \fIstr\fP to the newly-allocated memory. The pointerreturned will have no alignment constraints and can be used to store characterdata up to the length of \fIstr\fP. If \fIstr\fP is \fBNULL\fP and \fIsize\fPis non-zero then an error will be given and the null pointer will be returned.If the length of \fIstr\fP is greater than \fIsize\fP then only \fIsize\fPcharacters will be allocated and copied, with one additional byte for the nulcharacter. If there is not enough space in the heap then the null pointer willbe returned and \fBerrno\fP will be set to \fBENOMEM\fP. The allocated memorymust be deallocated with \fBfree\fP or reallocated with \fBrealloc\fP. Thisfunction is available for backwards compatibility with older C libraries andshould not be used in new code..TP\fBstrsave\fPAllocates exactly enough memory from the heap to duplicate \fIstr\fP (includingthe terminating nul character) and returns a pointer to the first byte of theallocation after copying \fIstr\fP to the newly-allocated memory. The pointerreturned will have no alignment constraints and can be used to store characterdata up to the length of \fIstr\fP. If \fIstr\fP is \fBNULL\fP then an errorwill be given and the null pointer will be returned. If there is not enoughspace in the heap then the null pointer will be returned and \fBerrno\fP willbe set to \fBENOMEM\fP. The allocated memory must be deallocated with\fBfree\fP or reallocated with \fBrealloc\fP. This function is available forbackwards compatibility with older C libraries and should not be used in newcode..TP\fBstrnsave\fPAllocates exactly enough memory from the heap to duplicate \fIstr\fP (includingthe terminating nul character) and returns a pointer to the first byte of theallocation after copying \fIstr\fP to the newly-allocated memory. The pointerreturned will have no alignment constraints and can be used to store characterdata up to the length of \fIstr\fP. If \fIstr\fP is \fBNULL\fP and \fIsize\fPis non-zero then an error will be given and the null pointer will be returned.If the length of \fIstr\fP is greater than \fIsize\fP then only \fIsize\fPcharacters will be allocated and copied, with one additional byte for the nulcharacter. If there is not enough space in the heap then the null pointer willbe returned and \fBerrno\fP will be set to \fBENOMEM\fP. The allocated memorymust be deallocated with \fBfree\fP or reallocated with \fBrealloc\fP. Thisfunction is available for backwards compatibility with older C libraries andshould not be used in new code..TP\fBstrdupa\fPAllocates exactly enough temporary memory from the heap to duplicate \fIstr\fP(including the terminating nul character) and returns a pointer to the firstbyte of the allocation after copying \fIstr\fP to the newly-allocated memory.The pointer returned will have no alignment constraints and can be used to storecharacter data up to the length of \fIstr\fP. If \fIstr\fP is \fBNULL\fP thenan error will be given and the null pointer will be returned. If there is notenough space in the heap then the program will be terminated and the\fIOUTMEM\fP error will be given. The \fBstrdupa\fP function normally allocatesits memory from the stack, with the result that all such allocations will befreed when the function returns. This version of \fBstrdupa\fP allocates itsmemory from the heap in order to provide better debugging, but the allocationsmay not necessarily be freed immediately when the function returns. Theallocated memory can be deallocated explicitly with \fBdealloca\fP, but may notbe reallocated or deallocated in any other way. This function is available forbackwards compatibility with older C source code and should not be used in newcode..TP\fBstrndupa\fPAllocates exactly enough temporary memory from the heap to duplicate \fIstr\fP(including the terminating nul character) and returns a pointer to the firstbyte of the allocation after copying \fIstr\fP to the newly-allocated memory.The pointer returned will have no alignment constraints and can be used to storecharacter data up to the length of \fIstr\fP. If \fIstr\fP is \fBNULL\fP and\fIsize\fP is non-zero then an error will be given and the null pointer will bereturned. If the length of \fIstr\fP is greater than \fIsize\fP then only\fIsize\fP characters will be allocated and copied, with one additional byte forthe nul character. If there is not enough space in the heap then the programwill be terminated and the \fIOUTMEM\fP error will be given. The \fBstrndupa\fPfunction normally allocates its memory from the stack, with the result that allsuch allocations will be freed when the function returns. This version of\fBstrndupa\fP allocates its memory from the heap in order to provide betterdebugging, but the allocations may not necessarily be freed immediately when thefunction returns. The allocated memory can be deallocated explicitly with\fBdealloca\fP, but may not be reallocated or deallocated in any other way.This function is available for backwards compatibility with older C source codeand should not be used in new code..TP\fBrealloc\fPResizes the memory allocation beginning at \fIptr\fP to \fIsize\fP bytes andreturns a pointer to the first byte of the new allocation after copying\fIptr\fP to the newly-allocated memory, which will be truncated if \fIsize\fPis smaller than the original allocation. The pointer returned will be suitablyaligned for casting to any type and can be used to store data of up to\fIsize\fP bytes in length. If \fIptr\fP is \fBNULL\fP then the call will beequivalent to \fBmalloc\fP. If \fIsize\fP is \fI0\fP then the existing memoryallocation will be freed and the null pointer will be returned. If \fIsize\fPis greater than the original allocation then the extra space will be filled withuninitialised bytes. If there is not enough space in the heap then the nullpointer will be returned and \fBerrno\fP will be set to \fBENOMEM\fP. Theallocated memory must be deallocated with \fBfree\fP and can be reallocatedagain with \fBrealloc\fP..TP\fBreallocf\fPResizes the memory allocation beginning at \fIptr\fP to \fIsize\fP bytes andreturns a pointer to the first byte of the new allocation after copying\fIptr\fP to the newly-allocated memory, which will be truncated if \fIsize\fPis smaller than the original allocation. The pointer returned will be suitablyaligned for casting to any type and can be used to store data of up to\fIsize\fP bytes in length. If \fIptr\fP is \fBNULL\fP then the call will beequivalent to \fBmalloc\fP. If \fIsize\fP is \fI0\fP then the existing memoryallocation will be freed and the null pointer will be returned. If \fIsize\fPis greater than the original allocation then the extra space will be filled withuninitialised bytes. If there is not enough space in the heap then the nullpointer will be returned, the original allocation will be freed and \fBerrno\fPwill be set to \fBENOMEM\fP. The allocated memory must be deallocated with\fBfree\fP and can be reallocated again with \fBrealloc\fP. This function isavailable for backwards compatibility with older C libraries and should not beused in new code..TP\fBrecalloc\fPResizes the memory allocation beginning at \fIptr\fP to \fInelem\fP elements of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -