QMALLOC(9)                                             QMALLOC(9)

     NAME
          qmalloc - quickfit malloc malloc, mallocz, smalloc, realloc,
          calloc, free, msize, setmalloctag, setrealloctag,
          getmalloctag, getrealloctag, mallocinit, mallocsummary -
          quickfit kernel memory allocator

     SYNOPSIS
          void* malloc(usize size)

          void* mallocalign(usize size, usize align, long offset,
          usize span)

          void* mallocz(usize size, int clr)

          void* smalloc(usize size)

          void* realloc(void *p, usize size)

          void* calloc(ulong n, usize szelem)

          void  free(void *ptr)

          ulong msize(void *ptr)

          void  setmalloctag(void *ptr, ulong tag)

          ulong getmalloctag(void *ptr)

          void  setrealloctag(void *ptr, ulong tag)

          ulong getrealloctag(void *ptr)

          void  mallocinit(void) void  mallocsummary(void)

     DESCRIPTION
          These are kernel versions of the functions in malloc(2).
          They allocate memory with Quickfit with an allocator from
          Kernighan & Ritchie.  The allocation is currently fixed, but
          could call physalloc(9nix) in the future.  All but smalloc
          (which calls sleep(9)) may safely be called by interrupt
          handlers.

          Malloc returns a pointer to a block of at least size bytes,
          initialised to zero.  The block is suitably aligned for
          storage of any type of object.  The call malloc(0) returns a
          valid pointer rather than null.  Mallocz is similar, but
          only clears the memory if clr is non-zero.

          Smalloc returns a pointer to a block of size bytes, ini-
          tialised to zero.  If the memory is not immediately

     Page 1                       Plan 9            (printed 12/22/24)

     QMALLOC(9)                                             QMALLOC(9)

          available, smalloc retries every 100 milliseconds until the
          memory is acquired.

          Mallocalign allocates a block of at least n bytes of memory
          respecting alignment contraints.  If align is non-zero, the
          returned pointer is aligned to be equal to offset modulo
          align. If span is non-zero, the n byte block allocated will
          not span a span-byte boundary.

          Realloc changes the size of the block pointed to by p to
          size bytes, 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.  Realloc takes on special meanings
          when one or both arguments are zero:

          realloc(0, size)
               means `malloc(size)'; returns a pointer to the newly-
               allocated memory

          realloc(ptr, 0)
               means `free(ptr)'; returns null

          realloc(0, 0)
               no-op; returns null

          Calloc returns a pointer to a block of memory of at least
          n*szelem bytes, initialised to zero.  New code should use
          mallocz instead.

          The argument to free is a pointer to a block of memory allo-
          cated by one of the routines above, which is returned to the
          allocation pool, or a null pointer, which is ignored.

          When a block is allocated, sometimes there is some extra
          unused space at the end.  Msize grows the block to encompass
          this unused space and returns the new number of bytes that
          may be used.

          The memory allocator does not maintain ``malloc tag'' and
          the ``realloc tag'', but the functions setmalloctag,
          getmalloctag, setrealloctag, and getrealloctag are provided
          for compaibility with the pool(2) library.

          Mallocinit must be called before malloc is used.
          Mallocsummary prints a short summary of the allocator's
          state on the console.

     SOURCE
          /sys/src/9/port/qmalloc.c
          /sys/src/nix/port/qmalloc.c

     Page 2                       Plan 9            (printed 12/22/24)

     QMALLOC(9)                                             QMALLOC(9)

     DIAGNOSTICS
          All functions except smalloc return a null pointer if space
          is unavailable.

     SEE ALSO
          The C Programming Language, 2ed, Brian Kernighan and Dennis
          Ritchie, chapter 8 §7: Example—Storage Allocator.
          Quickfit: An efficient algorithm for heap storage
          allocation" , Charles B. Weinstock and William A. Wulf. ACM
          SIGPLAN Notices, 23(10):141—144, October 1988.
          pool(2), physalloc(9nix)

     Page 3                       Plan 9            (printed 12/22/24)