[31] | 1 | /* |
---|
| 2 | * This is a really simple implementation of a stack which stores chunks |
---|
| 3 | * of memory of any type. It still needs work to eliminate memory |
---|
| 4 | * leaks. |
---|
| 5 | * |
---|
[81] | 6 | * Copyright (C) 2005,2007 Timothy D. Morgan |
---|
[31] | 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify |
---|
| 9 | * it under the terms of the GNU General Public License as published by |
---|
[111] | 10 | * the Free Software Foundation; version 3 of the License. |
---|
[31] | 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 20 | * |
---|
| 21 | * $Id: void_stack.c 147 2009-02-22 19:31:52Z tim $ |
---|
| 22 | */ |
---|
| 23 | |
---|
[147] | 24 | #include "void_stack.h" |
---|
[31] | 25 | |
---|
| 26 | void_stack* void_stack_new(unsigned short max_size) |
---|
| 27 | { |
---|
| 28 | void_stack* ret_val = (void_stack*)malloc(sizeof(void_stack)); |
---|
| 29 | |
---|
| 30 | if (ret_val != NULL) |
---|
| 31 | { |
---|
| 32 | memset(ret_val, 0, sizeof(*ret_val)); |
---|
| 33 | ret_val->elements = (void**)malloc(max_size*sizeof(void*)); |
---|
| 34 | if (ret_val->elements == NULL) |
---|
| 35 | { |
---|
| 36 | free(ret_val); |
---|
| 37 | ret_val = NULL; |
---|
| 38 | } |
---|
| 39 | else |
---|
| 40 | { |
---|
| 41 | memset(ret_val->elements, 0, max_size*sizeof(void*)); |
---|
| 42 | |
---|
| 43 | ret_val->max_size = max_size; |
---|
| 44 | ret_val->top = 0; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | return ret_val; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
[38] | 52 | void_stack* void_stack_copy(const void_stack* v) |
---|
| 53 | { |
---|
| 54 | unsigned int i; |
---|
| 55 | void_stack* ret_val; |
---|
| 56 | if(v == NULL) |
---|
| 57 | return NULL; |
---|
| 58 | |
---|
| 59 | ret_val = void_stack_new(v->max_size); |
---|
| 60 | if(ret_val == NULL) |
---|
| 61 | return NULL; |
---|
| 62 | |
---|
| 63 | for(i = 0; i < v->top; i++) |
---|
| 64 | ret_val->elements[i] = v->elements[i]; |
---|
| 65 | ret_val->top = v->top; |
---|
| 66 | |
---|
| 67 | return ret_val; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | void_stack* void_stack_copy_reverse(const void_stack* v) |
---|
| 72 | { |
---|
| 73 | unsigned int i; |
---|
| 74 | void_stack* ret_val; |
---|
| 75 | if(v == NULL) |
---|
| 76 | return NULL; |
---|
| 77 | |
---|
| 78 | ret_val = void_stack_new(v->max_size); |
---|
| 79 | if(ret_val == NULL) |
---|
| 80 | return NULL; |
---|
| 81 | |
---|
| 82 | for(i = 0; i < v->top; i++) |
---|
| 83 | ret_val->elements[i] = v->elements[v->top-i-1]; |
---|
| 84 | ret_val->top = v->top; |
---|
| 85 | |
---|
| 86 | return ret_val; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
[80] | 90 | void void_stack_free(void_stack* stack) |
---|
[31] | 91 | { |
---|
[52] | 92 | free(stack->elements); |
---|
[38] | 93 | free(stack); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
[80] | 97 | void void_stack_free_deep(void_stack* stack) |
---|
[38] | 98 | { |
---|
[31] | 99 | unsigned short i; |
---|
| 100 | for(i=0; i < stack->top; i++) |
---|
| 101 | free(stack->elements[i]); |
---|
[52] | 102 | free(stack->elements); |
---|
[31] | 103 | free(stack); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
[89] | 107 | unsigned short void_stack_size(const void_stack* stack) |
---|
[33] | 108 | { |
---|
| 109 | return stack->top; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
[31] | 113 | void* void_stack_pop(void_stack* stack) |
---|
| 114 | { |
---|
| 115 | void* ret_val = NULL; |
---|
| 116 | |
---|
| 117 | if(stack->top > 0) |
---|
| 118 | { |
---|
[52] | 119 | ret_val = stack->elements[--(stack->top)]; |
---|
[31] | 120 | stack->elements[stack->top] = NULL; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return ret_val; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
[89] | 127 | bool void_stack_push(void_stack* stack, void* e) |
---|
[31] | 128 | { |
---|
[89] | 129 | if(stack->top < stack->max_size) |
---|
| 130 | { |
---|
| 131 | stack->elements[stack->top++] = e; |
---|
| 132 | return true; |
---|
| 133 | } |
---|
| 134 | else |
---|
| 135 | return false; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | const void* void_stack_cur(const void_stack* stack) |
---|
| 140 | { |
---|
[31] | 141 | void* ret_val = NULL; |
---|
| 142 | |
---|
| 143 | if(stack->top > 0) |
---|
| 144 | ret_val = stack->elements[stack->top-1]; |
---|
| 145 | |
---|
| 146 | return ret_val; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
[89] | 150 | void_stack_iterator* void_stack_iterator_new(const void_stack* stack) |
---|
[31] | 151 | { |
---|
| 152 | void_stack_iterator* ret_val = NULL; |
---|
| 153 | |
---|
[89] | 154 | if(stack != NULL) |
---|
[31] | 155 | { |
---|
[89] | 156 | ret_val = (void_stack_iterator*)malloc(sizeof(void_stack_iterator)); |
---|
| 157 | if (ret_val != NULL) |
---|
| 158 | { |
---|
| 159 | ret_val->stack = stack; |
---|
| 160 | ret_val->cur = 0; |
---|
| 161 | } |
---|
[31] | 162 | } |
---|
| 163 | |
---|
| 164 | return ret_val; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
[80] | 168 | void void_stack_iterator_free(void_stack_iterator* iter) |
---|
[31] | 169 | { |
---|
| 170 | free(iter); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
[33] | 174 | const void* void_stack_iterator_next(void_stack_iterator* iter) |
---|
[31] | 175 | { |
---|
| 176 | if(iter->cur < iter->stack->top) |
---|
| 177 | return iter->stack->elements[iter->cur++]; |
---|
| 178 | else |
---|
| 179 | return NULL; |
---|
| 180 | } |
---|