From af869f2b46b3e7f99a1c2857ef7353b8337c267b Mon Sep 17 00:00:00 2001 From: Bailey Thompson Date: Mon, 10 Aug 2020 12:30:51 -0400 Subject: [PATCH] Reduce malloc calls in stack (#78) --- containers.h | 4 ++-- src/include/stack.h | 6 +++--- src/stack.c | 43 ++++++++++++------------------------------- tst/stack.c | 14 -------------- 4 files changed, 17 insertions(+), 50 deletions(-) diff --git a/containers.h b/containers.h index 312a3df..265492b 100644 --- a/containers.h +++ b/containers.h @@ -455,13 +455,13 @@ deque deque_destroy(deque me); * The stack data structure, which adapts a container to provide a stack * (last-in first-out). Adapts the deque container. */ -typedef struct internal_stack *stack; +typedef struct internal_deque *stack; /* Starting */ stack stack_init(size_t data_size); /* Utility */ -int stack_size(stack me); +size_t stack_size(stack me); int stack_is_empty(stack me); int stack_trim(stack me); void stack_copy_to_array(void *arr, stack me); diff --git a/src/include/stack.h b/src/include/stack.h index 337f88a..c53942e 100644 --- a/src/include/stack.h +++ b/src/include/stack.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Bailey Thompson + * Copyright (c) 2017-2020 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,13 +29,13 @@ * The stack data structure, which adapts a container to provide a stack * (last-in first-out). Adapts the deque container. */ -typedef struct internal_stack *stack; +typedef struct internal_deque *stack; /* Starting */ stack stack_init(size_t data_size); /* Utility */ -int stack_size(stack me); +size_t stack_size(stack me); int stack_is_empty(stack me); int stack_trim(stack me); void stack_copy_to_array(void *arr, stack me); diff --git a/src/stack.c b/src/stack.c index 3c90417..b37608e 100644 --- a/src/stack.c +++ b/src/stack.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Bailey Thompson + * Copyright (c) 2017-2020 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,10 +23,6 @@ #include "include/deque.h" #include "include/stack.h" -struct internal_stack { - deque deque_data; -}; - /** * Initializes a stack. * @@ -39,20 +35,7 @@ struct internal_stack { */ stack stack_init(const size_t data_size) { - struct internal_stack *init; - if (data_size == 0) { - return NULL; - } - init = malloc(sizeof(struct internal_stack)); - if (!init) { - return NULL; - } - init->deque_data = deque_init(data_size); - if (!init->deque_data) { - free(init); - return NULL; - } - return init; + return deque_init(data_size); } /** @@ -62,9 +45,9 @@ stack stack_init(const size_t data_size) * * @return the size of the stack */ -int stack_size(stack me) +size_t stack_size(stack me) { - return deque_size(me->deque_data); + return deque_size(me); } /** @@ -76,7 +59,7 @@ int stack_size(stack me) */ int stack_is_empty(stack me) { - return deque_is_empty(me->deque_data); + return deque_is_empty(me); } /** @@ -89,7 +72,7 @@ int stack_is_empty(stack me) */ int stack_trim(stack me) { - return deque_trim(me->deque_data); + return deque_trim(me); } /** @@ -103,7 +86,7 @@ int stack_trim(stack me) */ void stack_copy_to_array(void *const arr, stack me) { - deque_copy_to_array(arr, me->deque_data); + deque_copy_to_array(arr, me); } /** @@ -121,7 +104,7 @@ void stack_copy_to_array(void *const arr, stack me) */ int stack_push(stack me, void *const data) { - return deque_push_back(me->deque_data, data); + return deque_push_back(me, data); } /** @@ -139,7 +122,7 @@ int stack_push(stack me, void *const data) */ int stack_pop(void *const data, stack me) { - return deque_pop_back(data, me->deque_data) == 0; + return deque_pop_back(data, me) == 0; } /** @@ -156,7 +139,7 @@ int stack_pop(void *const data, stack me) */ int stack_top(void *const data, stack me) { - return deque_get_last(data, me->deque_data) == 0; + return deque_get_last(data, me) == 0; } /** @@ -169,7 +152,7 @@ int stack_top(void *const data, stack me) */ int stack_clear(stack me) { - return deque_clear(me->deque_data); + return deque_clear(me); } /** @@ -182,7 +165,5 @@ int stack_clear(stack me) */ stack stack_destroy(stack me) { - deque_destroy(me->deque_data); - free(me); - return NULL; + return deque_destroy(me); } diff --git a/tst/stack.c b/tst/stack.c index ac03a4b..f0c37b6 100644 --- a/tst/stack.c +++ b/tst/stack.c @@ -67,23 +67,9 @@ static void test_automated_trim(void) assert(!stack_destroy(me)); } -#if STUB_MALLOC -static void test_init_out_of_memory(void) -{ - fail_malloc = 1; - assert(!stack_init(sizeof(int))); - delay_fail_malloc = 1; - fail_malloc = 1; - assert(!stack_init(sizeof(int))); -} -#endif - void test_stack(void) { test_invalid_init(); test_basic(); test_automated_trim(); -#if STUB_MALLOC - test_init_out_of_memory(); -#endif }