\subsection{Useful general-purpose kernel APIs}

\begin{frame}
  \frametitle{Memory/string utilities}
  \begin{itemize}
  \item In \code{linux/string.h}
    \begin{itemize}
    \item Memory-related: \code{memset}, \code{memcpy},
      \code{memmove}, \code{memscan}, \code{memcmp}, \code{memchr}
    \item String-related: \code{strcpy}, \code{strcat}, \code{strcmp},
      \code{strchr}, \code{strrchr}, \code{strlen} and variants
    \item Allocate and copy a string: \code{kstrdup}, \code{kstrndup}
    \item Allocate and copy a memory area: \code{kmemdup}
    \end{itemize}
  \item In \code{linux/kernel.h}
    \begin{itemize}
    \item String to int conversion: \code{simple_strtoul},
      \code{simple_strtol}, \code{simple_strtoull},
      \code{simple_strtoll}
    \item Other string functions: \code{sprintf}, \code{sscanf}
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Linked lists}
  \begin{itemize}
  \item Convenient linked-list facility in \code{linux/list.h}
    \begin{itemize}
    \item Used in thousands of places in the kernel
    \end{itemize}
  \item Add a \code{struct list_head} member to the structure whose
    instances will be part of the linked list. It is usually named
    \code{node} when each instance needs to only be part of a single
    list.
  \item Define the list with the \code{LIST_HEAD} macro for a global
    list, or define a \code{struct list_head} element and initialize
    it with \code{INIT_LIST_HEAD} for lists embedded in a structure.
  \item Then use the \code{list_*()} API to manipulate the list
    \begin{itemize}
    \item Add elements: \code{list_add()}, \code{list_add_tail()}
    \item Remove, move or replace elements: \code{list_del()},
      \code{list_move()}, \code{list_move_tail()},
      \code{list_replace()}
    \item Test the list: \code{list_empty()}
    \item Iterate over the list: \code{list_for_each_*()} family of macros
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Linked Lists Examples (1)}
  \begin{itemize}
  \item From \code{include/linux/atmel_tc.h}
\begin{minted}{c}
/*
 * Definition of a list element, with a
 * struct list_head member
 */
struct atmel_tc
{
    /* some members */
    struct list_head node;
};
\end{minted}
\end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Linked Lists Examples (2)}
  \begin{itemize}
  \item From \code{drivers/misc/atmel_tclib.c}
  \end{itemize}
\begin{minted}[fontsize=\footnotesize]{c}
/* Define the global list */
static LIST_HEAD(tc_list);

static int __init tc_probe(struct platform_device *pdev) {
    struct atmel_tc *tc;
    tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
    /* Add an element to the list */
    list_add_tail(&tc->node, &tc_list);
}

struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
{
    struct atmel_tc *tc;
    /* Iterate over the list elements */
    list_for_each_entry(tc, &tc_list, node) {
        /* Do something with tc */
    }
    [...]
}
\end{minted}
\end{frame}
