Page MenuHome

dir_contents.patch

dir_contents.patch

commit e247a7486890b3c76cd036e561fe97562f22edf8
Author: Lawrence D'Oliveiro <ldo@geek-central.gen.nz>
Date: Sat Feb 16 07:42:21 2013 +0000
storage.c: Simplify BLI_dir_contents and make it and its internal subsidiary routines reentrant
Moved common code for disposal of a struct direntry to new routine BLI_free_filelist in storage.c, and put calls to it in interface_icons.c and filelist.c
Took out inclusion of BLI_fileops_types.h from BLI_fileops.h and put it explicitly into .c files that need it (which turned out to be only 7 of the 35 files that were including the former)
diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index b65be65..be4dbeb 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -34,14 +34,12 @@
#define __BLI_FILEOPS_H__
#include <stdio.h>
-
+#include <sys/stat.h>
#ifdef __cplusplus
extern "C" {
#endif
-#include "BLI_fileops_types.h"
-
/* for size_t (needed on windows) */
#include <stddef.h>
@@ -68,6 +66,7 @@ double BLI_dir_free_space(const char *dir);
char *BLI_current_working_dir(char *dir, const size_t maxlen);
unsigned int BLI_dir_contents(const char *dir, struct direntry **filelist);
+void BLI_free_filelist(struct direntry * filelist, unsigned int nrentries);
/* Files */
diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c
index f87d8c7..0f1a1e4 100644
--- a/source/blender/blenlib/intern/storage.c
+++ b/source/blender/blenlib/intern/storage.c
@@ -91,9 +91,7 @@
#include "BLI_fileops_types.h"
#include "BLI_path_util.h"
-/* vars: */
-static int totnum, actnum;
-static struct direntry *files; /* array[totnum] */
+#include "../imbuf/IMB_imbuf.h"
/* can return NULL when the size is not big enough */
char *BLI_current_working_dir(char *dir, const size_t maxncpy)
@@ -204,7 +202,13 @@ double BLI_dir_free_space(const char *dir)
#endif
}
-static void bli_builddir(const char *dirname, const char *relname)
+struct builddir_context
+ {
+ int nrfiles;
+ struct direntry *files; /* array[nrfiles] */
+ };
+
+static void bli_builddir(const char *dirname, const char *relname, struct builddir_context * context)
/* scans the directory named *dirname and appends entries for its contents to files.
Recorded pathnames will be prefixed by *relname if specified (FIXME: actually this
option is not used anywhere, might as well get rid of it). */
@@ -253,48 +257,45 @@ static void bli_builddir(const char *dirname, const char *relname)
if (newnum) {
- if (files) {
- void * const tmp = realloc(files, (totnum + newnum) * sizeof(struct direntry));
+ if (context->files) {
+ void * const tmp = realloc(context->files, (context->nrfiles + newnum) * sizeof(struct direntry));
if (tmp) {
- files = (struct direntry *)tmp;
+ context->files = (struct direntry *)tmp;
}
else { /* realloc fail */
- free(files);
- files = NULL;
+ free(context->files);
+ context->files = NULL;
}
}
- if (files == NULL)
- files = (struct direntry *)malloc(newnum * sizeof(struct direntry));
+ if (context->files == NULL)
+ context->files = (struct direntry *)malloc(newnum * sizeof(struct direntry));
- if (files) {
+ if (context->files) {
struct dirlink * dlink = (struct dirlink *) dirbase.first;
while (dlink) {
- memset(&files[actnum], 0, sizeof(struct direntry));
- files[actnum].relname = dlink->name;
- files[actnum].path = BLI_strdupcat(dirname, dlink->name);
+ memset(&context->files[context->nrfiles], 0, sizeof(struct direntry));
+ context->files[context->nrfiles].relname = dlink->name;
+ context->files[context->nrfiles].path = BLI_strdupcat(dirname, dlink->name);
// use 64 bit file size, only needed for WIN32 and WIN64.
// Excluding other than current MSVC compiler until able to test
#ifdef WIN32
{
wchar_t *name_16 = alloc_utf16_from_8(dlink->name, 0);
#if (defined(WIN32) || defined(WIN64)) && (_MSC_VER >= 1500)
- _wstat64(name_16, &files[actnum].s);
+ _wstat64(name_16, &context->files[context->nrfiles].s);
#elif defined(__MINGW32__)
- _stati64(dlink->name, &files[actnum].s);
+ _stati64(dlink->name, &context->files[context->nrfiles].s);
#endif
free(name_16);
}
#else
- stat(dlink->name, &files[actnum].s);
+ stat(dlink->name, &context->files[context->nrfiles].s);
#endif
- files[actnum].type = files[actnum].s.st_mode;
- files[actnum].flags = 0;
- /* FIXME: this is the only place where totnum and actnum are incremented,
- so they will always be equal, might as well get rid of one */
- totnum++;
- actnum++;
+ context->files[context->nrfiles].type = context->files[context->nrfiles].s.st_mode;
+ context->files[context->nrfiles].flags = 0;
+ context->nrfiles++;
dlink = dlink->next;
}
}
@@ -304,7 +305,7 @@ static void bli_builddir(const char *dirname, const char *relname)
}
BLI_freelist(&dirbase);
- if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare);
+ if (context->files) qsort(context->files, context->nrfiles, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare);
}
else {
printf("%s empty directory\n", dirname);
@@ -317,7 +318,7 @@ static void bli_builddir(const char *dirname, const char *relname)
}
}
-static void bli_adddirstrings(void)
+static void bli_adddirstrings(struct builddir_context * context)
/* fills in the "mode[123]", "size" and "string" fields in the elements of the files
array with descriptive details about each item. "string" will have a format similar to "ls -l". */
{
@@ -337,7 +338,7 @@ array with descriptive details about each item. "string" will have a format simi
struct tm *tm;
time_t zero = 0;
- for (num = 0, file = files; num < actnum; num++, file++) {
+ for (num = 0, file = context->files; num < context->nrfiles; num++, file++) {
#ifdef WIN32
mode = 0;
BLI_strncpy(file->mode1, types[0], sizeof(file->mode1));
@@ -442,28 +443,51 @@ unsigned int BLI_dir_contents(const char *dirname, struct direntry **filelist)
/* scans the contents of the directory named *dirname, and allocates and fills in an
array of entries describing them in *filelist. The length of the array is the function result. */
{
- /* reset global variables
- * memory stored in files is free()'d in
- * filesel.c:freefilelist() */
+ struct builddir_context context;
- actnum = totnum = 0;
- files = NULL;
+ context.nrfiles = 0;
+ context.files = NULL;
- bli_builddir(dirname, "");
- bli_adddirstrings();
+ bli_builddir(dirname, "", &context);
+ bli_adddirstrings(&context);
- if (files) {
- *(filelist) = files;
+ if (context.files) {
+ *filelist = context.files;
}
else {
// keep blender happy. Blender stores this in a variable
// where 0 has special meaning.....
- *(filelist) = files = malloc(sizeof(struct direntry));
+ *filelist = malloc(sizeof(struct direntry));
}
- return(actnum);
+ return context.nrfiles;
}
+void BLI_free_filelist
+ (
+ struct direntry * filelist, /* array[nrentries] */
+ unsigned int nrentries
+ )
+ /* frees storage for an array of direntries, including the array itself. */
+ {
+ unsigned int i;
+ for (i = 0; i < nrentries; ++i)
+ {
+ struct direntry * const entry = filelist + i;
+ if (entry->image) {
+ IMB_freeImBuf(entry->image);
+ }
+ if (entry->relname)
+ MEM_freeN(entry->relname);
+ if (entry->path)
+ MEM_freeN(entry->path);
+ if (entry->string)
+ MEM_freeN(entry->string);
+ /* entry->poin assumed not to point to anything needing freeing here */
+ } /*for*/
+ free(filelist);
+ } /*BLI_free_filelist*/
+
size_t BLI_file_descriptor_size(int file)
/* returns the file size of an opened file descriptor. */
diff --git a/source/blender/editors/interface/interface_icons.c b/source/blender/editors/interface/interface_icons.c
index 086e9da..d978b25 100644
--- a/source/blender/editors/interface/interface_icons.c
+++ b/source/blender/editors/interface/interface_icons.c
@@ -47,6 +47,7 @@
#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
+#include "BLI_fileops_types.h"
#include "DNA_brush_types.h"
#include "DNA_dynamicpaint_types.h"
@@ -766,18 +767,8 @@ static void init_iconfile_list(struct ListBase *list)
}
}
}
-
- /* free temporary direntry structure that's been created by BLI_dir_contents() */
- i = totfile - 1;
-
- for (; i >= 0; i--) {
- MEM_freeN(dir[i].relname);
- MEM_freeN(dir[i].path);
- if (dir[i].string) {
- MEM_freeN(dir[i].string);
- }
- }
- free(dir);
+
+ BLI_free_filelist(dir, totfile);
dir = NULL;
}
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index afe32ec..45acdc7 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -35,6 +35,7 @@
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "BLI_dynstr.h"
+#include "BLI_fileops_types.h"
#ifdef WIN32
# include "BLI_winstuff.h"
diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index 9349abb..4a70d40 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -30,6 +30,7 @@
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
+#include "BLI_fileops_types.h"
#include "BKE_context.h"
#include "BKE_screen.h"
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index 5f2e20e..5be780f 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -48,6 +48,7 @@
#include "BLI_linklist.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
+#include "BLI_fileops_types.h"
#ifdef WIN32
# include "BLI_winstuff.h"
@@ -545,8 +546,6 @@ FileList *filelist_new(short type)
void filelist_free(struct FileList *filelist)
{
- int i;
-
if (!filelist) {
printf("Attempting to delete empty filelist.\n");
return;
@@ -557,23 +556,8 @@ void filelist_free(struct FileList *filelist)
filelist->fidx = NULL;
}
- for (i = 0; i < filelist->numfiles; ++i) {
- if (filelist->filelist[i].image) {
- IMB_freeImBuf(filelist->filelist[i].image);
- }
- filelist->filelist[i].image = NULL;
- if (filelist->filelist[i].relname)
- MEM_freeN(filelist->filelist[i].relname);
- if (filelist->filelist[i].path)
- MEM_freeN(filelist->filelist[i].path);
- filelist->filelist[i].relname = NULL;
- if (filelist->filelist[i].string)
- MEM_freeN(filelist->filelist[i].string);
- filelist->filelist[i].string = NULL;
- }
-
+ BLI_free_filelist(filelist->filelist, filelist->numfiles);
filelist->numfiles = 0;
- free(filelist->filelist);
filelist->filelist = NULL;
filelist->filter = 0;
filelist->filter_glob[0] = '\0';
diff --git a/source/blender/editors/space_file/filesel.c b/source/blender/editors/space_file/filesel.c
index 55d0f49..a1929f9 100644
--- a/source/blender/editors/space_file/filesel.c
+++ b/source/blender/editors/space_file/filesel.c
@@ -58,6 +58,7 @@
#include "BLI_linklist.h"
#include "BLI_dynstr.h"
#include "BLI_utildefines.h"
+#include "BLI_fileops_types.h"
#include "BKE_context.h"
#include "BKE_global.h"
diff --git a/source/blender/editors/space_file/space_file.c b/source/blender/editors/space_file/space_file.c
index 3d63699..6932cf8 100644
--- a/source/blender/editors/space_file/space_file.c
+++ b/source/blender/editors/space_file/space_file.c
@@ -45,6 +45,7 @@
#include "BLI_math.h"
#include "BLI_rand.h"
#include "BLI_utildefines.h"
+#include "BLI_fileops_types.h"
#include "BKE_context.h"
#include "BKE_screen.h"

File Metadata

Mime Type
text/x-diff
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
eb/e9/c50ea605958e1f10a0aa2e61cd65

Event Timeline