henesyinit from p9fce6d0435 years agoHistory
109 lines · 2.0 KB · Text
RawDownload
.TH MALLOC 2.SH NAMEmalloc, free, realloc, calloc \- memory allocator.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.PP.ta \w'\fLvoid* 'u.Bvoid*	malloc(long size).PP.Bvoid	free(void *ptr).PP.Bvoid*	realloc(void *ptr, long size).PP.Bvoid*	calloc(long nelem, long elsize).SH DESCRIPTION.I Mallocand.I freeprovide a simple memory allocation package..I Mallocreturns a pointer to a new block of at least.I sizebytes.The block is suitably aligned for storage of any type of object.No two active pointers from.I mallocwill have the same value..PPThe argument to.I freeis a pointer to a block previously allocated by.IR malloc ;this space is made available for further allocation.It is legal to free a null pointer; the effect is a no-op..PP.I Reallocchanges the size of the block pointed to by.I ptrto.I sizebytes and returns a pointer to the (possibly moved)block.The contents will be unchanged up to thelesser of the new and old sizes.The call.B "realloc(0, size)means the same as.LR malloc(size) ..PP.I Callocallocates space foran array of.I nelemelements of size.IR elsize .The space is initialized to zeros..I Freefrees such a block..SS AlefExcept for.IR calloc ,these routines are available from Alef; they use the same arena as.BR alloc ..I Mallocand.I reallocexecute a.B checkwhen they fail, rather than return.BR nil .Memory returned by.I mallocis cleared..I Reallocdoes not guarantee new memory is cleared unless.I ptris.BR nil ..SH SOURCE.B /sys/src/libc/port/malloc.c.SH SEE ALSO.IR brk (2).SH DIAGNOSTICS.I Malloc, reallocand.I callocreturn 0 if there is no available memory..I Errstris likely to be set..SH BUGSThe different specification of.I callocis bizarre..PPUser errors can corrupt the storage arena.The most common gaffes are (1) freeing an already freed block,(2) storing beyond the bounds of an allocated block, and (3)freeing data that was not obtained from the allocator.When.I mallocand.I freedetect such corruption, they abort.