#ifndef UTILS_H
#define UTILS_H

#include <stdio.h>
#include <stdlib.h>

#ifndef FREE
#define FREE(p)                                                                                                        \
    do                                                                                                                 \
    {                                                                                                                  \
        free(p);                                                                                                       \
        p = NULL;                                                                                                      \
    } while (0)
#endif

#ifndef MALLOC
#define MALLOC(p, s, err)                                                                                              \
    do                                                                                                                 \
    {                                                                                                                  \
        p = malloc(s);                                                                                                 \
        if (p == NULL)                                                                                                 \
            goto err;                                                                                                  \
    } while (0)
#endif

#ifndef REALLOC
#define REALLOC(p, s, err)                                                                                             \
    do                                                                                                                 \
    {                                                                                                                  \
        p = realloc(p, s);                                                                                             \
        if (p == NULL)                                                                                                 \
            goto err;                                                                                                  \
    }
#endif

#ifndef CALLOC
#define CALLOC(p, s, err)                                                                                              \
    do                                                                                                                 \
    {                                                                                                                  \
        p = calloc(s, 1);                                                                                              \
        if (p == NULL)                                                                                                 \
            goto err;                                                                                                  \
    } while (0)
#endif

#ifndef ASSERT
#define ASSERT(cond, except, err)                                                                                      \
    do                                                                                                                 \
    {                                                                                                                  \
        if (cond != except)                                                                                            \
        {                                                                                                              \
            printf("assert failed! at line: %d in %s\n", __LINE__, __FUNCTION__);                                      \
            goto err;                                                                                                  \
        }                                                                                                              \
    } while (0)
#endif

#endif // UTILS_H