Pessimistic memory allocation.
* Added/removed headers as necessary * Removed 'key_event' function from keyboard
This commit is contained in:
		
							parent
							
								
									d1a7d6fc24
								
							
						
					
					
						commit
						75d1e5f23b
					
				| @ -1,8 +1,15 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <malloc.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| #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)); | ||||
| 
 | ||||
|  | ||||
| @ -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 *); | ||||
|  | ||||
							
								
								
									
										11
									
								
								lib/cursor.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								lib/cursor.c
									
									
									
									
									
								
							| @ -1,11 +1,18 @@ | ||||
| #include <ncurses.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <malloc.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| #include <ncurses.h> | ||||
| #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; | ||||
| } | ||||
|  | ||||
| @ -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 *); | ||||
|  | ||||
| @ -1,8 +1,15 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <malloc.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| #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)); | ||||
|  | ||||
| @ -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 *); | ||||
|  | ||||
| @ -1,14 +1,19 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <ncurses.h> | ||||
| #include <malloc.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| #include <ncurses.h> | ||||
| #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, | ||||
|  | ||||
| @ -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 *); | ||||
|  | ||||
| @ -1,8 +1,15 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <malloc.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| #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; | ||||
| } | ||||
|  | ||||
| @ -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 *); | ||||
|  | ||||
| @ -1,5 +1,8 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <malloc.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| #include <time.h> | ||||
| #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)); | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
|  | ||||
| @ -1,4 +1,3 @@ | ||||
| #include <stdio.h> | ||||
| #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': | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
							
								
								
									
										11
									
								
								lib/stack.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								lib/stack.c
									
									
									
									
									
								
							| @ -1,10 +1,17 @@ | ||||
| #include <malloc.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <stdbool.h> | ||||
| #include <malloc.h> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| #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)); | ||||
| 
 | ||||
|  | ||||
| @ -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 *); | ||||
|  | ||||
| @ -1,10 +1,13 @@ | ||||
| #include <stdlib.h> | ||||
| #include <curses.h> | ||||
| #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(); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user