summaryrefslogtreecommitdiff
path: root/minix/fs/ptyfs/node.c
blob: 0840eb6697d6793557a3cd089e5afc7e7da203e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* PTYFS slave node management */
/*
 * While the interface of this module should be flexible enough to implement
 * various memory management approaches, the current code simply relies on
 * NR_PTYS being small enough to preallocate all data structures.  In the
 * future, NR_PTYS will no longer be a system-global definition, and future
 * implementations of this module should not rely on NR_PTYS at all.
 */

#include <minix/drivers.h>

#include "node.h"

static bitchunk_t node_map[BITMAP_CHUNKS(NR_PTYS)];
static struct node_data node_data[NR_PTYS];

/*
 * Initialize the node module.
 */
void
init_nodes(void)
{

	memset(&node_map, 0, sizeof(node_map));
}

/*
 * Allocate a node with a given node index number, and save node data for it.
 * It is possible that the node is in use already; in that case, only update
 * its associated data.  Return OK on success, or an error code on failure.
 */
int
set_node(node_t index, struct node_data * data)
{

	if (index >= NR_PTYS)
		return ENOMEM;

	SET_BIT(node_map, index);

	node_data[index] = *data;

	return OK;
}

/*
 * Deallocate a node using its node index number.  This function always
 * succeeds, intentionally ignoring the case that the node was not allocated.
 */
void
clear_node(node_t index)
{

	UNSET_BIT(node_map, index);
}

/*
 * Return a pointer to the node data associated with the given node index
 * number.  If the node is not allocated, return NULL.
 */
struct node_data *
get_node(node_t index)
{

	if (index >= NR_PTYS || !GET_BIT(node_map, index))
		return NULL;

	return &node_data[index];
}

/*
 * Return the highest allocated node index number, plus one.  This value is
 * used to check given node indices and limit linear iterations.
 */
node_t
get_max_node(void)
{

	/*
	 * NR_PTYS is low enough that we can always return it instead of
	 * tracking the actual value.
	 */
	return NR_PTYS;
}