summaryrefslogtreecommitdiff
path: root/libs/androidfw/include
diff options
context:
space:
mode:
authorRyan Mitchell <rtmitchell@google.com>2020-09-29 17:22:52 -0700
committerRyan Mitchell <rtmitchell@google.com>2020-10-14 14:52:52 -0700
commitbf1f45b071bd2fb28cbea7e7cea86cd2e4944a7c (patch)
tree81521006b59c4e16a7b593f0fe123be5f2f54c0c /libs/androidfw/include
parent0f942f99cac4f5f61b40847d20ecb3a94c96c843 (diff)
Remove malloc/free for inline overlay values
Remove malloc/free of android::ResTable_entry for inline overlay values. Add `target_entry_inline` to the idmap format to encode inline overlay values separate from direct mapping of target resource to overlay resource. This reduces the number of bytes needed to represent a direct mapping of target resource to overlay resource from 9 bytes to 8 bytes per entry. Fixed all idmap alignment issues that required the framework to use "#pragma pack(push, 1)" when loading idmaps. Bug: 170341022 Test: idmap2_tests and libandroidfw_tests Change-Id: Iab4d3902508f02773464724913e0ee966e3689e4
Diffstat (limited to 'libs/androidfw/include')
-rw-r--r--libs/androidfw/include/androidfw/Idmap.h36
-rw-r--r--libs/androidfw/include/androidfw/ResourceTypes.h67
2 files changed, 35 insertions, 68 deletions
diff --git a/libs/androidfw/include/androidfw/Idmap.h b/libs/androidfw/include/androidfw/Idmap.h
index ecc1ce65d124..ab0f47f025d2 100644
--- a/libs/androidfw/include/androidfw/Idmap.h
+++ b/libs/androidfw/include/androidfw/Idmap.h
@@ -77,40 +77,40 @@ class OverlayDynamicRefTable : public DynamicRefTable {
// A mapping of target resource ids to a values or resource ids that should overlay the target.
class IdmapResMap {
public:
- // Represents the result of a idmap lookup. The result can be one of three possibillities:
+ // Represents the result of a idmap lookup. The result can be one of three possibilities:
// 1) The result is a resource id which represents the overlay resource that should act as an
// alias of the target resource.
// 2) The result is a table entry which overlays the type and value of the target resource.
// 3) The result is neither and the target resource is not overlaid.
class Result {
public:
- Result() : data_(nullptr) {};
+ Result() = default;
explicit Result(uint32_t value) : data_(value) {};
- explicit Result(ResTable_entry_handle&& value) : data_(value) { };
+ explicit Result(const Res_value& value) : data_(value) { };
// Returns `true` if the resource is overlaid.
- inline explicit operator bool() const {
- return !std::get_if<nullptr_t>(&data_);
+ explicit operator bool() const {
+ return std::get_if<std::monostate>(&data_) == nullptr;
}
- inline bool IsResourceId() const {
- return std::get_if<uint32_t>(&data_);
+ bool IsResourceId() const {
+ return std::get_if<uint32_t>(&data_) != nullptr;
}
- inline uint32_t GetResourceId() const {
- return *std::get_if<uint32_t>(&data_);
+ uint32_t GetResourceId() const {
+ return std::get<uint32_t>(data_);
}
- inline bool IsTableEntry() const {
- return std::get_if<ResTable_entry_handle>(&data_);
+ bool IsInlineValue() const {
+ return std::get_if<Res_value>(&data_) != nullptr;
}
- inline const ResTable_entry_handle& GetTableEntry() const {
- return *std::get_if<ResTable_entry_handle>(&data_);
+ const Res_value& GetInlineValue() const {
+ return std::get<Res_value>(data_);
}
private:
- std::variant<uint32_t, nullptr_t, ResTable_entry_handle> data_;
+ std::variant<std::monostate, uint32_t, Res_value> data_;
};
// Looks up the value that overlays the target resource id.
@@ -123,11 +123,13 @@ class IdmapResMap {
private:
explicit IdmapResMap(const Idmap_data_header* data_header,
const Idmap_target_entry* entries,
+ const Idmap_target_entry_inline* inline_entries,
uint8_t target_assigned_package_id,
const OverlayDynamicRefTable* overlay_ref_table);
const Idmap_data_header* data_header_;
const Idmap_target_entry* entries_;
+ const Idmap_target_entry_inline* inline_entries_;
const uint8_t target_assigned_package_id_;
const OverlayDynamicRefTable* overlay_ref_table_;
@@ -163,8 +165,8 @@ class LoadedIdmap {
// Returns a mapping from target resource ids to overlay values.
inline const IdmapResMap GetTargetResourcesMap(
uint8_t target_assigned_package_id, const OverlayDynamicRefTable* overlay_ref_table) const {
- return IdmapResMap(data_header_, target_entries_, target_assigned_package_id,
- overlay_ref_table);
+ return IdmapResMap(data_header_, target_entries_, target_inline_entries_,
+ target_assigned_package_id, overlay_ref_table);
}
// Returns a dynamic reference table for a loaded overlay package.
@@ -184,6 +186,7 @@ class LoadedIdmap {
const Idmap_header* header_;
const Idmap_data_header* data_header_;
const Idmap_target_entry* target_entries_;
+ const Idmap_target_entry_inline* target_inline_entries_;
const Idmap_overlay_entry* overlay_entries_;
const std::unique_ptr<ResStringPool> string_pool_;
@@ -200,6 +203,7 @@ class LoadedIdmap {
const Idmap_header* header,
const Idmap_data_header* data_header,
const Idmap_target_entry* target_entries,
+ const Idmap_target_entry_inline* target_inline_entries,
const Idmap_overlay_entry* overlay_entries,
ResStringPool* string_pool);
diff --git a/libs/androidfw/include/androidfw/ResourceTypes.h b/libs/androidfw/include/androidfw/ResourceTypes.h
index e10a7f3f5c61..04ba78b6705d 100644
--- a/libs/androidfw/include/androidfw/ResourceTypes.h
+++ b/libs/androidfw/include/androidfw/ResourceTypes.h
@@ -41,7 +41,7 @@
namespace android {
constexpr const static uint32_t kIdmapMagic = 0x504D4449u;
-constexpr const static uint32_t kIdmapCurrentVersion = 0x00000004u;
+constexpr const static uint32_t kIdmapCurrentVersion = 0x00000005u;
/**
* In C++11, char16_t is defined as *at least* 16 bits. We do a lot of
@@ -1476,7 +1476,7 @@ struct ResTable_entry
// If set, this is a weak resource and may be overriden by strong
// resources of the same name/type. This is only useful during
// linking with other resource tables.
- FLAG_WEAK = 0x0004
+ FLAG_WEAK = 0x0004,
};
uint16_t flags;
@@ -1586,50 +1586,6 @@ struct ResTable_map
Res_value value;
};
-
-// A ResTable_entry variant that either holds an unmanaged pointer to a constant ResTable_entry or
-// holds a ResTable_entry which is tied to the lifetime of the handle.
-class ResTable_entry_handle {
- public:
- ResTable_entry_handle() = default;
-
- ResTable_entry_handle(const ResTable_entry_handle& handle) {
- entry_ = handle.entry_;
- }
-
- ResTable_entry_handle(ResTable_entry_handle&& handle) noexcept {
- entry_ = handle.entry_;
- }
-
- inline static ResTable_entry_handle managed(ResTable_entry* entry, void (*deleter)(void *)) {
- return ResTable_entry_handle(std::shared_ptr<const ResTable_entry>(entry, deleter));
- }
-
- inline static ResTable_entry_handle unmanaged(const ResTable_entry* entry) {
- return ResTable_entry_handle(std::shared_ptr<const ResTable_entry>(entry, [](auto /*p */){}));
- }
-
- inline ResTable_entry_handle& operator=(const ResTable_entry_handle& handle) noexcept {
- entry_ = handle.entry_;
- return *this;
- }
-
- inline ResTable_entry_handle& operator=(ResTable_entry_handle&& handle) noexcept {
- entry_ = handle.entry_;
- return *this;
- }
-
- inline const ResTable_entry* operator*() & {
- return entry_.get();
- }
-
- private:
- explicit ResTable_entry_handle(std::shared_ptr<const ResTable_entry> entry)
- : entry_(std::move(entry)) { }
-
- std::shared_ptr<const ResTable_entry> entry_;
-};
-
/**
* A package-id to package name mapping for any shared libraries used
* in this resource table. The package-id's encoded in this resource
@@ -1740,7 +1696,6 @@ inline ResTable_overlayable_policy_header::PolicyFlags& operator |=(
return first;
}
-#pragma pack(push, 1)
struct Idmap_header {
// Always 0x504D4449 ('IDMP')
uint32_t magic;
@@ -1751,7 +1706,7 @@ struct Idmap_header {
uint32_t overlay_crc32;
uint32_t fulfilled_policies;
- uint8_t enforce_overlayable;
+ uint32_t enforce_overlayable;
uint8_t target_path[256];
uint8_t overlay_path[256];
@@ -1765,23 +1720,31 @@ struct Idmap_header {
struct Idmap_data_header {
uint8_t target_package_id;
uint8_t overlay_package_id;
+
+ // Padding to ensure 4 byte alignment for target_entry_count
+ uint16_t p0;
+
uint32_t target_entry_count;
+ uint32_t target_inline_entry_count;
uint32_t overlay_entry_count;
+
uint32_t string_pool_index_offset;
- uint32_t string_pool_length;
};
struct Idmap_target_entry {
uint32_t target_id;
- uint8_t type;
- uint32_t value;
+ uint32_t overlay_id;
+};
+
+struct Idmap_target_entry_inline {
+ uint32_t target_id;
+ Res_value value;
};
struct Idmap_overlay_entry {
uint32_t overlay_id;
uint32_t target_id;
};
-#pragma pack(pop)
class AssetManager2;