#ifndef __MEMUTIL_H
#define __MEMUTIL_H

#include <stdlib.h>
#include <string.h>

// booleans
typedef int bool;

// bit N
#define bit(N)  (1u << (N))

// size of a static or auto declared array
#define ARRAY_SIZE(A) (sizeof (A) / sizeof (A)[0])

// zero the memory of struct at *P to zero
#define SET_STRUCT_ZERO(P) memset((P), 0, sizeof *(P))

// checking memory allocators
void *safe_malloc(unsigned size);
void *safe_realloc(void *p, unsigned size);
char *safe_strdup(char *str);
void safe_free(void *p);

#define MEMORY_MANAGED_OBJECT_BASE_FIELDS int n_refs

typedef struct memory_managed_object_t {
  MEMORY_MANAGED_OBJECT_BASE_FIELDS;
} MEMORY_MANAGED_OBJECT;


void *ref(MEMORY_MANAGED_OBJECT *p);
void deref(MEMORY_MANAGED_OBJECT *p);
#define REF(P)    ref((MEMORY_MANAGED_OBJECT*)(P))
#define DEREF(P)  deref((MEMORY_MANAGED_OBJECT*)(P))

#define MALLOC_STRUCT(P)  (P) = safe_malloc(sizeof *(P))

// deliberately cause compilation errors if caller uses
// system memory routines
#ifdef _DEBUG
#define malloc(N)    __call_safe_malloc_instead()
#define realloc(P,N) __call_safe_alloc_instead()
#define strdup(S)    __call_safe_alloc_instead()
#define free(P)      __call_safe_free_instead()
#endif

#endif
