]> wimlib.net Git - wimlib/blobdiff - src/wim.c
blob_table: decrease initial blob table size
[wimlib] / src / wim.c
index 9b5e4b24856d8b70deec0bfc50059158ec0c6e0f..43b25aca950d7e1aecac523de7fc73399d0274a0 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -146,12 +146,12 @@ new_wim_struct(void)
        if (!wim)
                return NULL;
 
+       wim->refcnt = 1;
        filedes_invalidate(&wim->in_fd);
        filedes_invalidate(&wim->out_fd);
        wim->out_solid_compression_type = wim_default_solid_compression_type();
        wim->out_solid_chunk_size = wim_default_solid_chunk_size(
                                        wim->out_solid_compression_type);
-       INIT_LIST_HEAD(&wim->subwims);
        return wim;
 }
 
@@ -177,7 +177,7 @@ wimlib_create_new_wim(enum wimlib_compression_type ctype, WIMStruct **wim_ret)
                return WIMLIB_ERR_NOMEM;
 
        wim->xml_info = xml_new_info_struct();
-       wim->blob_table = new_blob_table(9001);
+       wim->blob_table = new_blob_table(64);
        if (!wim->xml_info || !wim->blob_table) {
                wimlib_free(wim);
                return WIMLIB_ERR_NOMEM;
@@ -325,11 +325,11 @@ select_wim_image(WIMStruct *wim, int image)
                return WIMLIB_ERR_METADATA_NOT_FOUND;
 
        /* If a valid image is currently selected, its metadata can be freed if
-        * it has not been modified.  */
+        * it is not dirty and no other WIMStructs may have it selected.  */
        deselect_current_wim_image(wim);
        wim->current_image = image;
        imd = wim_get_current_image_metadata(wim);
-       if (imd->root_dentry || imd->modified) {
+       if (imd->root_dentry || is_image_dirty(imd)) {
                ret = 0;
        } else {
                ret = read_metadata_resource(imd);
@@ -346,7 +346,7 @@ deselect_current_wim_image(WIMStruct *wim)
        if (wim->current_image == WIMLIB_NO_IMAGE)
                return;
        imd = wim_get_current_image_metadata(wim);
-       if (!imd->modified) {
+       if (!is_image_dirty(imd) && imd->refcnt == 1) {
                wimlib_assert(list_empty(&imd->unhashed_blobs));
                destroy_image_metadata(imd, NULL, false);
        }
@@ -729,10 +729,13 @@ begin_read(WIMStruct *wim, const void *wim_filename_or_fd, int open_flags)
        }
 
        if (open_flags & WIMLIB_OPEN_FLAG_FROM_PIPE) {
-               wim->blob_table = new_blob_table(9001);
+               wim->blob_table = new_blob_table(64);
                if (!wim->blob_table)
                        return WIMLIB_ERR_NOMEM;
        } else {
+               if (wim->hdr.blob_table_reshdr.uncompressed_size == 0 &&
+                   wim->hdr.xml_data_reshdr.uncompressed_size == 0)
+                       return WIMLIB_ERR_WIM_IS_INCOMPLETE;
 
                ret = read_wim_xml_data(wim);
                if (ret)
@@ -864,38 +867,45 @@ can_modify_wim(WIMStruct *wim)
        return 0;
 }
 
-/* API function documented in wimlib.h  */
-WIMLIBAPI void
-wimlib_free(WIMStruct *wim)
+/* Release a reference to a WIMStruct.  If the reference count reaches 0, the
+ * WIMStruct is freed.  */
+void
+wim_decrement_refcnt(WIMStruct *wim)
 {
-       if (!wim)
+       wimlib_assert(wim->refcnt > 0);
+       if (--wim->refcnt != 0)
                return;
-
-       while (!list_empty(&wim->subwims)) {
-               WIMStruct *subwim;
-
-               subwim = list_entry(wim->subwims.next, WIMStruct, subwim_node);
-               list_del(&subwim->subwim_node);
-               wimlib_free(subwim);
-       }
-
        if (filedes_valid(&wim->in_fd))
                filedes_close(&wim->in_fd);
        if (filedes_valid(&wim->out_fd))
                filedes_close(&wim->out_fd);
+       wimlib_free_decompressor(wim->decompressor);
+       xml_free_info_struct(wim->xml_info);
+       FREE(wim->filename);
+       FREE(wim);
+}
 
-       free_blob_table(wim->blob_table);
+/* API function documented in wimlib.h  */
+WIMLIBAPI void
+wimlib_free(WIMStruct *wim)
+{
+       if (!wim)
+               return;
 
-       wimlib_free_decompressor(wim->decompressor);
+       /* The blob table and image metadata are freed immediately, but other
+        * members of the WIMStruct such as the input file descriptor are
+        * retained until no more exported resources reference the WIMStruct. */
 
-       FREE(wim->filename);
-       xml_free_info_struct(wim->xml_info);
-       if (wim->image_metadata) {
-               for (unsigned i = 0; i < wim->hdr.image_count; i++)
+       free_blob_table(wim->blob_table);
+       wim->blob_table = NULL;
+       if (wim->image_metadata != NULL) {
+               for (int i = 0; i < wim->hdr.image_count; i++)
                        put_image_metadata(wim->image_metadata[i], NULL);
                FREE(wim->image_metadata);
+               wim->image_metadata = NULL;
        }
-       FREE(wim);
+
+       wim_decrement_refcnt(wim);
 }
 
 static bool