[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 | * |
---|
| 6 | * Copyright (C) 2005 Timothy D. Morgan |
---|
| 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 |
---|
| 10 | * the Free Software Foundation; version 2 of the License. |
---|
| 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 33 2005-07-17 19:03:02Z tim $ |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include "../include/void_stack.h" |
---|
| 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 | |
---|
| 52 | void void_stack_destroy(void_stack* stack) |
---|
| 53 | { |
---|
| 54 | unsigned short i; |
---|
| 55 | for(i=0; i < stack->top; i++) |
---|
| 56 | free(stack->elements[i]); |
---|
| 57 | free(stack); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
[33] | 61 | unsigned short void_stack_size(void_stack* stack) |
---|
| 62 | { |
---|
| 63 | return stack->top; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
[31] | 67 | bool void_stack_push(void_stack* stack, void* e) |
---|
| 68 | { |
---|
| 69 | if(stack->top < stack->max_size) |
---|
| 70 | { |
---|
| 71 | stack->elements[stack->top++] = e; |
---|
| 72 | return true; |
---|
| 73 | } |
---|
| 74 | else |
---|
| 75 | return false; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | void* void_stack_pop(void_stack* stack) |
---|
| 80 | { |
---|
| 81 | void* ret_val = NULL; |
---|
| 82 | |
---|
| 83 | if(stack->top > 0) |
---|
| 84 | { |
---|
| 85 | ret_val = stack->elements[--stack->top]; |
---|
| 86 | stack->elements[stack->top] = NULL; |
---|
| 87 | } |
---|
| 88 | else |
---|
| 89 | ret_val = NULL; |
---|
| 90 | |
---|
| 91 | return ret_val; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | const void* void_stack_cur(void_stack* stack) |
---|
| 96 | { |
---|
| 97 | void* ret_val = NULL; |
---|
| 98 | |
---|
| 99 | if(stack->top > 0) |
---|
| 100 | ret_val = stack->elements[stack->top-1]; |
---|
| 101 | else |
---|
| 102 | ret_val = NULL; |
---|
| 103 | |
---|
| 104 | return ret_val; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | void_stack_iterator* void_stack_iterator_new(void_stack* stack) |
---|
| 109 | { |
---|
| 110 | void_stack_iterator* ret_val = NULL; |
---|
| 111 | |
---|
| 112 | ret_val = (void_stack_iterator*)malloc(sizeof(void_stack_iterator)); |
---|
| 113 | if (ret_val != NULL) |
---|
| 114 | { |
---|
| 115 | ret_val->stack = stack; |
---|
| 116 | ret_val->cur = 0; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | return ret_val; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | void void_stack_iterator_destroy(void_stack_iterator* iter) |
---|
| 124 | { |
---|
| 125 | free(iter); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
[33] | 129 | const void* void_stack_iterator_next(void_stack_iterator* iter) |
---|
[31] | 130 | { |
---|
| 131 | if(iter->cur < iter->stack->top) |
---|
| 132 | return iter->stack->elements[iter->cur++]; |
---|
| 133 | else |
---|
| 134 | return NULL; |
---|
| 135 | } |
---|