/* @(#)memory.c 1.4 95/09/16 */

/*
 * Copyright (c) 1994, 1995 by Wayne C. Gramlich.  All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/* LINTLIBRARY */

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

#include "assert.h"
#include "memory_extern.h"

static void memory_zero(void *, unsigned);

/*
 * memory_allocate(size)
 *	This routine will return {size} bytes of memory aligned to
 *	a double word boundary or die trying.  malloc() is not
 *	called directly because lint constantly complains about
 *	casting malloc'ed memory pointers with wider alignment
 *	requirements.
 */
Pointer
memory_allocate(
    unsigned size)
{
    Pointer buffer;

    buffer = (Pointer)malloc(size);
    assert(buffer != (Pointer)0);    
    return buffer;
}

/*
 * memory_allocate_zeroed(size)
 *	This routine will return {size} bytes of zeroed memory aligned to
 *	a double word boundary or die trying.  
 */
Pointer
memory_allocate_zeroed(
    unsigned size)
{
    Pointer buffer;

    buffer = (Pointer)malloc(size);
    assert(buffer != (Pointer)0);    
    memory_zero((void *)buffer, size);
    return buffer;
}

/*
 * memory_copy(from, to, size)
 *	This routine will copy {size} bytes from {from} to {to}.
 */
void
memory_copy(
    void *from,
    void *to,
    unsigned size)
{
    (void)memcpy(from, to, size);
}

/*
 * memory_free(buffer)
 *	This routine will release the storage associated with {buffer}.
 */
void
memory_free(
    void *buffer)
{
    free(buffer);
}

/*
 * memory_reallocate(buffer, size)
 *	This routine will return a pointer to {size} bytes of
 *	memory aligned to a double word boundary.  If necessary
 *	for the expansion, any bytes of data already in {buffer}
 *	will be copied.
 */
Pointer
memory_reallocate(
    Pointer buffer,
    unsigned size)
{
    buffer = (Pointer)realloc((void *)buffer, size);
    assert(buffer != (Pointer)0);
    return buffer;
}

/*
 * memory_zero(buffer, size)
 *	Will zero out the the {size} bytes in {buffer}.
 */
static void
memory_zero(
    void *buffer,
    unsigned size)
{
    (void)memset(buffer, 0, size);
}

