#include <stdlib.h>
#include "allocator.h"

/* Interface Code */
static void *
default_allocate(struct its1_allocator *allocator, size_t n)
{
	(void) allocator;
	return malloc(n);
}

static void
default_deallocate(struct its1_allocator *allocator, void *ptr)
{
	(void) allocator;
	free(ptr);
}

struct its1_allocator its1_default_allocator = {
	.allocate = &default_allocate,
	.deallocate = &default_deallocate
};

/* Libary Code */
#include <string.h>

void *
its1_malloc(struct its1_allocator *allocator, size_t n)
{
	if (allocator == NULL) {
		allocator = &its1_default_allocator;
	}

	return allocator->allocate(allocator, n);
}

void
its1_free(struct its1_allocator *allocator, void *ptr)
{
	if (allocator == NULL) {
		allocator = &its1_default_allocator;
	}

	allocator->deallocate(allocator, ptr);
}

char *
its1_strdup(struct its1_allocator *allocator, const char *str)
{
	size_t n;
	char *dest;

	n = strlen(str) + 1;
	dest = its1_malloc(allocator, n);
	if (dest != NULL) {
		memcpy(dest, str, n);
	}

	return dest;
}

void *
its1_malloc_zero(struct its1_allocator *allocator, size_t n)
{
	void *ptr;

	ptr = its1_malloc(allocator, n);
	if (ptr != NULL) {
		memset(ptr, 0, n);
	}

	return ptr;
}

int main() {}
