天天看點

【程式設計好習慣】借助隐式初始化簡化程式邏輯

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)

   mprotector_section_add (SECTION_2, start, size);

圖1

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;

圖2

繼續閱讀