Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61284
  • 博文数量: 23
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 45
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-29 10:06
文章分类

全部博文(23)

文章存档

2015年(21)

2014年(2)

我的朋友

分类: 嵌入式

2015-04-01 10:31:35

原文地址:4. devices_init () 作者:changyongID

     1    typedef struct ListStructTag
     2        {
     3        int signature;              /* debugging aid */
     4        int percentIncrease;        /* %of current size to increase by when list is out of space */
     5        int minNumItemsIncrease;    /* fixed number of items to increase by when list is out of space */
     6        int listSize;               /* number of items than can fit in the currently allocated memory */
     7        int itemSize;               /* the size of each item in the list (same for every item) */
     8        int numItems;               /* number of items currently in the list */
     9        unsigned char itemList[1];  /* resizable array of list elements */
    10        } ListStruct;
    11    typedef struct ListStructTag **list_t;        /* The list abstract data type */

list_t devlist = 0; 即ListStructTag ** devlist;
devlist是个全局变量,devlist 中的值是ListStruct结构的指针的指针。即有

list_t devlist      (ListStruct *)            ListStruct
+----------+         +-----------+         +-------------+
|          | ------> |           | ------> |    ……       |
+----------+         +-----------+         +             +
                                           |    ……       |
                                           +             +
                                           |    ……       |
                                           +-------------+

    39    typedef void **Handle;
    40    Handle NewHandle (unsigned int numBytes)
    41    {
    42        void *memPtr;
    43        HandleRecord *hanPtr;

    44        memPtr = calloc (numBytes, 1);
    45        hanPtr = (HandleRecord *) calloc (sizeof (HandleRecord), 1);
    46        if (hanPtr && (memPtr || numBytes == 0)) {
    47            hanPtr->ptr = memPtr;
    48            hanPtr->size = numBytes;
    49            return (Handle) hanPtr;
    50        } else {
    51            free (memPtr);
    52            free (hanPtr);
    53            return NULL;
    54        }
    55    }

函数传进来的numBytes为sizeof (ListStruct), 即一个ListStruct结构大小的空间
44行,申请ListStruct结构大小的空间
45行,申请HandleRecord结构大小的空间,HandleRecord结构有两个成员
    typedef struct
    {
        void            *ptr;
        unsigned int    size;
    } HandleRecord;
46行,hanPtr和memPtr申请成功时,if判断为真,则执行47 48行,最后返回void **hanPtr

void **hanPtr         HandleRecord            ListStruct
+-----------+        +-----------+         +-------------+
|           | -----> | void *ptr | ------> |    ……       |
+-----------+        +-----------+         +             +
                     |   size    |         |    ……       |
                     +-----------+         +             +
                                           |    ……       |
                                           +-------------+

    23    list_t ListCreate (int elementSize)
    24    {
    25        list_t list;

    26        list = (list_t) (NewHandle (sizeof (ListStruct)));  /* create empty list */
    27        if (list) {
    28            (*list)->signature = LIST_SIGNATURE;
    29            (*list)->numItems = 0;
    30            (*list)->listSize = 0;
    31            (*list)->itemSize = elementSize;
    32            (*list)->percentIncrease =
    33                                    kDefaultAllocationPercentIncrease; //10
    34            (*list)->minNumItemsIncrease =
    35                    kDefaultAllocationminNumItemsIncrease; //4
    36        }

    37        return list;
    38    }
23行,传进来的参数为sizeof (device_t)
26行,调用NewHandle(),并将返回值格式化成list_t类型
28~35行,初始化 ListStruct 结构
 
                      HandleRecord            ListStruct
+-----------+        +-----------------+         +-----------------------------+
|           | -----> | ListStruct *ptr | ------> | signature = LIST_SIGNATURE  |
+-----------+        +-----------------+         +-----------------------------+
void **hanPtr        |   size          |         | percentIncrease = 10        |
     |               +-----------------+         +-----------------------------+
     v return from NewHandle()                   | minNumItemsIncrease =4      |
 list_t list                                     +-----------------------------+
                                                 | listSize = 0                |
                                                 +-----------------------------+
                                                 | itemSize = sizeof(device_t) |
                                                 +-----------------------------+
                                                 |numItems = 0                |    
                                                 +-----------------------------+
                                                 | unsigned char itemList[1]   |
                                                 +-----------------------------+

    12    int devices_init (void)
    13    {
    14        /* Initialize the list */
    15        devlist = ListCreate (sizeof (device_t));

    16        if (devlist == NULL) {
    17            eputs ("Cannot initialize the list of devices!\n");
    18            return -1;
    19        }
    20        drv_system_init ();

    21        return (0);
    22    }

15行,ListCreate返回给devlist, 所以现在的数据结构如下

                      HandleRecord            ListStruct
+-----------+        +-----------------+         +-----------------------------+
|           | -----> | ListStruct *ptr | ------> | signature = LIST_SIGNATURE  |
+-----------+        +-----------------+         +-----------------------------+
void **hanPtr        |   size          |         | percentIncrease = 10        |
     |               +-----------------+         +-----------------------------+
     v return from NewHandle()                   | minNumItemsIncrease =4      |
list_t list                                      +-----------------------------+
     |                                           | listSize = 0                |
     v return from ListCreate()                  +-----------------------------+
list_t devlist                                   | itemSize = sizeof(device_t) |
                                                 +-----------------------------+
                                                 | numItems = 0               |    
                                                 +-----------------------------+
                                                 | unsigned char itemList[1]   |
                                                 +-----------------------------+

20行, 初始系统设备

    56    typedef struct {
    57        int    flags;            /* Device flags: input/output/system    */
    58        int    ext;            /* Supported extensions            */
    59        char    name[16];        /* Device name                */
    60        int (*start) (void);        /* To start the device            */
    61        int (*stop) (void);        /* To stop the device            */
    62        void (*putc) (const char c);    /* To put a char            */
    63        void (*puts) (const char *s);    /* To put a string (accelerator)    */
    64        int (*tstc) (void);        /* To test if a char is ready...    */
    65        int (*getc) (void);        /* To get that char            */
    66        void *priv;            /* Private extensions            */
    67    } device_t;
    68    static void drv_system_init (void)
    69    {
    70        device_t dev;

    71        memset (&dev, 0, sizeof (dev));

    72        strcpy (dev.name, "serial");
    73        dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
    74        dev.putc = serial_putc;
    75        dev.puts = serial_puts;
    76        dev.getc = serial_getc;
    77        dev.tstc = serial_tstc;

    78        device_register (&dev);
    79    }

73~77 初始串口设备,一般我们也是使用串口来打印目标板信息以及向目标板发送命令。对应56~67行,可以
清楚这些成员的意义。

78行,注册这个设备。"serial"这个设备继续跟踪下去

    80    #define LIST_START -1 /* Handy Constants that substitute for item positions */
    81    #define LIST_END 0  /* END_OF_LIST means one past current length of list when */
    82    int device_register (device_t * dev)
    83    {
    84        ListInsertItem (devlist, dev, LIST_END);
    85        return 0;
    86    }


    87    int ListInsertItem (list_t list, void *ptrToItem, int itemPosition)
    88    {
    89        return ListInsertItems (list, ptrToItem, itemPosition, 1);
    90    }
87行,进入的参数分别为1 devlist, 这个是全局变量
                  2 dev  即drv_system_init中初始的"serial"设备 device_t dev 的指针
                  3 LIST_END == 0
 


    91    int ListInsertItems (list_t list, void *ptrToItems, int firstItemPosition,
    92                 int numItemsToInsert)
    93    {
    94        int numItems = (*list)->numItems;

    95        if (firstItemPosition == numItems + 1)
    96            firstItemPosition = LIST_END;
    97        else if (firstItemPosition > numItems)
    98            return 0;

    99        if ((*list)->numItems >= (*list)->listSize) {
   100            if (!ExpandListSpace (list, -numItemsToInsert))
   101                return 0;
   102        }

   103        if (firstItemPosition == LIST_START) {
   104            if (numItems == 0) {
   105                /* special case for empty list */
   106                firstItemPosition = LIST_END;
   107            } else {
   108                firstItemPosition = 1;
   109            }
   110        }

   111        if (firstItemPosition == LIST_END) {    /* add at the end of the list */
   112            if (ptrToItems)
   113                memcpy (ITEMPTR (list, numItems), ptrToItems,
   114                        (*list)->itemSize * numItemsToInsert);
   115            else
   116                memset (ITEMPTR (list, numItems), 0,
   117                        (*list)->itemSize * numItemsToInsert);

   118            (*list)->numItems += numItemsToInsert;
   119        } else {  /* move part of list up to make room for new item */
   120            memmove (ITEMPTR (list, firstItemPosition - 1 + numItemsToInsert),
   121                 ITEMPTR (list, firstItemPosition - 1),
   122                 (numItems + 1 - firstItemPosition) * (*list)->itemSize);

   123            if (ptrToItems)
   124                memmove (ITEMPTR (list, firstItemPosition - 1), ptrToItems,
   125                         (*list)->itemSize * numItemsToInsert);
   126            else
   127                memset (ITEMPTR (list, firstItemPosition - 1), 0,
   128                        (*list)->itemSize * numItemsToInsert);

   129            (*list)->numItems += numItemsToInsert;
   130        }

   131        return 1;
   132    }
94行,目前设备列表list里的设备数 ,这里注册第一个设备"serial",为0
95~97 此时firstItemPosition == LIST_END == 0, 所以这里判断均为假,跳过
100 行中 ExpandListSpace()会增加list里的设备数,并为这个设备分配相应的空间,这个空间是用relloc
这种机制来分配的,保证了这里的device_t与前面的ListStructTag连在一起
113行 将drv_system_init里初始的device_t结构内容复制到新分配的空间里
118行 设备数加1

完成了设备的注册
阅读(1029) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~