From 75d1e5f23be209e8d27eefb99fc69153a47c77c5 Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Sun, 6 Feb 2011 03:44:45 -0200 Subject: [PATCH] Pessimistic memory allocation. * Added/removed headers as necessary * Removed 'key_event' function from keyboard --- lib/card.c | 9 ++++++++- lib/card.h | 2 ++ lib/cursor.c | 11 +++++++++-- lib/cursor.h | 1 + lib/deck.c | 9 ++++++++- lib/deck.h | 2 ++ lib/display.c | 19 +++++++++++++++---- lib/display.h | 2 ++ lib/frame.c | 9 ++++++++- lib/frame.h | 2 ++ lib/game.c | 8 +++++++- lib/game.h | 1 + lib/keyboard.c | 9 --------- lib/keyboard.h | 1 - lib/stack.c | 11 +++++++++-- lib/stack.h | 2 ++ src/tty-solitaire.c | 7 +++++-- 17 files changed, 81 insertions(+), 24 deletions(-) diff --git a/lib/card.c b/lib/card.c index 61bfb69..cbdc9a9 100644 --- a/lib/card.c +++ b/lib/card.c @@ -1,8 +1,15 @@ +#include +#include #include +#include +#include #include "card.h" void allocate_card(struct card **card) { - *card = malloc(sizeof(**card)); + if (!(*card = malloc(sizeof(**card)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } allocate_frame(&((*card)->frame)); diff --git a/lib/card.h b/lib/card.h index 7398c6a..e32ac67 100644 --- a/lib/card.h +++ b/lib/card.h @@ -41,6 +41,8 @@ struct card { enum face face; }; +extern const char *program_name; + void allocate_card(struct card **); void initialize_card(struct card *); void delete_card(struct card *); diff --git a/lib/cursor.c b/lib/cursor.c index 6a9bc2a..031b59a 100644 --- a/lib/cursor.c +++ b/lib/cursor.c @@ -1,11 +1,18 @@ -#include +#include +#include #include +#include +#include +#include #include "display.h" #include "game.h" #include "cursor.h" void allocate_cursor(struct cursor **cursor) { - *cursor = malloc(sizeof(**cursor)); + if (!(*cursor = malloc(sizeof(**cursor)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } return; } diff --git a/lib/cursor.h b/lib/cursor.h index 3790d6b..e557fc8 100644 --- a/lib/cursor.h +++ b/lib/cursor.h @@ -32,6 +32,7 @@ struct cursor { enum movement { LEFT, DOWN, UP, RIGHT }; extern struct deck *deck; +extern const char *program_name; void allocate_cursor(struct cursor **); void initialize_cursor(struct cursor *); diff --git a/lib/deck.c b/lib/deck.c index 0016b61..ac8305a 100644 --- a/lib/deck.c +++ b/lib/deck.c @@ -1,8 +1,15 @@ +#include +#include #include +#include +#include #include "deck.h" void allocate_deck(struct deck **deck) { - *deck = malloc(sizeof(**deck)); + if (!(*deck = malloc(sizeof(**deck)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } allocate_stack(&((*deck)->stock)); allocate_stack(&((*deck)->waste_pile)); diff --git a/lib/deck.h b/lib/deck.h index 1a00cc0..421d73c 100644 --- a/lib/deck.h +++ b/lib/deck.h @@ -37,6 +37,8 @@ struct deck { struct stack *maneuvre_6; }; +extern const char *program_name; + void allocate_deck(struct deck **); void initialize_deck(struct deck *); void delete_deck(struct deck *); diff --git a/lib/display.c b/lib/display.c index ce1c795..e9e4120 100644 --- a/lib/display.c +++ b/lib/display.c @@ -1,14 +1,19 @@ +#include #include -#include #include #include +#include +#include #include "display.h" #include "game.h" char *card_suit(enum suit suit) { char *card_suit; - card_suit = malloc(5 * sizeof(*card_suit)); + if (!(card_suit = malloc(5 * sizeof(*card_suit)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } switch(suit) { case DIAMONDS: strcpy(card_suit, DIAMONDS_SYMBOL); break; @@ -24,7 +29,10 @@ char *card_suit(enum suit suit) { char *card_value(enum value value) { char *card_value; - card_value = malloc(2 * sizeof(*card_value)); + if (!(card_value = malloc(2 * sizeof(*card_value)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } switch(value) { case TWO: card_value = "2"; break; @@ -65,7 +73,10 @@ void erase_stack(struct stack *stack) { void draw_empty_stacks() { WINDOW **empty_stack; - empty_stack = malloc(EMPTY_STACKS_NUMBER * sizeof(**empty_stack)); + if (!(empty_stack = malloc(EMPTY_STACKS_NUMBER * sizeof(**empty_stack)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } empty_stack[0] = newwin(FRAME_HEIGHT, FRAME_WIDTH, diff --git a/lib/display.h b/lib/display.h index 9c801d2..7b76325 100644 --- a/lib/display.h +++ b/lib/display.h @@ -15,6 +15,8 @@ #define WHITE_ON_BLUE 3 #define WHITE_ON_GREEN 4 +extern const char *program_name; + char *card_suit(enum suit); char *card_value(enum value); void erase_stack(struct stack *); diff --git a/lib/frame.c b/lib/frame.c index 6cd6245..427576d 100644 --- a/lib/frame.c +++ b/lib/frame.c @@ -1,8 +1,15 @@ +#include +#include #include +#include +#include #include "frame.h" void allocate_frame(struct frame **frame) { - *frame = malloc(sizeof(**frame)); + if (!(*frame = malloc(sizeof(**frame)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } return; } diff --git a/lib/frame.h b/lib/frame.h index b943fcf..5d412e8 100644 --- a/lib/frame.h +++ b/lib/frame.h @@ -12,6 +12,8 @@ struct frame { int start_x; }; +extern const char *program_name; + void allocate_frame(struct frame **); void initialize_frame(struct frame *); void delete_frame(struct frame *); diff --git a/lib/game.c b/lib/game.c index b6b274d..ed106aa 100644 --- a/lib/game.c +++ b/lib/game.c @@ -1,5 +1,8 @@ +#include #include #include +#include +#include #include #include "display.h" #include "util.h" @@ -121,7 +124,10 @@ void shuffle_deck(struct deck *deck) { struct stack tmp; int random; - stack = malloc(NUMBER_OF_CARDS * sizeof(*stack)); + if (!(stack = malloc(NUMBER_OF_CARDS * sizeof(*stack)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } for (int i = 0; i < NUMBER_OF_CARDS; i++) { stack[i] = pop(&(deck->stock)); diff --git a/lib/game.h b/lib/game.h index ff8e67e..63efad0 100644 --- a/lib/game.h +++ b/lib/game.h @@ -27,6 +27,7 @@ #define MANEUVRE_5_STARTING_X 41 #define MANEUVRE_6_STARTING_X 49 +extern const char *program_name; struct deck *deck; struct cursor *cursor; diff --git a/lib/keyboard.c b/lib/keyboard.c index a15915d..1351463 100644 --- a/lib/keyboard.c +++ b/lib/keyboard.c @@ -1,4 +1,3 @@ -#include #include "display.h" #include "keyboard.h" @@ -104,14 +103,6 @@ void handle_card_movement(struct cursor *cursor) { return; } -int key_event() { - int pressed_key; - - pressed_key = getchar(); - - return(pressed_key); -} - void handle_keyboard_event(int key) { switch (key) { case 'h': diff --git a/lib/keyboard.h b/lib/keyboard.h index 78e0328..8cd2083 100644 --- a/lib/keyboard.h +++ b/lib/keyboard.h @@ -15,7 +15,6 @@ bool cursor_on_stack(struct cursor *, struct stack *); bool cursor_on_invalid_spot(struct cursor *); void handle_stock_event(); void handle_card_movement(struct cursor *); -int key_event(); void handle_keyboard_event(); #endif diff --git a/lib/stack.c b/lib/stack.c index adeab97..7ce107b 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -1,10 +1,17 @@ -#include +#include +#include #include +#include +#include +#include #include "stack.h" #include "game.h" void allocate_stack(struct stack **stack) { - *stack = malloc(sizeof(**stack)); + if (!(*stack = malloc(sizeof(**stack)))) { + fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); + exit(errno); + } allocate_card(&((*stack)->card)); diff --git a/lib/stack.h b/lib/stack.h index 9a4fa6c..5a31ab6 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -10,6 +10,8 @@ struct stack { struct stack *next; }; +extern const char *program_name; + void allocate_stack(struct stack **); void initialize_stack(struct stack *); void delete_stack(struct stack *); diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index 4a0988f..add22fe 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -1,10 +1,13 @@ #include +#include #include "../lib/util.h" #include "../lib/game.h" -#include "../lib/cursor.h" #include "../lib/keyboard.h" -int main() { +const char *program_name; + +int main(int argc, const char *argv[]) { + program_name = *argv; int key; initialize_curses();