Small improvements and optimizations.
All error catching is now in `main.c' so we can neatly release memory as well as adding the `restrict' optimizer to the `addToList()' function.
This commit is contained in:
parent
8b35c5aea3
commit
09166efe89
@ -12,7 +12,9 @@ int inputPrimes(char *file, List *list) {
|
|||||||
if(pFile == NULL) return 1;
|
if(pFile == NULL) return 1;
|
||||||
mpz_t n;
|
mpz_t n;
|
||||||
mpz_init(n);
|
mpz_init(n);
|
||||||
while(mpz_inp_raw(n, pFile) != 0) addToList(list, n);
|
while(mpz_inp_raw(n, pFile) != 0) {
|
||||||
|
if(addToList(list, n) == 1) return 3;
|
||||||
|
}
|
||||||
if(fclose(pFile) != 0) return 2;
|
if(fclose(pFile) != 0) return 2;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,8 @@
|
|||||||
* @param file File to input primes from.
|
* @param file File to input primes from.
|
||||||
* @param list List to load primes into.
|
* @param list List to load primes into.
|
||||||
* @returns If 0 then load was successful, if 1 then failed to open,
|
* @returns If 0 then load was successful, if 1 then failed to open,
|
||||||
* if 2 failed to close.
|
* if 2 failed to close, if 3 failed to allocate new memory to List
|
||||||
|
* (see `addToList()')
|
||||||
*/
|
*/
|
||||||
int inputPrimes(char *file, List *list);
|
int inputPrimes(char *file, List *list);
|
||||||
|
|
||||||
|
16
src/list.c
16
src/list.c
@ -7,14 +7,12 @@
|
|||||||
*/
|
*/
|
||||||
#define BLOCK_SIZE 1024
|
#define BLOCK_SIZE 1024
|
||||||
|
|
||||||
void initList(List *restrict l) {
|
int initList(List *restrict l) {
|
||||||
l->list = malloc(sizeof(mpz_t) * BLOCK_SIZE);
|
l->list = malloc(sizeof(mpz_t) * BLOCK_SIZE);
|
||||||
if(!l->list) {
|
if(!l->list) return 1;
|
||||||
fprintf(stderr, "Failed to allocate memory to list!\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
l->size = BLOCK_SIZE;
|
l->size = BLOCK_SIZE;
|
||||||
l->end = 0;
|
l->end = 0;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deInitList(List *restrict l) {
|
void deInitList(List *restrict l) {
|
||||||
@ -24,16 +22,14 @@ void deInitList(List *restrict l) {
|
|||||||
free(l->list);
|
free(l->list);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addToList(List *l, mpz_t n) {
|
int addToList(List *restrict l, mpz_t n) {
|
||||||
if(l->end == l->size) {
|
if(l->end == l->size) {
|
||||||
l->size += BLOCK_SIZE;
|
l->size += BLOCK_SIZE;
|
||||||
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
||||||
if(!tmp) {
|
if(!tmp) return 1;
|
||||||
fprintf(stderr, "Failed to allocate more memory to list!\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
l->list = tmp;
|
l->list = tmp;
|
||||||
}
|
}
|
||||||
mpz_init(l->list[l->end]);
|
mpz_init(l->list[l->end]);
|
||||||
mpz_set(l->list[l->end++], n);
|
mpz_set(l->list[l->end++], n);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,9 @@ typedef struct {
|
|||||||
* @details Initialize the list and its variables, allocating memory
|
* @details Initialize the list and its variables, allocating memory
|
||||||
* to the pointer array inside.
|
* to the pointer array inside.
|
||||||
* @param[in] l A pointer to a List type to be initialized.
|
* @param[in] l A pointer to a List type to be initialized.
|
||||||
|
* @returns Returns 0 if successful and 1 if failed.
|
||||||
*/
|
*/
|
||||||
void initList(List *restrict l);
|
int initList(List *restrict l);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Deinitialize a List.
|
* @brief Deinitialize a List.
|
||||||
@ -38,5 +39,6 @@ void deInitList(List *restrict l);
|
|||||||
* @details Add item `n' at the end of a List type.
|
* @details Add item `n' at the end of a List type.
|
||||||
* @param[out] l List to which the variable should be appended.
|
* @param[out] l List to which the variable should be appended.
|
||||||
* @param[in] n variable to be appended to the list.
|
* @param[in] n variable to be appended to the list.
|
||||||
|
* @returns Returns 0 if successful and 1 if failed.
|
||||||
*/
|
*/
|
||||||
void addToList(List *l, mpz_t n);
|
int addToList(List *restrict l, mpz_t n);
|
||||||
|
29
src/main.c
29
src/main.c
@ -45,7 +45,7 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Invalid base `%d'. Base must be between 2 and 62.\n",
|
"Invalid base `%d'. Base must be between 2 and 62.\n",
|
||||||
base);
|
base);
|
||||||
exit(1);
|
return 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
@ -93,7 +93,10 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
// Primes we've found
|
// Primes we've found
|
||||||
List primes;
|
List primes;
|
||||||
initList(&primes);
|
if(initList(&primes) == 1) {
|
||||||
|
fprintf(stderr, "Failed to initialize primes list.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// The number we're going to be testing for
|
// The number we're going to be testing for
|
||||||
mpz_t num;
|
mpz_t num;
|
||||||
@ -108,13 +111,13 @@ int main(int argc, char *argv[]) {
|
|||||||
if(newFile) {
|
if(newFile) {
|
||||||
// Add 2, a known prime to this list
|
// Add 2, a known prime to this list
|
||||||
mpz_set_ui(num, 2);
|
mpz_set_ui(num, 2);
|
||||||
addToList(&primes, num);
|
if(addToList(&primes, num) == 1) {
|
||||||
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
if(!f_quiet) {
|
if(!f_quiet) {
|
||||||
if(mpz_out_str(stdout, base, num) == 0) {
|
if(mpz_out_str(stdout, base, num) == 0)
|
||||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||||
exitCode = 1;
|
|
||||||
goto releaseMemory;
|
|
||||||
}
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
mpz_add_ui(num, num, 1);
|
mpz_add_ui(num, num, 1);
|
||||||
@ -129,6 +132,8 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr, "Failed to open Indivisible file `%s'.\n", file);
|
fprintf(stderr, "Failed to open Indivisible file `%s'.\n", file);
|
||||||
else if(err == 2)
|
else if(err == 2)
|
||||||
fprintf(stderr, "Failed to close Indivisible file `%s'.\n", file);
|
fprintf(stderr, "Failed to close Indivisible file `%s'.\n", file);
|
||||||
|
else if(err == 3)
|
||||||
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
||||||
exitCode = 1;
|
exitCode = 1;
|
||||||
goto releaseMemory;
|
goto releaseMemory;
|
||||||
}
|
}
|
||||||
@ -170,13 +175,13 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// `num' is a prime so we add it to the list and print it
|
// `num' is a prime so we add it to the list and print it
|
||||||
addToList(&primes, num);
|
if(addToList(&primes, num) == 1) {
|
||||||
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
if(!f_quiet) {
|
if(!f_quiet) {
|
||||||
if(mpz_out_str(stdout, base, num) == 0) {
|
if(mpz_out_str(stdout, base, num) == 0)
|
||||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||||
exitCode = 1;
|
|
||||||
goto releaseMemory;
|
|
||||||
}
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user