Pessimistic memory allocation.

* Added/removed headers as necessary
* Removed 'key_event' function from keyboard
This commit is contained in:
Murilo Pereira 2011-02-06 03:44:45 -02:00
parent d1a7d6fc24
commit 75d1e5f23b
17 changed files with 81 additions and 24 deletions

View File

@ -1,8 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h>
#include "card.h" #include "card.h"
void allocate_card(struct card **card) { 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)); allocate_frame(&((*card)->frame));

View File

@ -41,6 +41,8 @@ struct card {
enum face face; enum face face;
}; };
extern const char *program_name;
void allocate_card(struct card **); void allocate_card(struct card **);
void initialize_card(struct card *); void initialize_card(struct card *);
void delete_card(struct card *); void delete_card(struct card *);

View File

@ -1,11 +1,18 @@
#include <ncurses.h> #include <stdio.h>
#include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h>
#include <ncurses.h>
#include "display.h" #include "display.h"
#include "game.h" #include "game.h"
#include "cursor.h" #include "cursor.h"
void allocate_cursor(struct cursor **cursor) { 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; return;
} }

View File

@ -32,6 +32,7 @@ struct cursor {
enum movement { LEFT, DOWN, UP, RIGHT }; enum movement { LEFT, DOWN, UP, RIGHT };
extern struct deck *deck; extern struct deck *deck;
extern const char *program_name;
void allocate_cursor(struct cursor **); void allocate_cursor(struct cursor **);
void initialize_cursor(struct cursor *); void initialize_cursor(struct cursor *);

View File

@ -1,8 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h>
#include "deck.h" #include "deck.h"
void allocate_deck(struct deck **deck) { 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)->stock));
allocate_stack(&((*deck)->waste_pile)); allocate_stack(&((*deck)->waste_pile));

View File

@ -37,6 +37,8 @@ struct deck {
struct stack *maneuvre_6; struct stack *maneuvre_6;
}; };
extern const char *program_name;
void allocate_deck(struct deck **); void allocate_deck(struct deck **);
void initialize_deck(struct deck *); void initialize_deck(struct deck *);
void delete_deck(struct deck *); void delete_deck(struct deck *);

View File

@ -1,14 +1,19 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ncurses.h>
#include <malloc.h> #include <malloc.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <ncurses.h>
#include "display.h" #include "display.h"
#include "game.h" #include "game.h"
char *card_suit(enum suit suit) { char *card_suit(enum suit suit) {
char *card_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) { switch(suit) {
case DIAMONDS: strcpy(card_suit, DIAMONDS_SYMBOL); break; 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(enum value value) {
char *card_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) { switch(value) {
case TWO: card_value = "2"; break; case TWO: card_value = "2"; break;
@ -65,7 +73,10 @@ void erase_stack(struct stack *stack) {
void draw_empty_stacks() { void draw_empty_stacks() {
WINDOW **empty_stack; 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, empty_stack[0] = newwin(FRAME_HEIGHT,
FRAME_WIDTH, FRAME_WIDTH,

View File

@ -15,6 +15,8 @@
#define WHITE_ON_BLUE 3 #define WHITE_ON_BLUE 3
#define WHITE_ON_GREEN 4 #define WHITE_ON_GREEN 4
extern const char *program_name;
char *card_suit(enum suit); char *card_suit(enum suit);
char *card_value(enum value); char *card_value(enum value);
void erase_stack(struct stack *); void erase_stack(struct stack *);

View File

@ -1,8 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h>
#include "frame.h" #include "frame.h"
void allocate_frame(struct frame **frame) { 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; return;
} }

View File

@ -12,6 +12,8 @@ struct frame {
int start_x; int start_x;
}; };
extern const char *program_name;
void allocate_frame(struct frame **); void allocate_frame(struct frame **);
void initialize_frame(struct frame *); void initialize_frame(struct frame *);
void delete_frame(struct frame *); void delete_frame(struct frame *);

View File

@ -1,5 +1,8 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> #include <malloc.h>
#include <string.h>
#include <errno.h>
#include <time.h> #include <time.h>
#include "display.h" #include "display.h"
#include "util.h" #include "util.h"
@ -121,7 +124,10 @@ void shuffle_deck(struct deck *deck) {
struct stack tmp; struct stack tmp;
int random; 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++) { for (int i = 0; i < NUMBER_OF_CARDS; i++) {
stack[i] = pop(&(deck->stock)); stack[i] = pop(&(deck->stock));

View File

@ -27,6 +27,7 @@
#define MANEUVRE_5_STARTING_X 41 #define MANEUVRE_5_STARTING_X 41
#define MANEUVRE_6_STARTING_X 49 #define MANEUVRE_6_STARTING_X 49
extern const char *program_name;
struct deck *deck; struct deck *deck;
struct cursor *cursor; struct cursor *cursor;

View File

@ -1,4 +1,3 @@
#include <stdio.h>
#include "display.h" #include "display.h"
#include "keyboard.h" #include "keyboard.h"
@ -104,14 +103,6 @@ void handle_card_movement(struct cursor *cursor) {
return; return;
} }
int key_event() {
int pressed_key;
pressed_key = getchar();
return(pressed_key);
}
void handle_keyboard_event(int key) { void handle_keyboard_event(int key) {
switch (key) { switch (key) {
case 'h': case 'h':

View File

@ -15,7 +15,6 @@ bool cursor_on_stack(struct cursor *, struct stack *);
bool cursor_on_invalid_spot(struct cursor *); bool cursor_on_invalid_spot(struct cursor *);
void handle_stock_event(); void handle_stock_event();
void handle_card_movement(struct cursor *); void handle_card_movement(struct cursor *);
int key_event();
void handle_keyboard_event(); void handle_keyboard_event();
#endif #endif

View File

@ -1,10 +1,17 @@
#include <malloc.h> #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
#include "stack.h" #include "stack.h"
#include "game.h" #include "game.h"
void allocate_stack(struct stack **stack) { 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)); allocate_card(&((*stack)->card));

View File

@ -10,6 +10,8 @@ struct stack {
struct stack *next; struct stack *next;
}; };
extern const char *program_name;
void allocate_stack(struct stack **); void allocate_stack(struct stack **);
void initialize_stack(struct stack *); void initialize_stack(struct stack *);
void delete_stack(struct stack *); void delete_stack(struct stack *);

View File

@ -1,10 +1,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <curses.h>
#include "../lib/util.h" #include "../lib/util.h"
#include "../lib/game.h" #include "../lib/game.h"
#include "../lib/cursor.h"
#include "../lib/keyboard.h" #include "../lib/keyboard.h"
int main() { const char *program_name;
int main(int argc, const char *argv[]) {
program_name = *argv;
int key; int key;
initialize_curses(); initialize_curses();