array.c

Sapphire/src/sapphire/em/array.c · 1.4 KB

From the archive sapphire_0.3.zip

Download the archive   « Back to the archive contents

/*
 * Copyright (C) 1995-1997 Christopher D. Granz
 *
 * This header may not be removed.
 *
 * Refer to the file "License" included in this package for further
 * information and before using any of the following.
 */

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

#include "sapphire.h"
#include "emerald.h"


/*
 * Functions
 */

/*
 * Allocates a new array.
 */
EM_ARRAY *array_alloc( register size_t rsSize )
{
    register EM_ARRAY *p;

    p                     = alloc_mem( sizeof( EM_ARRAY ) );
    p->sCount             = 1;
    p->sSize              = rsSize;
    p->pElements          = alloc_mem( ( sizeof( EM_VALUE ) * rsSize ) );
    p->pElements[0].iType = TYPE_INVALID;
    return ( p );
}


/*
 * Decreases array usage count.  If usage is equal to 0 frees the array.
 */
void array_free( register EM_ARRAY *p )
{
    register EM_VALUE *pValue;

    if ( --p->sCount > 0 )
        return;

    if ( ( pValue = p->pElements ) != NULL )
    {
        if ( pValue->iType != TYPE_INVALID )
        {
            register size_t rs;

            for ( rs = p->sSize; rs > 0; rs--, pValue++ )
                delete_value( pValue );
        }

        free_mem( (void **) &p->pElements );
    }
}


/*
 * Copys a pointer to an array.
 */
EM_ARRAY *array_copy( register EM_ARRAY *p )
{
    if ( p == NULL )
        return ( NULL );

    p->sCount++;
    return ( p );
}


/*
 * End of array.c
 */

Site Stats

Snippets: 1791
Codebases: 571