Blender V5.3
undofile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <fcntl.h>
13
14/* open/close */
15#ifndef _WIN32
16# include <unistd.h>
17#else
18# include <io.h>
19#endif
20
21#include "MEM_guardedalloc.h"
22
23#include "DNA_listBase.h"
24
26#include "BLI_listbase.hh"
27
28#include "BLO_readfile.hh"
29#include "BLO_undofile.hh"
30
31#include "BKE_lib_id.hh"
32#include "BKE_main.hh"
33#include "BKE_undo_system.hh"
34
35#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
36
37#include "writefile.hh"
38
39namespace blender {
40
41/* **************** support for memory-write, for undo buffers *************** */
42
44{
45 while (MemFileChunk *chunk = static_cast<MemFileChunk *>(BLI_pophead(&memfile->chunks))) {
46 if (chunk->is_identical == false) {
47 MEM_delete(chunk->buf);
48 }
49 MEM_delete(chunk);
50 }
51 MEM_SAFE_DELETE(memfile->shared_storage);
52 memfile->size = 0;
53}
54
56{
58 /* Removing the user makes sure shared data is freed when the undo step was its last owner. */
59 data.sharing_info->remove_user_and_delete_if_last();
60 }
61}
62
63void BLO_memfile_merge(MemFile *first, MemFile *second)
64{
65 /* We use this mapping to store the memory buffers from second memfile chunks which are not owned
66 * by it (i.e. shared with some previous memory steps). */
67 Map<const char *, MemFileChunk *> buffer_to_second_memchunk;
68
69 /* First, detect all memchunks in second memfile that are not owned by it. */
70 for (MemFileChunk &sc : second->chunks) {
71 if (sc.is_identical) {
72 buffer_to_second_memchunk.add(sc.buf, &sc);
73 }
74 }
75
76 /* Now, check all chunks from first memfile (the one we are removing), and if a memchunk owned by
77 * it is also used by the second memfile, transfer the ownership. */
78 for (MemFileChunk &fc : first->chunks) {
79 if (!fc.is_identical) {
80 if (MemFileChunk *sc = buffer_to_second_memchunk.lookup_default(fc.buf, nullptr)) {
82 sc->is_identical = false;
83 fc.is_identical = true;
84 }
85 /* Note that if the second memfile does not use that chunk, we assume that the first one
86 * fully owns it without sharing it with any other memfile, and hence it should be freed with
87 * it. */
88 }
89 }
90
91 BLO_memfile_free(first);
92}
93
95{
96 for (MemFileChunk &chunk : memfile->chunks) {
97 chunk.is_identical_future = false;
98 }
99}
100
102 MemFileWriteData *mem_data,
103 MemFile *written_memfile,
104 MemFile *reference_memfile)
105{
106 wd->use_memfile = true;
107
108 mem_data->written_memfile = written_memfile;
109 mem_data->reference_memfile = reference_memfile;
110 mem_data->reference_current_chunk = reference_memfile ? static_cast<MemFileChunk *>(
111 reference_memfile->chunks.first_) :
112 nullptr;
113
114 /* If we have a reference memfile, we generate a mapping between the session_uid's of the
115 * IDs stored in that previous undo step, and its first matching memchunk. This will allow
116 * us to easily find the existing undo memory storage of IDs even when some re-ordering in
117 * current Main data-base broke the order matching with the memchunks from previous step.
118 */
119 if (reference_memfile != nullptr) {
120 uint current_session_uid = MAIN_ID_SESSION_UID_UNSET;
121 for (MemFileChunk &mem_chunk : reference_memfile->chunks) {
122 if (!ELEM(mem_chunk.id_session_uid, MAIN_ID_SESSION_UID_UNSET, current_session_uid)) {
123 current_session_uid = mem_chunk.id_session_uid;
124 mem_data->id_session_uid_mapping.add_new(current_session_uid, &mem_chunk);
125 }
126 }
127 }
128}
129
131{
132 mem_data->id_session_uid_mapping.clear();
133}
134
135void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t size)
136{
137 MemFile *memfile = mem_data->written_memfile;
138 MemFileChunk **compchunk_step = &mem_data->reference_current_chunk;
139
140 MemFileChunk *curchunk = MEM_new_uninitialized<MemFileChunk>("MemFileChunk");
141 curchunk->size = size;
142 curchunk->buf = nullptr;
143 curchunk->is_identical = false;
144 /* This is unsafe in the sense that an app handler or other code that does not
145 * perform an undo push may make changes after the last undo push that
146 * will then not be undo. Though it's not entirely clear that is wrong behavior. */
147 curchunk->is_identical_future = true;
148 curchunk->id_session_uid = mem_data->current_id_session_uid;
149 BLI_addtail(&memfile->chunks, curchunk);
150
151 /* we compare compchunk with buf */
152 if (*compchunk_step != nullptr) {
153 MemFileChunk *compchunk = *compchunk_step;
154 if (compchunk->size == curchunk->size) {
155 if (memcmp(compchunk->buf, buf, size) == 0) {
156 curchunk->buf = compchunk->buf;
157 curchunk->is_identical = true;
158 compchunk->is_identical_future = true;
159 }
160 }
161 *compchunk_step = static_cast<MemFileChunk *>(compchunk->next);
162 }
163
164 /* not equal... */
165 if (curchunk->buf == nullptr) {
166 char *buf_new = MEM_new_array_uninitialized<char>(size, "Chunk buffer");
167 memcpy(buf_new, buf, size);
168 curchunk->buf = buf_new;
169 memfile->size += size;
170 }
171}
172
173Main *BLO_memfile_main_get(MemFile *memfile, Main *bmain, Scene **r_scene)
174{
175 Main *bmain_undo = nullptr;
176 BlendFileReadParams read_params{};
178 bmain, BKE_main_blendfile_path(bmain), memfile, &read_params, nullptr);
179
180 if (bfd) {
181 bmain_undo = bfd->main;
182 if (r_scene) {
183 *r_scene = bfd->curscene;
184 }
185
186 MEM_delete(bfd);
187 }
188
189 return bmain_undo;
190}
191
192static int64_t undo_read(FileReader *reader, void *buffer, size_t size)
193{
194 UndoReader *undo = reinterpret_cast<UndoReader *>(reader);
195
196 static size_t seek = SIZE_MAX; /* The current position. */
197 static size_t offset = 0; /* Size of previous chunks. */
198 static MemFileChunk *chunk = nullptr;
199 size_t chunkoffset, readsize, totread;
200
201 undo->memchunk_identical = true;
202
203 if (size == 0) {
204 return 0;
205 }
206
207 if (seek != size_t(undo->reader.offset)) {
208 chunk = undo->memfile->chunks.first();
209 seek = 0;
210
211 while (chunk) {
212 if (seek + chunk->size > size_t(undo->reader.offset)) {
213 break;
214 }
215 seek += chunk->size;
216 chunk = static_cast<MemFileChunk *>(chunk->next);
217 }
218 offset = seek;
219 seek = size_t(undo->reader.offset);
220 }
221
222 if (chunk) {
223 totread = 0;
224
225 do {
226 /* First check if it's on the end if current chunk. */
227 if (seek - offset == chunk->size) {
228 offset += chunk->size;
229 chunk = static_cast<MemFileChunk *>(chunk->next);
230 }
231
232 /* Debug, should never happen. */
233 if (chunk == nullptr) {
234 printf("illegal read, chunk zero\n");
235 return 0;
236 }
237
238 chunkoffset = seek - offset;
239 readsize = size - totread;
240
241 /* Data can be spread over multiple chunks, so clamp size
242 * to within this chunk, and then it will read further in
243 * the next chunk. */
244 if (chunkoffset + readsize > chunk->size) {
245 readsize = chunk->size - chunkoffset;
246 }
247
248 memcpy(POINTER_OFFSET(buffer, totread), chunk->buf + chunkoffset, readsize);
249 totread += readsize;
250 undo->reader.offset += off64_t(readsize);
251 seek += readsize;
252
253 /* `is_identical` of current chunk represents whether it changed compared to previous undo
254 * step. this is fine in redo case, but not in undo case, where we need an extra flag
255 * defined when saving the next (future) step after the one we want to restore, as we are
256 * supposed to 'come from' that future undo step, and not the one before current one. */
257 undo->memchunk_identical &= undo->undo_direction == STEP_REDO ? chunk->is_identical :
258 chunk->is_identical_future;
259 } while (totread < size);
260
261 return int64_t(totread);
262 }
263
264 return 0;
265}
266
267static void undo_close(FileReader *reader)
268{
269 MEM_delete(reader);
270}
271
272FileReader *BLO_memfile_new_filereader(MemFile *memfile, int undo_direction)
273{
275
276 undo->memfile = memfile;
277 undo->undo_direction = undo_direction;
278
279 undo->reader.read = undo_read;
280 undo->reader.seek = nullptr;
281 undo->reader.close = undo_close;
282
283 return reinterpret_cast<FileReader *>(undo);
284}
285
286} // namespace blender
#define MAIN_ID_SESSION_UID_UNSET
#define SIZE_MAX
external readfile function prototypes.
These structs are the foundation for all linked lists in the library system.
Read Blender memory allocation and freeing.
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static int64_t undo_read(FileReader *reader, void *buffer, size_t size)
Definition undofile.cc:192
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BLO_memfile_free(MemFile *memfile)
Definition undofile.cc:43
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BMesh const char void * data
void BLO_memfile_write_finalize(WriteData *wd, MemFileWriteData *mem_data)
Definition undofile.cc:130
void BLO_memfile_merge(MemFile *first, MemFile *second)
Definition undofile.cc:63
Main * BLO_memfile_main_get(MemFile *memfile, Main *bmain, Scene **r_scene)
Definition undofile.cc:173
FileReader * BLO_memfile_new_filereader(MemFile *memfile, int undo_direction)
Definition undofile.cc:272
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t size)
Definition undofile.cc:135
void BLO_memfile_write_init(WriteData *wd, MemFileWriteData *mem_data, MemFile *written_memfile, MemFile *reference_memfile)
Definition undofile.cc:101
BlendFileData * BLO_read_from_memfile(Main *oldmain, const char *filepath, MemFile *memfile, const BlendFileReadParams *params, ReportList *reports)
static void undo_close(FileReader *reader)
Definition undofile.cc:267
void BLO_memfile_clear_future(MemFile *memfile)
Definition undofile.cc:94
Map< uint64_t, ImplicitSharingInfoAndData > sharing_info_by_address_id
Map< uint, MemFileChunk * > id_session_uid_mapping
MemFileChunk * reference_current_chunk
MemFileSharedStorage * shared_storage
ListBaseT< MemFileChunk > chunks