Mercurial > hg > Members > Moririn
diff src/parallel_execution/stack.c @ 87:9e139a340bd1 parallel_execution
rename directory
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 19 Jan 2016 16:16:26 +0900 |
parents | src/tmp/stack.c@e06e1a9e569e |
children | f57e9ffa7960 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/stack.c Tue Jan 19 16:16:26 2016 +0900 @@ -0,0 +1,68 @@ +#include <string.h> +#include "stack.h" + +stack_ptr stack_init(size_t size, int max) { + stack_ptr stack_ptr; + + if ((stack_ptr = calloc(1, sizeof(stack))) == NULL) + return NULL; + + if ((stack_ptr->data = calloc(max, size)) == NULL) { + free(stack_ptr); + return NULL; + } + + stack_ptr->size = size; + stack_ptr->max = max; + stack_ptr->num = 0; + + return stack_ptr; +} + +stack_ptr stack_realloc(stack_ptr stack_ptr, int max) { + if (stack_ptr == NULL) + return NULL; + + if ((stack_ptr->data = realloc(stack_ptr->data, stack_ptr->size*max)) == NULL) + return NULL; + + stack_ptr->max = max; + + return stack_ptr; +} + +void stack_free(stack_ptr stack_ptr) { + if (stack_ptr != NULL && stack_ptr->data != NULL) { + free(stack_ptr->data); + free(stack_ptr); + } +} + +int stack_push(stack_ptr stack_ptr, void* data) { + if (stack_ptr->max <= stack_ptr->num) + return -1; + + memcpy((char*)stack_ptr->data+stack_ptr->num*stack_ptr->size, data, stack_ptr->size); + stack_ptr->num++; + + return 0; +} + +int stack_pop(stack_ptr stack_ptr, void* data) { + if (stack_ptr->num == 0) + return -1; + + stack_ptr->num--; + + memcpy(data, (char*)stack_ptr->data+stack_ptr->num*stack_ptr->size, stack_ptr->size); + + return 0; +} + +int isMax(const stack_ptr stack_ptr) { + return stack_ptr->max<=stack_ptr->num; +} + +int isEmpty(const stack_ptr stack_ptr) { + return stack_ptr->num<=0; +}