00001 /* 00002 Copyright (c) 2008, Luke Benstead. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, 00006 are permitted provided that the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, 00009 this list of conditions and the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions and the following disclaimer in the documentation 00012 and/or other materials provided with the distribution. 00013 00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00015 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00016 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00017 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00018 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00019 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00020 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00021 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00022 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00023 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 00026 #include <malloc.h> 00027 #include <memory.h> 00028 #include <assert.h> 00029 #include <stdio.h> 00030 00031 #define INITIAL_SIZE 30 00032 #define INCREMENT 50 00033 00034 #include "mat4stack.h" 00035 00036 void km_mat4_stack_initialize(km_mat4_stack* stack) { 00037 stack->stack = (kmMat4*) malloc(sizeof(kmMat4) * INITIAL_SIZE); //allocate the memory 00038 stack->capacity = INITIAL_SIZE; //Set the capacity to 10 00039 stack->top = NULL; //Set the top to NULL 00040 stack->item_count = 0; 00041 }; 00042 00043 void km_mat4_stack_push(km_mat4_stack* stack, const kmMat4* item) { 00044 /*Set the top pointer to the beginning of the memory + num items (zero based) */ 00045 stack->top = stack->stack + (stack->item_count * sizeof(kmMat4)); 00046 memcpy(stack->top, item, sizeof(kmMat4)); /*Copy the new item to the top of the stack*/ 00047 stack->item_count++; /*Increment the item count*/ 00048 00049 /*If we have reached the capacity of the allocated memory 00050 * then we resize the stack */ 00051 if (stack->item_count == stack->capacity) { 00052 kmMat4* temp_stack = stack->stack; 00053 unsigned int new_size = sizeof(kmMat4) * (stack->capacity + INCREMENT); 00054 stack->stack = (kmMat4*) malloc(new_size); 00055 memcpy(stack->stack, temp_stack, stack->capacity * sizeof(kmMat4)); 00056 stack->capacity += INCREMENT; 00057 stack->top = stack->stack + ((stack->item_count - 1) * sizeof(kmMat4)); 00058 free(temp_stack); //Free the original memory 00059 } 00060 } 00061 00062 void km_mat4_stack_pop(km_mat4_stack* stack, kmMat4* pOut) { 00063 //Decrement the item count 00064 stack->item_count = stack->item_count - 1; 00065 00066 if (pOut != NULL) { 00067 //Copy the data from stack->top to the pOut pointer (if it was specified) 00068 memcpy(pOut, stack->top, sizeof(kmMat4)); 00069 } 00070 stack->top = stack->stack + ((stack->item_count - 1) * sizeof(kmMat4)); 00071 00072 assert(stack->item_count >= 0 && "Too many pops"); 00073 } 00074 00075 void km_mat4_stack_release(km_mat4_stack* stack) { 00076 free(stack->stack); 00077 stack->top = NULL; 00078 stack->item_count = 0; 00079 stack->capacity = 0; 00080 }