updated upstream url, changed to value types for vectors, added checked mallocs
Minclude/simple-go/simple-go.h
| @@ -37,8 +37,8 @@ typedef struct game_state | |||
|---|---|---|---|
| 37 | 37 | ||
| 38 | 38 | typedef struct go_score | |
| 39 | 39 | { | |
| 40 | - | Vector* white_groups; | |
| 41 | - | Vector* black_groups; | |
| 40 | + | Vector white_groups; | |
| 41 | + | Vector black_groups; | |
| 42 | 42 | double white_points; | |
| 43 | 43 | double black_points; | |
| 44 | 44 | } go_score; | |
Mmeson.build
| @@ -1,6 +1,6 @@ | |||
|---|---|---|---|
| 1 | 1 | project('simple-go', 'c') | |
| 2 | 2 | ||
| 3 | - | CFLAGS = ['-std=c11', '-fstrict-aliasing', '-Wall', '-Wpedantic', '-Wextra', '-Wno-unused-parameter', '-Wconversion', '-Wstrict-aliasing', '-Wstrict-overflow=5'] | |
| 3 | + | CFLAGS = ['-std=c11', '-fstrict-aliasing', '-Wall', '-Wpedantic', '-Wextra', '-Wno-unused-parameter', '-Wconversion', '-Wstrict-aliasing=1', '-Wstrict-overflow=5'] | |
| 4 | 4 | ||
| 5 | 5 | cutils_dep = dependency('cutils', fallback : ['cutils', 'cutils_dep']) | |
| 6 | 6 | ||
Msrc/simple-go.c
| @@ -2,10 +2,8 @@ | |||
|---|---|---|---|
| 2 | 2 | ||
| 3 | 3 | go_board* create_board(go_coordinate size) | |
| 4 | 4 | { | |
| 5 | - | go_board* board = malloc(sizeof(*board)); | |
| 6 | - | assert(board); | |
| 7 | - | board->field_array = malloc(size*size*sizeof(*board->field_array)); | |
| 8 | - | assert(board->field_array); | |
| 5 | + | go_board* board = malloc_die(sizeof(*board)); | |
| 6 | + | board->field_array = malloc_die(size*size*sizeof(*board->field_array)); | |
| 9 | 7 | for(go_coordinate i = 0; i < size*size; i++) | |
| 10 | 8 | { | |
| 11 | 9 | board->field_array[i] = EMPTY; | |
| @@ -23,7 +21,7 @@ void delete_board(go_board* board) | |||
|---|---|---|---|
| 23 | 21 | ||
| 24 | 22 | game_state* create_game(go_coordinate size, float komi) | |
| 25 | 23 | { | |
| 26 | - | game_state* game = malloc(sizeof(*game)); | |
| 24 | + | game_state* game = malloc_die(sizeof(*game)); | |
| 27 | 25 | game->board = create_board(size); | |
| 28 | 26 | game->black_turn = true; | |
| 29 | 27 | game->komi = komi; | |
| @@ -342,12 +340,26 @@ go_coordinate count_liberties(const go_board* board, const go_board* overlay) | |||
|---|---|---|---|
| 342 | 340 | return liberties; | |
| 343 | 341 | } | |
| 344 | 342 | ||
| 343 | + | void delete_board_wrapper(void* board) | |
| 344 | + | { | |
| 345 | + | delete_board(board); | |
| 346 | + | } | |
| 347 | + | ||
| 345 | 348 | go_score* score_game(const game_state* game) | |
| 346 | 349 | { | |
| 347 | - | go_score* ret = malloc(sizeof(*ret)); | |
| 350 | + | go_score* ret = malloc_die(sizeof(*ret)); | |
| 351 | + | ||
| 352 | + | if(!new_vector_adv(&ret->white_groups, VECTOR_DEFAULT_SIZE, delete_board_wrapper)) | |
| 353 | + | { | |
| 354 | + | perror("malloc"); | |
| 355 | + | exit(EXIT_FAILURE); | |
| 356 | + | } | |
| 357 | + | if(!new_vector_adv(&ret->black_groups, VECTOR_DEFAULT_SIZE, delete_board_wrapper)) | |
| 358 | + | { | |
| 359 | + | perror("malloc"); | |
| 360 | + | exit(EXIT_FAILURE); | |
| 361 | + | } | |
| 348 | 362 | ||
| 349 | - | ret->white_groups = new_vector(); | |
| 350 | - | ret->black_groups = new_vector(); | |
| 351 | 363 | ret->white_points = game->white_captured + game->komi; | |
| 352 | 364 | ret->black_points = game->black_captured; | |
| 353 | 365 | ||
| @@ -363,15 +375,15 @@ go_score* score_game(const game_state* game) | |||
|---|---|---|---|
| 363 | 375 | if(get_board_at(board, y, x) == EMPTY) | |
| 364 | 376 | { | |
| 365 | 377 | //check if field is already counted | |
| 366 | - | for(go_coordinate i = 0; i < ret->white_groups->length; i++) | |
| 378 | + | for(go_coordinate i = 0; i < ret->white_groups.length; i++) | |
| 367 | 379 | { | |
| 368 | - | current = vector_at(ret->white_groups, i); | |
| 380 | + | current = vector_at(&ret->white_groups, i); | |
| 369 | 381 | if(get_board_at(current, y, x) == GROUP) | |
| 370 | 382 | goto next_loop; | |
| 371 | 383 | } | |
| 372 | - | for(go_coordinate i = 0; i < ret->black_groups->length; i++) | |
| 384 | + | for(go_coordinate i = 0; i < ret->black_groups.length; i++) | |
| 373 | 385 | { | |
| 374 | - | current = vector_at(ret->black_groups, i); | |
| 386 | + | current = vector_at(&ret->black_groups, i); | |
| 375 | 387 | if(get_board_at(current, y, x) == GROUP) | |
| 376 | 388 | goto next_loop; | |
| 377 | 389 | } | |
| @@ -382,10 +394,10 @@ go_score* score_game(const game_state* game) | |||
|---|---|---|---|
| 382 | 394 | //check if field belongs to a group | |
| 383 | 395 | if((belongs = group_belongs(board, overlay)) == WHITE) | |
| 384 | 396 | { | |
| 385 | - | vector_push(ret->white_groups, overlay); | |
| 397 | + | vector_push(&ret->white_groups, overlay); | |
| 386 | 398 | ret->white_points += (double)group_size(overlay); | |
| 387 | 399 | } else if(belongs == BLACK) { | |
| 388 | - | vector_push(ret->black_groups, overlay); | |
| 400 | + | vector_push(&ret->black_groups, overlay); | |
| 389 | 401 | ret->black_points += (double)group_size(overlay); | |
| 390 | 402 | } else { | |
| 391 | 403 | delete_board(overlay); | |
| @@ -398,15 +410,10 @@ go_score* score_game(const game_state* game) | |||
|---|---|---|---|
| 398 | 410 | return ret; | |
| 399 | 411 | } | |
| 400 | 412 | ||
| 401 | - | void delete_board_wrapper(void* board) | |
| 402 | - | { | |
| 403 | - | delete_board(board); | |
| 404 | - | } | |
| 405 | - | ||
| 406 | 413 | void delete_score(go_score* score) | |
| 407 | 414 | { | |
| 408 | - | delete_vector(score->white_groups, delete_board_wrapper); | |
| 409 | - | delete_vector(score->black_groups, delete_board_wrapper); | |
| 415 | + | delete_vector(&score->white_groups); | |
| 416 | + | delete_vector(&score->black_groups); | |
| 410 | 417 | free(score); | |
| 411 | 418 | } | |
| 412 | 419 | ||
Msrc/simple-gtp-func.c
| @@ -5,7 +5,7 @@ const char* known_commands_array[] = {"protocol_version","name","version","known | |||
|---|---|---|---|
| 5 | 5 | ||
| 6 | 6 | static char* cmd_error(const char* msg, const char* id) | |
| 7 | 7 | { | |
| 8 | - | char* ret = malloc(strlen("? ") + (msg ? strlen(msg) : 0) + (id ? strlen(id) : 0) + 3); | |
| 8 | + | char* ret = malloc_die(strlen("? ") + (msg ? strlen(msg) : 0) + (id ? strlen(id) : 0) + 3); | |
| 9 | 9 | strcpy(ret, "?"); | |
| 10 | 10 | ||
| 11 | 11 | if(id) | |
| @@ -22,7 +22,7 @@ static char* cmd_error(const char* msg, const char* id) | |||
|---|---|---|---|
| 22 | 22 | ||
| 23 | 23 | static char* cmd_success(const char* msg, const char* id) | |
| 24 | 24 | { | |
| 25 | - | char* ret = malloc(strlen("= ") + (msg ? strlen(msg) : 0) + (id ? strlen(id) : 0) + 3); | |
| 25 | + | char* ret = malloc_die(strlen("= ") + (msg ? strlen(msg) : 0) + (id ? strlen(id) : 0) + 3); | |
| 26 | 26 | strcpy(ret, "="); | |
| 27 | 27 | ||
| 28 | 28 | if(id) | |
| @@ -109,7 +109,7 @@ char* clear_board_func(const Vector* arguments, const char* id, game_state* game | |||
|---|---|---|---|
| 109 | 109 | ||
| 110 | 110 | char* play_func(const Vector* arguments, const char* id, game_state* game) | |
| 111 | 111 | { | |
| 112 | - | char* color = malloc(6); | |
| 112 | + | char* color = malloc_die(6); | |
| 113 | 113 | char xchar; | |
| 114 | 114 | size_t x; | |
| 115 | 115 | size_t y; | |
| @@ -118,8 +118,8 @@ char* play_func(const Vector* arguments, const char* id, game_state* game) | |||
|---|---|---|---|
| 118 | 118 | goto error; | |
| 119 | 119 | if(snprintf(color, 6, "%s", (char*)vector_at(arguments,0)) <= 0) //"white" and "black" are only 6 chars | |
| 120 | 120 | goto error; | |
| 121 | - | if(!(strcmp_nocase(color, "white") == 0 || strcmp_nocase(color, "black") == 0 || // check color | |
| 122 | - | strcmp_nocase(color, "w") == 0 || strcmp_nocase(color, "b") == 0)) | |
| 121 | + | if(!(cutil_strcasecmp(color, "white") == 0 || cutil_strcasecmp(color, "black") == 0 || // check color | |
| 122 | + | cutil_strcasecmp(color, "w") == 0 || cutil_strcasecmp(color, "b") == 0)) | |
| 123 | 123 | goto error; | |
| 124 | 124 | if(sscanf(vector_at(arguments,1), "%c", &xchar) <= 0) // get horizontal coordinate | |
| 125 | 125 | goto error; | |
| @@ -150,7 +150,7 @@ error: | |||
|---|---|---|---|
| 150 | 150 | ||
| 151 | 151 | char* genmove_func(const Vector* arguments, const char* id, game_state* game) | |
| 152 | 152 | { | |
| 153 | - | char* color = malloc(6); | |
| 153 | + | char* color = malloc_die(6); | |
| 154 | 154 | if(arguments->length == 1 && | |
| 155 | 155 | (snprintf(color, 6, "%s", (char*)vector_at(arguments,0)) > 0) && | |
| 156 | 156 | (strcmp(color, "white") == 0 || strcmp(color, "black") == 0 || | |
Msrc/simple-gtp.c
| @@ -4,7 +4,7 @@ | |||
|---|---|---|---|
| 4 | 4 | typedef struct msg_formatted | |
| 5 | 5 | { | |
| 6 | 6 | char* command; | |
| 7 | - | Vector* arguments; | |
| 7 | + | Vector arguments; | |
| 8 | 8 | char* id; | |
| 9 | 9 | } msg_formatted; | |
| 10 | 10 | ||
| @@ -20,17 +20,21 @@ static msg_formatted format_msg(const char* msg) | |||
|---|---|---|---|
| 20 | 20 | ||
| 21 | 21 | if(index) | |
| 22 | 22 | { | |
| 23 | - | formatted.id = malloc(index+1); | |
| 23 | + | formatted.id = malloc_die(index+1); | |
| 24 | 24 | strncpy(formatted.id, msg, index); | |
| 25 | 25 | formatted.id[index] = '\0'; | |
| 26 | 26 | } | |
| 27 | 27 | ||
| 28 | - | formatted.command = malloc(strlen(msg+index) + 1); | |
| 28 | + | formatted.command = malloc_die(strlen(msg+index) + 1); | |
| 29 | 29 | sscanf(msg+index, "%s", formatted.command); | |
| 30 | 30 | ||
| 31 | - | formatted.arguments = new_vector(); | |
| 31 | + | if(!new_vector_adv(&formatted.arguments, VECTOR_DEFAULT_SIZE, free)) | |
| 32 | + | { | |
| 33 | + | perror("malloc"); | |
| 34 | + | exit(EXIT_FAILURE); | |
| 35 | + | } | |
| 32 | 36 | ||
| 33 | - | char* tmp_msg = malloc(strlen(msg+index)+1); | |
| 37 | + | char* tmp_msg = malloc_die(strlen(msg+index)+1); | |
| 34 | 38 | strcpy(tmp_msg, msg+index); | |
| 35 | 39 | ||
| 36 | 40 | ||
| @@ -39,14 +43,14 @@ static msg_formatted format_msg(const char* msg) | |||
|---|---|---|---|
| 39 | 43 | ||
| 40 | 44 | while(current) | |
| 41 | 45 | { | |
| 42 | - | tmp_arg = malloc(strlen(current)+1); | |
| 46 | + | tmp_arg = malloc_die(strlen(current)+1); | |
| 43 | 47 | strcpy(tmp_arg, current); | |
| 44 | - | vector_push(formatted.arguments, tmp_arg); | |
| 48 | + | vector_push(&formatted.arguments, tmp_arg); | |
| 45 | 49 | current = strtok(NULL, " "); | |
| 46 | 50 | } | |
| 47 | 51 | ||
| 48 | - | if(formatted.arguments->length > 1) | |
| 49 | - | vector_remove(formatted.arguments, 0, free); | |
| 52 | + | if(formatted.arguments.length > 1) | |
| 53 | + | vector_remove(&formatted.arguments, 0); | |
| 50 | 54 | ||
| 51 | 55 | free(tmp_msg); | |
| 52 | 56 | ||
| @@ -85,7 +89,7 @@ char* handle_gtp_cmd(const char* msg, game_state* game) | |||
|---|---|---|---|
| 85 | 89 | ||
| 86 | 90 | msg_formatted formatted = format_msg(msg_san); | |
| 87 | 91 | char* command = formatted.command; | |
| 88 | - | Vector* arguments = formatted.arguments; | |
| 92 | + | Vector arguments = formatted.arguments; | |
| 89 | 93 | char* id = formatted.id; | |
| 90 | 94 | ||
| 91 | 95 | char* ret; | |
| @@ -118,12 +122,12 @@ char* handle_gtp_cmd(const char* msg, game_state* game) | |||
|---|---|---|---|
| 118 | 122 | func_ptr = final_score_func; | |
| 119 | 123 | } | |
| 120 | 124 | ||
| 121 | - | ret = func_ptr(arguments, id, game); | |
| 125 | + | ret = func_ptr(&arguments, id, game); | |
| 122 | 126 | ||
| 123 | 127 | free(command); | |
| 124 | 128 | free(msg_san); | |
| 125 | 129 | free(id); | |
| 126 | - | delete_vector(arguments, free); | |
| 130 | + | delete_vector(&arguments); | |
| 127 | 131 | ||
| 128 | 132 | return ret; | |
| 129 | 133 | } | |
Msrc/test.c
| @@ -164,9 +164,9 @@ static void test5(void) | |||
|---|---|---|---|
| 164 | 164 | ||
| 165 | 165 | go_score* score = score_game(game); | |
| 166 | 166 | print_board(game->board); | |
| 167 | - | printf("White groups: %ld\n", score->white_groups->length); | |
| 167 | + | printf("White groups: %ld\n", score->white_groups.length); | |
| 168 | 168 | printf("White points: %lf\n", score->white_points); | |
| 169 | - | printf("Black groups: %ld\n", score->black_groups->length); | |
| 169 | + | printf("Black groups: %ld\n", score->black_groups.length); | |
| 170 | 170 | printf("Black points: %lf\n", score->black_points); | |
| 171 | 171 | ||
| 172 | 172 | ||