aboutsummaryrefslogtreecommitdiff
path: root/2025-linuxdays/pres.typ
blob: f0c87308ef25ca3092305506821eee5cda28dc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#import "@preview/polylux:0.4.0": *
#import "@preview/metropolis-polylux:0.1.0" as metropolis

#show: metropolis.setup
#show: metropolis.setup.with(footer: [Pokročilé praktiky v C])

#slide[
  #set page(header: none, footer: none, margin: 3em)

  #text(size: 1.3em)[*Pokročilé praktiky v C*]

  LinuxDays 2025

  #metropolis.divider

  #set text(size: .8em, weight: "light")
  Karel Kočí

  4.10.2025

  https://git.cynerd.cz/presentations/tree/2025-linuxdays
]

#slide[
= Svět C

Standardy: C89, C95, C99, *C11*, C17, *C23*

Kompilátory: *GCC*, *Clang*, MSVC, a další

Standardní knihovny: *GLibC*, *Musl*, BSD, Nuttx, CRT

\+ POSIX

```c
gcc -std=c23 ...
clang -std=c23 ...
```
]

#metropolis.new-section[Před C11\
  Tohle  asi používáte]

#slide[
= Bitfield
```c
  union pixel {
    struct c {
      unsigned r : 5;
      unsigned g : 6;
      unsigned b : 5;
    } c;
    uint16_t val;
  };
  union pixel p = {.c.r = 0x1b, .c.g = 0x4, .c.b = 0xa};
  printf("%.4" PRIx16 " == %" PRIx16 "\n",
   p.val, 0x1b | 0x4 << 5 | 0xa << 11);
```

```
$ gcc -std=c23 bitfield.c && ./a.out
509b == 509b
```
]

#slide[
= Struktura s veřejnou částí / Rozhraní
```c
struct pub { void (*print_name)(struct pub *); };
typedef struct pub *pub_t;
#define pub_print_name(PUB) (PUB)->print_name(PUB)

struct priv { struct pub pub; const char *name; };
void pname_priv(struct pub *pub) {
  struct priv *priv = (struct priv *)pub;
  printf("%s", priv->name);
}
int main(int argc, char *argv[]) {
  struct priv priv = {.pub = {pname_priv}, .name = "FOO"};
  pub_t p = &priv.pub;
  pub_print_name(p);
  putchar('\n');
```
]

#slide[
= Struktura s veřejnou částí / Rozhraní
```c
struct wrap { struct pub *subpub; struct pub pub; };
void pname_wrap(struct pub *pub) {
  struct wrap *wrap = (void *)pub - offsetof(struct wrap, pub);
  printf("Wrap: ");
  wrap->subpub->print_name(wrap->subpub);
}
int main(int argc, char *argv[]) {
  ...
  struct wrap wrap = {.pub = {pname_wrap}, .subpub = &priv.pub};
  wrap.pub.print_name(&wrap.pub);
  putchar('\n');
}
```
]

#slide[
= Double-pointer na funkci / Generická funkce
```c
typedef void (*cb_t)(void *, int);
typedef cb_t *scb_t;
struct cb { cb_t cb; const char *str; };
void cb(void *self, int v) {
  struct cb *s = self;
  printf("%s: %d\n", s->str, v);
}
#define callcb(CB, ...) (*(CB))(CB, __VA_ARGS__)
  struct cb c = {.cb = cb, .str = "Hello"};
  scb_t sc = &c.cb;
  callcb(sc, 42);
```

```
$ gcc -std=c23 funcptr.c && ./a.out
Hello: 42
```
]

#metropolis.new-section["Novinky" v C]

#slide[
= Anonymní struct a union (*C11*)
```c
  struct point {
    union {
      struct {
        uint32_t x, y;
      };
      uint64_t u64;
    };
  };

  struct point a = {.x = 0x56, .y = 0x38};
  printf("%.16" PRIx64 "\n", a.u64);
```
]

#slide[
= Static assert (*C11*)

- *C11*: `_Static_assert`
- *C23*: `static_assert`

```c
  static_assert(sizeof(intmax_t) >= 16,
    "Platform must support 64bit integers")
```

```
$ gcc -std=c23 static_assert.c
static_assert.c:4:1: error: static assertion failed: "Platform must support 128bit integers"
    4 | static_assert(sizeof(intmax_t) >= 16, "Platform must support 128bit integers");
      | ^~~~~~~~~~~~~
```
]

#slide[
= Atomic (*C11*)

```c
  #include <stdatomic.h>
  _Atomic _Bool a;                          atomic_bool a;
  _Atomic int val;                          atomic_int val;
  _Atomic struct foo *foo;
  _Bool aa = atomic_load(&a);               aa = a;
  atomic_store(&a, false);                  a = false;

  #if (ATOMIC_BOOL_LOCK_FREE, 2)
  // atomic_bool je vždy atomický bez zámku
  #elif (ATOMIC_BOOL_LOCK_FREE, 1)
  // atomic_bool někdy musí použít zámek
  #else
  // atomic_bool vždy použije zámek
  #endif
```
]

#slide[
= Atomické operace (*C11*)

```c
  _Bool was = atomic_exchange(&a, false);
  if (!atomic_compare_exchange_strong(&a, &was, true))
    printf("Somebody changed it in the meantime\n");

  int iwas = atomic_fetch_add(&val, 1);
  iwas = atomic_fetch_sub(&val, 1);
  iwas = atomic_fetch_or(&val, 0xf);
  iwas = atomic_fetch_xor(&val, 0x10);
  iwas = atomic_fetch_and(&val, 0xff);
```
]

#slide[
= Atomický příznak (*C11*)

```c
  atomic_flag event = ATOMIC_FLAG_INIT;
  _Bool was = atomic_flag_test_and_set(&event);
  atomic_flag_clear(&event);
```
]

#slide[
= Vlákna a další (*C11*)

- `<threads.h>`
- `thrd_t`, `mtx_t`, `cnd_t`
- `thread_local` (`_Thread_local`) a `tss_t`
]

#slide[
= Detekce přetečení a podtečení (*C23*)

```c
  int8_t add, sub, mul;
  assert(ckd_add(&add, 50, 85));
  assert(ckd_sub(&sub, 10, 150));
  assert(ckd_mul(&mul, 68, 2));
```

Pozor: `true` = nevalidní výsledek
]

#slide[
= Bitové operace (*C23*)

```c
  #include <stdbit.h>
  assert(stdc_leading_zeros(0xffU) == 24);
  assert(stdc_leading_ones(0xff000000U) == 8);
  assert(stdc_trailing_zeros(0xff000000U) == 24);
  assert(stdc_trailing_ones(0xffU) == 8);
  assert(stdc_first_leading_zero(0xff000000U) == 9);
  assert(stdc_first_leading_one(0xffU) == 25);
  assert(stdc_first_trailing_zero(0xffU) == 9);
  assert(stdc_first_trailing_one(0xff000000U) == 25);
  assert(stdc_count_zeros(0xffU) == 24);
  assert(stdc_count_ones(0xffU) == 8);
  assert(stdc_bit_width(0xf0U) == 8);
  assert(stdc_bit_floor(0xf1U) == 0x80);
  assert(stdc_bit_ceil(0xf1U) == 0x100);
```
]

#slide[
= \_Generic (*C11*)
```c
#define traceval(X) \
  fprintf(stderr, _Generic((X), \
    char *: "%s\n", int: "%d\n", void *: "%p\n"), X)

  traceval("Hello");
  traceval(42);
  traceval(NULL);
```

```
$ gcc -std=c23 generic.c && ./a.out
Hello
42
(nil)
```

Pozor: Všechny varianty musí být kompilovatelné.\
Každá varianta generuje errory i warningy!
]

#slide[
= Attributy (*C23*)

Bylo ```c __attribute__(...)``` a nyní ```c [[...]]```.
]

#metropolis.new-section[Nestandardní C\
  C podle kompilátoru]

#slide[
= Warning - switch

```c
int val = fgetc(stdin);
switch (val) {
  case ' ': printf("Secret space.... ");
  default: printf("Got: %c\n", val);
}
```
```
$ gcc -Wimplicit-fallthrough -Wall -std=c23 warn-switch.c && ./a.out
warn-switch.c: In function ‘main’:
warn-switch.c:7:5: warning: this statement may fall through [-Wimplicit-fallthrough=]
    7 |     printf("Secret space.. ");
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
warn-switch.c:8:3: note: here
    8 |   default:
      |   ^~~~~~~
```
]

#slide[
= Warning - switch

```c
int val = fgetc(stdin);
switch (val) {
  case ' ':
    printf("Secret space.... ");
    [[gnu::fallthrough]];
  default:
    printf("Got: %c\n", val);
}
```
]

#slide[
= Warningy

- ```c [[gnu::fallthrough]]```
- ```c [[gnu::nonstring]]```
- ```c [[gnu::unused]]```
]

#slide[
= nonnull
```c
[[gnu::nonnull]] // [[gnu::nonnull(1)]]
void prnt(char *str) { printf("%s\n", str); }
  prnt("Some text");
  prnt(NULL);
```

```
$ gcc -std=c23 nonnull.c && ./a.out
nonnull.c: In function ‘main’:
nonnull.c:10:3: warning: argument 1 null where non-null expected [-Wnonnull]
   10 |   prnt(NULL);
      |   ^~~~
nonnull.c:4:6: note: in a call to function ‘prnt’ declared ‘nonnull’
    4 | void prnt(char *str) {
      |      ^~~~
Some text
(null)
```
]

#slide[
= Další kontroly

- ```c [[clang::counted_by]]```, ```c [[gnu::counted_by]]```
- ```c [[gnu::malloc]]```
- ```c [[gnu::alloc_size]]```
- ```c [[gnu::fd_arg]]```, ```c [[gnu::fd_arg_read]]```, ```c [[gnu:fd_arg_write]]```
- ```c [[gnu:: format]]```, ```c [[gnu:: format_arg]]]```

https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
https://clang.llvm.org/docs/AttributeReference.html
]

#slide[
= Automatické uvolnění z heapy
```c
static void _free(char **ptr) {
  printf("Freeing %p=%p\n", ptr, *ptr);
  free(*ptr);
}
  [[gnu::cleanup(_free)]] char *buf = malloc(18);
  [[gnu::cleanup(_free)]] char *null = NULL;
  printf("Pointers: %p=%p %p=%p\n", &buf, buf, &null, null);
```

```
$ gcc -std=c23 cleanup.c && ./a.out
Pointers: 0x7ffffffdef68=0x4052a0 0x7ffffffdef70=(nil)
Freeing 0x7ffffffdef70=(nil)
Freeing 0x7ffffffdef68=0x4052a0
```
]

#slide[
= Weak
```c
[[gnu::weak]]
void weakfun(void) {
  printf("Weak function\n");
}
  weakfun();

void weakfun(void) { printf("Standard function\n"); }
```

```
$ gcc -std=c23 weak.c && ./a.out
Weak function
$ gcc -std=c23 weak.c weakfun.c && ./a.out
Standard function
```
]

#slide[
= Transparent union
```c
union [[gnu::transparent_union]] un {
  void *ptr;
  intptr_t i;
};
void pasi(union un v) { printf("%p\n", v.ptr); }
  pasi((intptr_t)0x42);
  pasi((void *)&pasi);
```

```
$ gcc -std=c23 transunion.c && ./a.out
0x42
0x401150
```
]

#slide[
= Transparent union callback
```c
union [[gnu::transparent_union]] un {
  char *ptr;
  intptr_t i;
};
void call(void (*cb)(union un)) { cb("FOO"); }
void cb(char *str) { printf("%s\n", str); }
  call(cb);
```

```
$ gcc -std=c23 transunionf.c && ./a.out
FOO
```
]

#slide[
= Constructor a Desctructor
```c
[[gnu::constructor]]
static void _our_constructor(void) { printf("Constructor\n"); }
[[gnu::destructor]]
static void _our_destructor(void) { printf("Destructor\n"); }
int main(int argc, char *argv[]) {
  printf("main\n");
}
```

```
$ gcc -std=c23 constructor.c && ./a.out
Constructor
main
Destructor
```
]

#slide[
= Switch range (GCC, Clang, ...)
```c
  int val = getchar();
  switch (val) {
  case '0' ... '9':
    printf("Number\n");
  break;
  default:
    printf("Not a number\n");
    break;
  }
```
]

#slide[
  #show: metropolis.focus
  Děkuji za pozornost

  Karel Kočí

  #metropolis.divider

  #text(size: 0.7em)[
    https://git.cynerd.cz

    https://gitlab.com/cynerd

    https://git.cynerd.cz/presentations/tree/2025-linuxdays
  ]
]