Compare commits

...

4 commits

Author SHA1 Message Date
5068a1b90d clang-format
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 4s
2024-09-21 11:43:25 +09:00
bb574d2a2c Use stdint.h types instead of "int", "long", etc.
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 7s
2024-09-20 21:40:35 +09:00
ed85ded967 Add single-ptr variants data structures' free fns
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 6s
2024-09-13 12:51:09 +09:00
6b862ab022 Refactor priority-heap insert fn
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 6s
priority-heap insert now takes a pointer to a priority-heap instead of a
double pointer.
2024-09-13 11:33:15 +09:00
14 changed files with 304 additions and 252 deletions

View file

@ -280,9 +280,9 @@ int write_files_fn(void *data, void *ud) {
// Write file to pipe, and read from other pipe. // Write file to pipe, and read from other pipe.
char write_buf[1024]; char write_buf[1024];
char read_buf[1024]; char read_buf[1024];
int write_again = 0; int_fast8_t write_again = 0;
int write_done = 0; int_fast8_t write_done = 0;
int read_done = 0; int_fast8_t read_done = 0;
size_t write_count; size_t write_count;
size_t read_count; size_t read_count;
ssize_t ret; ssize_t ret;
@ -412,7 +412,7 @@ int write_files_fn(void *data, void *ud) {
temp_to_write->buf = malloc(4); temp_to_write->buf = malloc(4);
temp_to_write->size = 4; temp_to_write->size = 4;
for (size_t idx = 0; idx < temp_to_write->size; ++idx) { for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
((unsigned char *)temp_to_write->buf)[idx] = 0; ((uint8_t *)temp_to_write->buf)[idx] = 0;
} }
// Get file stats. // Get file stats.
@ -431,31 +431,31 @@ int write_files_fn(void *data, void *ud) {
} }
if ((stat_buf.st_mode & S_IRUSR) != 0) { if ((stat_buf.st_mode & S_IRUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x2; ((uint8_t *)temp_to_write->buf)[0] |= 0x2;
} }
if ((stat_buf.st_mode & S_IWUSR) != 0) { if ((stat_buf.st_mode & S_IWUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x4; ((uint8_t *)temp_to_write->buf)[0] |= 0x4;
} }
if ((stat_buf.st_mode & S_IXUSR) != 0) { if ((stat_buf.st_mode & S_IXUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x8; ((uint8_t *)temp_to_write->buf)[0] |= 0x8;
} }
if ((stat_buf.st_mode & S_IRGRP) != 0) { if ((stat_buf.st_mode & S_IRGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x10; ((uint8_t *)temp_to_write->buf)[0] |= 0x10;
} }
if ((stat_buf.st_mode & S_IWGRP) != 0) { if ((stat_buf.st_mode & S_IWGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x20; ((uint8_t *)temp_to_write->buf)[0] |= 0x20;
} }
if ((stat_buf.st_mode & S_IXGRP) != 0) { if ((stat_buf.st_mode & S_IXGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x40; ((uint8_t *)temp_to_write->buf)[0] |= 0x40;
} }
if ((stat_buf.st_mode & S_IROTH) != 0) { if ((stat_buf.st_mode & S_IROTH) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x80; ((uint8_t *)temp_to_write->buf)[0] |= 0x80;
} }
if ((stat_buf.st_mode & S_IWOTH) != 0) { if ((stat_buf.st_mode & S_IWOTH) != 0) {
((unsigned char *)temp_to_write->buf)[1] |= 0x1; ((uint8_t *)temp_to_write->buf)[1] |= 0x1;
} }
if ((stat_buf.st_mode & S_IXOTH) != 0) { if ((stat_buf.st_mode & S_IXOTH) != 0) {
((unsigned char *)temp_to_write->buf)[1] |= 0x2; ((uint8_t *)temp_to_write->buf)[1] |= 0x2;
} }
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write); simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
@ -544,7 +544,7 @@ int write_files_fn(void *data, void *ud) {
temp_to_write->buf = malloc(4); temp_to_write->buf = malloc(4);
temp_to_write->size = 4; temp_to_write->size = 4;
for (size_t idx = 0; idx < temp_to_write->size; ++idx) { for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
((unsigned char *)temp_to_write->buf)[idx] = 0; ((uint8_t *)temp_to_write->buf)[idx] = 0;
} }
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
@ -568,35 +568,35 @@ int write_files_fn(void *data, void *ud) {
} }
if ((stat_buf.st_mode & S_IRUSR) != 0) { if ((stat_buf.st_mode & S_IRUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x2; ((uint8_t *)temp_to_write->buf)[0] |= 0x2;
} }
if ((stat_buf.st_mode & S_IWUSR) != 0) { if ((stat_buf.st_mode & S_IWUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x4; ((uint8_t *)temp_to_write->buf)[0] |= 0x4;
} }
if ((stat_buf.st_mode & S_IXUSR) != 0) { if ((stat_buf.st_mode & S_IXUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x8; ((uint8_t *)temp_to_write->buf)[0] |= 0x8;
} }
if ((stat_buf.st_mode & S_IRGRP) != 0) { if ((stat_buf.st_mode & S_IRGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x10; ((uint8_t *)temp_to_write->buf)[0] |= 0x10;
} }
if ((stat_buf.st_mode & S_IWGRP) != 0) { if ((stat_buf.st_mode & S_IWGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x20; ((uint8_t *)temp_to_write->buf)[0] |= 0x20;
} }
if ((stat_buf.st_mode & S_IXGRP) != 0) { if ((stat_buf.st_mode & S_IXGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x40; ((uint8_t *)temp_to_write->buf)[0] |= 0x40;
} }
if ((stat_buf.st_mode & S_IROTH) != 0) { if ((stat_buf.st_mode & S_IROTH) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x80; ((uint8_t *)temp_to_write->buf)[0] |= 0x80;
} }
if ((stat_buf.st_mode & S_IWOTH) != 0) { if ((stat_buf.st_mode & S_IWOTH) != 0) {
((unsigned char *)temp_to_write->buf)[1] |= 0x1; ((uint8_t *)temp_to_write->buf)[1] |= 0x1;
} }
if ((stat_buf.st_mode & S_IXOTH) != 0) { if ((stat_buf.st_mode & S_IXOTH) != 0) {
((unsigned char *)temp_to_write->buf)[1] |= 0x2; ((uint8_t *)temp_to_write->buf)[1] |= 0x2;
} }
#else #else
// Unsupported platform. Just set the permission bits for user. // Unsupported platform. Just set the permission bits for user.
((unsigned char *)temp_to_write->buf)[0] |= 0xE; ((uint8_t *)temp_to_write->buf)[0] |= 0xE;
#endif #endif
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write); simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
@ -685,11 +685,11 @@ int write_files_fn(void *data, void *ud) {
temp_to_write->buf = malloc(4); temp_to_write->buf = malloc(4);
temp_to_write->size = 4; temp_to_write->size = 4;
for (size_t idx = 0; idx < temp_to_write->size; ++idx) { for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
((unsigned char *)temp_to_write->buf)[idx] = 0; ((uint8_t *)temp_to_write->buf)[idx] = 0;
} }
// Set "is symbolic link" flag. // Set "is symbolic link" flag.
((unsigned char *)temp_to_write->buf)[0] = 1; ((uint8_t *)temp_to_write->buf)[0] = 1;
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
@ -705,35 +705,35 @@ int write_files_fn(void *data, void *ud) {
} }
if ((stat_buf.st_mode & S_IRUSR) != 0) { if ((stat_buf.st_mode & S_IRUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x2; ((uint8_t *)temp_to_write->buf)[0] |= 0x2;
} }
if ((stat_buf.st_mode & S_IWUSR) != 0) { if ((stat_buf.st_mode & S_IWUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x4; ((uint8_t *)temp_to_write->buf)[0] |= 0x4;
} }
if ((stat_buf.st_mode & S_IXUSR) != 0) { if ((stat_buf.st_mode & S_IXUSR) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x8; ((uint8_t *)temp_to_write->buf)[0] |= 0x8;
} }
if ((stat_buf.st_mode & S_IRGRP) != 0) { if ((stat_buf.st_mode & S_IRGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x10; ((uint8_t *)temp_to_write->buf)[0] |= 0x10;
} }
if ((stat_buf.st_mode & S_IWGRP) != 0) { if ((stat_buf.st_mode & S_IWGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x20; ((uint8_t *)temp_to_write->buf)[0] |= 0x20;
} }
if ((stat_buf.st_mode & S_IXGRP) != 0) { if ((stat_buf.st_mode & S_IXGRP) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x40; ((uint8_t *)temp_to_write->buf)[0] |= 0x40;
} }
if ((stat_buf.st_mode & S_IROTH) != 0) { if ((stat_buf.st_mode & S_IROTH) != 0) {
((unsigned char *)temp_to_write->buf)[0] |= 0x80; ((uint8_t *)temp_to_write->buf)[0] |= 0x80;
} }
if ((stat_buf.st_mode & S_IWOTH) != 0) { if ((stat_buf.st_mode & S_IWOTH) != 0) {
((unsigned char *)temp_to_write->buf)[1] |= 0x1; ((uint8_t *)temp_to_write->buf)[1] |= 0x1;
} }
if ((stat_buf.st_mode & S_IXOTH) != 0) { if ((stat_buf.st_mode & S_IXOTH) != 0) {
((unsigned char *)temp_to_write->buf)[1] |= 0x2; ((uint8_t *)temp_to_write->buf)[1] |= 0x2;
} }
#else #else
// Unsupported platform. Just set the permission bits for user. // Unsupported platform. Just set the permission bits for user.
((unsigned char *)temp_to_write->buf)[0] |= 0xE; ((uint8_t *)temp_to_write->buf)[0] |= 0xE;
#endif #endif
// Need to get abs_path for checking/setting a flag before storing flags. // Need to get abs_path for checking/setting a flag before storing flags.
@ -781,7 +781,7 @@ int write_files_fn(void *data, void *ud) {
// link_substr, dest_substr); // link_substr, dest_substr);
// Generate the relative path. // Generate the relative path.
int has_slash = 0; int_fast8_t has_slash = 0;
idx = 0; idx = 0;
do { do {
for (; link_substr[idx] != '/' && link_substr[idx] != 0; ++idx); for (; link_substr[idx] != '/' && link_substr[idx] != 0; ++idx);
@ -810,7 +810,7 @@ int write_files_fn(void *data, void *ud) {
fprintf(stderr, fprintf(stderr,
"NOTICE: abs_path exists, \"--no-abs-symlink\" not specified, " "NOTICE: abs_path exists, \"--no-abs-symlink\" not specified, "
"and link refers to file NOT in archive; preferring abs_path.\n"); "and link refers to file NOT in archive; preferring abs_path.\n");
((unsigned char *)temp_to_write->buf)[1] |= 0x4; ((uint8_t *)temp_to_write->buf)[1] |= 0x4;
} }
// Store the 4 byte bit-flags for file. // Store the 4 byte bit-flags for file.
@ -1063,7 +1063,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
return SDAS_NO_COMPRESSOR; return SDAS_NO_COMPRESSOR;
} else if (state->parsed->compressor && state->parsed->decompressor) { } else if (state->parsed->compressor && state->parsed->decompressor) {
// Write the four flag bytes with first bit set. // Write the four flag bytes with first bit set.
unsigned char c = 1; uint8_t c = 1;
if (fwrite(&c, 1, 1, out_f) != 1) { if (fwrite(&c, 1, 1, out_f) != 1) {
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} }
@ -1116,7 +1116,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
} }
} else { } else {
// Write the four flag bytes with first bit NOT set. // Write the four flag bytes with first bit NOT set.
unsigned char c = 0; uint8_t c = 0;
for (size_t i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
if (fwrite(&c, 1, 1, out_f) != 1) { if (fwrite(&c, 1, 1, out_f) != 1) {
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
@ -1171,14 +1171,14 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
return SDAS_SUCCESS; return SDAS_SUCCESS;
} }
int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state) { const SDArchiverState *state) {
unsigned char buf[1024]; uint8_t buf[1024];
memset(buf, 0, 1024); memset(buf, 0, 1024);
uint16_t u16; uint16_t u16;
uint32_t u32; uint32_t u32;
uint64_t u64; uint64_t u64;
int is_compressed = 0; int_fast8_t is_compressed = 0;
if (fread(buf, 1, 18, in_f) != 18) { if (fread(buf, 1, 18, in_f) != 18) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
@ -1226,7 +1226,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
__attribute__(( __attribute__((
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf = cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1); malloc(u16 + 1);
unsigned char *uc_heap_buf = heap_buf; uint8_t *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
@ -1253,7 +1253,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
__attribute__(( __attribute__((
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf = cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1); malloc(u16 + 1);
unsigned char *uc_heap_buf = heap_buf; uint8_t *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
@ -1274,7 +1274,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
const size_t digits = simple_archiver_helper_num_digits(size); const size_t digits = simple_archiver_helper_num_digits(size);
char format_str[128]; char format_str[128];
snprintf(format_str, 128, FILE_COUNTS_OUTPUT_FORMAT_STR_0, digits, digits); snprintf(format_str, 128, FILE_COUNTS_OUTPUT_FORMAT_STR_0, digits, digits);
int skip = 0; int_fast8_t skip = 0;
__attribute__((cleanup(simple_archiver_hash_map_free))) __attribute__((cleanup(simple_archiver_hash_map_free)))
SDArchiverHashMap *hash_map = NULL; SDArchiverHashMap *hash_map = NULL;
if (state && state->parsed->working_files && if (state && state->parsed->working_files &&
@ -1333,7 +1333,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
__attribute__(( __attribute__((
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf = cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
malloc(u16 + 1); malloc(u16 + 1);
unsigned char *uc_heap_buf = heap_buf; uint8_t *uc_heap_buf = heap_buf;
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
@ -1465,7 +1465,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
fprintf(stderr, " File size: %lu\n", u64); fprintf(stderr, " File size: %lu\n", u64);
} }
int skip_due_to_map = 0; int_fast8_t skip_due_to_map = 0;
if (hash_map != NULL && out_f_name) { if (hash_map != NULL && out_f_name) {
if (simple_archiver_hash_map_get(hash_map, out_f_name, if (simple_archiver_hash_map_get(hash_map, out_f_name,
strlen(out_f_name) + 1) == NULL) { strlen(out_f_name) + 1) == NULL) {
@ -1573,9 +1573,9 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
} }
uint64_t compressed_file_size = u64; uint64_t compressed_file_size = u64;
int write_again = 0; int_fast8_t write_again = 0;
int write_pipe_done = 0; int_fast8_t write_pipe_done = 0;
int read_pipe_done = 0; int_fast8_t read_pipe_done = 0;
size_t fread_ret; size_t fread_ret;
char recv_buf[1024]; char recv_buf[1024];
size_t amount_to_read; size_t amount_to_read;
@ -1747,7 +1747,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
} else { } else {
// Is a symbolic link. // Is a symbolic link.
int abs_preferred = (buf[1] & 0x4) != 0 ? 1 : 0; int_fast8_t abs_preferred = (buf[1] & 0x4) != 0 ? 1 : 0;
fprintf(stderr, " Absolute path is %s\n", fprintf(stderr, " Absolute path is %s\n",
(abs_preferred ? "preferred" : "NOT preferred")); (abs_preferred ? "preferred" : "NOT preferred"));

View file

@ -19,8 +19,11 @@
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_ #ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_ #define SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_
// Standard library includes.
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
// Local includes.
#include "data_structures/hash_map.h" #include "data_structures/hash_map.h"
#include "data_structures/linked_list.h" #include "data_structures/linked_list.h"
#include "parser.h" #include "parser.h"
@ -28,7 +31,7 @@
typedef struct SDArchiverState { typedef struct SDArchiverState {
/* /*
*/ */
unsigned int flags; uint32_t flags;
const SDArchiverParsed *parsed; const SDArchiverParsed *parsed;
FILE *out_f; FILE *out_f;
SDArchiverHashMap *map; SDArchiverHashMap *map;
@ -63,7 +66,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
const SDArchiverLinkedList *filenames); const SDArchiverLinkedList *filenames);
/// Returns zero on success. /// Returns zero on success.
int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state); const SDArchiverState *state);
/// Returns zero on success. /// Returns zero on success.

View file

@ -80,7 +80,7 @@ unsigned long long simple_archiver_hash_map_internal_key_to_hash(
unsigned long long temp = 0; unsigned long long temp = 0;
size_t count = 0; size_t count = 0;
for (size_t idx = 0; idx < key_size; ++idx) { for (size_t idx = 0; idx < key_size; ++idx) {
temp |= ((unsigned long long)*((unsigned char *)key + idx)) << (8 * count); temp |= ((unsigned long long)*((uint8_t *)key + idx)) << (8 * count);
++count; ++count;
if (count >= 8) { if (count >= 8) {
count = 0; count = 0;
@ -159,16 +159,21 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) {
return hash_map; return hash_map;
} }
void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) { void simple_archiver_hash_map_free_single_ptr(SDArchiverHashMap *hash_map) {
if (hash_map && *hash_map) { if (hash_map) {
for (size_t idx = 0; idx < (*hash_map)->buckets_size; ++idx) { for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) {
SDArchiverLinkedList **linked_list = (*hash_map)->buckets + idx; SDArchiverLinkedList **linked_list = hash_map->buckets + idx;
simple_archiver_list_free(linked_list); simple_archiver_list_free(linked_list);
} }
free((*hash_map)->buckets); free(hash_map->buckets);
free(*hash_map); free(hash_map);
}
}
void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
if (hash_map && *hash_map) {
simple_archiver_hash_map_free_single_ptr(*hash_map);
*hash_map = NULL; *hash_map = NULL;
} }
} }

View file

@ -21,8 +21,11 @@
#define SC_SA_DS_HASH_MAP_START_BUCKET_SIZE 32 #define SC_SA_DS_HASH_MAP_START_BUCKET_SIZE 32
// Standard library includes.
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
// Local includes.
#include "linked_list.h" #include "linked_list.h"
typedef struct SDArchiverHashMap { typedef struct SDArchiverHashMap {
@ -32,6 +35,11 @@ typedef struct SDArchiverHashMap {
} SDArchiverHashMap; } SDArchiverHashMap;
SDArchiverHashMap *simple_archiver_hash_map_init(void); SDArchiverHashMap *simple_archiver_hash_map_init(void);
/// It is recommended to use the double-pointer version of hash-map free as
/// that will ensure the variable holding the pointer will end up pointing to
/// NULL after free.
void simple_archiver_hash_map_free_single_ptr(SDArchiverHashMap *hash_map);
void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map); void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map);
/// Returns zero on success. /// Returns zero on success.

View file

@ -18,6 +18,8 @@
#include "linked_list.h" #include "linked_list.h"
// Standard library includes.
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
SDArchiverLinkedList *simple_archiver_list_init(void) { SDArchiverLinkedList *simple_archiver_list_init(void) {
@ -41,9 +43,9 @@ SDArchiverLinkedList *simple_archiver_list_init(void) {
return list; return list;
} }
void simple_archiver_list_free(SDArchiverLinkedList **list) { void simple_archiver_list_free_single_ptr(SDArchiverLinkedList *list) {
if (list && *list) { if (list) {
SDArchiverLLNode *node = (*list)->head; SDArchiverLLNode *node = list->head;
SDArchiverLLNode *prev; SDArchiverLLNode *prev;
while (node) { while (node) {
prev = node; prev = node;
@ -58,7 +60,13 @@ void simple_archiver_list_free(SDArchiverLinkedList **list) {
} }
} }
free(*list); free(list);
}
}
void simple_archiver_list_free(SDArchiverLinkedList **list) {
if (list && *list) {
simple_archiver_list_free_single_ptr(*list);
*list = NULL; *list = NULL;
} }
} }
@ -112,10 +120,10 @@ int simple_archiver_list_remove(SDArchiverLinkedList *list,
return 0; return 0;
} }
int removed_count = 0; int32_t removed_count = 0;
SDArchiverLLNode *node = list->head; SDArchiverLLNode *node = list->head;
int iter_removed = 0; int32_t iter_removed = 0;
while (node) { while (node) {
if (iter_removed == 0) { if (iter_removed == 0) {
node = node->next; node = node->next;

View file

@ -35,6 +35,11 @@ typedef struct SDArchiverLinkedList {
} SDArchiverLinkedList; } SDArchiverLinkedList;
SDArchiverLinkedList *simple_archiver_list_init(void); SDArchiverLinkedList *simple_archiver_list_init(void);
/// It is recommended to use the double-pointer version of list free as that
/// will ensure the variable holding the pointer will end up pointing to NULL
/// after free.
void simple_archiver_list_free_single_ptr(SDArchiverLinkedList *list);
void simple_archiver_list_free(SDArchiverLinkedList **list); void simple_archiver_list_free(SDArchiverLinkedList **list);
/// Returns 0 on success. Puts data at the end of the list /// Returns 0 on success. Puts data at the end of the list

View file

@ -21,32 +21,32 @@
#include <stdlib.h> #include <stdlib.h>
void simple_archiver_priority_heap_internal_realloc( void simple_archiver_priority_heap_internal_realloc(
SDArchiverPHeap **priority_heap) { SDArchiverPHeap *priority_heap) {
SDArchiverPHeap *new_priority_heap = malloc(sizeof(SDArchiverPHeap)); SDArchiverPHeap new_priority_heap;
new_priority_heap->capacity = (*priority_heap)->capacity * 2; new_priority_heap.capacity = priority_heap->capacity * 2;
new_priority_heap->size = 0; new_priority_heap.size = 0;
new_priority_heap->less_fn = (*priority_heap)->less_fn; new_priority_heap.less_fn = priority_heap->less_fn;
new_priority_heap->nodes = new_priority_heap.nodes =
calloc(new_priority_heap->capacity, sizeof(SDArchiverPHNode)); calloc(new_priority_heap.capacity, sizeof(SDArchiverPHNode));
for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) { for (size_t idx = 1; idx < priority_heap->size + 1; ++idx) {
if ((*priority_heap)->nodes[idx].is_valid != 0) { if (priority_heap->nodes[idx].is_valid != 0) {
simple_archiver_priority_heap_insert( simple_archiver_priority_heap_insert(
&new_priority_heap, (*priority_heap)->nodes[idx].priority, &new_priority_heap, priority_heap->nodes[idx].priority,
(*priority_heap)->nodes[idx].data, priority_heap->nodes[idx].data,
(*priority_heap)->nodes[idx].data_cleanup_fn); priority_heap->nodes[idx].data_cleanup_fn);
(*priority_heap)->nodes[idx].is_valid = 0; priority_heap->nodes[idx].is_valid = 0;
} }
} }
simple_archiver_priority_heap_free(priority_heap); free(priority_heap->nodes);
*priority_heap = new_priority_heap; *priority_heap = new_priority_heap;
} }
int simple_archiver_priority_heap_default_less(long long a, long long b) { int simple_archiver_priority_heap_default_less(int64_t a, int64_t b) {
return a < b ? 1 : 0; return a < b ? 1 : 0;
} }
@ -64,7 +64,7 @@ SDArchiverPHeap *simple_archiver_priority_heap_init(void) {
} }
SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn( SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
int (*less_fn)(long long, long long)) { int (*less_fn)(int64_t, int64_t)) {
SDArchiverPHeap *priority_heap = malloc(sizeof(SDArchiverPHeap)); SDArchiverPHeap *priority_heap = malloc(sizeof(SDArchiverPHeap));
priority_heap->capacity = SC_SA_DS_PRIORITY_HEAP_START_SIZE; priority_heap->capacity = SC_SA_DS_PRIORITY_HEAP_START_SIZE;
@ -77,54 +77,59 @@ SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
return priority_heap; return priority_heap;
} }
void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) { void simple_archiver_priority_heap_free_single_ptr(
if (priority_heap && *priority_heap) { SDArchiverPHeap *priority_heap) {
for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) { if (priority_heap) {
if ((*priority_heap)->nodes[idx].is_valid != 0) { for (size_t idx = 1; idx < priority_heap->size + 1; ++idx) {
if ((*priority_heap)->nodes[idx].data_cleanup_fn) { if (priority_heap->nodes[idx].is_valid != 0) {
(*priority_heap) if (priority_heap->nodes[idx].data_cleanup_fn) {
->nodes[idx] priority_heap->nodes[idx].data_cleanup_fn(
.data_cleanup_fn((*priority_heap)->nodes[idx].data); priority_heap->nodes[idx].data);
} else { } else {
free((*priority_heap)->nodes[idx].data); free(priority_heap->nodes[idx].data);
} }
(*priority_heap)->nodes[idx].is_valid = 0; priority_heap->nodes[idx].is_valid = 0;
} }
} }
free((*priority_heap)->nodes); free(priority_heap->nodes);
free(*priority_heap); free(priority_heap);
}
}
void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) {
if (priority_heap && *priority_heap) {
simple_archiver_priority_heap_free_single_ptr(*priority_heap);
*priority_heap = NULL; *priority_heap = NULL;
} }
} }
void simple_archiver_priority_heap_insert(SDArchiverPHeap **priority_heap, void simple_archiver_priority_heap_insert(SDArchiverPHeap *priority_heap,
long long priority, void *data, int64_t priority, void *data,
void (*data_cleanup_fn)(void *)) { void (*data_cleanup_fn)(void *)) {
if (!priority_heap || !*priority_heap) { if (!priority_heap) {
return; return;
} }
if ((*priority_heap)->size + 1 >= (*priority_heap)->capacity) { if (priority_heap->size + 1 >= priority_heap->capacity) {
simple_archiver_priority_heap_internal_realloc(priority_heap); simple_archiver_priority_heap_internal_realloc(priority_heap);
} }
size_t hole = (*priority_heap)->size + 1; size_t hole = priority_heap->size + 1;
while (hole > 1 && while (hole > 1 &&
(*priority_heap) priority_heap->less_fn(priority,
->less_fn(priority, priority_heap->nodes[hole / 2].priority) != 0) {
(*priority_heap)->nodes[hole / 2].priority) != 0) { priority_heap->nodes[hole] = priority_heap->nodes[hole / 2];
(*priority_heap)->nodes[hole] = (*priority_heap)->nodes[hole / 2];
hole /= 2; hole /= 2;
} }
(*priority_heap)->nodes[hole].priority = priority; priority_heap->nodes[hole].priority = priority;
(*priority_heap)->nodes[hole].data = data; priority_heap->nodes[hole].data = data;
(*priority_heap)->nodes[hole].data_cleanup_fn = data_cleanup_fn; priority_heap->nodes[hole].data_cleanup_fn = data_cleanup_fn;
(*priority_heap)->nodes[hole].is_valid = 1; priority_heap->nodes[hole].is_valid = 1;
++(*priority_heap)->size; ++priority_heap->size;
} }
void *simple_archiver_priority_heap_top(SDArchiverPHeap *priority_heap) { void *simple_archiver_priority_heap_top(SDArchiverPHeap *priority_heap) {

View file

@ -19,10 +19,13 @@
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_ #ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_ #define SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_
// Standard library includes.
#include <stdint.h>
#define SC_SA_DS_PRIORITY_HEAP_START_SIZE 32 #define SC_SA_DS_PRIORITY_HEAP_START_SIZE 32
typedef struct SDArchiverPHNode { typedef struct SDArchiverPHNode {
long long priority; int64_t priority;
void *data; void *data;
void (*data_cleanup_fn)(void *); void (*data_cleanup_fn)(void *);
/// Is non-zero if valid. /// Is non-zero if valid.
@ -31,23 +34,29 @@ typedef struct SDArchiverPHNode {
typedef struct SDArchiverPHeap { typedef struct SDArchiverPHeap {
SDArchiverPHNode *nodes; SDArchiverPHNode *nodes;
unsigned long long capacity; uint64_t capacity;
unsigned long long size; uint64_t size;
int (*less_fn)(long long, long long); int (*less_fn)(int64_t, int64_t);
} SDArchiverPHeap; } SDArchiverPHeap;
/// Default "less" function to determine if a has higher priority than b. /// Default "less" function to determine if a has higher priority than b.
/// Returns non-zero if "less". /// Returns non-zero if "less".
int simple_archiver_priority_heap_default_less(long long a, long long b); int simple_archiver_priority_heap_default_less(int64_t a, int64_t b);
SDArchiverPHeap *simple_archiver_priority_heap_init(void); SDArchiverPHeap *simple_archiver_priority_heap_init(void);
SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn( SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
int (*less_fn)(long long, long long)); int (*less_fn)(int64_t, int64_t));
/// It is recommended to use the double-pointer version of priority-heap free
/// as that will ensure the variable holding the pointer will end up pointing
/// to NULL after free.
void simple_archiver_priority_heap_free_single_ptr(
SDArchiverPHeap *priority_heap);
void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap); void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap);
/// If data_cleanup_fn is NULL, then "free()" is used on data when freed. /// If data_cleanup_fn is NULL, then "free()" is used on data when freed.
void simple_archiver_priority_heap_insert(SDArchiverPHeap **priority_heap, void simple_archiver_priority_heap_insert(SDArchiverPHeap *priority_heap,
long long priority, void *data, int64_t priority, void *data,
void (*data_cleanup_fn)(void *)); void (*data_cleanup_fn)(void *));
/// Returns NULL if empty or if priority_heap is NULL. /// Returns NULL if empty or if priority_heap is NULL.

View file

@ -16,10 +16,13 @@
// //
// `data_structures/test.c` is the source for testing data structure code. // `data_structures/test.c` is the source for testing data structure code.
// Standard library includes.
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// Local includes.
#include "../algorithms/linear_congruential_gen.h" #include "../algorithms/linear_congruential_gen.h"
#include "hash_map.h" #include "hash_map.h"
#include "linked_list.h" #include "linked_list.h"
@ -27,8 +30,8 @@
#define SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE 100 #define SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE 100
static int checks_checked = 0; static int32_t checks_checked = 0;
static int checks_passed = 0; static int32_t checks_passed = 0;
#define CHECK_TRUE(x) \ #define CHECK_TRUE(x) \
do { \ do { \
@ -63,7 +66,7 @@ int get_three_fn(void *data, __attribute__((unused)) void *ud) {
return strcmp(data, "three") == 0 ? 1 : 0; return strcmp(data, "three") == 0 ? 1 : 0;
} }
int more_fn(long long a, long long b) { return a > b ? 1 : 0; } int more_fn(int64_t a, int64_t b) { return a > b ? 1 : 0; }
int hash_map_iter_check_fn(__attribute__((unused)) const void *key, int hash_map_iter_check_fn(__attribute__((unused)) const void *key,
__attribute__((unused)) size_t key_size, __attribute__((unused)) size_t key_size,
@ -138,37 +141,37 @@ int main(void) {
hash_map = simple_archiver_hash_map_init(); hash_map = simple_archiver_hash_map_init();
{ {
int *value, *key; int32_t *value, *key;
for (unsigned int idx = 0; idx < 20; ++idx) { for (uint32_t idx = 0; idx < 20; ++idx) {
value = malloc(sizeof(int)); value = malloc(sizeof(int32_t));
key = malloc(sizeof(int)); key = malloc(sizeof(int32_t));
*value = idx; *value = idx;
*key = idx; *key = idx;
simple_archiver_hash_map_insert(hash_map, value, key, sizeof(int), NULL, simple_archiver_hash_map_insert(hash_map, value, key, sizeof(int32_t),
NULL); NULL, NULL);
} }
} }
int value, key; int32_t value, key;
void *value_ptr; void *value_ptr;
for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) { for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) {
value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int)); value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int32_t));
CHECK_TRUE(value_ptr != NULL); CHECK_TRUE(value_ptr != NULL);
CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int)) == 0); CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int32_t)) == 0);
} }
key = 5; key = 5;
simple_archiver_hash_map_remove(hash_map, &key, sizeof(int)); simple_archiver_hash_map_remove(hash_map, &key, sizeof(int32_t));
key = 15; key = 15;
simple_archiver_hash_map_remove(hash_map, &key, sizeof(int)); simple_archiver_hash_map_remove(hash_map, &key, sizeof(int32_t));
for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) { for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) {
value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int)); value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int32_t));
if (key != 5 && key != 15) { if (key != 5 && key != 15) {
CHECK_TRUE(value_ptr != NULL); CHECK_TRUE(value_ptr != NULL);
CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int)) == 0); CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int32_t)) == 0);
} else { } else {
CHECK_TRUE(value_ptr == NULL); CHECK_TRUE(value_ptr == NULL);
} }
@ -178,14 +181,14 @@ int main(void) {
// Rehash test for Memcheck. // Rehash test for Memcheck.
hash_map = simple_archiver_hash_map_init(); hash_map = simple_archiver_hash_map_init();
for (unsigned int idx = 0; idx < SC_SA_DS_HASH_MAP_START_BUCKET_SIZE + 1; for (uint32_t idx = 0; idx < SC_SA_DS_HASH_MAP_START_BUCKET_SIZE + 1;
++idx) { ++idx) {
unsigned int *copy_value = malloc(sizeof(unsigned int)); uint32_t *copy_value = malloc(sizeof(uint32_t));
*copy_value = idx; *copy_value = idx;
unsigned int *copy_key = malloc(sizeof(unsigned int)); uint32_t *copy_key = malloc(sizeof(uint32_t));
*copy_key = idx; *copy_key = idx;
simple_archiver_hash_map_insert(hash_map, copy_value, copy_key, simple_archiver_hash_map_insert(hash_map, copy_value, copy_key,
sizeof(unsigned int), NULL, NULL); sizeof(uint32_t), NULL, NULL);
} }
simple_archiver_hash_map_free(&hash_map); simple_archiver_hash_map_free(&hash_map);
@ -202,16 +205,14 @@ int main(void) {
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn, CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn,
found) == 0); found) == 0);
for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; for (uint32_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; ++idx) {
++idx) {
CHECK_TRUE(found[idx] == 1); CHECK_TRUE(found[idx] == 1);
} }
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn2, CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn2,
found) == 2); found) == 2);
for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; for (uint32_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; ++idx) {
++idx) {
CHECK_TRUE(found[idx] == 1); CHECK_TRUE(found[idx] == 1);
} }
@ -226,13 +227,13 @@ int main(void) {
priority_heap = simple_archiver_priority_heap_init(); priority_heap = simple_archiver_priority_heap_init();
// Just 3 elements. // Just 3 elements.
for (unsigned int idx = 0; idx < 3; ++idx) { for (uint32_t idx = 0; idx < 3; ++idx) {
unsigned int *data = malloc(sizeof(unsigned int)); uint32_t *data = malloc(sizeof(uint32_t));
*data = idx; *data = idx;
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL); simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
} }
for (unsigned int idx = 0; idx < 3; ++idx) { for (uint32_t idx = 0; idx < 3; ++idx) {
unsigned int *data = simple_archiver_priority_heap_top(priority_heap); uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
if (*data != idx) { if (*data != idx) {
printf("idx is %u, data is %u\n", idx, *data); printf("idx is %u, data is %u\n", idx, *data);
@ -246,16 +247,16 @@ int main(void) {
} }
// 100 elements. // 100 elements.
unsigned int max = 100; uint32_t max = 100;
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
unsigned int *data = malloc(sizeof(unsigned int)); uint32_t *data = malloc(sizeof(uint32_t));
*data = idx; *data = idx;
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL); simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
} }
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
unsigned int *data = simple_archiver_priority_heap_top(priority_heap); uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
data = simple_archiver_priority_heap_pop(priority_heap); data = simple_archiver_priority_heap_pop(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
@ -263,14 +264,14 @@ int main(void) {
} }
// Insert in reverse order. // Insert in reverse order.
for (unsigned int idx = max; idx-- > 0;) { for (uint32_t idx = max; idx-- > 0;) {
unsigned int *data = malloc(sizeof(unsigned int)); uint32_t *data = malloc(sizeof(uint32_t));
*data = idx; *data = idx;
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL); simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
} }
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
unsigned int *data = simple_archiver_priority_heap_top(priority_heap); uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
data = simple_archiver_priority_heap_pop(priority_heap); data = simple_archiver_priority_heap_pop(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
@ -278,30 +279,30 @@ int main(void) {
} }
// Insert in random order. // Insert in random order.
unsigned int *array = malloc(sizeof(unsigned int) * max); uint32_t *array = malloc(sizeof(uint32_t) * max);
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
array[idx] = idx; array[idx] = idx;
} }
// Deterministic randomization. // Deterministic randomization.
for (unsigned int idx = max - 1; idx-- > 0;) { for (uint32_t idx = max - 1; idx-- > 0;) {
unsigned int other_idx = simple_archiver_algo_lcg_defaults(idx) % uint32_t other_idx = simple_archiver_algo_lcg_defaults(idx) %
(unsigned long long)(idx + 1); (unsigned long long)(idx + 1);
if (max - 1 != other_idx) { if (max - 1 != other_idx) {
unsigned int temp = array[max - 1]; uint32_t temp = array[max - 1];
array[max - 1] = array[other_idx]; array[max - 1] = array[other_idx];
array[other_idx] = temp; array[other_idx] = temp;
} }
} }
// Insert the deterministically randomized array. // Insert the deterministically randomized array.
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
simple_archiver_priority_heap_insert(&priority_heap, array[idx], simple_archiver_priority_heap_insert(priority_heap, array[idx],
array + idx, no_free_fn); array + idx, no_free_fn);
} }
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
unsigned int *data = simple_archiver_priority_heap_top(priority_heap); uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
if (*data != idx) { if (*data != idx) {
printf("idx is %u, data is %u\n", idx, *data); printf("idx is %u, data is %u\n", idx, *data);
@ -318,24 +319,24 @@ int main(void) {
// Insert, don't pop, do free, for memcheck. // Insert, don't pop, do free, for memcheck.
priority_heap = simple_archiver_priority_heap_init(); priority_heap = simple_archiver_priority_heap_init();
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
unsigned int *data = malloc(sizeof(unsigned int)); uint32_t *data = malloc(sizeof(uint32_t));
*data = idx; *data = idx;
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL); simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
} }
simple_archiver_priority_heap_free(&priority_heap); simple_archiver_priority_heap_free(&priority_heap);
// Reverse priority. // Reverse priority.
priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn); priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn);
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
unsigned int *data = malloc(sizeof(unsigned int)); uint32_t *data = malloc(sizeof(uint32_t));
*data = idx; *data = idx;
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL); simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
} }
for (unsigned int idx = max; idx-- > 0;) { for (uint32_t idx = max; idx-- > 0;) {
unsigned int *data = simple_archiver_priority_heap_top(priority_heap); uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
data = simple_archiver_priority_heap_pop(priority_heap); data = simple_archiver_priority_heap_pop(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
@ -346,30 +347,30 @@ int main(void) {
// Insert in random order with reverse-priority-heap. // Insert in random order with reverse-priority-heap.
priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn); priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn);
array = malloc(sizeof(unsigned int) * max); array = malloc(sizeof(uint32_t) * max);
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
array[idx] = idx; array[idx] = idx;
} }
// Deterministic randomization. // Deterministic randomization.
for (unsigned int idx = max - 1; idx-- > 0;) { for (uint32_t idx = max - 1; idx-- > 0;) {
unsigned int other_idx = simple_archiver_algo_lcg_defaults(idx) % uint32_t other_idx = simple_archiver_algo_lcg_defaults(idx) %
(unsigned long long)(idx + 1); (unsigned long long)(idx + 1);
if (max - 1 != other_idx) { if (max - 1 != other_idx) {
unsigned int temp = array[max - 1]; uint32_t temp = array[max - 1];
array[max - 1] = array[other_idx]; array[max - 1] = array[other_idx];
array[other_idx] = temp; array[other_idx] = temp;
} }
} }
// Insert the deterministically randomized array. // Insert the deterministically randomized array.
for (unsigned int idx = 0; idx < max; ++idx) { for (uint32_t idx = 0; idx < max; ++idx) {
simple_archiver_priority_heap_insert(&priority_heap, array[idx], simple_archiver_priority_heap_insert(priority_heap, array[idx],
array + idx, no_free_fn); array + idx, no_free_fn);
} }
for (unsigned int idx = max; idx-- > 0;) { for (uint32_t idx = max; idx-- > 0;) {
unsigned int *data = simple_archiver_priority_heap_top(priority_heap); uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
CHECK_TRUE(*data == idx); CHECK_TRUE(*data == idx);
if (*data != idx) { if (*data != idx) {
printf("idx is %u, data is %u\n", idx, *data); printf("idx is %u, data is %u\n", idx, *data);

View file

@ -80,41 +80,41 @@ int simple_archiver_helper_is_big_endian(void) {
void simple_archiver_helper_16_bit_be(uint16_t *value) { void simple_archiver_helper_16_bit_be(uint16_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
unsigned char c = ((unsigned char *)value)[0]; uint8_t c = ((uint8_t *)value)[0];
((unsigned char *)value)[0] = ((unsigned char *)value)[1]; ((uint8_t *)value)[0] = ((uint8_t *)value)[1];
((unsigned char *)value)[1] = c; ((uint8_t *)value)[1] = c;
} }
} }
void simple_archiver_helper_32_bit_be(uint32_t *value) { void simple_archiver_helper_32_bit_be(uint32_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
for (unsigned int i = 0; i < 2; ++i) { for (uint32_t i = 0; i < 2; ++i) {
unsigned char c = ((unsigned char *)value)[i]; uint8_t c = ((uint8_t *)value)[i];
((unsigned char *)value)[i] = ((unsigned char *)value)[3 - i]; ((uint8_t *)value)[i] = ((uint8_t *)value)[3 - i];
((unsigned char *)value)[3 - i] = c; ((uint8_t *)value)[3 - i] = c;
} }
} }
} }
void simple_archiver_helper_64_bit_be(uint64_t *value) { void simple_archiver_helper_64_bit_be(uint64_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
for (unsigned int i = 0; i < 4; ++i) { for (uint32_t i = 0; i < 4; ++i) {
unsigned char c = ((unsigned char *)value)[i]; uint8_t c = ((uint8_t *)value)[i];
((unsigned char *)value)[i] = ((unsigned char *)value)[7 - i]; ((uint8_t *)value)[i] = ((uint8_t *)value)[7 - i];
((unsigned char *)value)[7 - i] = c; ((uint8_t *)value)[7 - i] = c;
} }
} }
} }
char **simple_archiver_helper_cmd_string_to_argv(const char *cmd) { char **simple_archiver_helper_cmd_string_to_argv(const char *cmd) {
unsigned int capacity = 16; uint32_t capacity = 16;
unsigned int idx = 0; uint32_t idx = 0;
// Size of every pointer is the same, so using size of (void*) should be ok. // Size of every pointer is the same, so using size of (void*) should be ok.
char **args = malloc(sizeof(void *) * capacity); char **args = malloc(sizeof(void *) * capacity);
memset(args, 0, sizeof(void *) * capacity); memset(args, 0, sizeof(void *) * capacity);
unsigned int word_capacity = 16; uint32_t word_capacity = 16;
unsigned int word_idx = 0; uint32_t word_idx = 0;
char *word = malloc(word_capacity); char *word = malloc(word_capacity);
memset(word, 0, word_capacity); memset(word, 0, word_capacity);
for (const char *c = cmd; *c != 0; ++c) { for (const char *c = cmd; *c != 0; ++c) {

View file

@ -19,10 +19,11 @@
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_ #ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_ #define SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
// Standard library includes.
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
static const unsigned int MAX_SYMBOLIC_LINK_SIZE = 512; static const uint32_t MAX_SYMBOLIC_LINK_SIZE = 512;
/// Returns non-zero if this system is big-endian. /// Returns non-zero if this system is big-endian.
int simple_archiver_helper_is_big_endian(void); int simple_archiver_helper_is_big_endian(void);

View file

@ -201,7 +201,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
--argc; --argc;
++argv; ++argv;
int is_remaining_args = 0; int_fast8_t is_remaining_args = 0;
while (argc > 0) { while (argc > 0) {
if (!is_remaining_args) { if (!is_remaining_args) {
@ -356,7 +356,7 @@ void simple_archiver_free_parsed(SDArchiverParsed *parsed) {
} }
if (parsed->working_files) { if (parsed->working_files) {
char **ptr = parsed->working_files; char **ptr = parsed->working_files;
unsigned int idx = 0; uint32_t idx = 0;
while (ptr[idx]) { while (ptr[idx]) {
free(ptr[idx]); free(ptr[idx]);
++idx; ++idx;
@ -585,8 +585,8 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
} }
// Remove "./" entries inside the file path. // Remove "./" entries inside the file path.
int slash_found = 0; int_fast8_t slash_found = 0;
int dot_found = 0; int_fast8_t dot_found = 0;
for (idx = strlen(file_info->filename); idx-- > 0;) { for (idx = strlen(file_info->filename); idx-- > 0;) {
if (file_info->filename[idx] == '/') { if (file_info->filename[idx] == '/') {
if (dot_found) { if (dot_found) {

View file

@ -19,6 +19,10 @@
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_ #ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_ #define SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_
// Standard library includes.
#include <stdint.h>
// Local includes.
#include "data_structures/linked_list.h" #include "data_structures/linked_list.h"
typedef struct SDArchiverParsed { typedef struct SDArchiverParsed {
@ -31,7 +35,7 @@ typedef struct SDArchiverParsed {
/// 0b xxxx 1xxx - Allow extract overwrite. /// 0b xxxx 1xxx - Allow extract overwrite.
/// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin. /// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin.
/// 0b xx1x xxxx - Do not save absolute paths for symlinks. /// 0b xx1x xxxx - Do not save absolute paths for symlinks.
unsigned int flags; uint32_t flags;
/// Null-terminated string. /// Null-terminated string.
char *filename; char *filename;
/// Null-terminated string. /// Null-terminated string.

View file

@ -16,15 +16,18 @@
// //
// `test.c` is the source for testing code. // `test.c` is the source for testing code.
// Standard library includes.
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// Local includes.
#include "helpers.h" #include "helpers.h"
#include "parser_internal.h" #include "parser_internal.h"
static int checks_checked = 0; static int32_t checks_checked = 0;
static int checks_passed = 0; static int32_t checks_passed = 0;
#define CHECK_TRUE(x) \ #define CHECK_TRUE(x) \
do { \ do { \
@ -57,7 +60,7 @@ static int checks_passed = 0;
int main(void) { int main(void) {
// Test parser. // Test parser.
{ {
unsigned int idx = size_t idx =
simple_archiver_parser_internal_get_first_non_current_idx("test"); simple_archiver_parser_internal_get_first_non_current_idx("test");
CHECK_TRUE(idx == 0); CHECK_TRUE(idx == 0);
@ -132,58 +135,58 @@ int main(void) {
// Only if system is little-endian. // Only if system is little-endian.
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
uint16_t u16 = 0x0102; uint16_t u16 = 0x0102;
CHECK_TRUE(((unsigned char *)&u16)[0] == 2); CHECK_TRUE(((uint8_t *)&u16)[0] == 2);
CHECK_TRUE(((unsigned char *)&u16)[1] == 1); CHECK_TRUE(((uint8_t *)&u16)[1] == 1);
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
CHECK_TRUE(((unsigned char *)&u16)[0] == 1); CHECK_TRUE(((uint8_t *)&u16)[0] == 1);
CHECK_TRUE(((unsigned char *)&u16)[1] == 2); CHECK_TRUE(((uint8_t *)&u16)[1] == 2);
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
CHECK_TRUE(((unsigned char *)&u16)[0] == 2); CHECK_TRUE(((uint8_t *)&u16)[0] == 2);
CHECK_TRUE(((unsigned char *)&u16)[1] == 1); CHECK_TRUE(((uint8_t *)&u16)[1] == 1);
uint32_t u32 = 0x01020304; uint32_t u32 = 0x01020304;
CHECK_TRUE(((unsigned char *)&u32)[0] == 4); CHECK_TRUE(((uint8_t *)&u32)[0] == 4);
CHECK_TRUE(((unsigned char *)&u32)[1] == 3); CHECK_TRUE(((uint8_t *)&u32)[1] == 3);
CHECK_TRUE(((unsigned char *)&u32)[2] == 2); CHECK_TRUE(((uint8_t *)&u32)[2] == 2);
CHECK_TRUE(((unsigned char *)&u32)[3] == 1); CHECK_TRUE(((uint8_t *)&u32)[3] == 1);
simple_archiver_helper_32_bit_be(&u32); simple_archiver_helper_32_bit_be(&u32);
CHECK_TRUE(((unsigned char *)&u32)[0] == 1); CHECK_TRUE(((uint8_t *)&u32)[0] == 1);
CHECK_TRUE(((unsigned char *)&u32)[1] == 2); CHECK_TRUE(((uint8_t *)&u32)[1] == 2);
CHECK_TRUE(((unsigned char *)&u32)[2] == 3); CHECK_TRUE(((uint8_t *)&u32)[2] == 3);
CHECK_TRUE(((unsigned char *)&u32)[3] == 4); CHECK_TRUE(((uint8_t *)&u32)[3] == 4);
simple_archiver_helper_32_bit_be(&u32); simple_archiver_helper_32_bit_be(&u32);
CHECK_TRUE(((unsigned char *)&u32)[0] == 4); CHECK_TRUE(((uint8_t *)&u32)[0] == 4);
CHECK_TRUE(((unsigned char *)&u32)[1] == 3); CHECK_TRUE(((uint8_t *)&u32)[1] == 3);
CHECK_TRUE(((unsigned char *)&u32)[2] == 2); CHECK_TRUE(((uint8_t *)&u32)[2] == 2);
CHECK_TRUE(((unsigned char *)&u32)[3] == 1); CHECK_TRUE(((uint8_t *)&u32)[3] == 1);
uint64_t u64 = 0x010203040a0b0c0d; uint64_t u64 = 0x010203040a0b0c0d;
CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd); CHECK_TRUE(((uint8_t *)&u64)[0] == 0xd);
CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc); CHECK_TRUE(((uint8_t *)&u64)[1] == 0xc);
CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb); CHECK_TRUE(((uint8_t *)&u64)[2] == 0xb);
CHECK_TRUE(((unsigned char *)&u64)[3] == 0xa); CHECK_TRUE(((uint8_t *)&u64)[3] == 0xa);
CHECK_TRUE(((unsigned char *)&u64)[4] == 0x4); CHECK_TRUE(((uint8_t *)&u64)[4] == 0x4);
CHECK_TRUE(((unsigned char *)&u64)[5] == 0x3); CHECK_TRUE(((uint8_t *)&u64)[5] == 0x3);
CHECK_TRUE(((unsigned char *)&u64)[6] == 0x2); CHECK_TRUE(((uint8_t *)&u64)[6] == 0x2);
CHECK_TRUE(((unsigned char *)&u64)[7] == 0x1); CHECK_TRUE(((uint8_t *)&u64)[7] == 0x1);
simple_archiver_helper_64_bit_be(&u64); simple_archiver_helper_64_bit_be(&u64);
CHECK_TRUE(((unsigned char *)&u64)[0] == 0x1); CHECK_TRUE(((uint8_t *)&u64)[0] == 0x1);
CHECK_TRUE(((unsigned char *)&u64)[1] == 0x2); CHECK_TRUE(((uint8_t *)&u64)[1] == 0x2);
CHECK_TRUE(((unsigned char *)&u64)[2] == 0x3); CHECK_TRUE(((uint8_t *)&u64)[2] == 0x3);
CHECK_TRUE(((unsigned char *)&u64)[3] == 0x4); CHECK_TRUE(((uint8_t *)&u64)[3] == 0x4);
CHECK_TRUE(((unsigned char *)&u64)[4] == 0xa); CHECK_TRUE(((uint8_t *)&u64)[4] == 0xa);
CHECK_TRUE(((unsigned char *)&u64)[5] == 0xb); CHECK_TRUE(((uint8_t *)&u64)[5] == 0xb);
CHECK_TRUE(((unsigned char *)&u64)[6] == 0xc); CHECK_TRUE(((uint8_t *)&u64)[6] == 0xc);
CHECK_TRUE(((unsigned char *)&u64)[7] == 0xd); CHECK_TRUE(((uint8_t *)&u64)[7] == 0xd);
simple_archiver_helper_64_bit_be(&u64); simple_archiver_helper_64_bit_be(&u64);
CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd); CHECK_TRUE(((uint8_t *)&u64)[0] == 0xd);
CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc); CHECK_TRUE(((uint8_t *)&u64)[1] == 0xc);
CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb); CHECK_TRUE(((uint8_t *)&u64)[2] == 0xb);
CHECK_TRUE(((unsigned char *)&u64)[3] == 0xa); CHECK_TRUE(((uint8_t *)&u64)[3] == 0xa);
CHECK_TRUE(((unsigned char *)&u64)[4] == 0x4); CHECK_TRUE(((uint8_t *)&u64)[4] == 0x4);
CHECK_TRUE(((unsigned char *)&u64)[5] == 0x3); CHECK_TRUE(((uint8_t *)&u64)[5] == 0x3);
CHECK_TRUE(((unsigned char *)&u64)[6] == 0x2); CHECK_TRUE(((uint8_t *)&u64)[6] == 0x2);
CHECK_TRUE(((unsigned char *)&u64)[7] == 0x1); CHECK_TRUE(((uint8_t *)&u64)[7] == 0x1);
} }
} }
@ -208,7 +211,7 @@ int main(void) {
// Test helpers cut substr. // Test helpers cut substr.
{ {
const char *s = "one two three."; const char *s = "one two three.";
unsigned int s_len = strlen(s); uint32_t s_len = strlen(s);
// Invalid range. // Invalid range.
char *out = simple_archiver_helper_cut_substr(s, 1, 0); char *out = simple_archiver_helper_cut_substr(s, 1, 0);
CHECK_FALSE(out); CHECK_FALSE(out);