xref: /minix/minix/fs/ptyfs/node.c (revision 0a6a1f1d)
1 /* PTYFS slave node management */
2 /*
3  * While the interface of this module should be flexible enough to implement
4  * various memory management approaches, the current code simply relies on
5  * NR_PTYS being small enough to preallocate all data structures.  In the
6  * future, NR_PTYS will no longer be a system-global definition, and future
7  * implementations of this module should not rely on NR_PTYS at all.
8  */
9 
10 #include <minix/drivers.h>
11 
12 #include "node.h"
13 
14 static bitchunk_t node_map[BITMAP_CHUNKS(NR_PTYS)];
15 static struct node_data node_data[NR_PTYS];
16 
17 /*
18  * Initialize the node module.
19  */
20 void
21 init_nodes(void)
22 {
23 
24 	memset(&node_map, 0, sizeof(node_map));
25 }
26 
27 /*
28  * Allocate a node with a given node index number, and save node data for it.
29  * It is possible that the node is in use already; in that case, only update
30  * its associated data.  Return OK on success, or an error code on failure.
31  */
32 int
33 set_node(node_t index, struct node_data * data)
34 {
35 
36 	if (index >= NR_PTYS)
37 		return ENOMEM;
38 
39 	SET_BIT(node_map, index);
40 
41 	node_data[index] = *data;
42 
43 	return OK;
44 }
45 
46 /*
47  * Deallocate a node using its node index number.  This function always
48  * succeeds, intentionally ignoring the case that the node was not allocated.
49  */
50 void
51 clear_node(node_t index)
52 {
53 
54 	UNSET_BIT(node_map, index);
55 }
56 
57 /*
58  * Return a pointer to the node data associated with the given node index
59  * number.  If the node is not allocated, return NULL.
60  */
61 struct node_data *
62 get_node(node_t index)
63 {
64 
65 	if (index >= NR_PTYS || !GET_BIT(node_map, index))
66 		return NULL;
67 
68 	return &node_data[index];
69 }
70 
71 /*
72  * Return the highest allocated node index number, plus one.  This value is
73  * used to check given node indices and limit linear iterations.
74  */
75 node_t
76 get_max_node(void)
77 {
78 
79 	/*
80 	 * NR_PTYS is low enough that we can always return it instead of
81 	 * tracking the actual value.
82 	 */
83 	return NR_PTYS;
84 }
85