Data Types in the Kernel

Use of Standard C Types

  1. /*
  2. * datasize.c -- print the size of common data items
  3. * This runs with any Linux kernel (not any Unix, because of <linux/types.h>)
  4. *
  5. * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
  6. * Copyright (C) 2001 O'Reilly & Associates
  7. *
  8. * The source code in this file can be freely used, adapted,
  9. * and redistributed in source or binary form, so long as an
  10. * acknowledgment appears in derived source files. The citation
  11. * should list that the code comes from the book "Linux Device
  12. * Drivers" by Alessandro Rubini and Jonathan Corbet, published
  13. * by O'Reilly & Associates. No warranty is attached;
  14. * we cannot take responsibility for errors or fitness for use.
  15. */
  16. #include <stdio.h>
  17. #include <sys/utsname.h>
  18. #include <linux/types.h>
  19.  
  20. int main(int argc, char **argv)
  21. {
  22. struct utsname name;
  23.  
  24. uname(&name); /* never fails :) */
  25. printf("arch Size: char short int long ptr long-long "
  26. " u8 u16 u32 u64\n");
  27. printf( "%-12s %3i %3i %3i %3i %3i %3i "
  28. "%3i %3i %3i %3i\n",
  29. name.machine,
  30. (int)sizeof(char), (int)sizeof(short), (int)sizeof(int),
  31. (int)sizeof(long),
  32. (int)sizeof(void *), (int)sizeof(long long), (int)sizeof(__u8),
  33. (int)sizeof(__u16), (int)sizeof(__u32), (int)sizeof(__u64));
  34. return 0;
  35. }


Therefore, generic memory addresses in the kernel are usually unsigned long , exploiting the fact that pointers and long integers are always the same size, at least on all the platforms currently supported by
Linux.

这个都简单,有意思的是遇到一个新的结构体——struct utsname

在kernel里面找了一会儿,老是找不到,找到的utsname是个指针函数。

忽然<sys/utsname.h>提醒我。。。这家伙user space,

于是去/usr/include/x86。。。/sys/里面找,找到了utsname.h

把整个sys/utsname.h 贴出来

  1. /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17.  
  18. /*
  19. * POSIX Standard: 4.4 System Identification <sys/utsname.h>
  20. */
  21.  
  22. #ifndef _SYS_UTSNAME_H
  23. #define _SYS_UTSNAME_H 1
  24.  
  25. #include <features.h>
  26.  
  27. __BEGIN_DECLS
  28.  
  29. #include <bits/utsname.h>
  30.  
  31. #ifndef _UTSNAME_SYSNAME_LENGTH
  32. # define _UTSNAME_SYSNAME_LENGTH _UTSNAME_LENGTH
  33. #endif
  34. #ifndef _UTSNAME_NODENAME_LENGTH
  35. # define _UTSNAME_NODENAME_LENGTH _UTSNAME_LENGTH
  36. #endif
  37. #ifndef _UTSNAME_RELEASE_LENGTH
  38. # define _UTSNAME_RELEASE_LENGTH _UTSNAME_LENGTH
  39. #endif
  40. #ifndef _UTSNAME_VERSION_LENGTH
  41. # define _UTSNAME_VERSION_LENGTH _UTSNAME_LENGTH
  42. #endif
  43. #ifndef _UTSNAME_MACHINE_LENGTH
  44. # define _UTSNAME_MACHINE_LENGTH _UTSNAME_LENGTH
  45. #endif
  46.  
  47. /* Structure describing the system and machine. */
  48. struct utsname
  49. {
  50. /* Name of the implementation of the operating system. */
  51. char sysname[_UTSNAME_SYSNAME_LENGTH];
  52.  
  53. /* Name of this node on the network. */
  54. char nodename[_UTSNAME_NODENAME_LENGTH];
  55.  
  56. /* Current release level of this implementation. */
  57. char release[_UTSNAME_RELEASE_LENGTH];
  58. /* Current version level of this release. */
  59. char version[_UTSNAME_VERSION_LENGTH];
  60.  
  61. /* Name of the hardware type the system is running on. */
  62. char machine[_UTSNAME_MACHINE_LENGTH];
  63.  
  64. #if _UTSNAME_DOMAIN_LENGTH - 0
  65. /* Name of the domain of this node on the network. */
  66. # ifdef __USE_GNU
  67. char domainname[_UTSNAME_DOMAIN_LENGTH];
  68. # else
  69. char __domainname[_UTSNAME_DOMAIN_LENGTH];
  70. # endif
  71. #endif
  72. };
  73.  
  74. #ifdef __USE_SVID
  75. /* Note that SVID assumes all members have the same size. */
  76. # define SYS_NMLN _UTSNAME_LENGTH
  77. #endif
  78.  
  79. /* Put information about the system in NAME. */
  80. extern int uname (struct utsname *__name) __THROW;
  81.  
  82. __END_DECLS
  83.  
  84. #endif /* sys/utsname.h */

哈哈,自己写个demo,把他们统统打印粗来,满足好奇心。

  1. /*************************************************
  2. code writer : EOF
  3. code date : 2014.08.14
  4. e-mail: jasonleaster@gmail.com
  5. code purpose:
  6. just a demo for how to use structure--utsname
  7.  
  8. If you find something wrong with my code,
  9. please touch me by e-mail. Thank you.
  10.  
  11. *************************************************/
  12. #include <stdio.h>
  13. #include <sys/utsname.h>
  14.  
  15. int main()
  16. {
  17. struct utsname demo;
  18.  
  19. uname(&demo);
  20.  
  21. printf("sysname: %s\nnodename: %s\nrelease :%s\nmachine: %s\n",
  22. demo.sysname,demo.nodename,demo.release,demo.machine);
  23. #ifdef __USE_GNU
  24. printf("domainname: %s\n",demo.domainname);
  25. #else
  26. printf("__domainname: %s\n",demo.__domainname);
  27. #endif
  28.  
  29. return 0;
  30. }

Assigning an Explicit Size to Data Items

Sometimes kernel code requires data items of a specific size, perhaps to match pre- defined binary structures, to communicate with user space, or to align data within structures by inserting “padding” fields (but refer to the section “Data Alignment” for
information about alignment issues).

 

              If a user-space program needs to use these types, it can prefix the names with a double underscore: __u8 and the other types are defined independent of __KERNEL__ . If, for example, a driver needs to exchange binary structures with a program running
in user space by means of ioctl, the header files should declare 32-bit fields in the structures as __u32 .

Time Intervals

When dealing with time intervals, don’t assume that there are 1000 jiffies per second.
Although this is currently true for the i386 architecture, not every Linux platform runs at this speed. The assumption can be false even for the x86 if you play with the HZ value (as some people do), and nobody knows what will happen in future

kernels. Whenever you calculate time intervals using jiffies, scale your times using HZ (the number of timer interrupts per second). For example, to check against a time-out of half a second, compare the elapsed time against HZ/2 . More generally, the number
of jiffies corresponding to msec milliseconds is always msec*HZ/1000 .

Page Size

When playing games with memory, remember that a memory page is PAGE_SIZE bytes, not 4 KB. Assuming that the page size is 4 KB and hardcoding the value is a common error among PC programmers, instead, supported platforms
show page sizes from 4 KB to 64 KB, and sometimes they differ between different implementations of the same platform. The relevant macros are PAGE_SIZE and PAGE_SHIFT . The latter contains the number of bits to shift an address to get its page number.

事实上在早期的0.1内核里面linus就是直接用的4KB那么去做的,反正那时候他没想到linux会被这么广泛的应用

Linked Lists

To use the list mechanism, your driver must include the file <linux/list.h>. This file defines a simple structure of type list_head :

  1. struct list_head {
  2. struct list_head *next, *prev;
  3. };

List heads must be initialized prior to use with the INIT_LIST_HEAD macro. A “things to do” list head could be declared and initialized with:

  1. struct list_head todo_list;
  2. INIT_LIST_HEAD(&todo_list);

Alternatively, lists can be initialized at compile time:

  1. LIST_HEAD(todo_list);

Several functions are defined in <linux/list.h> that work with lists:

  1. list_add(struct list_head *new, struct list_head *head);
  2. Adds the new entry immediately after the list headnormally at the beginning of the list. Therefore, it can be used to build stacks. Note, however, that the head need not be the nominal head of the list; if you pass a list_head structure that happens to be in the middle of the list somewhere, the new entry goes immediately after it. Since Linux lists are circular, the head of the list is not generally different from any other entry.

  1. list_add_tail(struct list_head *new, struct list_head *head);
  2. Adds a new entry just before the given list headat the end of the list, in other words. list_add_tail can, thus, be used to build first-in first-out queues.

  1. list_del(struct list_head *entry);
  2. list_del_init(struct list_head *entry);
  3. The given entry is removed from the list. If the entry might ever be reinserted into another list, you should use list_del_init, which reinitializes the linked list pointers.

  1. list_move(struct list_head *entry, struct list_head *head);
  2. list_move_tail(struct list_head *entry, struct list_head *head);
  3. The given entry is removed from its current list and added to the beginning of head . To put the entry at the end of the new list, use list_move_tail instead.
  1. list_empty(struct list_head *head);
  2. Returns a nonzero value if the given list is empty.

  1. list_splice(struct list_head *list, struct list_head *head);
  2. Joins two lists by inserting list immediately after head . The list_head structures are good for implementing a list of like structures, but the invoking program is usually more interested in the larger structures that make up the
  3. list as a whole.

A macro, list_entry, is provided that maps a list_head structure pointer back into a pointer to the structure that contains it. It is invoked as follows:

  1. list_entry(struct list_head *ptr, type_of_struct, field_name);
  2. where ptr is a pointer to the struct list_head being used, type_of_struct is the type of the structure containing the ptr , and field_name is the name of the list field within the structure. In our todo_struct structure from before, the list field is called simply list .

Thus, we would turn a list entry into its containing structure with a line such as:

  1. struct todo_struct *todo_ptr = list_entry(listptr, struct todo_struct, list);

The list_entry macro takes a little getting used to but is not that hard to use.

"像倦鸟归去留下的空寂,安安静静"

Data Types in the Kernel &lt;LDD3 学习笔记&gt;的更多相关文章

  1. 【12c】扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE

    [12c]扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE 在12c中,与早期版本相比,诸如VARCHAR2, NAVARCHAR2以及 RAW这些数据类型的 ...

  2. Linux kernel suspend resume学习:2.6.35与3.0.35比较【转】

    转自:http://blog.csdn.net/njuitjf/article/details/18317149 Linux kernel suspend resume学习:2.6.35与3.0.35 ...

  3. 【翻译】苹果官网的命名规范之 Naming Properties and Data Types

    苹果官方原文:Naming Properties and Data Types 前言:纯属练习英语和学习.翻译错误和不通顺的地方敬请谅解和指正.O(∩_∩)O 属性和数据类型的命名 本节讲述了属性定义 ...

  4. C and SQL data types for ODBC and CLI

    C and SQL data types for ODBC and CLI   This topic lists the C and SQL data types for ODBC and CLI a ...

  5. allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[System.DateTime]

    allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[S ...

  6. "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."

    "SQL Server does not handle comparison of NText, Text, Xml, or Image data types." sql2000 ...

  7. ExtJS笔记 Ext.data.Types

    This is a static class containing the system-supplied data types which may be given to a Field. Type ...

  8. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

  9. Core Java Volume I — 3.3. Data Types

    3.3. Data TypesJava is a strongly typed language(强类型语音). This means that every variable must have a ...

随机推荐

  1. Android源码中的FLAG为何使用16进制

    1.在阅读源码的时候经常发现有一些标志属性使用一些位操作来判断是否具有该标志,增加标志或者去除标志. 比如View.java中的 /** * This view does not want keyst ...

  2. bzoj 1070: [SCOI2007]修车 费用流

    1070: [SCOI2007]修车 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2785  Solved: 1110[Submit][Status] ...

  3. real-time application

    http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx ...

  4. USB OTG

    OTG检测的原理是:USB OTG标准在完全兼容USB2.0标准的基础上,增添了电源管理(节省功耗)功能,它允许设备既可作为主机,也可作为外设操作(两用OTG).USB OTG技术可实现没有主机时设备 ...

  5. Java+JQuery实现网页显示本地文件目录(含源码)

    原文地址:http://www.cnblogs.com/liaoyu/p/uudisk.html 源码地址:https://github.com/liaoyu/uudisk 前段时间为是练习JQuer ...

  6. axis2 调用.net基于https的WebService接口

    一.创建证书 1,在IE中访问WSDL的URL,弹出“安全警报”窗口,查看证书->详细信息标签页->复制到文件->下一步->下一步->指定文件名,将证书下载保存为.cer ...

  7. java基于xml配置的通用excel单表数据导入组件(四、DAO主处理类)

    package XXXXX.manage.importexcel; import java.beans.IntrospectionException; import java.io.BufferedR ...

  8. Android 应用页面延缓载入

    1.新建一个线程,使用handle的延缓运行线程 new Handler().postDelayed(new Runnable() { // 为了减少代码使用匿名Handler创建一个延时的调用 pu ...

  9. UIView frame, bounds and center

    http://stackoverflow.com/questions/5361369/uiview-frame-bounds-and-center Since the question I asked ...

  10. Yii AR Model CRUD数据库操作

    Yii AR很好很强大,但刚开始不知道怎么使用,可以先看下官方文档 官方文档:http://www.yiichina.com/guide/database.ar 下面是我对AR的一些理解 对于一个Mo ...