Blame extension/stack.c

Packit 575503
/*
Packit 575503
 * stack.c -- Implementation for stack functions for use by extensions.
Packit 575503
 */
Packit 575503
Packit 575503
/*
Packit 575503
 * Copyright (C) 2012, 2013 the Free Software Foundation, Inc.
Packit 575503
 *
Packit 575503
 * This file is part of GAWK, the GNU implementation of the
Packit 575503
 * AWK Programming Language.
Packit 575503
 *
Packit 575503
 * GAWK is free software; you can redistribute it and/or modify
Packit 575503
 * it under the terms of the GNU General Public License as published by
Packit 575503
 * the Free Software Foundation; either version 3 of the License, or
Packit 575503
 * (at your option) any later version.
Packit 575503
 *
Packit 575503
 * GAWK is distributed in the hope that it will be useful,
Packit 575503
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 575503
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 575503
 * GNU General Public License for more details.
Packit 575503
 *
Packit 575503
 * You should have received a copy of the GNU General Public License
Packit 575503
 * along with this program; if not, write to the Free Software
Packit 575503
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
Packit 575503
 */
Packit 575503
Packit 575503
#include <stdlib.h>
Packit 575503
Packit 575503
#include "stack.h"
Packit 575503
Packit 575503
#define INITIAL_STACK	20
Packit 575503
Packit 575503
static size_t size;
Packit 575503
static void **stack;
Packit 575503
static int index = -1;
Packit 575503
Packit 575503
/* stack_empty --- return true if stack is empty */
Packit 575503
Packit 575503
int
Packit 575503
stack_empty()
Packit 575503
{
Packit 575503
	return index < 0;
Packit 575503
}
Packit 575503
Packit 575503
/* stack_top --- return top object on the stack */
Packit 575503
Packit 575503
void *
Packit 575503
stack_top()
Packit 575503
{
Packit 575503
	if (stack_empty() || stack == NULL)
Packit 575503
		return NULL;
Packit 575503
Packit 575503
	return stack[index];
Packit 575503
}
Packit 575503
Packit 575503
/* stack_pop --- pop top object and return it */
Packit 575503
Packit 575503
void *
Packit 575503
stack_pop()
Packit 575503
{
Packit 575503
	if (stack_empty() || stack == NULL)
Packit 575503
		return NULL;
Packit 575503
Packit 575503
	return stack[index--];
Packit 575503
}
Packit 575503
Packit 575503
/* stack_push --- push an object onto the stack */
Packit 575503
Packit 575503
int stack_push(void *object)
Packit 575503
{
Packit 575503
	void **new_stack;
Packit 575503
	size_t new_size = 2 * size;
Packit 575503
Packit 575503
	if (stack == NULL) {
Packit 575503
		stack = (void **) malloc(INITIAL_STACK * sizeof(void *));
Packit 575503
		if (stack == NULL)
Packit 575503
			return 0;
Packit 575503
		size = INITIAL_STACK;
Packit 575503
	} else if (index + 1 >= size) {
Packit 575503
		if (new_size < size)
Packit 575503
			return 0;
Packit 575503
		new_stack = realloc(stack, new_size * sizeof(void *));
Packit 575503
		if (new_stack == NULL)
Packit 575503
			return 0;
Packit 575503
		size = new_size;
Packit 575503
		stack = new_stack;
Packit 575503
	}
Packit 575503
Packit 575503
	stack[++index] = object;
Packit 575503
	return 1;
Packit 575503
}