|Coding| C++ smart pointers: unique_ptr, shared_ptr, strong pointer(sp, Android)

In C++, smart pointers, unique_ptr and shared_ptr, are very common.
If you develop on Android, you may have seen strong pointer(sp), it is also a smart pointer.
Some notes about those pointers.

strong pointer in Android, can be saw as shared_ptr.
shared_ptr has a counter to record who is using this pointer in order to prevent dangerous pointer managements.

|Design| Synchronous v.s. Asynchronous

This article will distinguish Synchronous and Asynchronous.
I just want to say forget your Chinese ……

中文DefinitionAPI and callbackBlock
Synchronous同步While the API receiving the callback remains on the stack, you would expect that the callback had been invoked on each element.in the same threadBlock
Asynchronous非同步Callback is invoked after a function returns, or at least on another thread’s stack.
while the API receiving the callback remains on the stack, the callback may not have been called, since it’s waiting for the connection to complete.
multi-threadnon-block

Therefore, if you want to design asynchronous APIs, keep your threads safety.


這邊很常有個謬誤:
非同步,中文看起來很像是:排排隊,一個做完再做下一件事。
同步,看起來很像是:有兩個 thread,一起做事。
事實上,非常抱歉,以英文及程式的定義來說剛好反過來!


Ref.
Good article:
https://blog.ometer.com/2011/07/24/callbacks-synchronous-and-asynchronous/
Translation:
https://www.eebreakdown.com/2015/09/callback-apis.html?m=1

https://blog.huli.tw/2019/10/04/javascript-async-sync-and-callback/
https://www.ithome.com.tw/node/74544

|Code| LLVM initial

One day, if you want to implement a module on the platform x86_64 for arm64, you will need to know the tool chain, “cross compiler”.
The famous modern compiler is LLVM.

Why should we understand the compiler?
– build for multi-platform
– improve the efficiency of your code
– …..

I have a conclusion for the developer whose library is under the user space.
Use suitable optimization provided by LLVM, and make your code readable.
Dazzle coding is meaningless.

你所不知道的 C 語言:編譯器和最佳化原理篇
[共筆] 不深不淺,帶你認識 LLVM
“編譯器 LLVM 淺淺玩” by Pokai Chang

|Design| Communicate with Kernel driver

User space can not exchange data with kernel space directly.
They need to use system call.
e.g. fopen, ioctl, write, read …… etc.

In Kernel Space ……
1. Register your device with “register_chrdev”, defined in linux/fs.h .
 https://ithelp.ithome.com.tw/articles/10159749
2. Implement driver functions, ioctl, open,…
 struct file_operations fops = {
  .owner = THIS_MODULE,
  .read = device1_read,
  .write = device1_write,
  .ioctl = device1_ioctl,
  .open = device1_open,
  .release = device1_release,
 };

 int ioctl(struct inode *, struct file *, unsigned int, unsigned long);
 is in linux/ioctl.h
 http://ccckmit.wikidot.com/lk:io
 開發 driver 需要的基礎知識
 user space/kernel space 的IO觀念及實作

Copy data ……
 Kernel Space to User Space: copy_to_user()
 User Space to Kernel Space: copy_from_user()

In User Space ……
Use system call to control kernel driver.
 fopen (open)
 write
 read
 close
 seek
 poll / select
 ioctl
 mmap
 fcntl


e.g.

int main(int argc, char *argv[]){
  int devfd;
  int num = 0;

  if (argc > 1) num = atoi(argv[1]);
  if (num < 0) num = 0xff;

  devfd = open("/dev/debug", O_RDONLY);
  if (devfd == -1) {
    printf("Can't open /dev/debug\n");
    return -1;
  }

  printf("Write 0x%02x...\n", num);
  ioctl(devfd, IOCTL_WRITE, num);
  printf("Done. Wait 5 seconds...\n");
  sleep(5);
  close(devfd);

  return 0;
}