From 1c313238d0bf6faba73a9281559ef98233bfafdf Mon Sep 17 00:00:00 2001 From: Cubitect Date: Wed, 4 Jul 2018 10:53:35 +0100 Subject: [PATCH] improved thread-safety of hills --- layers.c | 21 ++++++++++----------- layers.h | 1 - 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/layers.c b/layers.c index 7d37557..aa57756 100644 --- a/layers.c +++ b/layers.c @@ -861,7 +861,6 @@ void mapBiomeEdge(Layer *l, int * __restrict out, int areaX, int areaZ, int area } -/* This function is not threadsafe. */ void mapHills(Layer *l, int * __restrict out, int areaX, int areaZ, int areaWidth, int areaHeight) { int pX = areaX - 1; @@ -878,13 +877,7 @@ void mapHills(Layer *l, int * __restrict out, int areaX, int areaZ, int areaWidt exit(1); } - if(bufsize < pWidth*pHeight) - { - if(buf != NULL) free(buf); - - bufsize = pWidth*pHeight; - buf = (int*)malloc(bufsize*sizeof(int)); - } + buf = (int *) malloc(pWidth*pHeight*sizeof(int)); l->p->getMap(l->p, out, pX, pZ, pWidth, pHeight); memcpy(buf, out, pWidth*pHeight*sizeof(int)); @@ -982,6 +975,8 @@ void mapHills(Layer *l, int * __restrict out, int areaX, int areaZ, int areaWidt } } } + + free(buf); } @@ -1202,7 +1197,8 @@ void mapShore(Layer *l, int * __restrict out, int areaX, int areaZ, int areaWidt void mapRiverMix(Layer *l, int * __restrict out, int areaX, int areaZ, int areaWidth, int areaHeight) { int idx; - int *buf = (int*)malloc(areaWidth*areaHeight*sizeof(int)); + int len; + int *buf; if(l->p2 == NULL) { @@ -1210,12 +1206,15 @@ void mapRiverMix(Layer *l, int * __restrict out, int areaX, int areaZ, int areaW exit(1); } + len = areaWidth*areaHeight; + buf = (int *) malloc(len*sizeof(int)); + l->p->getMap(l->p, out, areaX, areaZ, areaWidth, areaHeight); // biome chain - memcpy(buf, out, areaWidth*areaHeight*sizeof(int)); + memcpy(buf, out, len*sizeof(int)); l->p2->getMap(l->p2, out, areaX, areaZ, areaWidth, areaHeight); // rivers - for(idx = 0; idx < areaHeight*areaWidth; idx++) + for(idx = 0; idx < len; idx++) { if(isOceanic(buf[idx])) { diff --git a/layers.h b/layers.h index f707efc..9dfefcf 100644 --- a/layers.h +++ b/layers.h @@ -376,7 +376,6 @@ void mapDeepOcean(Layer *l, int * __restrict out, int x, int z, int w, int h); void mapBiome(Layer *l, int * __restrict out, int x, int z, int w, int h); void mapRiverInit(Layer *l, int * __restrict out, int x, int z, int w, int h); void mapBiomeEdge(Layer *l, int * __restrict out, int x, int z, int w, int h); -/* This function is not threadsafe. */ void mapHills(Layer *l, int * __restrict out, int x, int z, int w, int h); void mapRiver(Layer *l, int * __restrict out, int x, int z, int w, int h); void mapSmooth(Layer *l, int * __restrict out, int x, int z, int w, int h);