天天看点

C++多态-初始化变量之init-firststage

1. 结构关系

基类FirstStageMount.基类static std::unique_ptr Create(),就是返回类型为FirstStageMount类型。

66 class FirstStageMount {                                                         
 67   public:                                                                       
 68     FirstStageMount(Fstab fstab);                                                                                                                                                                       
 69     virtual ~FirstStageMount() = default;                                       
 70                                                                                 
 71     // The factory method to create either FirstStageMountVBootV1 or FirstStageMountVBootV2
 72     // based on device tree configurations.                                     
 73     static std::unique_ptr<FirstStageMount> Create();                           
 74     bool DoFirstStageMount();  // Mounts fstab entries read from device tree.   
 75     bool InitDevices();  
 .......

 77   protected:                                                                    
 78     ListenerAction HandleBlockDevice(const std::string& name, const Uevent&);   
 79     bool InitRequiredDevices();                                                 
 80     bool InitMappedDevice(const std::string& verity_device);                    
 81     bool InitDeviceMapper();                                                    
 82     bool CreateLogicalPartitions();                                             
 83     bool MountPartition(const Fstab::iterator& begin, bool erase_same_mounts,   
 84                         Fstab::iterator* end = nullptr);                        
 85                                                                                 
 86     bool MountPartitions();                                                     
 87     bool TrySwitchSystemAsRoot();                                               
 88     bool TrySkipMountingPartitions();                                           
 89     bool IsDmLinearEnabled();                                                   
 90     bool GetDmLinearMetadataDevice();                                           
 91     bool InitDmLinearBackingDevices(const android::fs_mgr::LpMetadata& metadata);
 92     void UseGsiIfPresent();                                                                                                                                                                             
 93                                                                                 
 94     ListenerAction UeventCallback(const Uevent& uevent);                        
 95                                                                                 
 96     // Pure virtual functions.                                                  
 97     virtual bool GetDmVerityDevices() = 0;                                      
 98     virtual bool SetUpDmVerity(FstabEntry* fstab_entry) = 0;                    
 99                                                                                 
100     bool need_dm_verity_;                                                       
101     bool gsi_not_on_userdata_ = false;                                          
102                                                                                 
103     Fstab fstab_;  
 
           

FirstStageMountVBootV1和FirstStageMountVBootV2类是FirstStageMount的子类。

111 class FirstStageMountVBootV1 : public FirstStageMount {                         
112   public:                                                                       
113     FirstStageMountVBootV1(Fstab fstab) : FirstStageMount(std::move(fstab)) {}  
114     ~FirstStageMountVBootV1() override = default;                               
115                                                                                 
116   protected:                                                                    
117     bool GetDmVerityDevices() override;                                         
118     bool SetUpDmVerity(FstabEntry* fstab_entry) override;                       
119 };                                                                              
120                                                                                 
121 class FirstStageMountVBootV2 : public FirstStageMount {                         
122   public:                                                                       
123     friend void SetInitAvbVersionInRecovery();                                  
124                                                                                 
125     FirstStageMountVBootV2(Fstab fstab);                                        
126     ~FirstStageMountVBootV2() override = default;                               
127                                                                                 
128   protected:                                                                    
129     bool GetDmVerityDevices() override;                                         
130     bool SetUpDmVerity(FstabEntry* fstab_entry) override;                       
131     bool InitAvbHandle();                                                       
132                                                                                 
133     std::vector<std::string> vbmeta_partitions_;                                
134     AvbUniquePtr avb_handle_;                                                   
135 };  
           

create函数。

230 std::unique_ptr<FirstStageMount> FirstStageMount::Create() {                    
231     auto fstab = ReadFirstStageFstab();                                         
232     if (IsDtVbmetaCompatible(fstab)) {                                          
233         return std::make_unique<FirstStageMountVBootV2>(std::move(fstab));                                                                                                                              
234     } else {                                                                    
235         return std::make_unique<FirstStageMountVBootV1>(std::move(fstab));      
236     }                                                                           
237 }    
           

如果执行到235行,则执行FirstStageMountVBootV1的构造函数,参数是fstab;然后构造出FirstStageMountVBootV1的对象。

FirstStageMountVBootV2类的构造函数是FirstStageMountVBootV2()定义如下:

712 FirstStageMountVBootV2::FirstStageMountVBootV2(Fstab fstab)                     
713     : FirstStageMount(std::move(fstab)), avb_handle_(nullptr) {                                                                                                                                         
714     std::string device_tree_vbmeta_parts;                                                                
715     read_android_dt_file("vbmeta/parts", &device_tree_vbmeta_parts);            
716                                                                                                          
717     for (auto&& partition : Split(device_tree_vbmeta_parts, ",")) {             
718         if (!partition.empty()) {                                                                        
719             vbmeta_partitions_.emplace_back(std::move(partition));              
720         }                                                                                   
721     }                                                                                                  
722                                                                                    
723     for (const auto& entry : fstab_) {                                                
724         if (!entry.vbmeta_partition.empty()) {                                  
725             vbmeta_partitions_.emplace_back(entry.vbmeta_partition);            
726         }                                                                                           
727     }                                                                                 
728                                                                                                     
729     if (vbmeta_partitions_.empty()) {                                           
730         LOG(ERROR) << "Failed to read vbmeta partitions.";                      
731     }                                                                                  
732 }   
           

构造函数的初始化分别执行了FirstStageMount(std::move(fstab)), avb_handle_(nullptr) .

220 FirstStageMount::FirstStageMount(Fstab fstab)                                   
221     : need_dm_verity_(false), fstab_(std::move(fstab)), uevent_listener_(16 * 1024 * 1024) {
222     auto boot_devices = android::fs_mgr::GetBootDevices();                      
223     device_handler_ = std::make_unique<DeviceHandler>(                          
224             std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, std::vector<Subsystem>{},
225             std::move(boot_devices), false);                                    
226                                                                                 
227     super_partition_name_ = fs_mgr_get_super_partition_name();                                                                                                                                          
228 }  
           

显然类中fstab_成员是传进来的fstab.

从上面的分析可以看出,在create创建的FirstStageMountVBootV1对象的fstab_成员是由ReadFirstStageFstab()函数中fstab赋值的,ReadFirstStageFstab会从目录/odm/etc/或者/vendor/etc/或者根目录/中找文件fstab.,如果hardware=uml,则会找文件fstab.uml文件。

448 // Identify path to fstab file. Lookup is based on pattern fstab.<hardware>,    
449 // fstab.<hardware.platform> in folders /odm/etc, vendor/etc, or /.             
450 std::string GetFstabPath() {                                                    
451     for (const char* prop : {"hardware", "hardware.platform"}) {                
452         std::string hw;                                                         
453                                                                                 
454         if (!fs_mgr_get_boot_config(prop, &hw)) continue;                       
455                                                                                 
456         for (const char* prefix : {"/odm/etc/fstab.", "/vendor/etc/fstab.", "/fstab."}) {                                                                                                               
457             std::string fstab_path = prefix + hw;                               
458             if (access(fstab_path.c_str(), F_OK) == 0) {                        
459                 return fstab_path;                                              
460             }                                                                                            
461         }                                                                                                
462     }                                                                                                    
463                                                                                                          
464     return "";                                                                                           
465 }  
           

2.

参考

template<typename T, typename... Ts>
    std::unique_ptr<T> make_unique(Ts&&... params)
    {
        return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
    }
           

make_unique只是完美转发了它的参数给要创建的对象的构造函数,然后用new产生的原始指针来构造一个std::unique_ptr,最后返回一个创建的std::unique_ptr.