| 1 | /** |
| 2 | This software and ancillary information (herein called "SOFTWARE") |
| 3 | called Supermon is made available under the terms described |
| 4 | here. The SOFTWARE has been approved for release with associated |
| 5 | LA-CC Number LA-CC 99-51. |
| 6 | |
| 7 | Unless otherwise indicated, this SOFTWARE has been authored by an |
| 8 | employee or employees of the University of California, operator of the |
| 9 | Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with |
| 10 | the U.S. Department of Energy. The U.S. Government has rights to use, |
| 11 | reproduce, and distribute this SOFTWARE, and to allow others to do so. |
| 12 | The public may copy, distribute, prepare derivative works and publicly |
| 13 | display this SOFTWARE without charge, provided that this Notice and |
| 14 | any statement of authorship are reproduced on all copies. Neither the |
| 15 | Government nor the University makes any warranty, express or implied, |
| 16 | or assumes any liability or responsibility for the use of this |
| 17 | SOFTWARE. |
| 18 | |
| 19 | If SOFTWARE is modified to produce derivative works, such modified |
| 20 | SOFTWARE should be clearly marked, so as not to confuse it with the |
| 21 | version available from LANL. |
| 22 | **/ |
| 23 | /** NOTICE: This software is licensed under the GNU Public License, which |
| 24 | is included as LICENSE_GPL in this source distribution. **/ |
| 25 | /** NOTE: This library is part of the supermon project, hence the name |
| 26 | supermon above. **/ |
| 27 | /** |
| 28 | * faststack.c : implementation of fast stack. |
| 29 | * |
| 30 | * matt sottile / [email protected] |
| 31 | */ |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include "faststack.h" |
| 35 | #include "sexp.h" |
| 36 | |
| 37 | /** |
| 38 | * create an empty stack. |
| 39 | */ |
| 40 | faststack_t * |
| 41 | make_stack () |
| 42 | { |
| 43 | faststack_t *s; |
| 44 | |
| 45 | s = (faststack_t *) malloc (sizeof (faststack_t)); |
| 46 | s->top = NULL; |
| 47 | s->bottom = NULL; |
| 48 | s->height = 0; |
| 49 | |
| 50 | return s; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * free all levels of a stack |
| 55 | */ |
| 56 | void |
| 57 | destroy_stack (faststack_t * s) |
| 58 | { |
| 59 | stack_lvl_t *sl; |
| 60 | |
| 61 | /* start at the bottom */ |
| 62 | sl = s->bottom; |
| 63 | |
| 64 | /* if bottom is null, no levels to free. */ |
| 65 | if (sl == NULL) |
| 66 | return; |
| 67 | |
| 68 | /* go up to the top of the allocated stack */ |
| 69 | while (sl->above != NULL) |
| 70 | sl = sl->above; |
| 71 | |
| 72 | /* until we get to the bottom (where below is null), free the data |
| 73 | at each level and the level itself. */ |
| 74 | while (sl->below != NULL) |
| 75 | { |
| 76 | sl = sl->below; |
| 77 | free (sl->above); |
| 78 | } |
| 79 | |
| 80 | /* free the bottom level */ |
| 81 | free (sl); |
| 82 | |
| 83 | /* free the stack wrapper itself. */ |
| 84 | free (s); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * push a level onto the cur_stack. reuse levels that have |
| 89 | * been previously allocated, allocate a new one if none |
| 90 | * are available. |
| 91 | */ |
| 92 | faststack_t * |
| 93 | push (faststack_t * cur_stack, void *data) |
| 94 | { |
| 95 | stack_lvl_t *top = cur_stack->top; |
| 96 | stack_lvl_t *tmp; |
| 97 | |
| 98 | /* if top isn't null, try to push above it. */ |
| 99 | if (top != NULL) |
| 100 | { |
| 101 | /* if above isn't null, set the stack top to it and set the |
| 102 | data */ |
| 103 | if (top->above != NULL) |
| 104 | { |
| 105 | top = cur_stack->top = cur_stack->top->above; |
| 106 | top->data = data; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | /* otherwise, allocate a new level. */ |
| 111 | tmp = top->above = (stack_lvl_t *) malloc (sizeof (stack_lvl_t)); |
| 112 | tmp->below = cur_stack->top; |
| 113 | tmp->above = NULL; |
| 114 | cur_stack->top = tmp; |
| 115 | tmp->data = data; |
| 116 | } |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | if (cur_stack->bottom != NULL) |
| 121 | { |
| 122 | cur_stack->top = cur_stack->bottom; |
| 123 | cur_stack->top->data = data; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | tmp = cur_stack->top = |
| 128 | (stack_lvl_t *) malloc (sizeof (stack_lvl_t)); |
| 129 | cur_stack->bottom = tmp; |
| 130 | tmp->above = NULL; |
| 131 | tmp->below = NULL; |
| 132 | tmp->data = data; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | cur_stack->height++; |
| 137 | |
| 138 | return cur_stack; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * pop the top of the stack, return the stack level that was |
| 143 | * popped of. |
| 144 | */ |
| 145 | stack_lvl_t * |
| 146 | pop (faststack_t * s) |
| 147 | { |
| 148 | stack_lvl_t *top; |
| 149 | top = s->top; |
| 150 | |
| 151 | /* if stack top isn't null, set the top pointer to the next |
| 152 | level down and return the old top. */ |
| 153 | if (top != NULL && s->height > 0) |
| 154 | { |
| 155 | s->top = s->top->below; |
| 156 | s->height--; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | fprintf(stderr,"STACK: non-null top, but height < 0!\n"); |
| 161 | } |
| 162 | |
| 163 | return top; |
| 164 | } |