184 lines · 3.8 KB · Text
RawDownload
.TH QMALLOC 9.SH NAMEqmalloc \- quickfit mallocmalloc, mallocz, smalloc, realloc, calloc, free, msize, setmalloctag, setrealloctag, getmalloctag, getrealloctag, mallocinit, mallocsummary \- quickfit kernel memory allocator.SH SYNOPSIS.ta \w'\fLvoid* 'u.Bvoid*	malloc(usize size).PP.Bvoid*	mallocalign(usize size, usize align, long offset, usize span).PP.Bvoid*	mallocz(usize size, int clr).PP.Bvoid*	smalloc(usize size).PP.Bvoid*	realloc(void *p, usize size).PP.Bvoid*	calloc(ulong n, usize szelem).PP.Bvoid	free(void *ptr).PP.Bulong	msize(void *ptr).PP.Bvoid	setmalloctag(void *ptr, ulong tag).PP.Bulong	getmalloctag(void *ptr).PP.Bvoid	setrealloctag(void *ptr, ulong tag).PP.Bulong	getrealloctag(void *ptr).PP.Bvoid	mallocinit(void).Bvoid	mallocsummary(void).PP.SH DESCRIPTIONThese are kernel versions of the functions in.IR malloc (2).They allocate memory with Quickfit with an allocatorfrom Kernighan & Ritchie.The allocation is currently fixed, but could call.IR physalloc (9nix)in the future.All but.I smalloc(which calls.IR sleep (9))may safely be called by interrupt handlers..PP.I Mallocreturns a pointer to a block of at least.I sizebytes, initialised to zero.The block is suitably aligned for storage of any type of object.The call.B malloc(0)returns a valid pointer rather than null..I Malloczis similar, but only clears the memory if.I clris non-zero..PP.I Smallocreturns a pointer to a block of.I sizebytes, initialised to zero.If the memory is not immediately available,.I smallocretries every 100 milliseconds until the memory is acquired..PP.I Mallocalignallocates a block of at least .I nbytes of memory respecting alignment contraints.If.I alignis non-zero,the returned pointer is aligned to be equal to.I offsetmodulo.IR align .If.I spanis non-zero,the.I nbyte block allocated will not span a.IR span -byteboundary..PP.I Reallocchanges the size of the block pointed to by.I pto.I sizebytes,if possible without moving the data,and returns a pointer to the block.The contents are unchanged up to the lesser of old and new sizes,and any new space allocated is initialised to zero..I Realloctakes on special meanings when one or both arguments are zero:.TP.B "realloc(0,\ size)means.LR malloc(size) ;returns a pointer to the newly-allocated memory.TP.B "realloc(ptr,\ 0)means.LR free(ptr) ;returns null.TP.B "realloc(0,\ 0)no-op; returns null.PD.PP.I Callocreturns a pointer to a block of memory of at least.I "n*szelem"bytes, initialised to zero.New code should use.I malloczinstead..PPThe argument to.I freeis a pointer to a block of memory allocated by one of the routines above, whichis returned to the allocation pool, or a null pointer, which is ignored..PPWhen a block is allocated, sometimes there is some extra unused space at the end..I Msizegrows the block to encompass this unused space and returns the new numberof bytes that may be used..PPThe memory allocator does not maintain ``malloc tag'' and the ``realloc tag'',but the functions .IR setmalloctag ,.IR getmalloctag ,.IR setrealloctag ,and.IR getrealloctag are provided for compaibility with the.IR pool (2)library..PP.I Mallocinitmust be called before.I mallocis used..I Mallocsummaryprints a short summary of the allocator's state on the console..SH SOURCE.B /sys/src/9/port/qmalloc.c.br.B /sys/src/nix/port/qmalloc.c.SH DIAGNOSTICSAll functions except.I smallocreturn a null pointer if space is unavailable..SH SEE ALSO.IR "The C Programming Language" ,2ed, Brian Kernighan and Dennis Ritchie,chapter 8 §7: Example—Storage Allocator..br.I "Quickfit: An efficient algorithm".I for heap storage allocation" ,Charles B. Weinstock and William A. Wulf. ACM SIGPLAN Notices, 23(10):141—144, October 1988..br.IR pool (2),.IR physalloc (9nix)