/[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 59 - (show annotations) (download) (as text)
Sun Oct 30 01:38:20 2005 UTC (18 years, 6 months ago) by jds
File MIME type: text/x-csrc
File size: 4257 byte(s)
- prototype unit test suite based on CUnit added.
- unit tests for base/generic/general and base/generic/utilites added.
- 2nd manual rework of doxygen documentation in general and utilities.
- bug fixes (mostly general & utilities) found during test development.
- added asc_assert prototype to redirect failures to Asc_Panic() and enable decoupling assertions from NDEBUG.
- some modifications of interface & implementation to facilitate testing.
- utilities/ascPrint & utilities/ascMalloc functions now always included in base libs to minimize recompilation when an interface chooses different options.
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 #ifndef MAX
37 #define MAX(a,b) (((a)>(b))?(a):(b))
38 #endif
39
40 #define STACKDEBUG 0
41 /* if STACKDEBUG != 0, we will do a bunch of initializing to 0 */
42 static const unsigned long LOWCAPACITY = 2;
43 /* Shallowest stack we will allow. It must not be 0! */
44 static const unsigned long MIN_INCREMENT = 8;
45 /* Minimum number of elements to increase when expanding stack. */
46
47 struct gs_stack_t *gs_stack_create(unsigned long capacity)
48 {
49 struct gs_stack_t *result;
50 capacity = MAX(LOWCAPACITY, capacity);
51 result = (struct gs_stack_t *)ascmalloc(sizeof(struct gs_stack_t));
52 if (result) {
53 result->size = 0;
54 result->capacity = capacity;
55 result->data = (VOIDPTR *)ascmalloc(capacity*sizeof(VOIDPTR));
56 return result;
57 }
58 else{
59 FPRINTF(ASCERR,"Unable to allocate memory for stack\n");
60 return NULL;
61 }
62 }
63
64 void gs_stack_clear(struct gs_stack_t *stack)
65 {
66 asc_assert(stack!=NULL);
67 #if STACKDEBUG
68 memset(stack->data,0,sizeof(void *)*stack->size);
69 #endif
70 stack->size = 0;
71 }
72
73 void gs_stack_destroy(struct gs_stack_t *stack,int dispose)
74 {
75 unsigned long c;
76 unsigned long size;
77 if (stack == NULL) return;
78 if (dispose) {
79 size = stack->size;
80 for (c=0;c<size;c++) {
81 ascfree(stack->data[c]);
82 #if STACKDEBUG
83 stack->data[c] = NULL;
84 #endif
85 }
86 }
87 ascfree(stack->data);
88 #if STACKDEBUG
89 stack->data = NULL;
90 stack->size = stack->capacity = 0;
91 #endif
92 ascfree(stack);
93 }
94
95 static void gs_stack_expand(struct gs_stack_t *stack)
96 {
97 unsigned long increment;
98 increment = (stack->capacity*50)/100;
99 if (increment < MIN_INCREMENT) increment = MIN_INCREMENT;
100 stack->capacity += increment;
101 if (!stack->capacity) {
102 stack->capacity += increment;
103 stack->data = (VOIDPTR *)ascmalloc(increment*sizeof(VOIDPTR));
104 } else {
105 stack->capacity += increment;
106 stack->data =
107 (VOIDPTR *)ascrealloc(stack->data,stack->capacity*sizeof(VOIDPTR));
108 }
109 asc_assert(stack->data!=NULL);
110 }
111
112 void gs_stack_push(struct gs_stack_t *stack, VOIDPTR ptr)
113 {
114 asc_assert(NULL != stack);
115 if (++(stack->size) > stack->capacity) /* expand stack capacity */
116 gs_stack_expand(stack);
117 stack->data[stack->size-1] = ptr;
118 }
119
120 VOIDPTR gs_stack_pop(struct gs_stack_t *stack)
121 {
122 VOIDPTR result;
123 asc_assert(NULL != stack);
124 if (stack->size > 0) {
125 result = stack->data[stack->size-1];
126 stack->size--;
127 return result;
128 }
129 else{ /* overpopped */
130 FPRINTF(ASCERR,"gs_stack_pop called too often.\n");
131 return NULL;
132 }
133 }
134
135 unsigned long gs_stack_size(CONST struct gs_stack_t *stack)
136 {
137 asc_assert(NULL != stack);
138 return stack->size;
139 }
140
141 int gs_stack_empty(CONST struct gs_stack_t *stack)
142 {
143 asc_assert(NULL != stack);
144 return (stack->size==0);
145 }
146
147 void gs_stack_apply(struct gs_stack_t *stack, void (*func) (VOIDPTR))
148 {
149 unsigned long size,c;
150 asc_assert(NULL != stack);
151 asc_assert(NULL != func);
152 size = stack->size;
153 for (c=0;c<size;c++) (*func)(stack->data[c]);
154 }

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