Skip to content

Commit 86b2118

Browse files
committed
make use of macros for malloc and free, so that the function can be replaced easily
1 parent 8bf3254 commit 86b2118

File tree

3 files changed

+98
-62
lines changed

3 files changed

+98
-62
lines changed

example/main.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void print_mino_stack(const OOPetrisMino* const stack) {
192192

193193
const size_t buffer_size = properties->height * properties->width;
194194

195-
char* buffer = malloc(buffer_size);
195+
char* buffer = OOPETRIS_MALLOC(buffer_size);
196196

197197
if (buffer == NULL) {
198198
return;
@@ -213,13 +213,13 @@ void print_mino_stack(const OOPetrisMino* const stack) {
213213
int result = write(STDOUT_FILENO, buffer + (y * properties->width), properties->width);
214214
#endif
215215
if (result < 0) {
216-
free(buffer);
216+
OOPETRIS_FREE(buffer);
217217
return;
218218
}
219219
printf("\n");
220220
}
221221

222-
free(buffer);
222+
OOPETRIS_FREE(buffer);
223223
FREE_AND_SET_NULL(oopetris_free_grid_properties, properties);
224224
}
225225

@@ -369,7 +369,7 @@ typedef struct {
369369

370370
static char* copy_str(const char* input) {
371371
size_t size = strlen(input);
372-
char* string = (char*) malloc(size + 1);
372+
char* string = (char*) OOPETRIS_MALLOC(size + 1);
373373

374374
if (string == NULL) {
375375
return NULL;
@@ -396,7 +396,7 @@ static float* get_float(const char* input) {
396396
return NULL;
397397
}
398398

399-
float* return_value = malloc(sizeof(float));
399+
float* return_value = OOPETRIS_MALLOC(sizeof(float));
400400

401401
if (return_value == NULL) {
402402
return NULL;
@@ -419,7 +419,7 @@ static double* get_double(const char* input) {
419419
return NULL;
420420
}
421421

422-
double* return_value = malloc(sizeof(double));
422+
double* return_value = OOPETRIS_MALLOC(sizeof(double));
423423

424424
if (return_value == NULL) {
425425
return NULL;
@@ -447,7 +447,7 @@ static long long* get_long(const char* input) {
447447
return NULL;
448448
}
449449

450-
long long* return_value = malloc(sizeof(long long));
450+
long long* return_value = OOPETRIS_MALLOC(sizeof(long long));
451451

452452
if (return_value == NULL) {
453453
return NULL;
@@ -473,7 +473,7 @@ static unsigned long long* get_ulong(const char* input) {
473473
return NULL;
474474
}
475475

476-
unsigned long long* return_value = malloc(sizeof(unsigned long long));
476+
unsigned long long* return_value = OOPETRIS_MALLOC(sizeof(unsigned long long));
477477

478478
if (return_value == NULL) {
479479
return NULL;
@@ -498,7 +498,7 @@ static bool* get_bool(const char* input) {
498498
}
499499

500500

501-
bool* return_value = malloc(sizeof(bool));
501+
bool* return_value = OOPETRIS_MALLOC(sizeof(bool));
502502

503503
if (return_value == NULL) {
504504
return NULL;
@@ -515,9 +515,9 @@ Command parse_command(State* state, void** data, const char* input) {
515515
#define ERROR_BUF_SIZE 0x200
516516
#define RETURN_ERROR(...) \
517517
do { \
518-
*data = malloc(ERROR_BUF_SIZE); \
518+
*data = OOPETRIS_MALLOC(ERROR_BUF_SIZE); \
519519
if (snprintf(*data, ERROR_BUF_SIZE, __VA_ARGS__) < 0) { \
520-
free(*data); \
520+
OOPETRIS_FREE(*data); \
521521
*data = NULL; \
522522
} \
523523
return CommandError; \
@@ -647,7 +647,7 @@ Command parse_command(State* state, void** data, const char* input) {
647647
char* malloced_str = copy_str(key);
648648
VERIFY_MALLOC(malloced_str);
649649

650-
TempData* return_value = (TempData*) malloc(sizeof(TempData));
650+
TempData* return_value = (TempData*) OOPETRIS_MALLOC(sizeof(TempData));
651651
VERIFY_MALLOC(return_value);
652652

653653
return_value->type = TempDataEnumAddInfoKey;
@@ -670,7 +670,7 @@ Command parse_command(State* state, void** data, const char* input) {
670670
ASSERT_OR_ERROR(value != NULL, "Not a float: %s", float_v);
671671

672672
field = oopetris_additional_information_create_float(*value);
673-
free(value);
673+
OOPETRIS_FREE(value);
674674
adding = "float";
675675
goto return_field_value;
676676
}
@@ -689,7 +689,7 @@ Command parse_command(State* state, void** data, const char* input) {
689689
ASSERT_OR_ERROR(value != NULL, "Not a double: %s", double_v);
690690

691691
field = oopetris_additional_information_create_double(*value);
692-
free(value);
692+
OOPETRIS_FREE(value);
693693
adding = "double";
694694
goto return_field_value;
695695
}
@@ -708,7 +708,7 @@ Command parse_command(State* state, void** data, const char* input) {
708708
ASSERT_OR_ERROR(value != NULL, "Not a bool: %s", bool_v);
709709

710710
field = oopetris_additional_information_create_bool(*value);
711-
free(value);
711+
OOPETRIS_FREE(value);
712712
adding = "bool";
713713
goto return_field_value;
714714
}
@@ -784,7 +784,7 @@ Command parse_command(State* state, void** data, const char* input) {
784784
RETURN_ERROR("PROGRAMMING ERROR: UNREACHABLE");
785785
}
786786

787-
free(value);
787+
OOPETRIS_FREE(value);
788788
adding = "unsigned int";
789789
goto return_field_value;
790790
}
@@ -854,7 +854,7 @@ Command parse_command(State* state, void** data, const char* input) {
854854
RETURN_ERROR("PROGRAMMING ERROR: UNREACHABLE");
855855
}
856856

857-
free(value);
857+
OOPETRIS_FREE(value);
858858
adding = "int";
859859
goto return_field_value;
860860
}
@@ -939,15 +939,15 @@ Command parse_command(State* state, void** data, const char* input) {
939939
oopetris_additional_information_create_vector(final_vec);
940940

941941

942-
AdditionalInformationData* return_value =
943-
(AdditionalInformationData*) malloc(sizeof(AdditionalInformationData));
942+
AdditionalInformationData* return_value = (AdditionalInformationData*)
943+
OOPETRIS_MALLOC(sizeof(AdditionalInformationData));
944944
VERIFY_MALLOC(return_value);
945945

946946
return_value->key = temp->value.vector.key;
947947
return_value->value = field;
948948

949949
stbds_arrfree(temp->value.vector.vectors);
950-
free(temp);
950+
OOPETRIS_FREE(temp);
951951

952952
*data = return_value;
953953
return CommandInsertAddInfo;
@@ -988,12 +988,12 @@ Command parse_command(State* state, void** data, const char* input) {
988988
// construct a normal CommandInsertAddInfo
989989

990990
AdditionalInformationData* return_value =
991-
(AdditionalInformationData*) malloc(sizeof(AdditionalInformationData));
991+
(AdditionalInformationData*) OOPETRIS_MALLOC(sizeof(AdditionalInformationData));
992992
VERIFY_MALLOC(return_value);
993993

994994
return_value->key = temp->value.add_info_key.key;
995995
return_value->value = field;
996-
free(temp);
996+
OOPETRIS_FREE(temp);
997997

998998
*data = return_value;
999999
return CommandInsertAddInfo;
@@ -1111,7 +1111,7 @@ int write_to_file(const char* file, bool failOnREPLError) {
11111111
}
11121112

11131113
printf("Error: %s\n", (const char*) data);
1114-
free(data);
1114+
OOPETRIS_FREE(data);
11151115
data = NULL;
11161116
if (failOnREPLError) {
11171117
return EXIT_FAILURE;
@@ -1120,23 +1120,23 @@ int write_to_file(const char* file, bool failOnREPLError) {
11201120
case CommandInsertAddInfo: {
11211121
AdditionalInformationData* ptr = (AdditionalInformationData*) data;
11221122
oopetris_add_information_field(information->information, ptr->key, ptr->value);
1123-
free(ptr->key);
1124-
free(ptr);
1123+
OOPETRIS_FREE(ptr->key);
1124+
OOPETRIS_FREE(ptr);
11251125
data = NULL;
11261126
break;
11271127
}
11281128
default:
11291129
break;
11301130
}
11311131

1132-
free(input);
1132+
OOPETRIS_FREE(input);
11331133
}
11341134

11351135
char* write_error = oopetris_write_to_file(information, file, false);
11361136

11371137
if (write_error != NULL) {
11381138
fprintf(stderr, "An error occured, while trying to write information to file '%s': %s\n", file, write_error);
1139-
free(write_error);
1139+
OOPETRIS_FREE(write_error);
11401140
oopetris_free_recording_information(information);
11411141
return EXIT_FAILURE;
11421142
}

src/oopetris_wrapper.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,33 @@
22

33
#pragma once
44

5-
// assure we only include the header and with correct defines
5+
// assure we only include the header with correct defines
66
#ifndef STBDS_NO_SHORT_NAMES
77
#define STBDS_NO_SHORT_NAMES
88
#endif
99
#ifdef STB_DS_IMPLEMENTATION
1010
#undef STB_DS_IMPLEMENTATION
1111
#endif
12+
13+
14+
#if defined(OOPETRIS_REALLOC) && !defined(OOPETRIS_FREE) || !defined(OOPETRIS_REALLOC) && defined(OOPETRIS_FREE)
15+
#error "You must define both OOPETRIS_REALLOC and OOPETRIS_FREE, or neither."
16+
#endif
17+
18+
#if defined(OOPETRIS_REALLOC) && defined(OOPETRIS_FREE)
19+
#define STBDS_REALLOC(c, p, s) OOPETRIS_REALLOC(p, s)
20+
#define STBDS_FREE(c, p) OOPETRIS_FREE(p)
21+
#else
22+
23+
#include <stdlib.h>
24+
25+
#define OOPETRIS_REALLOC(p, s) realloc(p, s)
26+
#define OOPETRIS_FREE(p) free(p)
27+
#endif
28+
29+
30+
#define OOPETRIS_MALLOC(s) OOPETRIS_REALLOC(NULL, s)
31+
1232
#include "./thirdparty/stb_ds.h"
1333

1434

0 commit comments

Comments
 (0)