aboutsummaryrefslogtreecommitdiff
path: root/fractal-gen.c
diff options
context:
space:
mode:
authorDavid Phillips <dbphillipsnz@gmail.com>2015-06-07 11:44:33 +1200
committerDavid Phillips <dbphillipsnz@gmail.com>2015-06-07 11:44:33 +1200
commitd27188d0aecb1bf79fda332abce124a22f722b0b (patch)
treeffd315e2e3fb4d9478ac412b69845ad07ebde50b /fractal-gen.c
parent906a9716e07393ffdbf655341035a22b95dd9df8 (diff)
downloadfractal-gen-d27188d0aecb1bf79fda332abce124a22f722b0b.tar.xz
Pulled malloc calls out of threads into main
Diffstat (limited to 'fractal-gen.c')
-rw-r--r--fractal-gen.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/fractal-gen.c b/fractal-gen.c
index 13bd6ba..bb10dfe 100644
--- a/fractal-gen.c
+++ b/fractal-gen.c
@@ -2,7 +2,7 @@
int main(int argc, char **argv)
{
- unsigned int size, iterat, cores, i, x, y, s;
+ unsigned int size, iterat, cores, i, x, y;
double power;
char* bname;
void *(*generator)(void *);
@@ -31,14 +31,13 @@ int main(int argc, char **argv)
// Fetch number of cores available on machine
+ // FIXME make the num threads selectable
cores = sysconf(_SC_NPROCESSORS_ONLN);
assert(size > 0);
assert(iterat > 0);
assert(cores > 0);
-
-
// Allocated memory for sections, bailing upon failure
data_section* sections = malloc(sizeof(data_section)*cores);
if (sections == NULL)
@@ -47,8 +46,8 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
- fprintf(stderr, "Spawning %d threads:\n", cores);
// Spawn all the threads! Something something interlacing
+ fprintf(stderr, "Spawning %d threads:\n", cores);
for (i = 0; i < cores; i++)
{
// Has to be a better way
@@ -57,7 +56,17 @@ int main(int argc, char **argv)
sections[i].size = size;
sections[i].power = power;
sections[i].iterat = iterat;
-
+ sections[i].data = malloc((size*size)/cores);
+ if (sections[i].data == NULL)
+ {
+ // Free already allocated chunks of memory
+ while(i-- + 1)
+ free(sections[i].data);
+
+ free(sections);
+ perror("malloc");
+ return EXIT_FAILURE;
+ }
fprintf(stderr, " -> Thread #%d\n", i);
pthread_create(&sections[i].thread, NULL, generator, &(sections[i]));
}
@@ -73,8 +82,8 @@ int main(int argc, char **argv)
putchar(sections[y%cores].data[(y/cores)*size + x]);
// Free the memory we allocated for point data
- for (s = 0; s < cores; s++)
- free(sections[s].data);
+ for (i = 0; i < cores; i++)
+ free(sections[i].data);
free(sections);
fprintf(stderr,"\n");