/Users/maurits/Documents/studie/afstuderen/biosphere/common/stack.c

Go to the documentation of this file.
00001 /*
00002  * Author: MA Hartman
00003  * Date: mar 5, 2007
00004  * 
00005  * Function:
00006  * General purpose stack implementation.
00007  * 
00008  * License information:
00009  * 
00010  * Copyright (c) 2006 Maurits Hartman
00011  *
00012  * Permission is hereby granted, free of charge, to any person
00013  * obtaining a copy of this software and associated documentation
00014  * files (the "Software"), to deal in the Software without
00015  * restriction, including without limitation the rights to use,
00016  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00017  * copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following
00019  * conditions:
00020  * 
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00026  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00028  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00029  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00031  * OTHER DEALINGS IN THE SOFTWARE.
00032  * 
00033  */
00034  
00035 #include "stack.h"
00036 #include "alloc.h"
00037 
00038 #include <assert.h>
00039 
00040 #define INITIAL_STACK_SIZE 16
00041 
00042 
00043 bs_stack *new_stack(apr_pool_t *mp)
00044 {
00045         apr_status_t rv;
00046         apr_pool_t *pool;
00047         bs_stack *stack = NULL;
00048         
00049         /* Create a subpool for this stack and its data: */
00050         rv = apr_pool_create(&pool, mp);
00051         assert(rv == APR_SUCCESS);
00052         
00053         /* Initialize data members: */
00054     stack                               = apr_pcalloc(pool, sizeof(bs_stack));
00055     stack->size         = 0;
00056     stack->allocated    = INITIAL_STACK_SIZE;
00057     stack->pool                 = pool;
00058     stack->data         = apr_pcalloc(pool, sizeof(void * [INITIAL_STACK_SIZE]));
00059         
00060         return stack;   
00061 }
00062 
00063 
00064 void delete_stack(bs_stack *stack)
00065 {
00066         if (stack != NULL) {
00067                 /* The stack is allocated from its own subpool, so this will delete
00068                  * it completely: */
00069         apr_pool_destroy(stack->pool);
00070     }   
00071 }
00072 
00073 
00074 void stack_push(bs_stack *stack, void *data)
00075 {
00076         assert(stack != NULL);
00077         
00078         /* Double the allocated space if it is full: */
00079     if (stack->size == stack->allocated) {
00080         stack->allocated *= 2;
00081         stack->data = pool_realloc(stack->data,
00082                         stack->size,
00083                         sizeof(void *) * stack->allocated,
00084                         stack->pool);
00085     }
00086 
00087     stack->data[stack->size++] = data;
00088 }
00089 
00090 
00091 void *stack_pop(bs_stack *stack)
00092 {
00093         void *ret;
00094         
00095         assert(stack != NULL);
00096         ret = stack_top(stack);
00097         
00098         /* This stack implementation will only release memory when it is deleted: */
00099         if (stack->size != 0) stack->size--;
00100         
00101         return ret;
00102 }
00103 
00104 
00105 void *stack_top(const bs_stack *stack)
00106 {
00107         assert(stack != NULL);
00108         if (stack->size == 0) return NULL;
00109         return stack->data[stack->size - 1];
00110 }
00111 
00112 
00113 int stack_empty(const bs_stack *stack)
00114 {
00115         assert(stack != NULL);
00116         return stack->size;
00117 }
00118 
00119 
00120 void *stack_index(const bs_stack *stack, unsigned index)
00121 {
00122         assert(stack != NULL && index < stack->size);
00123         return stack->data[index];
00124 }
00125 
00126 
00127 inline unsigned stack_size(const bs_stack *stack)
00128 {
00129         assert(stack != NULL);
00130         return stack->size;     
00131 }

Generated on Tue Jul 17 09:50:52 2007 for Bio-SPHERE by  doxygen 1.5.1