Ruby 4.1.0dev (2026-03-28 revision c42a6338f4041a918773ccd75dd987be14085bfa)
list.h
1#ifndef PRISM_INTERNAL_LIST_H
2#define PRISM_INTERNAL_LIST_H
3
4#include <stddef.h>
5
6/*
7 * This struct represents an abstract linked list that provides common
8 * functionality. It is meant to be used any time a linked list is necessary to
9 * store data.
10 *
11 * The linked list itself operates off a set of pointers. Because the pointers
12 * are not necessarily sequential, they can be of any size. We use this fact to
13 * allow the consumer of this linked list to extend the node struct to include
14 * any data they want. This is done by using the pm_list_node_t as the first
15 * member of the struct.
16 *
17 * For example, if we want to store a list of integers, we can do the following:
18 *
19 * ```c
20 * typedef struct {
21 * pm_list_node_t node;
22 * int value;
23 * } pm_int_node_t;
24 *
25 * pm_list_t list = { 0 };
26 * pm_int_node_t *node = xmalloc(sizeof(pm_int_node_t));
27 * node->value = 5;
28 *
29 * pm_list_append(&list, &node->node);
30 * ```
31 *
32 * The pm_list_t struct is used to represent the overall linked list. It
33 * contains a pointer to the head and tail of the list. This allows for easy
34 * iteration and appending of new nodes.
35 */
36typedef struct pm_list_node {
37 /* A pointer to the next node in the list. */
38 struct pm_list_node *next;
40
41/*
42 * This represents the overall linked list. It keeps a pointer to the head and
43 * tail so that iteration is easy and pushing new nodes is easy.
44 */
45typedef struct {
46 /* The size of the list. */
47 size_t size;
48
49 /* A pointer to the head of the list. */
50 pm_list_node_t *head;
51
52 /* A pointer to the tail of the list. */
53 pm_list_node_t *tail;
54} pm_list_t;
55
56/* Returns the size of the list. */
57size_t pm_list_size(pm_list_t *list);
58
59/* Append a node to the given list. */
60void pm_list_append(pm_list_t *list, pm_list_node_t *node);
61
62#endif