/[ascend]/trunk/base/generic/general/stack.c
ViewVC logotype

Contents of /trunk/base/generic/general/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 122 - (show annotations) (download) (as text)
Mon Dec 19 06:12:40 2005 UTC (18 years, 5 months ago) by johnpye
File MIME type: text/x-csrc
File size: 4233 byte(s)
Refactoring all MAX, MIN, ABS calls to general/mathmacros.
Adding a GCC optimisation for these macros.
1 /*
2 * Implementation of Stack Module
3 * Kirk A. Abbott
4 * Created Dec 20, 1994
5 * Version: $Revision: 1.1 $
6 * Version control file: $RCSfile: stack.c,v $
7 * Date last modified: $Date: 1997/07/18 11:35:37 $
8 * Last modified by: $Author: mthomas $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
13 *
14 * The Ascend Language Interpreter is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The Ascend Language Interpreter is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
22 * Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with the program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named COPYING.
27 */
28
29 #include<stdio.h>
30 #include<stdlib.h>
31 #include "utilities/ascConfig.h"
32 #include "compiler/compiler.h"
33 #include "utilities/ascMalloc.h"
34 #include "utilities/ascPanic.h"
35 #include "general/stack.h"
36 #include "general/mathmacros.h"
37
38 #define STACKDEBUG 0
39 /* if STACKDEBUG != 0, we will do a bunch of initializing to 0 */
40 static const unsigned long LOWCAPACITY = 2;
41 /* Shallowest stack we will allow. It must not be 0! */
42 static const unsigned long MIN_INCREMENT = 8;
43 /* Minimum number of elements to increase when expanding stack. */
44
45 struct gs_stack_t *gs_stack_create(unsigned long capacity)
46 {
47 struct gs_stack_t *result;
48 capacity = MAX(LOWCAPACITY, capacity);
49 result = (struct gs_stack_t *)ascmalloc(sizeof(struct gs_stack_t));
50 if (result) {
51 result->size = 0;
52 result->capacity = capacity;
53 result->data = (VOIDPTR *)ascmalloc(capacity*sizeof(VOIDPTR));
54 return result;
55 }
56 else{
57 FPRINTF(ASCERR,"Unable to allocate memory for stack\n");
58 return NULL;
59 }
60 }
61
62 void gs_stack_clear(struct gs_stack_t *stack)
63 {
64 asc_assert(stack!=NULL);
65 #if STACKDEBUG
66 memset(stack->data,0,sizeof(void *)*stack->size);
67 #endif
68 stack->size = 0;
69 }
70
71 void gs_stack_destroy(struct gs_stack_t *stack,int dispose)
72 {
73 unsigned long c;
74 unsigned long size;
75 if (stack == NULL) return;
76 if (dispose) {
77 size = stack->size;
78 for (c=0;c<size;c++) {
79 ascfree(stack->data[c]);
80 #if STACKDEBUG
81 stack->data[c] = NULL;
82 #endif
83 }
84 }
85 ascfree(stack->data);
86 #if STACKDEBUG
87 stack->data = NULL;
88 stack->size = stack->capacity = 0;
89 #endif
90 ascfree(stack);
91 }
92
93 static void gs_stack_expand(struct gs_stack_t *stack)
94 {
95 unsigned long increment;
96 increment = (stack->capacity*50)/100;
97 if (increment < MIN_INCREMENT) increment = MIN_INCREMENT;
98 stack->capacity += increment;
99 if (!stack->capacity) {
100 stack->capacity += increment;
101 stack->data = (VOIDPTR *)ascmalloc(increment*sizeof(VOIDPTR));
102 } else {
103 stack->capacity += increment;
104 stack->data =
105 (VOIDPTR *)ascrealloc(stack->data,stack->capacity*sizeof(VOIDPTR));
106 }
107 asc_assert(stack->data!=NULL);
108 }
109
110 void gs_stack_push(struct gs_stack_t *stack, VOIDPTR ptr)
111 {
112 asc_assert(NULL != stack);
113 if (++(stack->size) > stack->capacity) /* expand stack capacity */
114 gs_stack_expand(stack);
115 stack->data[stack->size-1] = ptr;
116 }
117
118 VOIDPTR gs_stack_pop(struct gs_stack_t *stack)
119 {
120 VOIDPTR result;
121 asc_assert(NULL != stack);
122 if (stack->size > 0) {
123 result = stack->data[stack->size-1];
124 stack->size--;
125 return result;
126 }
127 else{ /* overpopped */
128 FPRINTF(ASCERR,"gs_stack_pop called too often.\n");
129 return NULL;
130 }
131 }
132
133 unsigned long gs_stack_size(CONST struct gs_stack_t *stack)
134 {
135 asc_assert(NULL != stack);
136 return stack->size;
137 }
138
139 int gs_stack_empty(CONST struct gs_stack_t *stack)
140 {
141 asc_assert(NULL != stack);
142 return (stack->size==0);
143 }
144
145 void gs_stack_apply(struct gs_stack_t *stack, void (*func) (VOIDPTR))
146 {
147 unsigned long size,c;
148 asc_assert(NULL != stack);
149 asc_assert(NULL != func);
150 size = stack->size;
151 for (c=0;c<size;c++) (*func)(stack->data[c]);
152 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22