Data Structures in the Kernel Sarah Diesburg COP 5641.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Chapter 24 Lists, Stacks, and Queues
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked List Variations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
1 Week 8 Locking, Linked Lists, Scheduling Algorithms Classes COP4610 / CGS5765 Florida State University.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures: A Pseudocode Approach with C
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
Data Types in the Kernel Ted Baker  Andy Wang CIS 4930 / COP 5641.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Introduction to Data Structures Systems Programming Concepts.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Kernel data structures. outline linked list queues maps binary trees algorithmic complexity.
Cpt S 122 – Data Structures Abstract Data Types
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Pointers and Linked Lists
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
12 C Data Structures.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Programming Abstractions
Stack and Queue APURBO DATTA.
Data Structures and Database Applications Queues in C#
Circular Buffers, Linked Lists
Programming Abstractions
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Chapter 16 Linked Structures
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Pointers & Dynamic Data Structures
Presentation transcript:

Data Structures in the Kernel Sarah Diesburg COP 5641

Linked Lists Linux provides a standard implementation of circular, doubly linked lists  List functions perform no locking To use the list mechanism, include, which contains struct list_head { struct list_head *next, *prev; };

Linked Lists To use the Linux list facility  Need to embed a list_head in the structures that make up the list struct todo_struct { struct list_head list; int priority; /* driver specific */ /*... add other driver-specific fields */ };

Linked Lists

More Fun with Linked Lists list_head sorted_by_char list_head sorted_by_num A 3 B 1 C 2 Can allocate list elements as an array What if a structure owns its own list?

Linked Lists The head of the list is usually a standalone structure To declare and initialize a list head, call struct list_head todo_list; INIT_LIST_HEAD(&todo_list); To initialize at compile time, call LIST_HEAD(todo_list);

Linked Lists See for a list of list functions /* add the new entry after the list head */ /* use it to build stacks */ list_add(struct list_head *new, struct list_head *head); /* add the new entry before the list head (tail) */ /* use it to build FIFO queues */ list_add_tail(struct list_head *new, struct list_head *head);

Linked Lists /* the given entry is removed from the list */ /* if the entry might be reinserted into another list, call list_del_init */ list_del(struct list_head *entry); list_del_init(struct list_head *entry); /* remove the entry from one list and insert into another list */ list_move(struct list_head *entry, struct list_head *head); list_move_tail(struct list_head *entry, struct list_head *head); /* return a nonzero value if the given list is empty */ list_empty(struct list_head *head);

Linked Lists /* insert a list immediately after head */ list_splice(struct list_head *list, struct list_head *head); To access the data structure itself, use list_entry(struct list_head *ptr, type_of_struct, field_name);  Same as container_of()  ptr is a pointer to a struct list_head entry

Linked Lists  type_of_struct is the type of the structure containing the ptr  field_name is the name of the list field within the structure  Example struct todo_struct *todo_ptr = list_entry(listptr, struct todo_struct, list); #define container_of(ptr, type, member) ({ const typeof(((type *)0->member) *__mptr = (ptr); (type *) ((char *)__mptr – offsetof(type, member)); }) Type checking

Linked Lists To traverse the linked list, one can follow the prev and next pointers void todo_add_entry(struct todo_struct *new) { struct list_head *ptr; struct todo_struct *entry; for (ptr = todo_list.next; ptr != &todo_list; ptr = ptr->next) { entry = list_entry(ptr, struct todo_struct, list); if (entry->priority priority) { list_add_tail(&new->list, ptr); return; } list_add_tail(&new->list, &todo_struct) }

Linked Lists One can also use predefined macros void todo_add_entry(struct todo_struct *new) { struct list_head *ptr; struct todo_struct *entry; list_for_each(ptr, &todo_list) { entry = list_entry(ptr, struct todo_struct, list); if (entry->priority priority) { list_add_tail(&new->list, ptr); return; } list_add_tail(&new->list, &todo_struct) }

Linked Lists Predefined macros avoid simple programming errors  See /* creates a loop that executes once with cursor pointing at each successive entry */ /* be careful about changing the list while iterating */ list_for_each(struct list_head *cursor, struct list_head *list) /* iterates backward */ list_for_each_prev(struct list_head *cursor, struct list_head *list)

Linked Lists /* for deleting entries in the list */ /* stores the next entry in next at the beginning of the loop */ list_for_each_safe(struct list_head *cursor, struct list_head *next, struct list_head *list) /* ease the process of dealing with a list containing a given type */ /* no need to call list_entry inside the loop */ list_for_each_entry(type *cursor, struct list_head *list, member) list_for_each_entry_safe(type *cursor, type *next, struct list_head *list, member)

Queues Producer/consumer model Have we seen this before?

Queues Called kfifo in Two main operations  Enqueue called kfifo_in  Dequeue called kfifo_out

Queues Create a queue int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask);  fifo – pointer to a struct kfifo  size – total size of kfifo  gfp_mask – memory alloctation flag (e.g. GFP_KERNEL )

Queues Enqueuing data unsigned int kfifo_in(struct kfifo *fifo, const void *from, unsigned int len);  Copies the len bytes starting at from into the queue represented by fifo  Returns number of bytes enqueued May return less than requested if no room

Queues Dequeuing data unsigned int kfifo_out(struct kfifo *fifo, void *to, unsigned int len);  Copies at most len bytes from the queue pointed at by fifo to the buffer pointed at by to  Returns number of bytes dequeued

Queues kfifo_out_peek  Same as kfifo_out, but does not actually dequeue data kfifo_size  Obtain size of buffer in fifo kfifo_len/kfifo_available  Obtain number of bytes used/number of bytes available Other macros  kfifo_is_empty  kfifo_is_full

Queues Reset the queue static inline void kfifo_reset(struct kfifo *fifo); Destroy the queue void kfifo_free(struct kfifo *fifo);

Maps Collection of unique keys, where each key is associated with specific value Relationship between key and its value is called a mapping Linux implementation used to map a unique ID number (UID) to a pointer  Any guess as to the backing store data structure?

Maps idr data structure used to map UID to an associated kernel data structure Initialize an idr void idr_init(struct idr *idp);

Maps Allocating a new UID  Done in two steps so that backing store resize does not need to lock 1. int idr_pre_get(struct idr *idp, gfp_t gfp_mask);  Resizes the backing tree

Maps 2. int idr_get_new(struct idr *idp, void *ptr, int *id);  Uses the idr pointed at by idp to allocate a new UID and associate it with the pointer ptr  On success, returns zero and stores the new UID in id  On error, returns a nonzero error code: - EAGAIN if you need to (again) call idr_pre_get() and -ENOSPC if the idr is full

Maps Look up a UID void *idr_find(struct idr *idp, int id);  On success, returns pointer associated with the UID in the idr pointed at by idp  On error, the function returns NULL

Maps Remove a UID from an idr void idr_remove(struct idr *idp, int id); Destroy entire idr 1. void idr_remove_all(struct idr *idp); 2. void idr_destroy(struct idr *idp);

Binary Trees Linux uses red-black trees called rbtrees  Self-balancing binary search tree Does not provide search and insert routines – must define your own  So we may use our own comparison operators when traversing the tree

Allocating a rbtree The root of an rbtree is represented by the rb_root structure To create a new tree, we allocate a new rb_root and initialize it to the special value RB_ROOT struct rb_root root = RB_ROOT;

Searching a rbtree Searching  The following function implements a search of Linux’s page cache for a chunk of a file  Each inode has its own rbtree, keyed off of page offsets into file  This function thus searches the given inode’s rbtree for a matching offset value

rbtree Searching Example struct page * rb_search_page_cache(struct inode *inode, unsigned long offset) { struct rb_node *n = inode->i_rb_page_cache.rb_node; while (n) { struct page *page = rb_entry(n, struct page, rb_page_cache); if (offset offset) n = n->rb_left; else if (offset > page->offset) n = n->rb_right; else return page; } return NULL; }

rbtree Searching and Adding Example struct page * rb_insert_page_cache(struct inode *inode, unsigned long offset, struct rb_node *node) { struct rb_node **p = &inode->i_rb_page_cache.rb_node; struct rb_node *parent = NULL; struct page *page; while (*p) { parent = *p; page = rb_entry(parent, struct page, rb_page_cache);

rbtree Searching and Adding Example if (offset offset) p = &(*p)->rb_left; else if (offset > page->offset) p = &(*p)->rb_right; else return page; } /* Insert new node */ rb_link_node(node, parent, p); /* Perform tree rebalancing */ rb_insert_color(node, &inode->i_rb_page_cache); return NULL; }

What to Use? GoalStructure Iteration over dataLinked lists Producer/consumer patterQueue (FIFO) Map a UID to an objectMaps Store large amount of data and look it up effectively Red-black tree