| 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 | /** |
| 29 | * \file faststack.h |
| 30 | * |
| 31 | * \brief Implementation of a fast stack with smart memory management. |
| 32 | * |
| 33 | * Author: Matt Sottile ([email protected]) |
| 34 | * See LICENSE for info on licensing. |
| 35 | */ |
| 36 | #ifndef __FASTSTACK_H__ |
| 37 | #define __FASTSTACK_H__ |
| 38 | |
| 39 | #ifdef _DEBUG_MALLOCS_ |
| 40 | #include "malloc_util.h" |
| 41 | #endif |
| 42 | |
| 43 | /** |
| 44 | * Structure representing a single level in the stack. Has a pointer to the |
| 45 | * level above and below itself and a pointer to a generic blob of data |
| 46 | * associated with this level. |
| 47 | */ |
| 48 | typedef struct stack_level { |
| 49 | /** |
| 50 | * Pointer to the level above this one. If NULL, then this level is the |
| 51 | * top of the stack. If above is non-NULL, this level *may* be the top, |
| 52 | * but all that can be guaranteed is that there are other allocated |
| 53 | * but potentially unused levels above this one. |
| 54 | */ |
| 55 | struct stack_level *above; |
| 56 | |
| 57 | /** |
| 58 | * Pointer to the level below this one. If NULL, then this level is the |
| 59 | * bottom. |
| 60 | */ |
| 61 | struct stack_level *below; |
| 62 | |
| 63 | /** |
| 64 | * Pointer to some data associated with this level. User is responsible |
| 65 | * for knowing what to cast the \c void \c * pointer into. |
| 66 | */ |
| 67 | void *data; |
| 68 | } stack_lvl_t; |
| 69 | |
| 70 | /** |
| 71 | * Wrapper around the stack levels - keeps a pointer to the current top and |
| 72 | * bottom of the stack and a count of the current height. This allows the top |
| 73 | * to have non-null above pointer resulting from previously allocated stack |
| 74 | * levels that may be recycled later without \c malloc overhead. |
| 75 | */ |
| 76 | typedef struct stack_wrapper { |
| 77 | /** |
| 78 | * The top of the stack. If this is NULL, the stack is empty. |
| 79 | */ |
| 80 | stack_lvl_t *top; |
| 81 | |
| 82 | /** |
| 83 | * The bottom of the stack. If this is NULL, the stack is empty. |
| 84 | */ |
| 85 | stack_lvl_t *bottom; |
| 86 | |
| 87 | /** |
| 88 | * The current height of the stack, in terms of allocated and used levels. |
| 89 | */ |
| 90 | int height; |
| 91 | } faststack_t; |
| 92 | |
| 93 | /** functions **/ |
| 94 | |
| 95 | /* this is for C++ */ |
| 96 | #ifdef __cplusplus |
| 97 | extern "C" { |
| 98 | #endif |
| 99 | |
| 100 | /** |
| 101 | * Return a pointer to an empty stack structure. |
| 102 | */ |
| 103 | faststack_t *make_stack(); |
| 104 | |
| 105 | /** |
| 106 | * Given a stack structure, destroy it and free all of the stack levels. |
| 107 | * <B>Important note</B> : This function <I>does not</I> free the data |
| 108 | * pointed to from each level of the stack - the user is responsible |
| 109 | * for freeing this data themselves before calling this function to |
| 110 | * prevent memory leakage. |
| 111 | */ |
| 112 | void destroy_stack(faststack_t *s); |
| 113 | |
| 114 | /** |
| 115 | * Given a stack, push a new level on referring to the data pointer. |
| 116 | */ |
| 117 | faststack_t *push(faststack_t *cur_stack, void *data); |
| 118 | |
| 119 | /** |
| 120 | * Given a stack, pop a level off and return a pointer to that level. |
| 121 | * The user is responsible for extracting the data, but the stack_lvl_t |
| 122 | * structures pointed to from the level (above and below) should be left |
| 123 | * alone. |
| 124 | */ |
| 125 | stack_lvl_t *pop(faststack_t *s); |
| 126 | |
| 127 | /* this is for C++ */ |
| 128 | #ifdef __cplusplus |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | /** |
| 133 | * Given a stack \a s, examine the data pointer at the top. |
| 134 | */ |
| 135 | #define top_data(s) (s->top->data) |
| 136 | |
| 137 | /** |
| 138 | * Given a stack \a s, check to see if the stack is empty or not. Value |
| 139 | * is boolean true or false. |
| 140 | */ |
| 141 | #define empty_stack(s) (s->top == NULL) |
| 142 | |
| 143 | #endif /* __FASTSTACK_H__ */ |