#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "memwrap.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define RETRY_SEC     0
#define RETRY_NSEC    250000000
#define MSG_BUF_LEN   256

static void show_errmsg (void) {
	int errno_backup = errno;

	const char fail_msg[] = "Fail to allocate memory: ";
	const size_t fail_len = LBS_STR_STATIC_STRLEN (fail_msg);
	write (STDERR_FILENO, fail_msg, fail_len);

	char err_msg[MSG_BUF_LEN];
	size_t err_len;
	if (strerror_r (errno_backup, err_msg, MSG_BUF_LEN)) {
		const char unknown_msg[] = "Unknown error code";
		const size_t unknown_len = LBS_STR_STATIC_STRLEN (unknown_msg);
		write (STDERR_FILENO, unknown_msg, unknown_len);
	} else {
		err_len = strlen (err_msg);
		write (STDERR_FILENO, err_msg, err_len);
	}

	const char retry_msg[] = ". Retry ...\n";
	const size_t retry_len = LBS_STR_STATIC_STRLEN (retry_msg);
	write (STDERR_FILENO, retry_msg, retry_len);

	errno = errno_backup;
}

void* xmalloc (size_t size) {
	void* memptr;
	while ((memptr = malloc (size)) == NULL) {
		show_errmsg ();
		nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
	}
	return memptr;
}

void* xrealloc (void* ptr, size_t size) {
	void* newptr;
	while ((newptr = realloc (ptr, size)) == NULL) {
		show_errmsg ();
		nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
	}
	return newptr;
}

char* xstrdup (const char* str) {
	char* newstr;
	while ((newstr = strdup (str)) == NULL) {
		show_errmsg ();
		nanosleep (&(struct timespec) { RETRY_SEC, RETRY_NSEC }, NULL);
	}
	return newstr;
}