1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-01-10 17:32:05 +05:30

reverse-ramdisk.c: convert macro names to upper case

This commit is contained in:
パチュリー・ノーレッジ 2024-01-03 21:21:29 +03:00
parent a5898629f1
commit 888b257a7b
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -29,12 +29,12 @@ To-Do: Add thread-safe versions of functions (use postfix `_r`)
#endif #endif
#ifdef DEBUG #ifdef DEBUG
# define line_fail(x) printf("failed on line %d\n", __LINE__ + x) # define LINE_FAIL(x) printf("failed on line %d\n", __LINE__ + x)
#else #else
# define line_fail(x) # define LINE_FAIL(x)
#endif #endif
#define retreat(s) do { \ #define RETREAT(s) do { \
perror(s); \ perror(s); \
exit(EXIT_FAILURE); \ exit(EXIT_FAILURE); \
} while (0) } while (0)
@ -68,13 +68,13 @@ int tf_alloc(size_t n, size_t type_size) {
size_t file_path_len = len_digit + strlen("tf_.tmp"); size_t file_path_len = len_digit + strlen("tf_.tmp");
char* file_path = malloc((file_path_len + 1) * sizeof(char)); char* file_path = malloc((file_path_len + 1) * sizeof(char));
if (file_path == NULL) { if (file_path == NULL) {
line_fail(-2); LINE_FAIL(-2);
return -1; return -1;
} }
int res = snprintf(file_path, file_path_len + 1, "tf_%" PRIuMAX ".tmp", int res = snprintf(file_path, file_path_len + 1, "tf_%" PRIuMAX ".tmp",
(uintmax_t) num_temp_files); (uintmax_t) num_temp_files);
if ((size_t) res != file_path_len) { if ((size_t) res != file_path_len) {
line_fail(-2); LINE_FAIL(-2);
return -1; return -1;
} }
@ -85,14 +85,14 @@ int tf_alloc(size_t n, size_t type_size) {
FILE* file = fopen(file_path, "w+b"); FILE* file = fopen(file_path, "w+b");
if (file == NULL) { if (file == NULL) {
#endif #endif
line_fail(-2); LINE_FAIL(-2);
return -1; return -1;
} }
// Allocate memory for the TempFile struct // Allocate memory for the TempFile struct
TempFile* temp_file = malloc(sizeof(TempFile)); TempFile* temp_file = malloc(sizeof(TempFile));
if (temp_file == NULL) { if (temp_file == NULL) {
line_fail(-2); LINE_FAIL(-2);
return -1; return -1;
} }
@ -109,7 +109,7 @@ int tf_alloc(size_t n, size_t type_size) {
temp_files = realloc(temp_files, temp_files = realloc(temp_files,
(size_t) num_temp_files * sizeof(TempFile)); (size_t) num_temp_files * sizeof(TempFile));
if (temp_files == NULL) { if (temp_files == NULL) {
line_fail(-2); LINE_FAIL(-2);
return -1; return -1;
} }
@ -136,7 +136,7 @@ int tf_free(int ID) {
// Delete the file // Delete the file
if (remove(temp_files[index].file_path) != 0) { if (remove(temp_files[index].file_path) != 0) {
line_fail(-1); LINE_FAIL(-1);
return -1; return -1;
} }
@ -150,7 +150,7 @@ int tf_free(int ID) {
if (--num_temp_files > 0) { if (--num_temp_files > 0) {
if ((temp_files = realloc(temp_files, num_temp_files if ((temp_files = realloc(temp_files, num_temp_files
* sizeof(TempFile))) == NULL) { * sizeof(TempFile))) == NULL) {
line_fail(-2); LINE_FAIL(-2);
return -1; return -1;
} }
} }
@ -186,7 +186,7 @@ int tf_write(int ID, size_t offset, void* src, size_t data_size) {
#else #else
if (fseek(file, offset, SEEK_SET) == -1) { if (fseek(file, offset, SEEK_SET) == -1) {
#endif #endif
line_fail(-1); LINE_FAIL(-1);
return -1; return -1;
} }
@ -208,7 +208,7 @@ int tf_write(int ID, size_t offset, void* src, size_t data_size) {
#if IS_POSIX #if IS_POSIX
if (fsync(file) == -1) { if (fsync(file) == -1) {
line_fail(-1); LINE_FAIL(-1);
return -1; return -1;
} }
#else #else
@ -246,7 +246,7 @@ int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
#else #else
fclose(file); fclose(file);
#endif #endif
line_fail(-7); LINE_FAIL(-7);
return -1; return -1;
} }
@ -257,7 +257,7 @@ int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
#else #else
if (fseek(file, offset, SEEK_SET) == -1) { if (fseek(file, offset, SEEK_SET) == -1) {
#endif #endif
line_fail(-1); LINE_FAIL(-1);
return -1; return -1;
} }
@ -298,13 +298,13 @@ int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
int main(void) { int main(void) {
int ID = tf_alloc(4, sizeof(int)); int ID = tf_alloc(4, sizeof(int));
if (ID == -1) if (ID == -1)
retreat("tf_alloc"); RETREAT("tf_alloc");
int test_data[4] = {123, 456, 789, -123}; int test_data[4] = {123, 456, 789, -123};
for (size_t i = 0; i < 4; i++) for (size_t i = 0; i < 4; i++)
if (tf_write(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1) if (tf_write(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1)
retreat("tf_write"); RETREAT("tf_write");
// round-trip // round-trip
test_data[0] = 111; test_data[0] = 111;
@ -314,7 +314,7 @@ int main(void) {
for (size_t i = 0; i < 4; i++) for (size_t i = 0; i < 4; i++)
if (tf_read(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1) if (tf_read(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1)
retreat("tf_read"); RETREAT("tf_read");
printf("Values: %d %d %d %d\n", printf("Values: %d %d %d %d\n",
test_data[0], test_data[1], test_data[2], test_data[3]); test_data[0], test_data[1], test_data[2], test_data[3]);