BLO: move blenloader to C++

Differential Revision: https://developer.blender.org/D15965
This commit is contained in:
Jacques Lucke 2022-09-15 19:13:01 +02:00
parent 9f76d0c8e6
commit 8b26349d57
13 changed files with 626 additions and 527 deletions

View File

@ -12,6 +12,8 @@
#pragma once
#include "BLI_utildefines.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -57,6 +59,7 @@ typedef enum eBPathForeachFlag {
* \note Only used by Image IDType currently. */
BKE_BPATH_FOREACH_PATH_RELOAD_EDITED = (1 << 9),
} eBPathForeachFlag;
ENUM_OPERATORS(eBPathForeachFlag, BKE_BPATH_FOREACH_PATH_RELOAD_EDITED)
struct BPathForeachPathData;

View File

@ -20,6 +20,8 @@ extern "C" {
#include "BLI_compiler_attrs.h"
#include "DNA_ID_enums.h"
typedef void (*DrawInfoFreeFP)(void *drawinfo);
enum {
@ -77,8 +79,6 @@ struct PreviewImage;
struct StudioLight;
struct bGPDlayer;
enum eIconSizes;
void BKE_icons_init(int first_dyn_id);
/**

View File

@ -12,6 +12,10 @@
struct Main;
struct ReportList;
#ifdef __cplusplus
extern "C" {
#endif
/**
* Check (but do *not* fix) that all linked data-blocks are still valid
* (i.e. pointing to the right library).
@ -21,3 +25,7 @@ bool BLO_main_validate_libraries(struct Main *bmain, struct ReportList *reports)
* * Check (and fix if needed) that shape key's 'from' pointer is valid.
*/
bool BLO_main_validate_shapekeys(struct Main *bmain, struct ReportList *reports);
#ifdef __cplusplus
}
#endif

View File

@ -61,6 +61,10 @@ typedef struct {
bool memchunk_identical;
} UndoReader;
#ifdef __cplusplus
extern "C" {
#endif
/* Actually only used `writefile.c`. */
void BLO_memfile_write_init(MemFileWriteData *mem_data,
@ -101,3 +105,7 @@ extern struct Main *BLO_memfile_main_get(struct MemFile *memfile,
extern bool BLO_memfile_write_file(struct MemFile *memfile, const char *filepath);
FileReader *BLO_memfile_new_filereader(MemFile *memfile, int undo_direction);
#ifdef __cplusplus
}
#endif

View File

@ -33,11 +33,11 @@ set(INC_SYS
set(SRC
${CMAKE_SOURCE_DIR}/release/datafiles/userdef/userdef_default_theme.c
intern/blend_validate.c
intern/readblenentry.c
intern/readfile.c
intern/readfile_tempload.c
intern/undofile.c
intern/blend_validate.cc
intern/readblenentry.cc
intern/readfile.cc
intern/readfile_tempload.cc
intern/undofile.cc
intern/versioning_250.c
intern/versioning_260.c
intern/versioning_270.c
@ -51,7 +51,7 @@ set(SRC
intern/versioning_dna.c
intern/versioning_legacy.c
intern/versioning_userdef.c
intern/writefile.c
intern/writefile.cc
BLO_blend_defs.h
BLO_blend_validate.h
@ -83,6 +83,18 @@ if(WITH_ALEMBIC)
add_definitions(-DWITH_ALEMBIC)
endif()
if(WITH_TBB)
list(APPEND INC_SYS
${TBB_INCLUDE_DIRS}
)
add_definitions(-DWITH_TBB)
if(WIN32)
# TBB includes Windows.h which will define min/max macros
# that will collide with the stl versions.
add_definitions(-DNOMINMAX)
endif()
endif()
blender_add_lib(bf_blenloader "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
# needed so writefile.c can use dna_type_offsets.h

View File

@ -45,7 +45,8 @@ bool BLO_main_validate_libraries(Main *bmain, ReportList *reports)
ListBase *lbarray[INDEX_ID_MAX];
int i = set_listbasepointers(bmain, lbarray);
while (i--) {
for (ID *id = lbarray[i]->first; id != NULL; id = id->next) {
for (ID *id = static_cast<ID *>(lbarray[i]->first); id != nullptr;
id = static_cast<ID *>(id->next)) {
if (ID_IS_LINKED(id)) {
is_valid = false;
BKE_reportf(reports,
@ -57,18 +58,19 @@ bool BLO_main_validate_libraries(Main *bmain, ReportList *reports)
}
}
for (Main *curmain = bmain->next; curmain != NULL; curmain = curmain->next) {
for (Main *curmain = bmain->next; curmain != nullptr; curmain = curmain->next) {
Library *curlib = curmain->curlib;
if (curlib == NULL) {
BKE_report(reports, RPT_ERROR, "Library database with NULL library data-block!");
if (curlib == nullptr) {
BKE_report(reports, RPT_ERROR, "Library database with nullptr library data-block!");
continue;
}
BKE_library_filepath_set(bmain, curlib, curlib->filepath);
BlendFileReadReport bf_reports = {.reports = reports};
BlendFileReadReport bf_reports{};
bf_reports.reports = reports;
BlendHandle *bh = BLO_blendhandle_from_file(curlib->filepath_abs, &bf_reports);
if (bh == NULL) {
if (bh == nullptr) {
BKE_reportf(reports,
RPT_ERROR,
"Library ID %s not found at expected path %s!",
@ -79,8 +81,8 @@ bool BLO_main_validate_libraries(Main *bmain, ReportList *reports)
i = set_listbasepointers(curmain, lbarray);
while (i--) {
ID *id = lbarray[i]->first;
if (id == NULL) {
ID *id = static_cast<ID *>(lbarray[i]->first);
if (id == nullptr) {
continue;
}
@ -96,12 +98,12 @@ bool BLO_main_validate_libraries(Main *bmain, ReportList *reports)
int totnames = 0;
LinkNode *names = BLO_blendhandle_get_datablock_names(bh, GS(id->name), false, &totnames);
for (; id != NULL; id = id->next) {
for (; id != nullptr; id = static_cast<ID *>(id->next)) {
if (!ID_IS_LINKED(id)) {
is_valid = false;
BKE_reportf(reports,
RPT_ERROR,
"ID %s has NULL lib pointer while being in library %s!",
"ID %s has nullptr lib pointer while being in library %s!",
id->name,
curlib->filepath);
continue;
@ -120,7 +122,7 @@ bool BLO_main_validate_libraries(Main *bmain, ReportList *reports)
}
}
if (name == NULL) {
if (name == nullptr) {
is_valid = false;
BKE_reportf(reports,
RPT_ERROR,
@ -163,7 +165,7 @@ bool BLO_main_validate_shapekeys(Main *bmain, ReportList *reports)
if (!ID_IS_LINKED(id)) {
/* We assume lib data is valid... */
Key *shapekey = BKE_key_from_id(id);
if (shapekey != NULL && shapekey->from != id) {
if (shapekey != nullptr && shapekey->from != id) {
is_valid = false;
BKE_reportf(reports,
RPT_ERROR,
@ -184,7 +186,7 @@ bool BLO_main_validate_shapekeys(Main *bmain, ReportList *reports)
/* NOTE: #BKE_id_delete also locks `bmain`, so we need to do this loop outside of the lock here.
*/
LISTBASE_FOREACH_MUTABLE (Key *, shapekey, &bmain->shapekeys) {
if (shapekey->from != NULL) {
if (shapekey->from != nullptr) {
continue;
}

View File

@ -70,7 +70,7 @@ void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp)
FileData *fd = (FileData *)bh;
BHead *bhead;
fprintf(fp, "[\n");
fprintf(static_cast<FILE *>(fp), "[\n");
for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
if (bhead->code == ENDB) {
break;
@ -90,14 +90,14 @@ void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp)
buf[2] = buf[2] ? buf[2] : ' ';
buf[3] = buf[3] ? buf[3] : ' ';
fprintf(fp,
fprintf(static_cast<FILE *>(fp),
"['%.4s', '%s', %d, %ld ],\n",
buf,
name,
bhead->nr,
(long int)(bhead->len + sizeof(BHead)));
}
fprintf(fp, "]\n");
fprintf(static_cast<FILE *>(fp), "]\n");
}
LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh,
@ -152,7 +152,8 @@ LinkNode *BLO_blendhandle_get_datablock_info(BlendHandle *bh,
if (skip_datablock) {
continue;
}
struct BLODataBlockInfo *info = MEM_mallocN(sizeof(*info), __func__);
struct BLODataBlockInfo *info = static_cast<BLODataBlockInfo *>(
MEM_mallocN(sizeof(*info), __func__));
/* Lastly, read asset data from the following blocks. */
if (asset_meta_data) {
@ -199,7 +200,8 @@ static BHead *blo_blendhandle_read_preview_rects(FileData *fd,
bhead = blo_bhead_next(fd, bhead);
BLI_assert((preview_from_file->w[preview_index] * preview_from_file->h[preview_index] *
sizeof(uint)) == bhead->len);
result->rect[preview_index] = BLO_library_read_struct(fd, bhead, "PreviewImage Icon Rect");
result->rect[preview_index] = static_cast<uint *>(
BLO_library_read_struct(fd, bhead, "PreviewImage Icon Rect"));
}
else {
/* This should not be needed, but can happen in 'broken' .blend files,
@ -227,13 +229,14 @@ PreviewImage *BLO_blendhandle_get_preview_for_id(BlendHandle *bh,
for (BHead *bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
if (bhead->code == DATA) {
if (looking && bhead->SDNAnr == sdna_preview_image) {
PreviewImage *preview_from_file = BLO_library_read_struct(fd, bhead, "PreviewImage");
PreviewImage *preview_from_file = static_cast<PreviewImage *>(
BLO_library_read_struct(fd, bhead, "PreviewImage"));
if (preview_from_file == NULL) {
break;
}
PreviewImage *result = MEM_dupallocN(preview_from_file);
PreviewImage *result = static_cast<PreviewImage *>(MEM_dupallocN(preview_from_file));
bhead = blo_blendhandle_read_preview_rects(fd, bhead, result, preview_from_file);
MEM_freeN(preview_from_file);
return result;
@ -279,7 +282,7 @@ LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *r_
case ID_SCE: /* fall through */
case ID_AC: /* fall through */
case ID_NT: /* fall through */
new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
new_prv = static_cast<PreviewImage *>(MEM_callocN(sizeof(PreviewImage), "newpreview"));
BLI_linklist_prepend(&previews, new_prv);
tot++;
looking = 1;
@ -291,7 +294,7 @@ LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *r_
else if (bhead->code == DATA) {
if (looking) {
if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage")) {
prv = BLO_library_read_struct(fd, bhead, "PreviewImage");
prv = static_cast<PreviewImage *>(BLO_library_read_struct(fd, bhead, "PreviewImage"));
if (prv) {
memcpy(new_prv, prv, sizeof(PreviewImage));
@ -375,7 +378,8 @@ BlendFileData *BLO_read_from_memory(const void *mem,
{
BlendFileData *bfd = NULL;
FileData *fd;
BlendFileReadReport bf_reports = {.reports = reports};
BlendFileReadReport bf_reports{};
bf_reports.reports = reports;
fd = blo_filedata_from_memory(mem, memsize, &bf_reports);
if (fd) {
@ -396,11 +400,12 @@ BlendFileData *BLO_read_from_memfile(Main *oldmain,
BlendFileData *bfd = NULL;
FileData *fd;
ListBase old_mainlist;
BlendFileReadReport bf_reports = {.reports = reports};
BlendFileReadReport bf_reports{};
bf_reports.reports = reports;
fd = blo_filedata_from_memfile(memfile, params, &bf_reports);
if (fd) {
fd->skip_flags = params->skip_flags;
fd->skip_flags = eBLOReadSkip(params->skip_flags);
BLI_strncpy(fd->relabase, filepath, sizeof(fd->relabase));
/* separate libraries from old main */
@ -411,7 +416,7 @@ BlendFileData *BLO_read_from_memfile(Main *oldmain,
if ((params->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0) {
/* Build idmap of old main (we only care about local data here, so we can do that after
* split_main() call. */
blo_make_old_idmap_from_main(fd, old_mainlist.first);
blo_make_old_idmap_from_main(fd, static_cast<Main *>(old_mainlist.first));
}
/* removed packed data from this trick - it's internal data that needs saves */

View File

@ -42,6 +42,7 @@ enum eFileDataFlag {
/* XXX Unused in practice (checked once but never set). */
FD_FLAGS_NOT_MY_LIBMAP = 1 << 5,
};
ENUM_OPERATORS(eFileDataFlag, FD_FLAGS_NOT_MY_LIBMAP)
/* Disallow since it's 32bit on ms-windows. */
#ifdef __GNUC__

View File

@ -20,7 +20,8 @@ TempLibraryContext *BLO_library_temp_load_id(struct Main *real_main,
const char *idname,
struct ReportList *reports)
{
TempLibraryContext *temp_lib_ctx = MEM_callocN(sizeof(*temp_lib_ctx), __func__);
TempLibraryContext *temp_lib_ctx = static_cast<TempLibraryContext *>(
MEM_callocN(sizeof(*temp_lib_ctx), __func__));
temp_lib_ctx->bmain_base = BKE_main_new();
temp_lib_ctx->bf_reports.reports = reports;

View File

@ -42,7 +42,7 @@ void BLO_memfile_free(MemFile *memfile)
{
MemFileChunk *chunk;
while ((chunk = BLI_pophead(&memfile->chunks))) {
while ((chunk = static_cast<MemFileChunk *>(BLI_pophead(&memfile->chunks)))) {
if (chunk->is_identical == false) {
MEM_freeN((void *)chunk->buf);
}
@ -59,7 +59,8 @@ void BLO_memfile_merge(MemFile *first, MemFile *second)
BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
/* First, detect all memchunks in second memfile that are not owned by it. */
for (MemFileChunk *sc = second->chunks.first; sc != NULL; sc = sc->next) {
for (MemFileChunk *sc = static_cast<MemFileChunk *>(second->chunks.first); sc != nullptr;
sc = static_cast<MemFileChunk *>(sc->next)) {
if (sc->is_identical) {
BLI_ghash_insert(buffer_to_second_memchunk, (void *)sc->buf, sc);
}
@ -67,10 +68,12 @@ void BLO_memfile_merge(MemFile *first, MemFile *second)
/* Now, check all chunks from first memfile (the one we are removing), and if a memchunk owned by
* it is also used by the second memfile, transfer the ownership. */
for (MemFileChunk *fc = first->chunks.first; fc != NULL; fc = fc->next) {
for (MemFileChunk *fc = static_cast<MemFileChunk *>(first->chunks.first); fc != nullptr;
fc = static_cast<MemFileChunk *>(fc->next)) {
if (!fc->is_identical) {
MemFileChunk *sc = BLI_ghash_lookup(buffer_to_second_memchunk, fc->buf);
if (sc != NULL) {
MemFileChunk *sc = static_cast<MemFileChunk *>(
BLI_ghash_lookup(buffer_to_second_memchunk, fc->buf));
if (sc != nullptr) {
BLI_assert(sc->is_identical);
sc->is_identical = false;
fc->is_identical = true;
@ -81,7 +84,7 @@ void BLO_memfile_merge(MemFile *first, MemFile *second)
}
}
BLI_ghash_free(buffer_to_second_memchunk, NULL, NULL);
BLI_ghash_free(buffer_to_second_memchunk, nullptr, nullptr);
BLO_memfile_free(first);
}
@ -99,14 +102,16 @@ void BLO_memfile_write_init(MemFileWriteData *mem_data,
{
mem_data->written_memfile = written_memfile;
mem_data->reference_memfile = reference_memfile;
mem_data->reference_current_chunk = reference_memfile ? reference_memfile->chunks.first : NULL;
mem_data->reference_current_chunk = reference_memfile ? static_cast<MemFileChunk *>(
reference_memfile->chunks.first) :
nullptr;
/* If we have a reference memfile, we generate a mapping between the session_uuid's of the
* IDs stored in that previous undo step, and its first matching memchunk. This will allow
* us to easily find the existing undo memory storage of IDs even when some re-ordering in
* current Main data-base broke the order matching with the memchunks from previous step.
*/
if (reference_memfile != NULL) {
if (reference_memfile != nullptr) {
mem_data->id_session_uuid_mapping = BLI_ghash_new(
BLI_ghashutil_inthash_p_simple, BLI_ghashutil_intcmp, __func__);
uint current_session_uuid = MAIN_ID_SESSION_UUID_UNSET;
@ -129,8 +134,8 @@ void BLO_memfile_write_init(MemFileWriteData *mem_data,
void BLO_memfile_write_finalize(MemFileWriteData *mem_data)
{
if (mem_data->id_session_uuid_mapping != NULL) {
BLI_ghash_free(mem_data->id_session_uuid_mapping, NULL, NULL);
if (mem_data->id_session_uuid_mapping != nullptr) {
BLI_ghash_free(mem_data->id_session_uuid_mapping, nullptr, nullptr);
}
}
@ -139,9 +144,10 @@ void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t s
MemFile *memfile = mem_data->written_memfile;
MemFileChunk **compchunk_step = &mem_data->reference_current_chunk;
MemFileChunk *curchunk = MEM_mallocN(sizeof(MemFileChunk), "MemFileChunk");
MemFileChunk *curchunk = static_cast<MemFileChunk *>(
MEM_mallocN(sizeof(MemFileChunk), "MemFileChunk"));
curchunk->size = size;
curchunk->buf = NULL;
curchunk->buf = nullptr;
curchunk->is_identical = false;
/* This is unsafe in the sense that an app handler or other code that does not
* perform an undo push may make changes after the last undo push that
@ -151,7 +157,7 @@ void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t s
BLI_addtail(&memfile->chunks, curchunk);
/* we compare compchunk with buf */
if (*compchunk_step != NULL) {
if (*compchunk_step != nullptr) {
MemFileChunk *compchunk = *compchunk_step;
if (compchunk->size == curchunk->size) {
if (memcmp(compchunk->buf, buf, size) == 0) {
@ -160,12 +166,12 @@ void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t s
compchunk->is_identical_future = true;
}
}
*compchunk_step = compchunk->next;
*compchunk_step = static_cast<MemFileChunk *>(compchunk->next);
}
/* not equal... */
if (curchunk->buf == NULL) {
char *buf_new = MEM_mallocN(size, "Chunk buffer");
if (curchunk->buf == nullptr) {
char *buf_new = static_cast<char *>(MEM_mallocN(size, "Chunk buffer"));
memcpy(buf_new, buf, size);
curchunk->buf = buf_new;
memfile->size += size;
@ -176,12 +182,10 @@ struct Main *BLO_memfile_main_get(struct MemFile *memfile,
struct Main *bmain,
struct Scene **r_scene)
{
struct Main *bmain_undo = NULL;
BlendFileData *bfd = BLO_read_from_memfile(bmain,
BKE_main_blendfile_path(bmain),
memfile,
&(const struct BlendFileReadParams){0},
NULL);
struct Main *bmain_undo = nullptr;
BlendFileReadParams read_params{};
BlendFileData *bfd = BLO_read_from_memfile(
bmain, BKE_main_blendfile_path(bmain), memfile, &read_params, nullptr);
if (bfd) {
bmain_undo = bfd->main;
@ -226,7 +230,8 @@ bool BLO_memfile_write_file(struct MemFile *memfile, const char *filepath)
return false;
}
for (chunk = memfile->chunks.first; chunk; chunk = chunk->next) {
for (chunk = static_cast<MemFileChunk *>(memfile->chunks.first); chunk;
chunk = static_cast<MemFileChunk *>(chunk->next)) {
#ifdef _WIN32
if ((size_t)write(file, chunk->buf, (uint)chunk->size) != chunk->size)
#else
@ -255,7 +260,7 @@ static ssize_t undo_read(FileReader *reader, void *buffer, size_t size)
static size_t seek = SIZE_MAX; /* The current position. */
static size_t offset = 0; /* Size of previous chunks. */
static MemFileChunk *chunk = NULL;
static MemFileChunk *chunk = nullptr;
size_t chunkoffset, readsize, totread;
undo->memchunk_identical = true;
@ -265,7 +270,7 @@ static ssize_t undo_read(FileReader *reader, void *buffer, size_t size)
}
if (seek != (size_t)undo->reader.offset) {
chunk = undo->memfile->chunks.first;
chunk = static_cast<MemFileChunk *>(undo->memfile->chunks.first);
seek = 0;
while (chunk) {
@ -273,7 +278,7 @@ static ssize_t undo_read(FileReader *reader, void *buffer, size_t size)
break;
}
seek += chunk->size;
chunk = chunk->next;
chunk = static_cast<MemFileChunk *>(chunk->next);
}
offset = seek;
seek = (size_t)undo->reader.offset;
@ -286,11 +291,11 @@ static ssize_t undo_read(FileReader *reader, void *buffer, size_t size)
/* First check if it's on the end if current chunk. */
if (seek - offset == chunk->size) {
offset += chunk->size;
chunk = chunk->next;
chunk = static_cast<MemFileChunk *>(chunk->next);
}
/* Debug, should never happen. */
if (chunk == NULL) {
if (chunk == nullptr) {
printf("illegal read, chunk zero\n");
return 0;
}
@ -331,13 +336,13 @@ static void undo_close(FileReader *reader)
FileReader *BLO_memfile_new_filereader(MemFile *memfile, int undo_direction)
{
UndoReader *undo = MEM_callocN(sizeof(UndoReader), __func__);
UndoReader *undo = static_cast<UndoReader *>(MEM_callocN(sizeof(UndoReader), __func__));
undo->memfile = memfile;
undo->undo_direction = undo_direction;
undo->reader.read = undo_read;
undo->reader.seek = NULL;
undo->reader.seek = nullptr;
undo->reader.close = undo_close;
return (FileReader *)undo;

View File

@ -207,17 +207,17 @@ static size_t ww_write_none(WriteWrap *ww, const char *buf, size_t buf_len)
/* zstd */
typedef struct {
struct ZstdWriteBlockTask *next, *prev;
struct ZstdWriteBlockTask {
ZstdWriteBlockTask *next, *prev;
void *data;
size_t size;
int frame_number;
WriteWrap *ww;
} ZstdWriteBlockTask;
};
static void *zstd_write_task(void *userdata)
{
ZstdWriteBlockTask *task = userdata;
ZstdWriteBlockTask *task = static_cast<ZstdWriteBlockTask *>(userdata);
WriteWrap *ww = task->ww;
size_t out_buf_len = ZSTD_compressBound(task->size);
@ -237,8 +237,9 @@ static void *zstd_write_task(void *userdata)
ww->zstd.write_error = true;
}
else {
if (ww_write_none(ww, out_buf, out_size) == out_size) {
ZstdFrame *frameinfo = MEM_mallocN(sizeof(ZstdFrame), "zstd frameinfo");
if (ww_write_none(ww, static_cast<const char *>(out_buf), out_size) == out_size) {
ZstdFrame *frameinfo = static_cast<ZstdFrame *>(
MEM_mallocN(sizeof(ZstdFrame), "zstd frameinfo"));
frameinfo->uncompressed_size = task->size;
frameinfo->compressed_size = out_size;
BLI_addtail(&ww->zstd.frames, frameinfo);
@ -254,7 +255,7 @@ static void *zstd_write_task(void *userdata)
BLI_condition_notify_all(&ww->zstd.condition);
MEM_freeN(out_buf);
return NULL;
return nullptr;
}
static bool ww_open_zstd(WriteWrap *ww, const char *filepath)
@ -333,7 +334,8 @@ static size_t ww_write_zstd(WriteWrap *ww, const char *buf, size_t buf_len)
return 0;
}
ZstdWriteBlockTask *task = MEM_mallocN(sizeof(ZstdWriteBlockTask), __func__);
ZstdWriteBlockTask *task = static_cast<ZstdWriteBlockTask *>(
MEM_mallocN(sizeof(ZstdWriteBlockTask), __func__));
task->data = MEM_mallocN(buf_len, __func__);
memcpy(task->data, buf, buf_len);
task->size = buf_len;
@ -347,7 +349,7 @@ static size_t ww_write_zstd(WriteWrap *ww, const char *buf, size_t buf_len)
* Otherwise, we wait for the earliest thread to finish.
* We look up the earliest thread while holding the mutex, but release it
* before joining the thread to prevent a deadlock. */
ZstdWriteBlockTask *first_task = ww->zstd.tasks.first;
ZstdWriteBlockTask *first_task = static_cast<ZstdWriteBlockTask *>(ww->zstd.tasks.first);
BLI_mutex_unlock(&ww->zstd.mutex);
if (!BLI_available_threads(&ww->zstd.threadpool)) {
BLI_threadpool_remove(&ww->zstd.threadpool, first_task);
@ -424,7 +426,7 @@ typedef struct {
/**
* Wrap writing, so we can use zstd or
* other compression types later, see: G_FILE_COMPRESS
* Will be NULL for UNDO.
* Will be nullptr for UNDO.
*/
WriteWrap *ww;
} WriteData;
@ -435,14 +437,14 @@ typedef struct BlendWriter {
static WriteData *writedata_new(WriteWrap *ww)
{
WriteData *wd = MEM_callocN(sizeof(*wd), "writedata");
WriteData *wd = static_cast<WriteData *>(MEM_callocN(sizeof(*wd), "writedata"));
wd->sdna = DNA_sdna_current_get();
wd->ww = ww;
if ((ww == NULL) || (ww->use_buf)) {
if (ww == NULL) {
if ((ww == nullptr) || (ww->use_buf)) {
if (ww == nullptr) {
wd->buffer.max_size = MEM_BUFFER_SIZE;
wd->buffer.chunk_size = MEM_CHUNK_SIZE;
}
@ -450,7 +452,7 @@ static WriteData *writedata_new(WriteWrap *ww)
wd->buffer.max_size = ZSTD_BUFFER_SIZE;
wd->buffer.chunk_size = ZSTD_CHUNK_SIZE;
}
wd->buffer.buf = MEM_mallocN(wd->buffer.max_size, "wd->buffer.buf");
wd->buffer.buf = static_cast<uchar *>(MEM_mallocN(wd->buffer.max_size, "wd->buffer.buf"));
}
return wd;
@ -458,7 +460,7 @@ static WriteData *writedata_new(WriteWrap *ww)
static void writedata_do_write(WriteData *wd, const void *mem, size_t memlen)
{
if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) {
if ((wd == nullptr) || wd->error || (mem == nullptr) || memlen < 1) {
return;
}
@ -473,10 +475,10 @@ static void writedata_do_write(WriteData *wd, const void *mem, size_t memlen)
/* memory based save */
if (wd->use_memfile) {
BLO_memfile_chunk_add(&wd->mem, mem, memlen);
BLO_memfile_chunk_add(&wd->mem, static_cast<const char *>(mem), memlen);
}
else {
if (wd->ww->write(wd->ww, mem, memlen) != memlen) {
if (wd->ww->write(wd->ww, static_cast<const char *>(mem), memlen) != memlen) {
wd->error = true;
}
}
@ -519,7 +521,7 @@ static void mywrite(WriteData *wd, const void *adr, size_t len)
return;
}
if (UNLIKELY(adr == NULL)) {
if (UNLIKELY(adr == nullptr)) {
BLI_assert(0);
return;
}
@ -528,7 +530,7 @@ static void mywrite(WriteData *wd, const void *adr, size_t len)
wd->write_len += len;
#endif
if (wd->buffer.buf == NULL) {
if (wd->buffer.buf == nullptr) {
writedata_do_write(wd, adr, len);
}
else {
@ -565,15 +567,15 @@ static void mywrite(WriteData *wd, const void *adr, size_t len)
/**
* BeGiN initializer for mywrite
* \param ww: File write wrapper.
* \param compare: Previous memory file (can be NULL).
* \param current: The current memory file (can be NULL).
* \param compare: Previous memory file (can be nullptr).
* \param current: The current memory file (can be nullptr).
* \warning Talks to other functions with global parameters
*/
static WriteData *mywrite_begin(WriteWrap *ww, MemFile *compare, MemFile *current)
{
WriteData *wd = writedata_new(ww);
if (current != NULL) {
if (current != nullptr) {
BLO_memfile_write_init(&wd->mem, current, compare);
wd->use_memfile = true;
}
@ -617,15 +619,17 @@ static void mywrite_id_begin(WriteData *wd, ID *id)
/* If current next memchunk does not match the ID we are about to write, or is not the _first_
* one for said ID, try to find the correct memchunk in the mapping using ID's session_uuid. */
MemFileChunk *curr_memchunk = wd->mem.reference_current_chunk;
MemFileChunk *prev_memchunk = curr_memchunk != NULL ? curr_memchunk->prev : NULL;
if (wd->mem.id_session_uuid_mapping != NULL &&
(curr_memchunk == NULL || curr_memchunk->id_session_uuid != id->session_uuid ||
(prev_memchunk != NULL &&
MemFileChunk *prev_memchunk = curr_memchunk != nullptr ?
static_cast<MemFileChunk *>(curr_memchunk->prev) :
nullptr;
if (wd->mem.id_session_uuid_mapping != nullptr &&
(curr_memchunk == nullptr || curr_memchunk->id_session_uuid != id->session_uuid ||
(prev_memchunk != nullptr &&
(prev_memchunk->id_session_uuid == curr_memchunk->id_session_uuid)))) {
void *ref = BLI_ghash_lookup(wd->mem.id_session_uuid_mapping,
POINTER_FROM_UINT(id->session_uuid));
if (ref != NULL) {
wd->mem.reference_current_chunk = ref;
if (ref != nullptr) {
wd->mem.reference_current_chunk = static_cast<MemFileChunk *>(ref);
}
/* Else, no existing memchunk found, i.e. this is supposed to be a new ID. */
}
@ -662,7 +666,7 @@ static void writestruct_at_address_nr(
BLI_assert(struct_nr > 0 && struct_nr < SDNA_TYPE_MAX);
if (adr == NULL || data == NULL || nr == 0) {
if (adr == nullptr || data == nullptr || nr == 0) {
return;
}
@ -695,7 +699,7 @@ static void writedata(WriteData *wd, int filecode, size_t len, const void *adr)
{
BHead bh;
if (adr == NULL || len == 0) {
if (adr == nullptr || len == 0) {
return;
}
@ -721,7 +725,7 @@ static void writedata(WriteData *wd, int filecode, size_t len, const void *adr)
/* use this to force writing of lists in same order as reading (using link_list) */
static void writelist_nr(WriteData *wd, int filecode, const int struct_nr, const ListBase *lb)
{
const Link *link = lb->first;
const Link *link = static_cast<Link *>(lb->first);
while (link) {
writestruct_nr(wd, filecode, struct_nr, 1, link);
@ -777,35 +781,35 @@ static void current_screen_compat(Main *mainvar,
ViewLayer **r_view_layer)
{
wmWindowManager *wm;
wmWindow *window = NULL;
wmWindow *window = nullptr;
/* find a global current screen in the first open window, to have
* a reasonable default for reading in older versions */
wm = mainvar->wm.first;
wm = static_cast<wmWindowManager *>(mainvar->wm.first);
if (wm) {
if (use_active_win) {
/* write the active window into the file, needed for multi-window undo T43424 */
for (window = wm->windows.first; window; window = window->next) {
for (window = static_cast<wmWindow *>(wm->windows.first); window; window = window->next) {
if (window->active) {
break;
}
}
/* fallback */
if (window == NULL) {
window = wm->windows.first;
if (window == nullptr) {
window = static_cast<wmWindow *>(wm->windows.first);
}
}
else {
window = wm->windows.first;
window = static_cast<wmWindow *>(wm->windows.first);
}
}
*r_screen = (window) ? BKE_workspace_active_screen_get(window->workspace_hook) : NULL;
*r_scene = (window) ? window->scene : NULL;
*r_screen = (window) ? BKE_workspace_active_screen_get(window->workspace_hook) : nullptr;
*r_scene = (window) ? window->scene : nullptr;
*r_view_layer = (window && *r_scene) ? BKE_view_layer_find(*r_scene, window->view_layer_name) :
NULL;
nullptr;
}
typedef struct RenderInfo {
@ -823,7 +827,7 @@ typedef struct RenderInfo {
static void write_renderinfo(WriteData *wd, Main *mainvar)
{
bScreen *curscreen;
Scene *curscene = NULL;
Scene *curscene = nullptr;
ViewLayer *view_layer;
/* XXX in future, handle multiple windows with multiple screens? */
@ -951,7 +955,7 @@ static void write_libraries(WriteData *wd, Main *main)
else {
found_one = false;
while (!found_one && tot--) {
for (id = lbarray[tot]->first; id; id = id->next) {
for (id = static_cast<ID *>(lbarray[tot]->first); id; id = static_cast<ID *>(id->next)) {
if (id->us > 0 &&
((id->tag & LIB_TAG_EXTERN) ||
((id->tag & LIB_TAG_INDIRECT) && (id->flag & LIB_INDIRECT_WEAK_LINK)))) {
@ -970,13 +974,13 @@ static void write_libraries(WriteData *wd, Main *main)
/* Not overridable. */
void *runtime_name_data = main->curlib->runtime.name_map;
main->curlib->runtime.name_map = NULL;
main->curlib->runtime.name_map = nullptr;
BlendWriter writer = {wd};
writestruct(wd, ID_LI, Library, 1, main->curlib);
BKE_id_blend_write(&writer, &main->curlib->id);
main->curlib->runtime.name_map = runtime_name_data;
main->curlib->runtime.name_map = static_cast<UniqueName_Map *>(runtime_name_data);
if (main->curlib->packedfile) {
BKE_packedfile_blend_write(&writer, main->curlib->packedfile);
@ -987,7 +991,7 @@ static void write_libraries(WriteData *wd, Main *main)
/* Write link placeholders for all direct linked IDs. */
while (a--) {
for (id = lbarray[a]->first; id; id = id->next) {
for (id = static_cast<ID *>(lbarray[a]->first); id; id = static_cast<ID *>(id->next)) {
if (id->us > 0 &&
((id->tag & LIB_TAG_EXTERN) ||
((id->tag & LIB_TAG_INDIRECT) && (id->flag & LIB_INDIRECT_WEAK_LINK)))) {
@ -1008,6 +1012,11 @@ static void write_libraries(WriteData *wd, Main *main)
mywrite_flush(wd);
}
#ifdef WITH_BUILDINFO
extern "C" unsigned long build_commit_timestamp;
extern "C" char build_hash[];
#endif
/* context is usually defined by WM, two cases where no WM is available:
* - for forward compatibility, curscreen has to be saved
* - for undofile, curscene needs to be saved */
@ -1024,7 +1033,7 @@ static void write_global(WriteData *wd, int fileflags, Main *mainvar)
memset(fg._pad, 0, sizeof(fg._pad));
memset(fg.filepath, 0, sizeof(fg.filepath));
memset(fg.build_hash, 0, sizeof(fg.build_hash));
fg._pad1 = NULL;
fg._pad1 = nullptr;
current_screen_compat(mainvar, is_undo, &screen, &scene, &view_layer);
@ -1048,13 +1057,9 @@ static void write_global(WriteData *wd, int fileflags, Main *mainvar)
fg.minversion = BLENDER_FILE_MIN_VERSION;
fg.minsubversion = BLENDER_FILE_MIN_SUBVERSION;
#ifdef WITH_BUILDINFO
{
extern unsigned long build_commit_timestamp;
extern char build_hash[];
/* TODO(sergey): Add branch name to file as well? */
fg.build_commit_timestamp = build_commit_timestamp;
BLI_strncpy(fg.build_hash, build_hash, sizeof(fg.build_hash));
}
/* TODO(sergey): Add branch name to file as well? */
fg.build_commit_timestamp = build_commit_timestamp;
BLI_strncpy(fg.build_hash, build_hash, sizeof(fg.build_hash));
#else
fg.build_commit_timestamp = 0;
BLI_strncpy(fg.build_hash, "unknown", sizeof(fg.build_hash));
@ -1116,7 +1121,7 @@ static bool write_file_handle(Main *mainvar,
mywrite_flush(wd);
OverrideLibraryStorage *override_storage = wd->use_memfile ?
NULL :
nullptr :
BKE_lib_override_library_operations_store_init();
#define ID_BUFFER_STATIC_SIZE 8192
@ -1128,9 +1133,9 @@ static bool write_file_handle(Main *mainvar,
ListBase *lbarray[INDEX_ID_MAX];
int a = set_listbasepointers(bmain, lbarray);
while (a--) {
ID *id = lbarray[a]->first;
ID *id = static_cast<ID *>(lbarray[a]->first);
if (id == NULL || GS(id->name) == ID_LI) {
if (id == nullptr || GS(id->name) == ID_LI) {
continue; /* Libraries are handled separately below. */
}
@ -1148,7 +1153,7 @@ static bool write_file_handle(Main *mainvar,
id_buffer = MEM_mallocN(idtype_struct_size, __func__);
}
for (; id; id = id->next) {
for (; id; id = static_cast<ID *>(id->next)) {
/* We should never attempt to write non-regular IDs
* (i.e. all kind of temp/runtime ones). */
BLI_assert(
@ -1156,13 +1161,13 @@ static bool write_file_handle(Main *mainvar,
/* We only write unused IDs in undo case.
* NOTE: All Scenes, WindowManagers and WorkSpaces should always be written to disk, so
* their usercount should never be NULL currently. */
* their usercount should never be nullptr currently. */
if (id->us == 0 && !wd->use_memfile) {
BLI_assert(!ELEM(GS(id->name), ID_SCE, ID_WM, ID_WS));
continue;
}
const bool do_override = !ELEM(override_storage, NULL, bmain) &&
const bool do_override = !ELEM(override_storage, nullptr, bmain) &&
ID_IS_OVERRIDE_LIBRARY_REAL(id);
if (do_override) {
@ -1177,13 +1182,13 @@ static bool write_file_handle(Main *mainvar,
id->recalc_after_undo_push = 0;
bNodeTree *nodetree = ntreeFromID(id);
if (nodetree != NULL) {
if (nodetree != nullptr) {
nodetree->id.recalc_up_to_undo_push = nodetree->id.recalc_after_undo_push;
nodetree->id.recalc_after_undo_push = 0;
}
if (GS(id->name) == ID_SCE) {
Scene *scene = (Scene *)id;
if (scene->master_collection != NULL) {
if (scene->master_collection != nullptr) {
scene->master_collection->id.recalc_up_to_undo_push =
scene->master_collection->id.recalc_after_undo_push;
scene->master_collection->id.recalc_after_undo_push = 0;
@ -1202,18 +1207,18 @@ static bool write_file_handle(Main *mainvar,
/* Those listbase data change every time we add/remove an ID, and also often when
* renaming one (due to re-sorting). This avoids generating a lot of false 'is changed'
* detections between undo steps. */
((ID *)id_buffer)->prev = NULL;
((ID *)id_buffer)->next = NULL;
((ID *)id_buffer)->prev = nullptr;
((ID *)id_buffer)->next = nullptr;
/* Those runtime pointers should never be set during writing stage, but just in case clear
* them too. */
((ID *)id_buffer)->orig_id = NULL;
((ID *)id_buffer)->newid = NULL;
((ID *)id_buffer)->orig_id = nullptr;
((ID *)id_buffer)->newid = nullptr;
/* Even though in theory we could be able to preserve this python instance across undo even
* when we need to re-read the ID into its original address, this is currently cleared in
* #direct_link_id_common in `readfile.c` anyway, */
((ID *)id_buffer)->py_instance = NULL;
((ID *)id_buffer)->py_instance = nullptr;
if (id_type->blend_write != NULL) {
if (id_type->blend_write != nullptr) {
id_type->blend_write(&writer, (ID *)id_buffer, id);
}
@ -1234,7 +1239,7 @@ static bool write_file_handle(Main *mainvar,
if (override_storage) {
BKE_lib_override_library_operations_store_finalize(override_storage);
override_storage = NULL;
override_storage = nullptr;
}
/* Special handling, operating over split Mains... */
@ -1331,11 +1336,11 @@ bool BLO_write_file(Main *mainvar,
const bool relbase_valid = (mainvar->filepath[0] != '\0');
/* path backup/restore */
void *path_list_backup = NULL;
void *path_list_backup = nullptr;
const eBPathForeachFlag path_list_flag = (BKE_BPATH_FOREACH_PATH_SKIP_LINKED |
BKE_BPATH_FOREACH_PATH_SKIP_MULTIFILE);
if (G.debug & G_DEBUG_IO && mainvar->lock != NULL) {
if (G.debug & G_DEBUG_IO && mainvar->lock != nullptr) {
BKE_report(reports, RPT_INFO, "Checking sanity of current .blend file *BEFORE* save to disk");
BLO_main_validate_libraries(mainvar, reports);
BLO_main_validate_shapekeys(mainvar, reports);
@ -1375,13 +1380,13 @@ bool BLO_write_file(Main *mainvar,
/* Normalize the paths in case there is some subtle difference (so they can be compared). */
if (relbase_valid) {
BLI_split_dir_part(mainvar->filepath, dir_src, sizeof(dir_src));
BLI_path_normalize(NULL, dir_src);
BLI_path_normalize(nullptr, dir_src);
}
else {
dir_src[0] = '\0';
}
BLI_split_dir_part(filepath, dir_dst, sizeof(dir_dst));
BLI_path_normalize(NULL, dir_dst);
BLI_path_normalize(nullptr, dir_dst);
/* Only for relative, not relative-all, as this means making existing paths relative. */
if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {
@ -1415,16 +1420,16 @@ bool BLO_write_file(Main *mainvar,
case BLO_WRITE_PATH_REMAP_RELATIVE:
/* Saved, make relative paths relative to new location (if possible). */
BLI_assert(relbase_valid);
BKE_bpath_relative_rebase(mainvar, dir_src, dir_dst, NULL);
BKE_bpath_relative_rebase(mainvar, dir_src, dir_dst, nullptr);
break;
case BLO_WRITE_PATH_REMAP_RELATIVE_ALL:
/* Make all relative (when requested or unsaved). */
BKE_bpath_relative_convert(mainvar, dir_dst, NULL);
BKE_bpath_relative_convert(mainvar, dir_dst, nullptr);
break;
case BLO_WRITE_PATH_REMAP_ABSOLUTE:
/* Make all absolute (when requested or unsaved). */
BLI_assert(relbase_valid);
BKE_bpath_absolute_convert(mainvar, dir_src, NULL);
BKE_bpath_absolute_convert(mainvar, dir_src, nullptr);
break;
case BLO_WRITE_PATH_REMAP_NONE:
BLI_assert_unreachable(); /* Unreachable. */
@ -1436,7 +1441,8 @@ bool BLO_write_file(Main *mainvar,
}
/* actual file writing */
const bool err = write_file_handle(mainvar, &ww, NULL, NULL, write_flags, use_userdef, thumb);
const bool err = write_file_handle(
mainvar, &ww, nullptr, nullptr, write_flags, use_userdef, thumb);
ww.close(&ww);
@ -1467,7 +1473,7 @@ bool BLO_write_file(Main *mainvar,
return false;
}
if (G.debug & G_DEBUG_IO && mainvar->lock != NULL) {
if (G.debug & G_DEBUG_IO && mainvar->lock != nullptr) {
BKE_report(reports, RPT_INFO, "Checking sanity of current .blend file *AFTER* save to disk");
BLO_main_validate_libraries(mainvar, reports);
}
@ -1480,7 +1486,7 @@ bool BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int w
bool use_userdef = false;
const bool err = write_file_handle(
mainvar, NULL, compare, current, write_flags, use_userdef, NULL);
mainvar, nullptr, compare, current, write_flags, use_userdef, nullptr);
return (err == 0);
}
@ -1599,7 +1605,7 @@ void BLO_write_float3_array(BlendWriter *writer, uint num, const float *data_ptr
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
{
if (data_ptr != NULL) {
if (data_ptr != nullptr) {
BLO_write_raw(writer, strlen(data_ptr) + 1, data_ptr);
}
}

View File

@ -11,6 +11,10 @@
struct bArgs;
struct bContext;
#ifdef __cplusplus
extern "C" {
#endif
#ifndef WITH_PYTHON_MODULE
/* creator_args.c */
@ -87,3 +91,7 @@ extern char build_cxxflags[];
extern char build_linkflags[];
extern char build_system[];
#endif /* BUILD_DATE */
#ifdef __cplusplus
}
#endif