#include <ncbi.h>
#include <gishlib.h>
#ifdef OS_UNIX
#include <sys/lock.h>
#endif

VoidPtr _cdecl
mem_malloc(len)
	size_t	len;
{
#if defined(OS_UNIX) && defined(UNLOCK)
	register VoidPtr	vp;

	if (len == 0)
		return NULL;
	vp = Nlm_Malloc(len);
	if (vp != NULL)
		return vp;
	plock(UNLOCK);
	vp = Nlm_Malloc(len);
	if (vp != NULL)
		return vp;
	return NULL;
#else
	if (len == 0)
		return NULL;
	return Nlm_Malloc(len);
#endif
}

VoidPtr _cdecl
mem_calloc(nelem, elsize)
	size_t	nelem;
	size_t	elsize;
{
#if defined(OS_UNIX) && defined(UNLOCK)
	register VoidPtr	vp;

	if (nelem == 0 || elsize == 0)
		return NULL;
	vp = Nlm_Calloc(nelem, elsize);
	if (vp != NULL)
		return vp;
	plock(UNLOCK);
	return Nlm_Calloc(nelem, elsize);
#else
	if (nelem == 0 || elsize == 0)
		return NULL;
	return Nlm_Calloc(nelem, elsize);
#endif
}

VoidPtr _cdecl
mem_realloc(ptr, size)
	VoidPtr	ptr;
	size_t	size;
{
#if defined(OS_UNIX) && defined(UNLOCK)
	register VoidPtr	vp;

	if (size == 0) {
		mem_free(ptr);
		return NULL;
	}
	if (ptr == NULL)
		vp = Nlm_Malloc(size);
	else
		vp = Nlm_Realloc(ptr, size);
	if (vp != NULL)
		return vp;
	plock(UNLOCK);
	if (ptr == NULL)
		return Nlm_Malloc(size);
	return Nlm_Realloc(ptr, size);
#else
	if (size == 0) {
		mem_free(ptr);
		return NULL;
	}
	if (ptr == NULL)
		return Nlm_Malloc(size);
	return Nlm_Realloc(ptr, size);
#endif
}

void _cdecl
mem_free(ptr)
	VoidPtr	ptr;
{
	if (ptr == NULL)
		return;
	Nlm_Free(ptr);
}
