dmalloc — dmalloc API manualp page

NAME | SYNOPSIS | DESCRIPTION | RETURN VALUE | ERRORS | ATTRIBUTES | STANDARDS | EXAMPLES

dmalloc                   dmalloc API functions manual                   dmalloc

NAME         top

        dmalloc - allocates memory.

SYNOPSIS         top


        #include "dmalloc.h"

        void *dmalloc(size_t size, darena_t *arena);

DESCRIPTION         top

        The function dmalloc() allocates size bytes of memory in the arena
        pointed to by arena. If arena is NULL, the memory is allocated on the
        heap. The memory is not initialized. If size is 0, then dmalloc()
        returns a unique pointer value that can later be successfully passed to
        dfree(). When non-NULL, arena must be initialized by darenainit() before
        being passed to dmalloc(). Not doing so results in undefined behaviour.

RETURN VALUE         top

        On success, dmalloc() returns a pointer to the allocated memory. On
        failure, it returns NULL and sets errno.

ERRORS         top

        ENOMEM  Out of memory. dmalloc() may have failed for a handful of
                reasons, depending on where the memory was supposed to be
                allocated. It possibly failed because an arena allocation in an
                arena with not enough available space was attempted. Otherwise,
                for the case of a heap allocation, the function failed because
                the heap was full or because an error occurred while the heap
                was being initialized.

ATTRIBUTES         top

        For an explanation of the terms used in this section, see attributes(7).
        ┌────────────────────────────────────┬───────────────┬───────────┐
        │ InterfaceAttributeValue     │
        ├────────────────────────────────────┼───────────────┼───────────┤
        │ dmalloc()                          │ Thread safety │ MT-Safe   │
        └────────────────────────────────────┴───────────────┴───────────┘

STANDARDS         top

        dmalloc() is supposed to behave like malloc() when dealing with the
        heap. For this reason, it follows the POSIX standard on top of the 
        ISO/IEC 9899:2024 §7.24.3.6 C standard.

EXAMPLES         top

        #include "dmalloc.h"
        #include <stdio.h>  /* For perror() and printf(). */
        #include <stdlib.h> /* For exit() and the EXIT_* macros. */

        #define CAPACITY 1024

        int main()
        {
            char arena[CAPACITY];

            /* The arena must be initialized first. */
            if(darenainit(arena, CAPACITY) < 0)
            {
                perror("darenainit");
                exit(EXIT_FAILURE);
            }

            void *p = dmalloc(512, arena);

            if(p == NULL)
            {
                perror("dmalloc");
                exit(EXIT_FAILURE);
            }

            printf("Allocated memory starts at address: %p.\n", p);    

            dfree(p, arena);

            /*  It is not mandatory to destroy an arena at the end of an
                application, especially in a single-threaded one. Yet, it is
                good practice along with memory deallocation. */
            if(darenadestroy(arena) < 0)
            {
                perror("darenadestroy");
                exit(EXIT_FAILURE);
            }

            exit(EXIT_SUCCESS);
        }
dmalloc API functions manual       2026-04-13                            dmalloc