Cleanup: remove null check in blo_filedata_free

All callers except for one were already checking, add ATTR_NONNULL
attribute to functions that take a FileData to make it clear that
it's not expected to be null.
This commit is contained in:
Campbell Barton
2024-04-25 22:54:23 +10:00
parent fe0e2907b3
commit d291ec37e0
3 changed files with 76 additions and 81 deletions

View File

@@ -329,7 +329,7 @@ LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh);
*
* \param bh: The handle to close.
*/
void BLO_blendhandle_close(BlendHandle *bh);
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1);
/**
* Mark the given Main (and the 'root' local one in case of lib-split Mains) as invalid, and

View File

@@ -1316,63 +1316,60 @@ FileData *blo_filedata_from_memfile(MemFile *memfile,
void blo_filedata_free(FileData *fd)
{
if (fd) {
/* Free all BHeadN data blocks */
/* Free all BHeadN data blocks */
#ifndef NDEBUG
BLI_freelistN(&fd->bhead_list);
BLI_freelistN(&fd->bhead_list);
#else
/* Sanity check we're not keeping memory we don't need. */
LISTBASE_FOREACH_MUTABLE (BHeadN *, new_bhead, &fd->bhead_list) {
if (fd->file->seek != nullptr && BHEAD_USE_READ_ON_DEMAND(&new_bhead->bhead)) {
BLI_assert(new_bhead->has_data == 0);
}
MEM_freeN(new_bhead);
/* Sanity check we're not keeping memory we don't need. */
LISTBASE_FOREACH_MUTABLE (BHeadN *, new_bhead, &fd->bhead_list) {
if (fd->file->seek != nullptr && BHEAD_USE_READ_ON_DEMAND(&new_bhead->bhead)) {
BLI_assert(new_bhead->has_data == 0);
}
MEM_freeN(new_bhead);
}
#endif
fd->file->close(fd->file);
fd->file->close(fd->file);
if (fd->filesdna) {
DNA_sdna_free(fd->filesdna);
}
if (fd->compflags) {
MEM_freeN((void *)fd->compflags);
}
if (fd->reconstruct_info) {
DNA_reconstruct_info_free(fd->reconstruct_info);
}
if (fd->filesdna) {
DNA_sdna_free(fd->filesdna);
}
if (fd->compflags) {
MEM_freeN((void *)fd->compflags);
}
if (fd->reconstruct_info) {
DNA_reconstruct_info_free(fd->reconstruct_info);
}
if (fd->datamap) {
oldnewmap_free(fd->datamap);
}
if (fd->globmap) {
oldnewmap_free(fd->globmap);
}
if (fd->packedmap) {
oldnewmap_free(fd->packedmap);
}
if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP)) {
oldnewmap_free(fd->libmap);
}
if (fd->old_idmap_uid != nullptr) {
BKE_main_idmap_destroy(fd->old_idmap_uid);
}
if (fd->new_idmap_uid != nullptr) {
BKE_main_idmap_destroy(fd->new_idmap_uid);
}
blo_cache_storage_end(fd);
if (fd->bheadmap) {
MEM_freeN(fd->bheadmap);
}
if (fd->datamap) {
oldnewmap_free(fd->datamap);
}
if (fd->globmap) {
oldnewmap_free(fd->globmap);
}
if (fd->packedmap) {
oldnewmap_free(fd->packedmap);
}
if (fd->libmap && !(fd->flags & FD_FLAGS_NOT_MY_LIBMAP)) {
oldnewmap_free(fd->libmap);
}
if (fd->old_idmap_uid != nullptr) {
BKE_main_idmap_destroy(fd->old_idmap_uid);
}
if (fd->new_idmap_uid != nullptr) {
BKE_main_idmap_destroy(fd->new_idmap_uid);
}
blo_cache_storage_end(fd);
if (fd->bheadmap) {
MEM_freeN(fd->bheadmap);
}
#ifdef USE_GHASH_BHEAD
if (fd->bhead_idname_hash) {
BLI_ghash_free(fd->bhead_idname_hash, nullptr, nullptr);
}
if (fd->bhead_idname_hash) {
BLI_ghash_free(fd->bhead_idname_hash, nullptr, nullptr);
}
#endif
MEM_freeN(fd);
}
MEM_freeN(fd);
}
/** \} */
@@ -1383,31 +1380,28 @@ void blo_filedata_free(FileData *fd)
BlendThumbnail *BLO_thumbnail_from_file(const char *filepath)
{
FileData *fd;
BlendThumbnail *data = nullptr;
const int *fd_data;
fd = blo_filedata_from_file_minimal(filepath);
fd_data = fd ? read_file_thumbnail(fd) : nullptr;
if (fd_data) {
const int width = fd_data[0];
const int height = fd_data[1];
if (BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
const size_t data_size = BLEN_THUMB_MEMSIZE(width, height);
data = static_cast<BlendThumbnail *>(MEM_mallocN(data_size, __func__));
if (data) {
BLI_assert((data_size - sizeof(*data)) ==
(BLEN_THUMB_MEMSIZE_FILE(width, height) - (sizeof(*fd_data) * 2)));
data->width = width;
data->height = height;
memcpy(data->rect, &fd_data[2], data_size - sizeof(*data));
FileData *fd = blo_filedata_from_file_minimal(filepath);
if (fd) {
if (const int *fd_data = read_file_thumbnail(fd)) {
const int width = fd_data[0];
const int height = fd_data[1];
if (BLEN_THUMB_MEMSIZE_IS_VALID(width, height)) {
const size_t data_size = BLEN_THUMB_MEMSIZE(width, height);
data = static_cast<BlendThumbnail *>(MEM_mallocN(data_size, __func__));
if (data) {
BLI_assert((data_size - sizeof(*data)) ==
(BLEN_THUMB_MEMSIZE_FILE(width, height) - (sizeof(*fd_data) * 2)));
data->width = width;
data->height = height;
memcpy(data->rect, &fd_data[2], data_size - sizeof(*data));
}
}
}
blo_filedata_free(fd);
}
blo_filedata_free(fd);
return data;
}

View File

@@ -147,7 +147,7 @@ struct FileData {
void blo_join_main(ListBase *mainlist);
void blo_split_main(ListBase *mainlist, Main *main);
BlendFileData *blo_read_file_internal(FileData *fd, const char *filepath);
BlendFileData *blo_read_file_internal(FileData *fd, const char *filepath) ATTR_NONNULL(1, 2);
/**
* On each new library added, it now checks for the current #FileData and expands relativeness
@@ -160,29 +160,30 @@ FileData *blo_filedata_from_memfile(MemFile *memfile,
const BlendFileReadParams *params,
BlendFileReadReport *reports);
void blo_make_packed_pointer_map(FileData *fd, Main *oldmain);
void blo_make_packed_pointer_map(FileData *fd, Main *oldmain) ATTR_NONNULL(1, 2);
/**
* Set old main packed data to zero if it has been restored
* this works because freeing old main only happens after this call.
*/
void blo_end_packed_pointer_map(FileData *fd, Main *oldmain);
void blo_end_packed_pointer_map(FileData *fd, Main *oldmain) ATTR_NONNULL(1, 2);
/**
* Build a #GSet of old main (we only care about local data here,
* so we can do that after #blo_split_main() call.
*/
void blo_make_old_idmap_from_main(FileData *fd, Main *bmain);
void blo_make_old_idmap_from_main(FileData *fd, Main *bmain) ATTR_NONNULL(1, 2);
BHead *blo_read_asset_data_block(FileData *fd, BHead *bhead, AssetMetaData **r_asset_data);
BHead *blo_read_asset_data_block(FileData *fd, BHead *bhead, AssetMetaData **r_asset_data)
ATTR_NONNULL(1, 2);
void blo_cache_storage_init(FileData *fd, Main *bmain);
void blo_cache_storage_old_bmain_clear(FileData *fd, Main *bmain_old);
void blo_cache_storage_end(FileData *fd);
void blo_cache_storage_init(FileData *fd, Main *bmain) ATTR_NONNULL(1, 2);
void blo_cache_storage_old_bmain_clear(FileData *fd, Main *bmain_old) ATTR_NONNULL(1, 2);
void blo_cache_storage_end(FileData *fd) ATTR_NONNULL(1);
void blo_filedata_free(FileData *fd);
void blo_filedata_free(FileData *fd) ATTR_NONNULL(1);
BHead *blo_bhead_first(FileData *fd);
BHead *blo_bhead_next(FileData *fd, BHead *thisblock);
BHead *blo_bhead_prev(FileData *fd, BHead *thisblock);
BHead *blo_bhead_first(FileData *fd) ATTR_NONNULL(1);
BHead *blo_bhead_next(FileData *fd, BHead *thisblock) ATTR_NONNULL(1);
BHead *blo_bhead_prev(FileData *fd, BHead *thisblock) ATTR_NONNULL(1, 2);
/**
* Warning! Caller's responsibility to ensure given bhead **is** an ID one!
@@ -254,8 +255,8 @@ void do_versions_after_setup(Main *new_bmain, BlendFileReadReport *reports);
* \note This is rather unfortunate to have to expose this here,
* but better use that nasty hack in do_version than readfile itself.
*/
void *blo_read_get_new_globaldata_address(FileData *fd, const void *adr);
void *blo_read_get_new_globaldata_address(FileData *fd, const void *adr) ATTR_NONNULL(1);
/* Mark the Main data as invalid (.blend file reading should be aborted ASAP, and the already read
* data should be discarded). Also add an error report to `fd` including given `message`. */
void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message);
void blo_readfile_invalidate(FileData *fd, Main *bmain, const char *message) ATTR_NONNULL(1, 2, 3);