blob: db8842cc4641017808e8215c4e6d9dd1e003e7f9 (
plain) (
blame)
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
|
--- ./newlib/libc/sys/psp/fdman.c.orig 2012-01-25 19:33:12.000000000 +0000
+++ ./newlib/libc/sys/psp/fdman.c 2012-01-25 19:33:12.000000000 +0000
@@ -0,0 +1,115 @@
+/*
+ * PSP Software Development Kit - http://www.pspdev.org
+ * -----------------------------------------------------------------------
+ * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
+ *
+ * fdman.c - File descriptor management.
+ *
+ * Copyright (c) 2006 Rafael Cabezas <rafpsp@gmail.com>
+ */
+#include <psptypes.h>
+#include <errno.h>
+#include "fdman.h"
+
+extern int pspDisableInterrupts();
+extern void pspEnableInterrupts(int);
+
+static __psp_descriptormap_type __psp_descriptor_data_pool[__PSP_FILENO_MAX];
+__psp_descriptormap_type *__psp_descriptormap [__PSP_FILENO_MAX];
+
+void __psp_fdman_init()
+{
+ int scefd;
+
+ /* Initialize descriptor data*/
+ memset(__psp_descriptor_data_pool, 0, sizeof(__psp_descriptormap_type) *__PSP_FILENO_MAX);
+ /* Initialize descriptor map*/
+ memset(__psp_descriptormap, 0, sizeof(__psp_descriptormap_type*)*__PSP_FILENO_MAX);
+
+ scefd = sceKernelStdin();
+ if (scefd >= 0) {
+ __psp_descriptormap[0] = &__psp_descriptor_data_pool[0];
+ __psp_descriptormap[0]->sce_descriptor = scefd;
+ __psp_descriptormap[0]->type = __PSP_DESCRIPTOR_TYPE_TTY;
+ }
+ scefd = sceKernelStdout();
+ if (scefd >= 0) {
+ __psp_descriptormap[1] = &__psp_descriptor_data_pool[1];
+ __psp_descriptormap[1]->sce_descriptor = scefd;
+ __psp_descriptormap[1]->type = __PSP_DESCRIPTOR_TYPE_TTY;
+ }
+ scefd = sceKernelStderr();
+ if (scefd >= 0) {
+ __psp_descriptormap[2] = &__psp_descriptor_data_pool[2];
+ __psp_descriptormap[2]->sce_descriptor = scefd;
+ __psp_descriptormap[2]->type = __PSP_DESCRIPTOR_TYPE_TTY;
+ }
+}
+
+int __psp_fdman_get_new_descriptor()
+{
+ int i = 0;
+ int inten;
+
+ inten = pspDisableInterrupts(); /* lock here to make thread safe */
+ for (i = 0; i < __PSP_FILENO_MAX; i++) {
+ if (__psp_descriptormap[i] == NULL) {
+ __psp_descriptormap[i] = &__psp_descriptor_data_pool[i];
+ __psp_descriptormap[i]->ref_count++;
+ pspEnableInterrupts(inten); /* release lock */
+ return i;
+ }
+ }
+ pspEnableInterrupts(inten); /* release lock */
+
+ errno = ENOMEM;
+ return -1;
+}
+
+int __psp_fdman_get_dup_descriptor(int fd)
+{
+ int i = 0;
+ int inten;
+
+ if (!__PSP_IS_FD_VALID(fd)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ inten = pspDisableInterrupts(); /* lock here to make thread safe */
+ for (i = 0; i < __PSP_FILENO_MAX; i++) {
+ if (__psp_descriptormap[i] == NULL) {
+ __psp_descriptormap[i] = &__psp_descriptor_data_pool[fd];
+ __psp_descriptormap[i]->ref_count++;
+ pspEnableInterrupts(inten); /* release lock */
+ return i;
+ }
+ }
+ pspEnableInterrupts(inten); /* release lock */
+
+ errno = ENOMEM;
+ return -1;
+}
+
+void __psp_fdman_release_descriptor(int fd)
+{
+ if (!__PSP_IS_FD_VALID(fd)) {
+ errno = EBADF;
+ return;
+ }
+
+ __psp_descriptormap[fd]->ref_count--;
+
+ if (__psp_descriptormap[fd]->ref_count == 0) {
+
+ if (__psp_descriptormap[fd]->filename != NULL) {
+ free(__psp_descriptormap[fd]->filename);
+ }
+ __psp_descriptormap[fd]->filename = NULL;
+ __psp_descriptormap[fd]->sce_descriptor = 0;
+ __psp_descriptormap[fd]->type = 0;
+ __psp_descriptormap[fd]->flags = 0;
+
+ }
+ __psp_descriptormap[fd] = NULL;
+}
|