内核中链表的使用

一. 链表初识

  1.1. 什么是链表

    a. 链表是一种常见的基础数据结构

    b. 链表是由节点(结构体)组成的,节点中包含:有效数据和指针。

    c. 链表的内存要求比较灵活,一般不能用栈,也不能用data数据段。只能用堆内存。

  1.2. 链表与数组差别  

    1.2.1. 链表就是用来解决数组的大小不能动态扩展的问题,所以链表其实就是当数组用的

    1.2.2. 数组定义好长度后不能重新改变数组长度,而链表可以不受限制扩展

二. 内核中链表分析

  2.1. 链表头文件分析

    a. 此文件是参考linux内核中相关链表文件

    b. 链表结构体中只有前链表指针和后链表指针,如此实现链表指针和数据分离

#ifndef __MYLIST__H
#define __MYLIST__H

#define offsetof(type,member) ((int)&(((type *)0)->member))


//通过结构体成员指针获取结构体指针位置
#define container_of(ptr, type, member) ({            \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})
 
 
//链表结构体
struct list_head {
    struct list_head *next, *prev;
};
 
//链表初始化
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}
 
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next)
{
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next);
#endif
 
 
//添加至链表首部
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}
 
//添加到链表尾部
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}
 
//判断链表是否为空
/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
 /* if empty return 1,else 0 */
static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}
 
/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}
 
 
//删除操作
static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = NULL;
    entry->prev = NULL;
}
 
//获取链表的数据指针
#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)
    
//遍历链表
#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)
 
//遍历过程中如果对链表有删除操作需要使用这个接口
#define list_for_each_safe(pos, n, head) \
        for (pos = (head)->next, n = pos->next; pos != (head); \
            pos = n, n = pos->next)
            
 
//遍历链表元素
#define list_for_each_entry(pos, head, member)                \
    for (pos = list_entry((head)->next, typeof(*pos), member);    \
         &pos->member != (head);     \
         pos = list_entry(pos->member.next, typeof(*pos), member))
         
#define list_for_each_entry_safe(pos, n, head, member)            \
    for (pos = list_entry((head)->next, typeof(*pos), member),    \
        n = list_entry(pos->member.next, typeof(*pos), member);    \
         &pos->member != (head);                     \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))
         
//取第一个元素
#define list_first_entry(ptr, type, member) \
        list_entry((ptr)->next, type, member)
 
#endif
View Code

   2.2. 定义数据结构体

    2.2.1. 在数据结构体中包含

      a. 此结构体可以将数据和链表关联起来

typedef struct  
{
    int msgid;
    char pathname[NAME_MAX_LEN];
    e_pictureType pictureType;
    struct list_head list;
}s_pictureInfo;
View Code

   2.3. 链表的使用

    2.3.1. 定义链表头并初始化

struct list_head msg_head;
INIT_LIST_HEAD(&msg_head);     //链表初始化

    2.3.2. 定义数据结构体指针并申请空间

s_pictureInfo *pPictureInfo = NULL;
pPictureInfo = calloc(1, sizeof(s_pictureInfo));

    2.3.3. 填充数据并插入链表中

                strcpy(pPictureInfo->pathname,pathname);
                if(!is_bmp(pathname))
                    pPictureInfo->pictureType = BMP_TYPE;
                else if(!is_jpeg(pathname))
                    pPictureInfo->pictureType = JPEG_TYPE;
                else if(!is_png(pathname))
                    pPictureInfo->pictureType = PNG_TYPE;
                else 
                {
                    DEBUG_Printf("pictureType fail \n");
                    break;
                }
                pPictureInfo->msgid = id++;
                DEBUG_Printf(" add id = %d\n",pPictureInfo->msgid);
                list_add_tail(&pPictureInfo->list, &msg_head);  //插入尾部,list_add()插入首部

    2.3.4. 遍历链表

static int showListPicture(const struct list_head *list_head)
{
    s_pictureInfo *pPictureInfo = NULL;

    //遍历查询
    list_for_each_entry(pPictureInfo, list_head, list) 
    {
        DEBUG_Printf("pathname:%s\n",pPictureInfo->pathname);
        switch(pPictureInfo->pictureType)
        {
            case PNG_TYPE:     fb_draw_back(SCREEN_BLACK); show_png(-1,-1,pPictureInfo->pathname); break;
            case JPEG_TYPE:    fb_draw_back(SCREEN_BLACK); show_jpeg(-1,-1,pPictureInfo->pathname); break;
            case BMP_TYPE:     fb_draw_back(SCREEN_BLACK); show_bmp(-1,-1,pPictureInfo->pathname); break;
            default :break;
        }
        sleep(2);    
    }
    return 0;
}

  2.4. 应用源码如下:

/*
 *  Description : linux应用层管理图片
 *  Date        :20181230
 *  Author      :musk
 *  Mail        : 739112417@qq.com
 *
 */


#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <strings.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <fb.h>

#include <fb_bmp.h>
#include <fb_png.h>
#include <fb_jpeg.h>

#include <pictureList.h>
#include <picturePlayerConfig.h>
#include <linux/input.h>

#define DEVICE_TOUCHSCREEN "/dev/input/event1"
struct list_head msg_head;
INIT_LIST_HEAD(&msg_head);     //链表初始化 
int id =0;

static int readFileList(char *basePath);;
static inline void getPathname(char *basePath,char *name,char *pathname)
{
    bzero(pathname,strlen(pathname));
    strcat(pathname,basePath);
    strcat(pathname,"/");
    strcat(pathname,name);
}

static int readFileList(char *basePath)
 {

    DIR *dir;
    struct dirent *ptr;
    struct stat sb;
    char pathname[1000] = {0};
    s_pictureInfo *pPictureInfo = NULL;
       
    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        return -1;
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;                    
        getPathname(basePath,ptr->d_name,pathname);    
        
        if (stat(pathname, &sb) == -1) 
        {
            perror("stat");
            closedir(dir);
            exit(EXIT_FAILURE);
        }
        switch(sb.st_mode & S_IFMT)
        {
            case S_IFREG: 
            {
                
                pPictureInfo = calloc(1, sizeof(s_pictureInfo));
                if(!pPictureInfo){
                    DEBUG_Printf("insert fail \n");
                    break;
                }
                strcpy(pPictureInfo->pathname,pathname);
                if(!is_bmp(pathname))
                    pPictureInfo->pictureType = BMP_TYPE;
                else if(!is_jpeg(pathname))
                    pPictureInfo->pictureType = JPEG_TYPE;
                else if(!is_png(pathname))
                    pPictureInfo->pictureType = PNG_TYPE;
                else 
                {
                    DEBUG_Printf("pictureType fail \n");
                    break;
                }
                pPictureInfo->msgid = id++;
                DEBUG_Printf(" add id = %d\n",pPictureInfo->msgid);
                list_add_tail(&pPictureInfo->list, &msg_head);  //插入尾部,list_add()插入首部
                break;
            }
            case S_IFDIR: 
            {
                readFileList(pathname);
                break;
            }
            break;
            default : DEBUG_Printf(" involid file \n");break;
        }
    }
    closedir(dir);
    return 0;
}


static int showListPicture(const struct list_head *list_head)
{
    s_pictureInfo *pPictureInfo = NULL;

    //遍历查询
    list_for_each_entry(pPictureInfo, list_head, list) 
    {
        DEBUG_Printf("pathname:%s\n",pPictureInfo->pathname);
        switch(pPictureInfo->pictureType)
        {
            case PNG_TYPE:     fb_draw_back(SCREEN_BLACK); show_png(-1,-1,pPictureInfo->pathname); break;
            case JPEG_TYPE:    fb_draw_back(SCREEN_BLACK); show_jpeg(-1,-1,pPictureInfo->pathname); break;
            case BMP_TYPE:     fb_draw_back(SCREEN_BLACK); show_bmp(-1,-1,pPictureInfo->pathname); break;
            default :break;
        }
        sleep(2);    
    }
    return 0;
}
static int showOnePicture(const struct list_head *list_head)
{
    s_pictureInfo *pPictureInfo = NULL;

    pPictureInfo = list_first_entry(list_head, s_pictureInfo, list); 
    
        DEBUG_Printf(" id = %d\n",pPictureInfo->msgid);
        switch(pPictureInfo->pictureType)
        {
            case PNG_TYPE:     fb_draw_back(SCREEN_BLACK); show_png(-1,-1,pPictureInfo->pathname); break;
            case JPEG_TYPE:    fb_draw_back(SCREEN_BLACK); show_jpeg(-1,-1,pPictureInfo->pathname); break;
            case BMP_TYPE:     fb_draw_back(SCREEN_BLACK); show_bmp(-1,-1,pPictureInfo->pathname); break;
            default :break;
        }    
    msg_head = *list_head;
    return 0;
}

void pictureAutoPlayer(char *basePath)
{
    readFileList(basePath);
    while(1)
        showListPicture(&msg_head);
}


// 本函数实现通过触摸屏来对图片翻页显示
int pictureTouchPlayer(char *basePath)
{
    // 第一步: 触摸屏的触摸操作检测
    int fd = -1,ret = -1;
    struct input_event ev;
        
    fd = open(DEVICE_TOUCHSCREEN, O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        return -1;
    }

    readFileList(basePath);    
    showOnePicture(&msg_head);
    while (1)
    {
        memset(&ev, 0, sizeof(struct input_event));
        ret = read(fd, &ev, sizeof(struct input_event));
        if (ret != sizeof(struct input_event))
        {
            perror("read");
            close(fd);
            return -1;
        }

        // 第二步: 根据触摸坐标来翻页
        if ((ev.type == EV_ABS) && (ev.code == ABS_X))
        {
            // 确定这个是x坐标
            if ((ev.value >= 0) && (ev.value < 100))
            {
                // 上翻页
                if(&msg_head != msg_head.prev)
                    showOnePicture(msg_head.prev);
            }
            else if ((ev.value > 900) && (ev.value <= 1024))
            {
                // 下翻页                
                if(msg_head.next->next != &msg_head)
                    showOnePicture(msg_head.next);
                
            }
            else
            {
                // 不翻页
            }
        }
    }    
    close(fd);

    return 0;
    
}
View Code

 

posted @ 2019-01-12 10:05  三七鸽  阅读(460)  评论(0编辑  收藏  举报