/*
 * frostmem.c
 *
 *  Created on: Jun 29, 2012
 *      Author: ergino
 */

#include "frost_mem.h"
#include <stdio.h>
#include <stdlib.h>

void
frost_free (void *ptr)
{
    if (ptr != NULL)
        free (ptr);
    ptr = 0 ;
}

void*
frost_memdup (const void * mem, unsigned int byte_size)
{
    void* ptr;

    if (mem == NULL)
        return NULL;

    ptr = frost_malloc (byte_size);
    if (ptr != NULL)
        memcpy (ptr, mem, byte_size);

    return ptr;
}

void* frost_realloc (void* obj, unsigned long size)
{
    void* ptr;
    if (!size) {
        frost_free (obj);
        return 0;
    }
    ptr = realloc (obj, size);
    if (ptr)
        return ptr;
}

void*
frost_malloc (unsigned long x)
{
    void* ptr;
    if (!x)
        return 0;
    ptr = malloc (x);
    if (ptr)
    {
        memset(ptr,0,sizeof(x));
        return ptr;
    }
 }

void* frost_calloc (unsigned long x)
{
    void* ptr;
    if (!x)
        return 0;
    ptr = calloc(1,x);
    if (ptr)
    {
        memset(ptr,0,sizeof(x));
        return ptr;
    }
}

void* frost_try_malloc (unsigned long x)
{
    if (x)
        return malloc (x);
    return 0;
}


void* frost_try_realloc (void* obj, unsigned long size)
{
    if (!size) {
        frost_free (obj);
        return 0;
    }
    return realloc (obj, size);
}
