Samuel Thibault 9 anni fa
parent
commit
7a72a87830

+ 37 - 1
src/common/utils.c

@@ -1,6 +1,6 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
- * Copyright (C) 2010, 2012-2015  Université de Bordeaux
+ * Copyright (C) 2010, 2012-2016  Université de Bordeaux
  * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015  CNRS
  *
  * StarPU is free software; you can redistribute it and/or modify
@@ -34,6 +34,10 @@
 #endif
 #endif
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 int _starpu_silent;
 
 void _starpu_util_init(void)
@@ -136,6 +140,38 @@ void _starpu_mkpath_and_check(const char *path, mode_t mode)
 	}
 }
 
+char *_starpu_mktemp(const char *directory, int flags, int *fd)
+{
+	/* create template for mkstemp */
+	const char *tmp = "STARPU_XXXXXX";
+	char *baseCpy = malloc(strlen(directory)+1+strlen(tmp)+1);
+	STARPU_ASSERT(baseCpy != NULL);
+
+	strcpy(baseCpy, directory);
+	strcat(baseCpy,"/");
+	strcat(baseCpy,tmp);
+
+#if defined(STARPU_HAVE_WINDOWS)
+	_mktemp(baseCpy);
+	*fd = open(baseCpy, flags);
+#elif defined (HAVE_MKOSTEMP)
+	*fd = mkostemp(baseCpy, flags);
+#else
+	STARPU_ASSERT(flags == (O_RDWR | O_BINARY));
+	*fd = mkstemp(baseCpy);
+#endif
+
+	/* fail */
+	if (*fd < 0)
+	{
+		_STARPU_DISP("Could not create temporary file in directory '%s', mskostemp failed with error '%s'\n", directory, strerror(errno));
+		free(baseCpy);
+		return NULL;
+	}
+
+	return baseCpy;
+}
+
 int _starpu_ftruncate(FILE *file)
 {
 	return ftruncate(fileno(file), 0);

+ 2 - 1
src/common/utils.h

@@ -1,6 +1,6 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
- * Copyright (C) 2010, 2012-2015  Université de Bordeaux
+ * Copyright (C) 2010, 2012-2016  Université de Bordeaux
  * Copyright (C) 2010, 2011, 2012, 2013, 2015  CNRS
  *
  * StarPU is free software; you can redistribute it and/or modify
@@ -124,6 +124,7 @@
 
 int _starpu_mkpath(const char *s, mode_t mode);
 void _starpu_mkpath_and_check(const char *s, mode_t mode);
+char *_starpu_mktemp(const char *directory, int flags, int *fd);
 int _starpu_ftruncate(FILE *file);
 int _starpu_frdlock(FILE *file);
 int _starpu_frdunlock(FILE *file);

+ 7 - 19
src/core/disk_ops/disk_stdio.c

@@ -36,6 +36,10 @@
 
 #define NITER	_starpu_calibration_minimum
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 /* ------------------- use STDIO to write on disk -------------------  */
 
 struct starpu_stdio_obj
@@ -52,30 +56,14 @@ static void *starpu_stdio_alloc(void *base, size_t size)
 {
 	struct starpu_stdio_obj *obj = malloc(sizeof(struct starpu_stdio_obj));
 	STARPU_ASSERT(obj != NULL);
-	int id = -1;
-
-	/* create template for mkstemp */
-	const char *tmp = "STARPU_XXXXXX";
-	char *baseCpy = malloc(strlen(base)+1+strlen(tmp)+1);
-	STARPU_ASSERT(baseCpy != NULL);
-
-	strcpy(baseCpy, (char *) base);
-	strcat(baseCpy,"/");
-	strcat(baseCpy,tmp);
 
-#ifdef STARPU_HAVE_WINDOWS
-        _mktemp(baseCpy);
-        id = open(baseCpy, O_RDWR | O_BINARY);
-#else
-	id = mkstemp(baseCpy);
-#endif
+	int id;
+	char *baseCpy = _starpu_mktemp(base, O_RDWR | O_BINARY, &id);
 
 	/* fail */
-	if (id < 0)
+	if (!baseCpy)
 	{
-		_STARPU_DISP("Could not create temporary file in directory '%s', mskostemp failed with error '%s'\n", (char*)base, strerror(errno));
 		free(obj);
-		free(baseCpy);
 		return NULL;
 	}
 

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

@@ -55,33 +55,13 @@
 /* allocation memory on disk */
 void *starpu_unistd_global_alloc(struct starpu_unistd_global_obj *obj, void *base, size_t size)
 {
-	int id = -1;
-
-	/* create template for mkstemp */
-	const char *tmp = "STARPU_XXXXXX";
-	char *baseCpy = malloc(strlen(base)+1+strlen(tmp)+1);
-	STARPU_ASSERT(baseCpy != NULL);
-
-	strcpy(baseCpy, (char *) base);
-	strcat(baseCpy,"/");
-	strcat(baseCpy,tmp);
-
-#if defined(STARPU_HAVE_WINDOWS)
-	_mktemp(baseCpy);
-	id = open(baseCpy, obj->flags);
-#elif defined (HAVE_MKOSTEMP)
-	id = mkostemp(baseCpy, obj->flags);
-#else
-	STARPU_ASSERT(obj->flags == (O_RDWR | O_BINARY));
-	id = mkstemp(baseCpy);
-#endif
+	int id;
+	char *baseCpy = _starpu_mktemp(base, obj->flags, &id);
 
 	/* fail */
-	if (id < 0)
+	if (!baseCpy)
 	{
-		_STARPU_DISP("Could not create temporary file in directory '%s', mskostemp failed with error '%s'\n", (char*)base, strerror(errno));
 		free(obj);
-		free(baseCpy);
 		return NULL;
 	}