#ifndef ULIB_LINEARLIST_H_
#define ULIB_LINEARLIST_H_

#include <stddef.h>
#include <umalloc/umalloc.h>

#define LINLIST_APPEND(lst, d) \
  do { \
    if ((lst)->data == NULL) { \
      (lst)->capacity = 1; \
      (lst)->data = umalloc(sizeof(*(lst)->data) * (lst)->capacity); \
    } else { \
      if ((lst)->count == (lst)->capacity) { \
        (lst)->capacity *= 2; \
        (lst)->data = urealloc((lst)->data, sizeof(*(lst)->data) * (lst)->capacity); \
      } \
    } \
    (lst)->data[(lst)->count++] = (d); \
  } while(0)

#define LINLIST_FREE(lst) \
  do { \
    if ((lst)->data != NULL) { \
      ufree((lst)->data); \
      (lst)->data = NULL; \
    } \
    (lst)->count = 0; \
  } while(0)

#endif // ULIB_LINEARLIST_H_
