1.. include:: ../disclaimer-zh_CN.rst 2 3:Original: Documentation/process/coding-style.rst 4 5.. _cn_codingstyle: 6 7:������: 8 - ������ Zhang Le <r0bertz@gentoo.org> 9 - Andy Deng <theandy.deng@gmail.com> 10 - ��������� <bobwxc@email.cn> 11 12:������: 13 - ������ Wang Cong <xiyou.wangcong@gmail.com> 14 - wheelz <kernel.zeng@gmail.com> 15 - ��������� Xudong Guan <xudong.guan@gmail.com> 16 - Li Zefan <lizf@cn.fujitsu.com> 17 - Wang Chen <wangchen@cn.fujitsu.com> 18 19Linux ������������������ 20================== 21 22��������������������������������������� linux ��������������������������������������������������������������� 23������������������������������������������������������������������������������������������������������������������ 24��������������������������������������������������������������������� (���������������) ������������������������ 25������������������ 26 27��������������������������������� GNU ��������������������������������������������������������������������������� 28��������������������� 29 30������������������������������������ 31 32 331) ������ 34------- 35 36������������ 8 ������������������������������ 8 ��������������������������������������������������� 4 (������ 372���) ��������������������������������������������������������������� 3��� 38 39������������������������������������������������������������������������������������������������������������������ 40������������������ 20 ��������������������������������������������������������������������������������� 41 42��������������������������� 8 ������������������������������������������������������������ 80 ������������������ 43��������������������������������������������������������������������������������� 3 ������������������������������ 44������������������������������������������������������������������������ 45 46���������������8 ������������������������������������������������������������������������������������������������ 47������������������������������������������������������ 48 49��� switch ��������������������������������������������������� ``switch`` ������������������ ``case`` 50������������������������������������ ``������������`` ``case`` ������������������ 51 52.. code-block:: c 53 54 switch (suffix) { 55 case 'G': 56 case 'g': 57 mem <<= 30; 58 break; 59 case 'M': 60 case 'm': 61 mem <<= 20; 62 break; 63 case 'K': 64 case 'k': 65 mem <<= 10; 66 fallthrough; 67 default: 68 break; 69 } 70 71��������������������������������������������������������������������������� 72 73.. code-block:: c 74 75 if (condition) do_this; 76 do_something_everytime; 77 78��������������������������������������������� 79 80.. code-block:: c 81 82 if (condition) 83 do_this(), do_that(); 84 85��������������������������������� 86 87.. code-block:: c 88 89 if (condition) { 90 do_this(); 91 do_that(); 92 } 93 94������������������������������������������������������������������������������������������������������������������ 95��������������� 96 97������������������������ Kconfig ������������������������������������������������������������������������������ 98������ 99 100��������������������������������������������������������� 101 102 1032) ������������������������������ 104----------------------- 105 106������������������������������������������������������������������������������������������������������ 107 108������������������������������ 80 ������������������������������������������������ 109 110������ 80 ��������������������������������������������������������� 80 ��������������������������������������� 111������������������ 112 113������������������������������������������������������������������������������������������������������������������������ 114 115��������������������������������������������������������� 116 117��������������������������������������������������������������� printk ������������������������ 118��������������� grep��� 119 120 1213) ��������������������������� 122--------------------- 123 124C ��������������������������������������������������������������������������������������������������������������� 125��������������������������������������������������������������������������� Kernighan ��� Ritchie ������ 126������������������������������������������������������������������������������������������������ 127 128.. code-block:: c 129 130 if (x is true) { 131 we do y 132 } 133 134��������������������������������������� (if, switch, for, while, do)������������ 135 136.. code-block:: c 137 138 switch (action) { 139 case KOBJ_ADD: 140 return "add"; 141 case KOBJ_REMOVE: 142 return "remove"; 143 case KOBJ_CHANGE: 144 return "change"; 145 default: 146 return NULL; 147 } 148 149������������������������������������������������������������������������������������������������������������ 150 151.. code-block:: c 152 153 int function(int x) 154 { 155 body of function 156 } 157 158��������������������������������������������������������������������������������������������������������������� 159��������� (a) K&R ��� **���������** ������ (b) K&R ��������������������������������������������������� 160������ (C ������������������������)��� 161 162��������������������������������������������������������������������������������������������������������� do ��� 163��������� ``while`` ������ if ������������ ``else`` ��������������� 164 165.. code-block:: c 166 167 do { 168 body of do-loop 169 } while (condition); 170 171��� 172 173.. code-block:: c 174 175 if (x == y) { 176 .. 177 } else if (x > y) { 178 ... 179 } else { 180 .... 181 } 182 183���������K&R��� 184 185������������������������������������������������������ (���������������������) ��������������������������������� 186��������������������������������������������������������������������������� (������ 25 ������������������)������ 187������������������������������������������ 188 189��������������������������������������������������������������������������� 190 191.. code-block:: c 192 193 if (condition) 194 action(); 195 196��� 197 198.. code-block:: c 199 200 if (condition) 201 do_this(); 202 else 203 do_that(); 204 205������������������������������������������������������������������������������������������������������������ 206 207.. code-block:: c 208 209 if (condition) { 210 do_this(); 211 do_that(); 212 } else { 213 otherwise(); 214 } 215 2163.1) ������ 217********* 218 219Linux ��������������������������� (������) ���������������������������������������������(���������) ��������� 220������������������������������������������������ sizeof, typeof, alignof ��� __attribute__������ 221������������������������������������������������ (��������� Linux ��������������������������������������������� 222��� C ��������������������������������������������� ``struct fileinfo info;`` ��������������� 223``sizeof info``)��� 224 225���������������������������������������������:: 226 227 if, switch, case, for, do, while 228 229��������������� sizeof, typeof, alignof ������ __attribute__ ��������������������������������� 230��������� 231 232.. code-block:: c 233 234 s = sizeof(struct file); 235 236��������������������������������������������������������������� **������** ��� 237 238.. code-block:: c 239 240 s = sizeof( struct file ); 241 242������������������������������������������������������������ ``*`` ��������������������������������������������� 243������������������������������������������������������ 244 245.. code-block:: c 246 247 char *linux_banner; 248 unsigned long long memparse(char *ptr, char **retptr); 249 char *match_strdup(substring_t *s); 250 251������������������������������������������������������������������������������������������������:: 252 253 = + - < > * / % | & ^ <= >= == != ? : 254 255���������������������������������������:: 256 257 & * + - ~ ! sizeof typeof alignof __attribute__ defined 258 259���������������������������������������������������:: 260 261 ++ -- 262 263���������������������������������������������������:: 264 265 ++ -- 266 267``.`` ��� ``->`` ��������������������������������������������� 268 269������������������������������������������������������������������������������������������������������������������ 270������������������������������������������������������������������������������������������������������������������ 271������������������������������������������������������������������������������������������������������������������ 272������������������ 273 274��� git ��������������������������������������������������������������������������������������������������������� 275������������������������������������������������������������������������������������������������������������������ 276������������ 277 278 2794) ������ 280------- 281 282C ������������������������������������������������������������ Modula-2 ��� Pascal ������������������ 283C ������������������������ ThisVariableIsATemporaryCounter ������������������������C ������������ 284������������������ ``tmp`` ������������������������������������������������������������������������ 285 286������������������������������������������������������������������������������������������������������������������ 287��������������������������������� ``foo`` ��������������������������������� 288 289������������ (������������ **������** ������������������������������) ������������������������������������������ 290��������������������������������������������������������������������������������������������� 291``count_active_users()`` ������������������������������������������ ``cntuser()`` ��� 292 293��������������������������������� (���������������������������) ��������������������������������������������������� 294������������������������������������������������������������������������������ 295 296������������������������������������������������������������������������������������������������������������������ 297��������������������������� ``i`` ��������� ``loop_counter`` ������������������������������������������ 298��������������������������� ``tmp`` ������������������������������������������������ 299 300������������������������������������������������������������������������������������������������������������������ 301������������������������ (������)��� 302 303���������������������������������������������������master/slave���������������������master���������slave������ 304������blacklist/whitelist������ 305 306���master/slave��������������������� 307 '{primary,main} / {secondary,replica,subordinate}' 308 '{initiator,requester} / {target,responder}' 309 '{controller,host} / {device,worker,proxy}' 310 'leader/follower' 311 'director/performer' 312 313���blacklist/whitelist��������������������� 314 'denylist/allowlist' 315 'blocklist/passlist' 316 317������������������������������������������������������ABI/API���������������������������2020��������������� 318��������������������������������������������������������������������������������������������������������������� 319��������������� 320 321.. warning:: 322 ��������������������������������������������������������������������������������������� 323 3245) Typedef 325---------- 326 327������������������ ``vps_t`` ������������������ 328 329��������������������������� typedef ��������� **������** ������������������������������ 330 331.. code-block:: c 332 333 vps_t a; 334 335��������������������������� 336 337������������������������ 338 339.. code-block:: c 340 341 struct virtual_container *a; 342 343������������ ``a`` ��������������� 344 345��������������� typedef ``������������������`` ��������������������������������������������������������������� 346 347 (a) ������������������������ (������������������������������ typedef ��� **������** ��������������������� 348 ���������)��� 349 350 ��������� ``pte_t`` ������������������������������������������������������������������������ 351 352 .. note:: 353 354 ������������������������������������������������������������������ pte_t ��������������������������� 355 ��������������������������������������������������� 356 357 (b) ������������������������������������������������������ **������** ��������������� ``int`` ������ 358 ``long`` ������������ 359 360 u8/u16/u32 ������������������������ typedef������������������������������ (d) ������������������ 361 362 .. note:: 363 364 ��������������������������������������������������������� ``unsigned long`` ��������������������� 365 366 typedef unsigned long myflags_t; 367 368 ������������������������������������������������������������������������������������ ``unsigned int`` 369 ������������������������������ ``unsigned long`` ������������������������������������������ 370 typedef��� 371 372 (c) ������������ sparse ������������������������ **���** ������������������������������������ 373 374 (d) ��������� C99 ������������������������������������������������������ 375 376 ��������������������������������������������������������� ``uint32_t`` ������������������������������ 377 ��������������������������������������� 378 379 ���������Linux ��������������������������������� ``u8/u16/u32/u64`` ��������������������������� 380 ������������������������������������������������������������������������������������������������������ 381 382 ������������������������������������������������������������������������������������������������������������ 383 ������ 384 385 (e) ��������������������������������������������� 386 387 ��������������������������������������������������������������� C99 ������������������������������������ 388 ``u32`` ��������������������������������������������������������������������������� __u32 ��������� 389 ������������ 390 391������������������������������������������������������ **������������** ������ typedef��������������������� 392��������������������������������������������� 393 394������������������������������������������������������������������������������������������������������������������ 395��������������������� typedef��� 396 397 3986) ������ 399------- 400 401��������������������������������������������������������������������������������������������������������� (������ 402��������� ISO/ANSI ��������������� 80x24)��������������������������������������������� 403 404������������������������������������������������������������������������������������������������������������������ 405������������������������������������ (������������) ��� case ������������������������������������������ case 406��������������������������������������������������������������������������������� 407 408������������������������������������������������������������������������������������������������������������������ 409������������������������������������������������������������������������������������������������������������������ 410������������������������������������ (��������������������������������������������������������������������������� 411���������������������������������������������������������������������������) 412 413��������������������������������������������������������������������������������� 5���10 ������������������������ 414������������������������������������������������������������������������������������������������������������������ 415��������������� 7 ������������������������������������������������������������������������������������������������ 416������������������ 2 ������������������������������ 417 418��������������������������������������������������������������������������������������������� **EXPORT** ��� 419������������������������������������������������������ 420 421.. code-block:: c 422 423 int system_is_up(void) 424 { 425 return system_state == SYSTEM_RUNNING; 426 } 427 EXPORT_SYMBOL(system_is_up); 428 4296.1) ������������ 430************* 431 432������������������������������������������������������������������ C ��������������������������������������� 433Linux ������������������������������������������������������������������������������������������������������ 434 435������������������������������ ``extern`` ������������������������������������������������������������������ 436������������ 437 438������������������������������ `������������������ <https://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com/>`_ ��� 439������������������������:: 440 441 __init void * __must_check action(enum magic value, size_t size, u8 count, 442 char *fmt, ...) __printf(4, 5) __malloc; 443 444��������������������������������������� 445 446- ������������������������ ``static __always_inline`` ��������� ``__always_inline`` 447 ��������������������������������������� ``inline`` ��� 448- ������������������������������ ``__init`` ������������������������������ ``__cold`` ��� 449- ������������������������ ``void *`` ��� 450- ������������������������������ ``__must_check`` ��� 451- ��������������������� ``action`` ��� 452- ������������������������ ``(enum magic value, size_t size, u8 count, char *fmt, ...)`` ��� 453 ������������������������������ 454- ������������������������������ ``__printf(4, 5)`` ��� 455- ������������������������������ ``__malloc`` ��� 456 457������������������������ **������** ��������������������������������������������������������������������������� 458��������������������������������������������������������������������������������������������������� **������** 459��������������������������������� ``__printf(4, 5)`` ���������������������������:: 460 461 static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value, 462 size_t size, u8 count, char *fmt, ...) __malloc 463 { 464 ... 465 } 466 4677) ��������������������������� 468--------------------- 469 470��������������������������������������������� goto ������������������������������������������������������������ 471��������������������������������� 472 473���������������������������������������������������������������������������������������������goto ��������������� 474������������������������������������������������������ return ��������� 475 476������������������������ goto ��������������������������������������������� goto ��������� ``buffer``, 477������������������������������ ``out_free_buffer:`` ������������������ ``err1:`` ��� ``err2:`` 478���������GW_BASIC ������������������������������������������ (���������) ������������������������������������ 479��������������������������������������������������� 480 481������ goto ��������������� 482 483- ������������������������������������ 484- ������������������ 485- ������������������������������������������������������������������������ 486- ��������������������������������������������� ;) 487 488.. code-block:: c 489 490 int fun(int a) 491 { 492 int result = 0; 493 char *buffer; 494 495 buffer = kmalloc(SIZE, GFP_KERNEL); 496 if (!buffer) 497 return -ENOMEM; 498 499 if (condition1) { 500 while (loop1) { 501 ... 502 } 503 result = 1; 504 goto out_free_buffer; 505 } 506 ... 507 out_free_buffer: 508 kfree(buffer); 509 return result; 510 } 511 512������������������������������������ ``��� err ������`` ������������������ 513 514.. code-block:: c 515 516 err: 517 kfree(foo->bar); 518 kfree(foo); 519 return ret; 520 521��������������������������������������������������� ``foo`` ��� NULL��������������������������������������� 522��������������������� ``err_free_bar:`` ��� ``err_free_foo:`` ������������������������ 523 524.. code-block:: c 525 526 err_free_bar: 527 kfree(foo->bar); 528 err_free_foo: 529 kfree(foo); 530 return ret; 531 532��������������������������������������������������������������������� 533 534 5358) ������ 536------- 537 538������������������������������������������������������������������������������������������������������������������ 539������������������������������������������������������������������������������������������������������������ 540 541��������������������������������������������������������������������������������������������������������� 542������������������������������������������������������������������������������������������������������������������ 543��������������������������������������������������������������������������������������������� (������������) ��� 544������������������������������������������������������������������������������������������������������������������ 545��������������������������������������������� 546 547��������������� API ��������������������� kernel-doc ��������������� 548Documentation/translations/zh_CN/doc-guide/index.rst ��� scripts/kernel-doc ��� 549 550��� (������) ��������������������������� 551 552.. code-block:: c 553 554 /* 555 * This is the preferred style for multi-line 556 * comments in the Linux kernel source code. 557 * Please use it consistently. 558 * 559 * Description: A column of asterisks on the left side, 560 * with beginning and ending almost-blank lines. 561 */ 562 563��������� net/ ��� drivers/net/ ������������������������ (������) ��������������������������� 564 565.. code-block:: c 566 567 /* The preferred comment style for files in net/ and drivers/net 568 * looks like this. 569 * 570 * It is nearly the same as the generally preferred comment style, 571 * but there is no initial almost-blank line. 572 */ 573 574������������������������������������������������������������������������������������������������������������������ 575������������������������ (���������������������������������������������)������������������������������������������ 576������������������������������������������������ 577 578 5799) ��������������������������� 580--------------------- 581 582��������������������������������������������������������� Unix ������������������������ 583``GNU emacs`` ������������������������ C ������������������������������������������������������������������ 584��������������������������������������������������� (��������������������������������������������������������������� 585��� GNU emacs ���������������������������������������������) 586*(���������Infinite Monkey Theorem)* 587 588��������������������� GNU emacs������������������������������������������������������������������������������ 589��������������������������������������� .emacs ������������ 590 591.. code-block:: elisp 592 593 (defun c-lineup-arglist-tabs-only (ignored) 594 "Line up argument lists by tabs, not spaces" 595 (let* ((anchor (c-langelem-pos c-syntactic-element)) 596 (column (c-langelem-2nd-pos c-syntactic-element)) 597 (offset (- (1+ column) anchor)) 598 (steps (floor offset c-basic-offset))) 599 (* (max steps 1) 600 c-basic-offset))) 601 602 (dir-locals-set-class-variables 603 'linux-kernel 604 '((c-mode . ( 605 (c-basic-offset . 8) 606 (c-label-minimum-indentation . 0) 607 (c-offsets-alist . ( 608 (arglist-close . c-lineup-arglist-tabs-only) 609 (arglist-cont-nonempty . 610 (c-lineup-gcc-asm-reg c-lineup-arglist-tabs-only)) 611 (arglist-intro . +) 612 (brace-list-intro . +) 613 (c . c-lineup-C-comments) 614 (case-label . 0) 615 (comment-intro . c-lineup-comment) 616 (cpp-define-intro . +) 617 (cpp-macro . -1000) 618 (cpp-macro-cont . +) 619 (defun-block-intro . +) 620 (else-clause . 0) 621 (func-decl-cont . +) 622 (inclass . +) 623 (inher-cont . c-lineup-multi-inher) 624 (knr-argdecl-intro . 0) 625 (label . -1000) 626 (statement . 0) 627 (statement-block-intro . +) 628 (statement-case-intro . +) 629 (statement-cont . +) 630 (substatement . +) 631 )) 632 (indent-tabs-mode . t) 633 (show-trailing-whitespace . t) 634 )))) 635 636 (dir-locals-set-directory-class 637 (expand-file-name "~/src/linux-trees") 638 'linux-kernel) 639 640��������� emacs ��� ``~/src/linux-trees`` ������ C ��������������������������������������������� 641 642������������������������ emacs ��������������������������������������������������������������������������������� 643������ ``indent`` ��� 644 645���������GNU indent ��������� GNU emacs ��������������������������������������������������������������� 646������������������������������������������������������ GNU indent ������������������ K&R ������������ 647(GNU ���������������������������������������������������������������������������)��������������������� indent 648������������ ``-kr -i8`` (������ ``K&R���8 ������������``)������������ ``scripts/Lindent`` 649������������������������������������������������������ 650 651``indent`` ��������������������������������������������������������������������������������������������������� 652��������������� ``indent`` ��������������������������������� 653 654������������������������������ ``clang-format`` ��������������������������������������������������������� 655������������������������������������������������������������������������������������������������������������������ 656������������������ ``#include`` ���������������/������������������������������������������ 657������ Documentation/process/clang-format.rst ��� 658 659 66010) Kconfig ������������ 661-------------------- 662 663������������������������������ Kconfig* ��������������������������������������������������������������� 664``config`` ������������������������������������������������ help ������������������������������ 2 ������ 665������������������:: 666 667 config AUDIT 668 bool "Auditing support" 669 depends on NET 670 help 671 Enable auditing infrastructure that can be used with another 672 kernel subsystem, such as SELinux (which requires this for 673 logging of avc messages output). Does not do system-call 674 auditing without CONFIG_AUDITSYSCALL. 675 676������������������������ (������������������������������������) ������������������������������������������������ 677������������:: 678 679 config ADFS_FS_RW 680 bool "ADFS write support (DANGEROUS)" 681 depends on ADFS_FS 682 ... 683 684��������������������������������������������� Documentation/kbuild/kconfig-language.rst ��� 685 686 68711) ������������ 688------------ 689 690������������������������������������������������������������������������������������������������������������������ 691������������������������������������������ (���������������������������������������������������)������������������ 692������������������������������������������������������������ 693 694��������������������������������������������������������������������������������������������������������������������� 695������������������������������������������������������������������������������������������������������������������ 696������������������������������������ 697 698������������ **������** ������������������������������������������������������������������������������������������ 699��������������������������������������������������������������������������� 700 701������������������������������ 2 ��������������������������������������� ``���`` ������������������������������ 702��������������������������������������������������������������������������������������� 703 704������ ``������������������`` ������������������������������ (``struct mm_struct``: mm_users ��� 705mm_count)������������������ (``struct super_block``: s_count ��� s_active) ������������ 706 707������������������������������������������������������������������������������������������������������������������ 708��������������������������� bug��� 709 710 71112) ���������������RTL 712----------------- 713 714��������������������������������������������������������������������� 715 716.. code-block:: c 717 718 #define CONSTANT 0x12345 719 720������������������������������������������������������ 721 722������������������������������������������������������������������������������������������ 723 724������������������������������������������������������������������ 725 726������������������������������������������������ do-while ��������������� 727 728.. code-block:: c 729 730 #define macrofun(a, b, c) \ 731 do { \ 732 if (a == 5) \ 733 do_this(b, c); \ 734 } while (0) 735 736��������������������������������������� 737 7381) ��������������������������� 739 740.. code-block:: c 741 742 #define FOO(x) \ 743 do { \ 744 if (blah(x) < 0) \ 745 return -EBUGGERED; \ 746 } while (0) 747 748**������** ��������������������������������������������������������� ``������`` ������������������������������ 749��������������������������������������� 750 7512) ��������������������������������������������������� 752 753.. code-block:: c 754 755 #define FOO(val) bar(index, val) 756 757��������������������������������������������������������������������������������������������������������������������� 758������������������������������������ 759 7603) ��������������������������������� FOO(x) = y������������������ FOO ������������������������������������ 761 ��������������������������� 762 7634) ��������������������������������������������������������������������������������������������������������������� 764 ��������������������������������� 765 766.. code-block:: c 767 768 #define CONSTANT 0x4000 769 #define CONSTEXP (CONSTANT | 3) 770 7715) ������������������������������������������������������������ 772 773.. code-block:: c 774 775 #define FOO(x) \ 776 ({ \ 777 typeof(x) ret; \ 778 ret = calc_ret(x); \ 779 (ret); \ 780 }) 781 782ret ������������������������������������ __foo_ret ������������������������������������������������ 783 784cpp ���������������������������������gcc internals ������������������������ RTL������������������������ 785��������������������� 786 787 78813) ������������������ 789---------------- 790 791������������������������������������������������������������������������������������������������������������ 792��������������������������������� ``dont``������������ ``do not`` ������ ``don't`` ������������������ 793������������������������������ 794 795������������������������������������������ 796 797��������������������������� (%d) ��������������������������������������������� 798 799<linux/device.h> ������������������������������������������������������������������������������������������ 800������������������������������������������������������������������������������dev_err(), dev_warn(), 801dev_info() ������������������������������������������������������������������<linux/printk.h> ������ 802��� pr_notice(), pr_info(), pr_warn(), pr_err() ������������ 803 804������������������������������������������������������������������������������������������������������������������ 805��������������������������������������������������������������������������������������������������� pr_XXX() 806������������������������������pr_debug() ������������������������������������������������������������ DEBUG 807������������ CONFIG_DYNAMIC_DEBUG��������������������������� dev_dbg()������������������������������ 808������������������ DEBUG ������������ VERBOSE_DEBUG ��������� dev_vdbg()��� 809 810��������������������� Kconfig ��������������������������� Makefile ��������� -DDEBUG������������ 811������������������������������ #define DEBUG��������������������������������������������������������������� 812��������������������������������������� #ifdef ���������printk(KERN_DEBUG ...) ������������������ 813 814 81514) ������������ 816------------ 817 818������������������������������������������������������������ 819kmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc() ��� vzalloc()��� 820��������� API ��������������������������������������������� 821Documentation/translations/zh_CN/core-api/memory-allocation.rst ��� 822 823��������������������������������������������������� 824 825.. code-block:: c 826 827 p = kmalloc(sizeof(*p), ...); 828 829������������������������������sizeof ��������������������������������������������������������������������������� 830��������� bug��������������������������������������������������������������������������������������� sizeof 831������������������ 832 833������������������ void ������������������������������C ������������������������ void ��������������������� 834������������������������������������������ 835 836������������������������������������������������ 837 838.. code-block:: c 839 840 p = kmalloc_array(n, sizeof(...), ...); 841 842������������������������������������������������������ 843 844.. code-block:: c 845 846 p = kcalloc(n, sizeof(...), ...); 847 848������������������������������ n * sizeof(...) ��������������������������������������������� NULL��� 849 850��������� __GFP_NOWARN ��������������������������������������������������������������������������������������� 851���������������NULL������������������������������������������������ 852 85315) ������������ 854------------ 855 856��������������������������� ``������`` ��� gcc ��������������������������������������������������������������� 857������������������������������������ (���������������������������������������������������������)������������������ 858���������������������inline ������������������������������������������������������������������������������ 859��������������������������������������������������������������������������� pagecache ������������������������ 860��������������������� pagecache ��������������������������������������������������� 5 ���������5 ��������� 861��������� CPU ������������������������������ 862 863��������������������������������������������� 3 ������������������������������������������������������������������ 864������������������������������������������������������������������������������������������������������������������ 865��������������������������������������������������������������������������������� inline ������������ 866kmalloc() ������������������������������������������ 867 868��������������������� static ��������������������������������������� inline��������������������������������� 869������������������������������������������������������������������������������������������������������������������ 870inline gcc ��������������������������������������������������������������������� inline������������������ 871��������������� inline ��������������������������������������� 872 873 87416) ������������������������ 875-------------------- 876 877��������������������������������������������������������������������������������������������������������������������� 878��������������������������������������������������� (-Exxx������������0���������) ������������ ``������`` 879��������� (0���������������0���������)��� 880 881��������������������������������������������������� bug ������������������ C ��������������������������������� 882���������������������������������������������������������������������... ������ C ������������������������������ 883������������ bug���������������������������:: 884 885 ������������������������������������������������������������������������������������������������������ 886 ������������������������������������������������������������������������������������������ 887 888��������� ``add work`` ������������������������ add_work() ������������������ 0��������������������� 889-EBUSY��������������������� ``PCI device present`` ������������������������ pci_dev_present() 890��������������������������������������������������� 1��������������������������������� 0��� 891 892������ EXPORTed ������������������������������������������������������������������������������������ 893(static) ��������������������������������������������������������� 894 895������������������������������������������������������������������������������������������������������������ 896������������������������������������������������������������������������������������������������������������������ 897������������ NULL ������ ERR_PTR ������������������������ 898 89917) ������������������ 900---------------- 901 902Linux���������������bool������������C99 _Bool������������������������������������0���1������������������ 903���������������������������������������������true���false��������������������������� **���������** ��������� 904��������������������������� 905 906������������������������������true���false������������������1���0��� 907 908��������������������������������������������������������������������������������������������������������������������� 909���������������������������������������int������������ 910 911������������������������������������������������������������������������������������������������������������������ 912��������������������������������������������������������������������������������������������� 913 914������������������������������true/false���������������������������������������1������������������������������ 915������������������������������������u8��� 916 917���������������������������������������true/false��������������������������������������������������������������� 918���������������true/false��������������������������������������������������������������������� 919 920������������������������������������������������������������������������������ 921 92218) ��������������������������� 923---------------------- 924 925��������� include/linux/kernel.h ��������������������������������������������������������������������� 926��������������������������������������������������������������������������������������� 927 928.. code-block:: c 929 930 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 931 932������������������������������������������������������������������ 933 934.. code-block:: c 935 936 #define sizeof_field(t, f) (sizeof(((t*)0)->f)) 937 938��������������������������������������� min() ��� max() ��������������������������������������������������� 939������������������������������������������������������������������������������������������������������������������ 940��������������������������������������� 941 942 94319) ������������������������������������������������ 944------------------------------------ 945 946������������������������������������������������������������������������������������������������������������emacs 947��������������������������������������� 948 949.. code-block:: c 950 951 -*- mode: c -*- 952 953������������������ 954 955.. code-block:: c 956 957 /* 958 Local Variables: 959 compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c" 960 End: 961 */ 962 963Vim ������������������������������ 964 965.. code-block:: c 966 967 /* vim:set sw=8 noet */ 968 969������������������������������������������������������������������������������������������������������������������ 970������������������������������������������������������������������������������������������������������������������ 971��������������������������������������������������������������������������� 972 973 97420) ������������ 975------------ 976 977������������������������������������������������������������ CPU ������������������������������������������������ 978������������������������������ C ��������������������������������������������������������������������������������� 979��������������������������������� C ������������������ 980 981��������������������������������� (wrap common bits) ��������������������������������������������������� 982������������������������������������������������������������������������ C ��������� 983 984������������������������������������������������������ .S ������������������������ C ��������������� C ������ 985������������������������ C ������������������ ``asmlinkage`` ��� 986 987��������������������������������������� volatile��������������� GCC ��������������������������������������� 988��������������������������������������������������������������������������������������� 989 990������������������������������������������������������������������������������������������������������������������ 991��������������������������������������������������������� ``\n\t`` ������������������������������������������ 992������������������ 993 994.. code-block:: c 995 996 asm ("magic %reg1, #42\n\t" 997 "more_magic %reg2, %reg3" 998 : /* outputs */ : /* inputs */ : /* clobbers */); 999 1000 100121) ������������ 1002------------ 1003 1004��������������������������� .c ��������������������������������� (#if, #ifdef)������������������������������ 1005������������������������������������������������������������������������������������������������������ .c ������ 1006��������������� #else ������������������ (no-op stub) ������������������ .c ��������������������������� 1007������ (������������������������) ��������������������������������������������������� (stub) ��������������� 1008������������������������������������������������������������������������ 1009 1010��������������������������������������������������������������������������������������������������������� ifdef 1011������������������������������������������������������������������������������������������������������������������ 1012��������������������������������� 1013 1014������������������������������������������������������������������������������������������������������������������ 1015������������������������������ __maybe_unused ���������������������������������������������������(��������� 1016������������������������������������������������������������������) 1017 1018��������������������������������� IS_ENABLED ������������������ Kconfig ��������� C ��������� 1019��������������������������� C ��������������������� 1020 1021.. code-block:: c 1022 1023 if (IS_ENABLED(CONFIG_SOMETHING)) { 1024 ... 1025 } 1026 1027������������������������������������������������ #ifdef ������������������������������������������������������ 1028������������������������������������������������������������ C ��������������������������������������������������� 1029������ (���������������������������������������)������������������������������������������������������������������ 1030������������������������������������ #ifdef��� 1031 1032��������������������� #if ��� #ifdef ������������ (���������������)������ #endif ������������������������ 1033������������������������������������������������ 1034 1035.. code-block:: c 1036 1037 #ifdef CONFIG_SOMETHING 1038 ... 1039 #endif /* CONFIG_SOMETHING */ 1040 1041 1042������ I) ������������ 1043---------------- 1044 1045The C Programming Language, 2nd Edition 1046���������Brian W. Kernighan ��� Denni M. Ritchie. 1047Prentice Hall, Inc., 1988. 1048ISBN 0-13-110362-8 (������), 0-13-110370-9 (������). 1049 1050.. note:: 1051 1052 ���C������������������������2��������� 1053 ���������[���] Brian W. Kernighan / [���] Dennis M. Ritchie 1054 ������������������ / ������ / ��������������������� 1055 ������������������������������������2019 1056 ISBN���9787111617945 1057 1058The Practice of Programming 1059���������Brian W. Kernighan ��� Rob Pike. 1060Addison-Wesley, Inc., 1999. 1061ISBN 0-201-61586-X. 1062 1063.. note:: 1064 1065 ������������������������ 1066 ���������[���] Brian W. Kernighan / [���] Rob Pike 1067 ������������������������������������2005 1068 ISBN���9787111091578 1069 1070 ������������������������ 1071 ���������[���] Brian W. Kernighan / Rob Pike 1072 ������������������ 1073 ������������������������������������2000 1074 ISBN���9787111075738 1075 1076GNU ������ - ������ K&R ������������������ - cpp, gcc, gcc internals and indent, 1077������������ https://www.gnu.org/manual/ ������ 1078 1079WG14 ��� C ������������������������������������URL: http://www.open-std.org/JTC1/SC22/WG14/ 1080 1081������������ Documentation/process/coding-style.rst��� 1082������ greg@kroah.com ��������� OLS 2002��� 1083http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ 1084