[Good Programming habits] simplify program logic with implicit Initialization

Source: Internet
Author: User

In Figure 1, the prototype of the three APIS is mprotector_init (), mprotector_fini (), and mprotector_section_add (). Assume that mprotector_section_add () will be called by multiple tasks to initialize data related to each task. However, before mprotector_section_add () is called, you must ensure that mprotector_init () it has been executed and can only be executed once. In addition, the figure shows that task A and Task B call mprotector_section_add () in their entry functions to perform corresponding operations. Now the question is, where should mprotector_init () be called? Obviously, it is not good to put the function in the entry of task A and Task B, which results in A completely different result after the initialization sequence or priority of the task changes. Another easy way to think of is to call mprotector_init () before creating tasks A and B, so that the problem is solved. Is there a better way?

Example. c
Int mprotector_init ();
Int mprotector_fini ();
Int mprotector_section_add (section_id_t _ id, maddr_t _ start, msize_t _ size );

Void task_entry_A (void * _ p_arg)
{
Maddr_t start;
Msize_t size;
...
Mprotector_section_add (SECTION_1, start, size );
...
}

Void task_entry_ B (void * _ p_arg)
{
Maddr_t start;
Msize_t size;
...
Mprotector_section_add (SECTION_2, start, size );
...
}

Figure 1

A better method is to use the implicit initialization method, which requires some changes to the mprotector_init () and mprotector_section_add () functions, as shown in figure 2. The change in mprotector_init () allows it to be called multiple times, but adds a static local variable to record whether it has been actually initialized. When mprotector_init () if this variable is changed to true, 0 is returned directly, indicating that the operation is successful. Otherwise, subsequent Initialization is required. Finally, call mprotector_section_add () to mprotector_init.

Example. c
Int mprotector_init ()
{
Static bool initialized = false;

If (initialized ){
Return 0;
}
...
Initialized = true;
Return 0;
}

Int mprotector_section_add (section_id_t _ id, maddr_t _ start, msize_t _ size)
{
If (0! = Mprotector_init ()){
Return-1;
}
...
}
Figure 2

With these changes, users who use the mprotector_section_add () function do not need to consider where to call the mprotector_init () function, which simplifies the program logic.

This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/271278

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.