Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement lightweight pointers in Android system

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how the lightweight pointer is realized in the Android system". In the daily operation, I believe that many people have doubts about how to realize the lightweight pointer in the Android system. The editor consulted all kinds of materials and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the question of "how to realize the lightweight pointer in the Android system". Next, please follow the editor to study!

Smart pointer source

Pointer errors are often caused by the following situations:

1. Applied for memory space, but forgot to release the memory space occupied by the object pointed to by the pointer.

two。 An invalid pointer was used.

Therefore, the smart pointer technology is used in the C++ code part of android. Smart pointers use a technique that automatically harms object reference counting. To solve the defect of the pointer in C++.

Three types of C++ smart pointers are provided in the android system: lightweight smart pointers, strong pointers and weak pointers.

The following is mainly by analyzing the implementation principle of lightweight pointers.

Lightweight pointer

Lightweight pointers maintain the life cycle of an object by using a simple reference count class. If an object of a class supports the use of lightweight pointers, it must inherit from the LightRefBase class, because this LightRefBase class provides a simple reference counter.

LightRefBase class definition

The following source code is mainly based on the android5.0 source code analysis. The definition of the LightRefBase class is in the file\ frameworks\ rs\ cpp\ util\ RefBase.h on the android system.

The LightRefBase class is also a template class. The template parameter T represents the actual type of the object, and it must inherit from the class LightRefBase.

/ / template class template class LightRefBase {public: / / Common inline function inline LightRefBase (): mCount (0) {} inline void incStrong (_ _ attribute__ (unused)) const void* id) const {_ _ sync_fetch_and_add (& mCount, 1) } inline void decStrong (_ _ attribute__ ((unused)) const void* id) const {if (_ _ sync_fetch_and_sub (& mCount, 1) = = 1) {delete static_cast (this);}} / /! DEBUGGING ONLY: Get current strong ref count. Inline int32_t getStrongCount () const {return mCount;} typedef LightRefBase basetype;protected: / / inline fictional function inline ~ LightRefBase () {} private: friend class ReferenceMover; inline static void moveReferences (void*, void const*, size_t, const ReferenceConverterBase&) {} private: mutable volatile int32_t mCount;}

The above LightRefBase class definition involves several knowledge points:

1. There are three access rights for C++: public, protected and private.

Public: can be accessed by any entity.

Protected: only subclasses and member functions of this class are allowed to access.

Private: only member functions of this class are allowed to access.

2. Inline inline functions in C++

If a function with the inline keyword is included in the first part of the function, then the function is represented as an inline function. The main purpose of inline functions is to solve the problem that some frequently called small functions consume a lot of stack space (stack memory).

The use of inline is limited. Inline is only suitable for simple culvert numbers in the body, and cannot contain complex structural control statements such as while and switch, and the inline function itself cannot be a direct recursive function (recursive function: you also call your own function internally).

3. Friend friend function in C++

The friend mechanism in C++ allows non-public members of a class to be accessed by a class or function. Friends are divided into three types according to types: ordinary non-class member functions as friends, class member functions as friends, and classes as friends.

A friend function is a non-member function that can directly access private members of a class. It is an ordinary function defined outside the class, it does not belong to any class, but needs to be declared in the definition of the class, just add the keyword friend to the name of the friend.

4. Mutable keyword in C++

Mutable is set up to break through the limitations of const. Variables modified by mutable will always be in a mutable state, even in a const function.

5. Template class templates in C++

A class template (also known as a generic class or class-generated class) allows the user to define a pattern for the class. It enables some data members in the class, the parameters of the dictation member functions, and the return values of some member functions to take arbitrary types (including those defined in advance by the system and defined by the user).

Application scenario of class template: multiple classes have common operations, but the data types are different.

Implementation class of LightRefBase

The implementation class of the lightweight LightRefBase class is the wp class, which is also in the file\ frameworks\ rs\ cpp\ util\ RefBase.h. Wp is also a template class, and the template parameter T represents the actual type of the object, which must inherit the LightRefBase class. Since the wp class is also an implementation class for strong pointers, we only need to analyze the implementation of the lightweight pointer class in the wp class.

/ / template class template class wp {public: typedef typename RefBase::weakref_type weakref_type; / / Constructor inline wp (): m_ptr (0) {} wp (T* other); wp (const wp& other); wp (const sp& other); template wp (U* other); template wp (const sp& other); template wp (const wp& other) / / fictional functions to be used for lightweight pointers ~ wp (); / / Assignment wp& operator = (T* other); wp& operator = (const wp& other); wp& operator = (const sp& other); template wp& operator = (U * other); template wp& operator = (const wp& other); template wp& operator = (const sp& other); void set_object_and_refs (T * other, weakref_type* refs) / / promotion to sp sp promote () const; / / Reset void clear (); / / Accessors inline weakref_type* get_refs () const {return masked refs;} inline T* unsafe_get () const {return mrefs;} / / Operators COMPARE_WEAK (= =) COMPARE_WEAK (! =) COMPARE_WEAK (>) COMPARE_WEAK ((const wp& o) const {return (m_ptr = = o.m_ptr)? (m_refs > o.m_refs): (m_ptr > o.m_ptr);} template inline bool operator > (const wp& o) const {return (m_ptr = = o.m_ptr)? (m_refs > o.m_refs): (m_ptr > o.m_ptr);} inline bool operator

< (const wp& o) const { return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); } template inline bool operator < (const wp& o) const { return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); } inline bool operator != (const wp& o) const { return m_refs != o.m_refs; } template inline bool operator != (const wp& o) const { return !operator == (o); } inline bool operator (o); } template inline bool operator (o); } inline bool operator >

= (const wp& o) const {return! operator

< (o); } template inline bool operator >

= (const wp& o) const {return! operator < (o);} private: template friend class sp; template friend class wp; / / lightweight pointers will use the variable m_ptr T* mpointer; weakref_type* masked refs;}; at this point, the study of "how lightweight pointers are implemented in the Android system" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How to integrate Shiro into access Control Module in Spring

    This article mainly introduces Spring how to integrate Shiro to do access control module, the article is very detailed, has a certain reference value, interested friends must read it! 1. Introduce Maven dependency of Shiro

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report