Przeglądaj źródła

Factorize disk init/close/fini operations

Samuel Thibault 9 lat temu
rodzic
commit
59a170b9a4

+ 52 - 57
src/core/disk_ops/disk_stdio.c

@@ -53,33 +53,53 @@ struct starpu_stdio_obj
 	starpu_pthread_mutex_t mutex;
 };
 
-/* allocation memory on disk */
-static void *starpu_stdio_alloc(void *base, size_t size)
+static struct starpu_stdio_obj *_starpu_stdio_init(int descriptor, char *path, size_t size)
 {
 	struct starpu_stdio_obj *obj = malloc(sizeof(struct starpu_stdio_obj));
 	STARPU_ASSERT(obj != NULL);
 
-	int id;
-	char *baseCpy = _starpu_mktemp_many(base, TEMP_HIERARCHY_DEPTH, O_RDWR | O_BINARY, &id);
-
-	/* fail */
-	if (!baseCpy)
+	FILE *f = fdopen(descriptor,"rb+");
+	if (f == NULL)
 	{
 		free(obj);
 		return NULL;
 	}
 
-	FILE *f = fdopen(id, "rb+");
+	STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
+
+	obj->descriptor = descriptor;
+	obj->file = f;
+	obj->path = path;
+	obj->size = size;
+
+	return (void *) obj;
+}
+
+static void _starpu_stdio_close(struct starpu_stdio_obj *obj)
+{
+	fclose(obj->file);
+	close(obj->descriptor);
+}
+
+static void _starpu_stdio_fini(struct starpu_stdio_obj *obj)
+{
+	STARPU_PTHREAD_MUTEX_DESTROY(&obj->mutex);
+
+	free(obj->path);
+	free(obj);
+}
+
+/* allocation memory on disk */
+static void *starpu_stdio_alloc(void *base, size_t size)
+{
+	struct starpu_stdio_obj *obj;
+
+	int id;
+	char *baseCpy = _starpu_mktemp_many(base, TEMP_HIERARCHY_DEPTH, O_RDWR | O_BINARY, &id);
+
 	/* fail */
-	if (f == NULL)
-	{
-		/* delete fic */
-		close(id);
-		unlink(baseCpy);
-		free(baseCpy);
-		free(obj);
+	if (!baseCpy)
 		return NULL;
-	}
 
 #ifdef STARPU_HAVE_WINDOWS
 	int val = _chsize(id, size);
@@ -94,22 +114,21 @@ static void *starpu_stdio_alloc(void *base, size_t size)
 #else
 		_STARPU_DISP("Could not truncate file, ftruncate failed with error '%s'\n", strerror(errno));
 #endif
-		fclose(f);
 		close(id);
 		unlink(baseCpy);
 		free(baseCpy);
-		free(obj);
 		return NULL;
 	}
 
-	STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
-
-	obj->descriptor = id;
-	obj->file = f;
-	obj->path = baseCpy;
-	obj->size = size;
+	obj = _starpu_stdio_init(id, baseCpy, size);
+	if (!obj)
+	{
+		close(id);
+		unlink(baseCpy);
+		free(baseCpy);
+	}
 
-	return (void *) obj;
+	return obj;
 }
 
 /* free memory on disk */
@@ -117,23 +136,16 @@ static void starpu_stdio_free(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, siz
 {
 	struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
 
-	STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
-
-	fclose(tmp->file);
-	close(tmp->descriptor);
+	_starpu_stdio_close(tmp);
 	unlink(tmp->path);
 	_starpu_rmtemp_many(tmp->path, TEMP_HIERARCHY_DEPTH);
-
-	free(tmp->path);
-	free(tmp);
+	_starpu_stdio_fini(tmp);
 }
 
 /* open an existing memory on disk */
 static void *starpu_stdio_open(void *base, void *pos, size_t size)
 {
-	struct starpu_stdio_obj *obj = malloc(sizeof(struct starpu_stdio_obj));
-	STARPU_ASSERT(obj != NULL);
-
+	struct starpu_stdio_obj *obj;
 	/* create template */
 	char *baseCpy = malloc(strlen(base)+1+strlen(pos)+1);
 	STARPU_ASSERT(baseCpy != NULL);
@@ -144,27 +156,14 @@ static void *starpu_stdio_open(void *base, void *pos, size_t size)
 	int id = open(baseCpy, O_RDWR);
 	if (id < 0)
 	{
-		free(obj);
 		free(baseCpy);
 		return NULL;
 	}
 
-	FILE *f = fdopen(id,"rb+");
-	if (f == NULL)
-	{
-		free(obj);
+	obj = _starpu_stdio_init(id, baseCpy, size);
+	if (!obj)
 		free(baseCpy);
-		return NULL;
-	}
-
-	STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
-
-	obj->descriptor = id;
-	obj->file = f;
-	obj->path = baseCpy;
-	obj->size = size;
-
-	return (void *) obj;
+	return obj;
 }
 
 /* free memory without delete it */
@@ -172,12 +171,8 @@ static void starpu_stdio_close(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, si
 {
 	struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
 
-	STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
-
-	fclose(tmp->file);
-	close(tmp->descriptor);
-	free(tmp->path);
-	free(tmp);
+	_starpu_stdio_close(tmp);
+	_starpu_stdio_fini(tmp);
 }
 
 /* read the memory disk */

+ 30 - 23
src/core/disk_ops/unistd/disk_unistd_global.c

@@ -54,6 +54,28 @@
 
 /* ------------------- use UNISTD to write on disk -------------------  */
 
+static void _starpu_unistd_init(struct starpu_unistd_global_obj *obj, int descriptor, char *path, size_t size)
+{
+	STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
+
+	obj->descriptor = descriptor;
+	obj->path = path;
+	obj->size = size;
+}
+
+static void _starpu_unistd_close(struct starpu_unistd_global_obj *obj)
+{
+	close(obj->descriptor);
+}
+
+static void _starpu_unistd_fini(struct starpu_unistd_global_obj *obj)
+{
+	STARPU_PTHREAD_MUTEX_DESTROY(&obj->mutex);
+
+	free(obj->path);
+	free(obj);
+}
+
 /* allocation memory on disk */
 void *starpu_unistd_global_alloc(struct starpu_unistd_global_obj *obj, void *base, size_t size)
 {
@@ -87,13 +109,9 @@ void *starpu_unistd_global_alloc(struct starpu_unistd_global_obj *obj, void *bas
 		return NULL;
 	}
 
-	STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
-
-	obj->descriptor = id;
-	obj->path = baseCpy;
-	obj->size = size;
+	_starpu_unistd_init(obj, id, baseCpy, size);
 
-	return (void *) obj;
+	return obj;
 }
 
 /* free memory on disk */
@@ -101,14 +119,10 @@ void starpu_unistd_global_free(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, si
 {
 	struct starpu_unistd_global_obj *tmp = (struct starpu_unistd_global_obj *) obj;
 
-	STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
-
-	close(tmp->descriptor);
+	_starpu_unistd_close(tmp);
 	unlink(tmp->path);
 	_starpu_rmtemp_many(tmp->path, TEMP_HIERARCHY_DEPTH);
-
-	free(tmp->path);
-	free(tmp);
+	_starpu_unistd_fini(tmp);
 }
 
 /* open an existing memory on disk */
@@ -129,13 +143,9 @@ void *starpu_unistd_global_open(struct starpu_unistd_global_obj *obj, void *base
 		return NULL;
 	}
 
-	STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
+	_starpu_unistd_init(obj, id, baseCpy, size);
 
-	obj->descriptor = id;
-	obj->path = baseCpy;
-	obj->size = size;
-
-	return (void *) obj;
+	return obj;
 }
 
 /* free memory without delete it */
@@ -143,11 +153,8 @@ void starpu_unistd_global_close(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, s
 {
 	struct starpu_unistd_global_obj *tmp = (struct starpu_unistd_global_obj *) obj;
 
-	STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
-
-	close(tmp->descriptor);
-	free(tmp->path);
-	free(tmp);
+	_starpu_unistd_close(tmp);
+	_starpu_unistd_fini(tmp);
 }
 
 /* read the memory disk */