-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfd.c
More file actions
157 lines (126 loc) · 3.51 KB
/
fd.c
File metadata and controls
157 lines (126 loc) · 3.51 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "fd.h"
#include "common.h"
/* 전역 파일 디스크립터 테이블 */
struct fd_table global_fd_table;
/* UART 파일 디스크립터 연산 */
static int uart_fd_read(void *ctx, void *buf, size_t count) {
(void)ctx;
char *cbuf = (char *)buf;
size_t i;
for (i = 0; i < count; i++) {
if (!uart_rx_ready()) {
break;
}
cbuf[i] = uart_getchar();
}
return (int)i;
}
static int uart_fd_write(void *ctx, const void *buf, size_t count) {
(void)ctx;
const char *cbuf = (const char *)buf;
for (size_t i = 0; i < count; i++) {
uart_putchar(cbuf[i]);
}
return (int)count;
}
static int uart_fd_poll(void *ctx) {
(void)ctx;
int flags = 0;
/* UART는 항상 쓰기 가능 */
flags |= FD_WRITABLE;
/* 읽기 가능한지 확인 */
if (uart_rx_ready()) {
flags |= FD_READABLE;
}
return flags;
}
static void uart_fd_close(void *ctx) {
(void)ctx;
/* UART는 실제로 닫을 수 없음 */
}
struct fd_ops uart_fd_ops = {
.read = uart_fd_read,
.write = uart_fd_write,
.poll = uart_fd_poll,
.close = uart_fd_close,
};
/* 파일 디스크립터 서브시스템 초기화 */
void fd_init(void) {
for (int i = 0; i < MAX_FDS; i++) {
global_fd_table.fds[i].fd_num = i;
global_fd_table.fds[i].type = FD_TYPE_UNUSED;
global_fd_table.fds[i].flags = 0;
global_fd_table.fds[i].context = NULL;
global_fd_table.fds[i].ops = NULL;
global_fd_table.fds[i].ref_count = 0;
}
global_fd_table.next_fd = 0;
printf("File descriptor subsystem initialized\n");
}
/* 새 파일 디스크립터 할당 */
int fd_alloc(int type, void *context, struct fd_ops *ops) {
for (int i = 0; i < MAX_FDS; i++) {
int fd_num = (global_fd_table.next_fd + i) % MAX_FDS;
struct fd *fd = &global_fd_table.fds[fd_num];
if (fd->type == FD_TYPE_UNUSED) {
fd->type = type;
fd->flags = 0;
fd->context = context;
fd->ops = ops;
fd->ref_count = 1;
global_fd_table.next_fd = (fd_num + 1) % MAX_FDS;
printf("FD: Allocated fd %d (type=%d)\n", fd_num, type);
return fd_num;
}
}
printf("FD: No free file descriptors\n");
return -1;
}
/* 파일 디스크립터 구조체 가져오기 */
struct fd *fd_get(int fd_num) {
if (fd_num < 0 || fd_num >= MAX_FDS) {
return NULL;
}
struct fd *fd = &global_fd_table.fds[fd_num];
if (fd->type == FD_TYPE_UNUSED) {
return NULL;
}
return fd;
}
/* 파일 디스크립터 닫기 */
int fd_close(int fd_num) {
struct fd *fd = fd_get(fd_num);
if (!fd) {
return -1;
}
fd->ref_count--;
if (fd->ref_count <= 0) {
if (fd->ops && fd->ops->close) {
fd->ops->close(fd->context);
}
fd->type = FD_TYPE_UNUSED;
fd->flags = 0;
fd->context = NULL;
fd->ops = NULL;
fd->ref_count = 0;
printf("FD: Closed fd %d\n", fd_num);
}
return 0;
}
/* 이벤트에 대해 파일 디스크립터 폴링 */
int fd_poll(int fd_num) {
struct fd *fd = fd_get(fd_num);
if (!fd || !fd->ops || !fd->ops->poll) {
return 0;
}
int flags = fd->ops->poll(fd->context);
fd->flags = flags;
return flags;
}
/* 파일 디스크립터 플래그 업데이트 */
void fd_update_flags(int fd_num, int flags) {
struct fd *fd = fd_get(fd_num);
if (fd) {
fd->flags = flags;
}
}