楊志璿
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    FFI === 似乎蠻多人對 FFI, ABI issue 沒有深入探討 筆者也早在埋坑清單上面留下 ABI issue 預計要編寫,然而遇到另外一篇專案原作者表示需要用到其他不同語言的三方依賴庫,而他認為這部份如果不使用跟三方相依的程式語言會有開發困難,於是跟著寫三方語言。 筆者認為這部份,需要考驗程式設計師對於程式如何運作的基本功。所以先撰寫這篇 FFI 吧! ## 如何在 Python 呼叫 C++ 撰寫的程式? 眾所周知,C++ 多數情況跑得比 Python 快,很多時候在 Python domain 做運算過度耗時,我們有沒有辦法把一些 sub-routine 搬到 C++ domain 執行呢? 顯然答案是肯定的 ### binding tools 諸如 [cppyy](https://cppyy.readthedocs.io/en/latest/), [pybind11](https://github.com/pybind/pybind11), [swig](https://www.swig.org/), [ctype](https://docs.python.org/3/library/ctypes.html) 等等,不論這些工具如何闡述他們 JIT 的部分,最終都是以 run time library 的形式被呼叫。我們可以做出以下實驗: 使用 strace 追蹤 system call ```c // file name: hello.c #include <stdio.h> void hello(void) { printf("Hello, world!\n"); } ``` Compile C to shared library: ```shell cc -fPIC -shared -o libhello.so ./hello.c ``` The Python code: ```python import ctypes if __name__ == '__main__': lib = ctypes.cdll.LoadLibrary('./libhello.so') lib.hello() print('Done!') ``` 執行效果: ![](https://hackmd.io/_uploads/SJQZzG0-T.png) ![](https://hackmd.io/_uploads/rkLb6GOMT.png) ~~因為 mac 要開 dtruss 很麻煩,所以寫到一半跑去開 Linux(但其實也隔了幾天)~~ 而後我們看一下 strace: ```shell strace python ./main.py 2>&1 | less -N ``` 前面其實有一些 python 在開始執行的 library loading 這部份不用看 > 除非你跟我一樣曾經自幹 [standalone python](https://github.com/25077667/standalone-python) 需要深入了解 How Python bootstraping ``` ... 753 newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=2518, ...}, AT_EMPTY_PATH) = 0 754 read(3, "import sys\nfrom ctypes import *\n"..., 2519) = 2518 755 read(3, "", 1) = 0 756 close(3) = 0 757 openat(AT_FDCWD, "./libhello.so", O_RDONLY|O_CLOEXEC) = 3 758 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 832) = 832 759 newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=14984, ...}, AT_EMPTY_PATH) = 0 760 getcwd("/tmp/foo", 128) = 9 761 mmap(NULL, 16408, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fe3e9a3c000 762 mmap(0x7fe3e9a3d000, 4096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1000) = 0x7fe3e9a3d000 763 mmap(0x7fe3e9a3e000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7fe3e9a3e000 764 mmap(0x7fe3e9a3f000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7fe3e9a3f000 765 close(3) = 0 766 mprotect(0x7fe3e9a3f000, 4096, PROT_READ) = 0 767 newfstatat(1, "", {st_mode=S_IFIFO|0600, st_size=0, ...}, AT_EMPTY_PATH) = 0 768 write(1, "Done!\n", 6Done! 769 ) = 6 770 rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK, sa_restorer=0x7fe3e925c710}, {sa_handler=0x7fe3e9579e10, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK, sa_restorer=0x7fe3e925c710}, 8) = 0 771 munmap(0x7fe3e9ab9000, 16384) = 0 772 write(1, "Hello, world!\n", 14Hello, world! 773 ) = 14 774 exit_group(0) = ? 775 +++ exited with 0 +++ ``` 在 line 754 真正把檔案抓進來 Python interpretation,這邊可以看到接下來解析過程不需要 call syscall 而後直接在 line 757 直接打了一個 system call 說:`openat(AT_FDCWD, "./libhello.so", O_RDONLY|O_CLOEXEC) = 3` 顯然就是 open system call 的新版, system call 257 [link](https://github.com/torvalds/linux/blob/master/arch/x86/entry/syscalls/syscall_64.tbl) - 把檔案讀進來 - 取當前目錄 - 把檔案映射上記憶體 - 取得起始位置 `0x7fe3e9a3c000` - 對不同記憶體區段(偏移量)映射不同權限 - 關檔案 - 設定記憶體屬性保護 - (跳上去執行,此步驟不需要系統呼叫,因為已經在 process 當中) - 在 `libhello.so` 當中呼叫到 `write(1, "Done!\n", 6Done!)` 如此我們可以知道 Process 就是這樣透過 dynamic linking,跳上去外部程式語言的呼叫。 ## C++ 如何對接 C 語言 姑且不論讀者使用 C++ 對 C 語言 compatible part ,進而直接呼叫 C library 在 libstdc++ 的實做。 請問 C++ 如何與 C 語言進行互動? ### extern "C" 可能讀者會在 third-party library 看到這樣的語句,並且使用 macro 包起來,確定 defined \_\_cplusplus 才採用這樣的宣告。 會什麼我們會需要這樣呢? 這邊筆者留另外一篇講解 C++ 的 name mangling,讀者可自行搜尋。 而這邊 extern "C" 見:https://en.cppreference.com/w/cpp/language/language_linkage 是指我在這邊「宣告」一個需要被 linker link 進來的 declaration 跟編譯器保證,這個東西在外部存在,請交給 linker 執行,至於是 compile time 的 linking,還是 run time 的 linking (loading) 都可以,人工保證有,請 compiler 先不要亂動。 所以我們可以做到 C++ 跨程式語言到 C 語言,進行執行。 見下面筆者在學生時期,懶的寫兩份 code 所作的例子: ```cpp // stack.hpp #ifndef __STACK_HPP__ #define __STACK_HPP__ struct stack; class Stack { struct stack *my_stack; public: Stack(); ~Stack(); void push(int x); int pop(); }; #endif ``` ```cpp // stack.cpp #include "stack.h" #include "../../src/c/stack.h" Stack::Stack() { this->my_stack = ::new_stack(); } Stack::~Stack() { ::delete_stack(this->my_stack); } void Stack::push(int x) { ::push(this->my_stack, x); } int Stack::pop() { return ::pop(this->my_stack); } ``` 這邊筆者就直接呼叫 outer namespace 的 `new_stack` 與 `delete_stack` function ```c // stack.h #ifndef __STACK_H__ #define __STACK_H__ #define STACK_SIZE 100 struct stack { int sp; int stk[STACK_SIZE]; }; #ifdef __cplusplus extern "C" { #endif extern void push(struct stack *_this, int x); extern int pop(struct stack *_this); extern struct stack *new_stack(); extern void delete_stack(struct stack *stk); #ifdef __cplusplus } #endif #endif ``` 這個 C 語言定義的 function 就是上面所呼叫到的地方。 上面 stack.cpp 這個 compilation unit 就不會直接看到 C 語言當中這幾個具體的定義: ```c extern void push(struct stack *_this, int x); extern int pop(struct stack *_this); extern struct stack *new_stack(); extern void delete_stack(struct stack *stk); ``` 而後才在另外一個 stack.c 才定義這幾個 function 該如何實做 ```c /* The lisked-list of single stack */ typedef struct ll_stack { struct stack *block; struct ll_stack *next; } ll_stack_t; typedef struct pool { ll_stack_t *me; struct pool *prev; } pool_t; //... void push(struct stack *this, int x) { /* The new comming element is out of range */ if (__glibc_unlikely(this->sp > 98)) { /* Get this block's ll_stack_t */ pool_t *curr = stack_pool; while (curr && curr->me && curr->me->block != this) curr = curr->prev; /* Create a new block */ /* Must be guaranteed which could be found! */ ll_stack_t *new_ll_stack = malloc(sizeof(ll_stack_t)); new_ll_stack->block = malloc(sizeof(struct stack)); new_ll_stack->next = curr->me->next; /* Copy old stack to new allocated */ memcpy(new_ll_stack->block, this, sizeof(struct stack)); curr->me->next = new_ll_stack; /* Initialize old stack */ this->sp = -1; } this->stk[++this->sp] = x; } //... ``` :::info 突擊測驗! 1. 請指出上述 push 函式還可以如何最佳化? hint: stack_pool 2. 請修改這個 push 可以 thread-safe,也請修改讓這個 push 能夠 reentrant 3. 請解釋 this pointer。 ::: ## Break ABI to Save C++ 這邊我們留到 ABI 篇再講

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully