#include <stddef.h>
#include "../alloc.h"

struct malloc_header {
  size_t size;
};

void* malloc(size_t sz) {
  size_t total = sz + sizeof(struct malloc_header);

  struct malloc_header* mh = alloc(total);
  if(!mh)
    return mh;
  mh->size = total;
  return (void*)(&mh[1]);
}

void* calloc(size_t sz, size_t n) {
  // for now ignore the case of sz * n > 1 << 64
  size_t N = sz * n;
  u8* ptr = malloc(N);
  for(size_t i = 0; i < N; i++)
    ptr[i] = 0;
}

void free(void* ptr) {
  if(!ptr) {
    return;
  }
  size_t sz = ((struct malloc_header*)ptr)[-1].size;

  dealloc(ptr, sz);
}