0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

システムコールのデザインを読んだらZigの標準ライブラリのデザインが分かった気がした

Posted at

例えば、POSIXにおけるread(2)は次の通りの宣言になってる。

int read(int fd, void *buf, int len);

非負整数が返る場合は読み込めたバイト数を表し、エラーの場合はマイナス1が返るとされてる。

しかし本当のカーネル呼びのコールだったらマイナス1に限らず色んな負の値で具体的なエラーを表すもんだという記述を見つけることができた。

これに対し、Zigの@import("std").io.AnyReader.read()は次のとおりである。

pub fn read(self: Self, buffer: []u8) anyerror!usize {
    return self.readFn(self.context, buffer);
}

もしもこのコードがコンパイルされたら多分bufferの引数の次にbuffer.lenが続くんだろうなという感じのアセンブリにはなりそうである。

よって、Zigの標準ライブラリの設計はシステムコールのデザインに似てて面白いと思った。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?