[Bf-blender-cvs] [cbd4a79c6d8] master: Undo: refactor memfile writing

Campbell Barton noreply at git.blender.org
Sat Apr 14 13:36:07 CEST 2018


Commit: cbd4a79c6d8bf479fc0914df29f8d367b232bb1a
Author: Campbell Barton
Date:   Sat Apr 14 12:33:19 2018 +0200
Branches: master
https://developer.blender.org/rBcbd4a79c6d8bf479fc0914df29f8d367b232bb1a

Undo: refactor memfile writing

- Move static undo variable into 'WriteData',
  'memfile_chunk_add' used arguments in a confusing way,
  sometimes to set/clear static var.

- Replace checks for 'wd->current' with 'wd->use_memfile'
  move memfile vars into 'wd->mem' struct.

===================================================================

M	source/blender/blenloader/BLO_undofile.h
M	source/blender/blenloader/intern/undofile.c
M	source/blender/blenloader/intern/writefile.c

===================================================================

diff --git a/source/blender/blenloader/BLO_undofile.h b/source/blender/blenloader/BLO_undofile.h
index b713b963056..c407ea31b9b 100644
--- a/source/blender/blenloader/BLO_undofile.h
+++ b/source/blender/blenloader/BLO_undofile.h
@@ -56,7 +56,9 @@ typedef struct MemFileUndoData {
 } MemFileUndoData;
 
 /* actually only used writefile.c */
-extern void memfile_chunk_add(MemFile *compare, MemFile *current, const char *buf, unsigned int size);
+extern void memfile_chunk_add(
+        MemFile *memfile, const char *buf, unsigned int size,
+        MemFileChunk **compchunk_step);
 
 /* exports */
 extern void BLO_memfile_free(MemFile *memfile);
diff --git a/source/blender/blenloader/intern/undofile.c b/source/blender/blenloader/intern/undofile.c
index f6584ecf25f..7133dee0e82 100644
--- a/source/blender/blenloader/intern/undofile.c
+++ b/source/blender/blenloader/intern/undofile.c
@@ -96,36 +96,26 @@ void BLO_memfile_merge(MemFile *first, MemFile *second)
 	BLO_memfile_free(first);
 }
 
-void memfile_chunk_add(MemFile *compare, MemFile *current, const char *buf, unsigned int size)
+void memfile_chunk_add(
+        MemFile *memfile, const char *buf, unsigned int size,
+        MemFileChunk **compchunk_step)
 {
-	static MemFileChunk *compchunk = NULL;
-	MemFileChunk *curchunk;
-	
-	/* this function inits when compare != NULL or when current == NULL  */
-	if (compare) {
-		compchunk = compare->chunks.first;
-		return;
-	}
-	if (current == NULL) {
-		compchunk = NULL;
-		return;
-	}
-	
-	curchunk = MEM_mallocN(sizeof(MemFileChunk), "MemFileChunk");
+	MemFileChunk *curchunk = MEM_mallocN(sizeof(MemFileChunk), "MemFileChunk");
 	curchunk->size = size;
 	curchunk->buf = NULL;
 	curchunk->is_identical = false;
-	BLI_addtail(&current->chunks, curchunk);
-	
+	BLI_addtail(&memfile->chunks, curchunk);
+
 	/* we compare compchunk with buf */
-	if (compchunk) {
+	if (*compchunk_step != NULL) {
+		MemFileChunk *compchunk = *compchunk_step;
 		if (compchunk->size == curchunk->size) {
 			if (memcmp(compchunk->buf, buf, size) == 0) {
 				curchunk->buf = compchunk->buf;
 				curchunk->is_identical = true;
 			}
 		}
-		compchunk = compchunk->next;
+		*compchunk_step = compchunk->next;
 	}
 
 	/* not equal... */
@@ -133,7 +123,7 @@ void memfile_chunk_add(MemFile *compare, MemFile *current, const char *buf, unsi
 		char *buf_new = MEM_mallocN(size, "Chunk buffer");
 		memcpy(buf_new, buf, size);
 		curchunk->buf = buf_new;
-		current->size += size;
+		memfile->size += size;
 	}
 }
 
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 5afab804929..6ee97833e47 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -306,15 +306,26 @@ static void ww_handle_init(eWriteWrapType ww_type, WriteWrap *r_ww)
 typedef struct {
 	const struct SDNA *sdna;
 
-	unsigned char *buf;
-	MemFile *compare, *current;
-
+	/** Use for file and memory writing (fixed size of #MYWRITE_BUFFER_SIZE). */
+	uchar *buf;
 	int tot, count;
 	bool error;
 
-	/* Wrap writing, so we can use zlib or
+	/** #MemFile writing (used for undo). */
+	struct {
+		MemFile      *current;
+		MemFile      *compare;
+		/** Use to de-duplicate chunks when writing. */
+		MemFileChunk *compare_chunk;
+	} mem;
+	/** When true, write to #WriteData.current, could also call 'is_undo'. */
+	bool use_memfile;
+
+	/**
+	 * Wrap writing, so we can use zlib or
 	 * other compression types later, see: G_FILE_COMPRESS
-	 * Will be NULL for UNDO. */
+	 * Will be NULL for UNDO.
+	 */
 	WriteWrap *ww;
 
 #ifdef USE_BMESH_SAVE_AS_COMPAT
@@ -346,8 +357,8 @@ static void writedata_do_write(WriteData *wd, const void *mem, int memlen)
 	}
 
 	/* memory based save */
-	if (wd->current) {
-		memfile_chunk_add(NULL, wd->current, mem, memlen);
+	if (wd->use_memfile) {
+		memfile_chunk_add(wd->mem.current, mem, memlen, &wd->mem.compare_chunk);
 	}
 	else {
 		if (wd->ww->write(wd->ww, mem, memlen) != memlen) {
@@ -435,15 +446,13 @@ static WriteData *bgnwrite(WriteWrap *ww, MemFile *compare, MemFile *current)
 {
 	WriteData *wd = writedata_new(ww);
 
-	if (wd == NULL) {
-		return NULL;
+	if (current != NULL) {
+		wd->mem.current = current;
+		wd->mem.compare = compare;
+		wd->mem.compare_chunk = compare ? compare->chunks.first : NULL;
+		wd->use_memfile = true;
 	}
 
-	wd->compare = compare;
-	wd->current = current;
-	/* this inits comparing */
-	memfile_chunk_add(compare, NULL, NULL, 0);
-
 	return wd;
 }
 
@@ -801,7 +810,7 @@ static void write_fcurves(WriteData *wd, ListBase *fcurves)
 
 static void write_action(WriteData *wd, bAction *act)
 {
-	if (act->id.us > 0 || wd->current) {
+	if (act->id.us > 0 || wd->use_memfile) {
 		writestruct(wd, ID_AC, bAction, 1, act);
 		write_iddata(wd, &act->id);
 
@@ -909,7 +918,7 @@ static void write_node_socket(WriteData *wd, bNodeTree *UNUSED(ntree), bNode *no
 {
 #ifdef USE_NODE_COMPAT_CUSTOMNODES
 	/* forward compatibility code, so older blenders still open (not for undo) */
-	if (wd->current == NULL) {
+	if (wd->use_memfile == false) {
 		sock->stack_type = 1;
 
 		if (node->type == NODE_GROUP) {
@@ -1029,7 +1038,7 @@ static void write_nodetree_nolib(WriteData *wd, bNodeTree *ntree)
 				 * Not ideal (there is no ideal solution here), but should do for now. */
 				NodeGlare *ndg = node->storage;
 				/* Not in undo case. */
-				if (!wd->current) {
+				if (wd->use_memfile == false) {
 					switch (ndg->type) {
 						case 2:  /* Grrrr! magic numbers :( */
 							ndg->angle = ndg->streaks;
@@ -1286,7 +1295,7 @@ static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
 
 static void write_particlesettings(WriteData *wd, ParticleSettings *part)
 {
-	if (part->id.us > 0 || wd->current) {
+	if (part->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_PA, ParticleSettings, 1, part);
 		write_iddata(wd, &part->id);
@@ -1861,7 +1870,7 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
 
 static void write_object(WriteData *wd, Object *ob)
 {
-	if (ob->id.us > 0 || wd->current) {
+	if (ob->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_OB, Object, 1, ob);
 		write_iddata(wd, &ob->id);
@@ -1924,7 +1933,7 @@ static void write_object(WriteData *wd, Object *ob)
 
 static void write_vfont(WriteData *wd, VFont *vf)
 {
-	if (vf->id.us > 0 || wd->current) {
+	if (vf->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_VF, VFont, 1, vf);
 		write_iddata(wd, &vf->id);
@@ -1941,7 +1950,7 @@ static void write_vfont(WriteData *wd, VFont *vf)
 
 static void write_key(WriteData *wd, Key *key)
 {
-	if (key->id.us > 0 || wd->current) {
+	if (key->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_KE, Key, 1, key);
 		write_iddata(wd, &key->id);
@@ -1962,7 +1971,7 @@ static void write_key(WriteData *wd, Key *key)
 
 static void write_camera(WriteData *wd, Camera *cam)
 {
-	if (cam->id.us > 0 || wd->current) {
+	if (cam->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_CA, Camera, 1, cam);
 		write_iddata(wd, &cam->id);
@@ -1975,7 +1984,7 @@ static void write_camera(WriteData *wd, Camera *cam)
 
 static void write_mball(WriteData *wd, MetaBall *mb)
 {
-	if (mb->id.us > 0 || wd->current) {
+	if (mb->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_MB, MetaBall, 1, mb);
 		write_iddata(wd, &mb->id);
@@ -1994,7 +2003,7 @@ static void write_mball(WriteData *wd, MetaBall *mb)
 
 static void write_curve(WriteData *wd, Curve *cu)
 {
-	if (cu->id.us > 0 || wd->current) {
+	if (cu->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_CU, Curve, 1, cu);
 		write_iddata(wd, &cu->id);
@@ -2095,7 +2104,7 @@ static void write_customdata(
 	int i;
 
 	/* write external customdata (not for undo) */
-	if (data->external && !wd->current) {
+	if (data->external && (wd->use_memfile == false)) {
 		CustomData_external_write(data, id, CD_MASK_MESH, count, 0);
 	}
 
@@ -2161,7 +2170,7 @@ static void write_mesh(WriteData *wd, Mesh *mesh)
 	CustomDataLayer *llayers = NULL, llayers_buff[CD_TEMP_CHUNK_SIZE];
 	CustomDataLayer *players = NULL, players_buff[CD_TEMP_CHUNK_SIZE];
 
-	if (mesh->id.us > 0 || wd->current) {
+	if (mesh->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		if (!save_for_old_blender) {
 			/* write a copy of the mesh, don't modify in place because it is
@@ -2303,7 +2312,7 @@ static void write_mesh(WriteData *wd, Mesh *mesh)
 
 static void write_lattice(WriteData *wd, Lattice *lt)
 {
-	if (lt->id.us > 0 || wd->current) {
+	if (lt->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_LT, Lattice, 1, lt);
 		write_iddata(wd, &lt->id);
@@ -2322,7 +2331,7 @@ static void write_lattice(WriteData *wd, Lattice *lt)
 
 static void write_image(WriteData *wd, Image *ima)
 {
-	if (ima->id.us > 0 || wd->current) {
+	if (ima->id.us > 0 || wd->use_memfile) {
 		ImagePackedFile *imapf;
 
 		/* Some trickery to keep forward compatibility of packed images. */
@@ -2358,7 +2367,7 @@ static void write_image(WriteData *wd, Image *ima)
 
 static void write_texture(WriteData *wd, Tex *tex)
 {
-	if (tex->id.us > 0 || wd->current) {
+	if (tex->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_TE, Tex, 1, tex);
 		write_iddata(wd, &tex->id);
@@ -2402,7 +2411,7 @@ static void write_texture(WriteData *wd, Tex *tex)
 
 static void write_material(WriteData *wd, Material *ma)
 {
-	if (ma->id.us > 0 || wd->current) {
+	if (ma->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_MA, Material, 1, ma);
 		write_iddata(wd, &ma->id);
@@ -2436,7 +2445,7 @@ static void write_material(WriteData *wd, Material *ma)
 
 static void write_world(WriteData *wd, World *wrld)
 {
-	if (wrld->id.us > 0 || wd->current) {
+	if (wrld->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 		writestruct(wd, ID_WO, World, 1, wrld);
 		write_iddata(wd, &wrld->id);
@@ -2463,7 +2472,7 @@ static void write_world(WriteData *wd, World *wrld)
 
 static void write_lamp(WriteData *wd, Lamp *la)
 {
-	if (la->id.us > 0 || wd->current) {
+	if (la->id.us > 0 || wd->use_memfile) {
 		/* write LibData */
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list