on master help sign in
Implement missing idris_mkFileError for windows rts files
9da8762Heather
rts/idris_heap.c+12−12
@@ -7,11 +7,11 @@
7
7
8
8
9/* Used for initializing the heap. */
9/* Used for initializing the heap. */
10−void alloc_heap(Heap * h, size_t heap_size, size_t growth, char * old)
10+void alloc_heap(Heap * h, size_t heap_size, size_t growth, char * old)
11{
11{
12− char * mem = malloc(heap_size);
12+ char * mem = malloc(heap_size);
13 if (mem == NULL) {
13 if (mem == NULL) {
14− fprintf(stderr,
14+ fprintf(stderr,
15 "RTS ERROR: Unable to allocate heap. Requested %zd bytes.\n",
15 "RTS ERROR: Unable to allocate heap. Requested %zd bytes.\n",
16 heap_size);
16 heap_size);
17 exit(EXIT_FAILURE);
17 exit(EXIT_FAILURE);
@@ -37,8 +37,8 @@ void alloc_heap(Heap * h, size_t heap_size, size_t growth, char * old)
37void free_heap(Heap * h) {
37void free_heap(Heap * h) {
38 free(h->heap);
38 free(h->heap);
39
39
40− if (h->old != NULL) {
40+ if (h->old != NULL) {
41− free(h->old);
41+ free(h->old);
42 }
42 }
43}
43}
44
44
@@ -47,7 +47,7 @@ void free_heap(Heap * h) {
47/******************** Heap testing ********************************************/
47/******************** Heap testing ********************************************/
48void heap_check_underflow(Heap * heap) {
48void heap_check_underflow(Heap * heap) {
49 if (!(heap->heap <= heap->next)) {
49 if (!(heap->heap <= heap->next)) {
50− fprintf(stderr, "RTS ERROR: HEAP UNDERFLOW <bot %p> <cur %p>\n",
50+ fprintf(stderr, "RTS ERROR: HEAP UNDERFLOW <bot %p> <cur %p>\n",
51 heap->heap, heap->next);
51 heap->heap, heap->next);
52 exit(EXIT_FAILURE);
52 exit(EXIT_FAILURE);
53 }
53 }
@@ -55,7 +55,7 @@ void heap_check_underflow(Heap * heap) {
55
55
56void heap_check_overflow(Heap * heap) {
56void heap_check_overflow(Heap * heap) {
57 if (!(heap->next <= heap->end)) {
57 if (!(heap->next <= heap->end)) {
58− fprintf(stderr, "RTS ERROR: HEAP OVERFLOW <cur %p> <end %p>\n",
58+ fprintf(stderr, "RTS ERROR: HEAP OVERFLOW <cur %p> <end %p>\n",
59 heap->next, heap->end);
59 heap->next, heap->end);
60 exit(EXIT_FAILURE);
60 exit(EXIT_FAILURE);
61 }
61 }
@@ -65,13 +65,13 @@ int is_valid_ref(VAL v) {
65 return (v != NULL) && !(ISINT(v));
65 return (v != NULL) && !(ISINT(v));
66}
66}
67
67
68−int ref_in_heap(Heap * heap, VAL v) {
68+int ref_in_heap(Heap * heap, VAL v) {
69 return ((VAL)heap->heap <= v) && (v < (VAL)heap->next);
69 return ((VAL)heap->heap <= v) && (v < (VAL)heap->next);
70}
70}
71
71
72// Checks three important properties:
72// Checks three important properties:
73// 1. Closure.
73// 1. Closure.
74−// Check if all pointers in the _heap_ points only to heap.
74+// Check if all pointers in the _heap_ points only to heap.
75// 2. Unidirectionality. (if compact gc)
75// 2. Unidirectionality. (if compact gc)
76// All references in the heap should be are unidirectional. In other words,
76// All references in the heap should be are unidirectional. In other words,
77// more recently allocated closure can point only to earlier allocated one.
77// more recently allocated closure can point only to earlier allocated one.
@@ -79,7 +79,7 @@ int ref_in_heap(Heap * heap, VAL v) {
79//
79//
80void heap_check_pointers(Heap * heap) {
80void heap_check_pointers(Heap * heap) {
81 char* scan = NULL;
81 char* scan = NULL;
82−
82+
83 size_t item_size = 0;
83 size_t item_size = 0;
84 for(scan = heap->heap; scan < heap->next; scan += item_size) {
84 for(scan = heap->heap; scan < heap->next; scan += item_size) {
85 item_size = *((size_t*)scan);
85 item_size = *((size_t*)scan);
@@ -96,7 +96,7 @@ void heap_check_pointers(Heap * heap) {
96 if (is_valid_ref(ptr)) {
96 if (is_valid_ref(ptr)) {
97 // Check for closure.
97 // Check for closure.
98 if (!ref_in_heap(heap, ptr)) {
98 if (!ref_in_heap(heap, ptr)) {
99− fprintf(stderr,
99+ fprintf(stderr,
100 "RTS ERROR: heap closure broken. "\
100 "RTS ERROR: heap closure broken. "\
101 "<HEAP %p %p %p> <REF %p>\n",
101 "<HEAP %p %p %p> <REF %p>\n",
102 heap->heap, heap->next, heap->end, ptr);
102 heap->heap, heap->next, heap->end, ptr);
@@ -105,7 +105,7 @@ void heap_check_pointers(Heap * heap) {
105#if 0 // TODO macro
105#if 0 // TODO macro
106 // Check for unidirectionality.
106 // Check for unidirectionality.
107 if (!(ptr < heap_item)) {
107 if (!(ptr < heap_item)) {
108− fprintf(stderr,
108+ fprintf(stderr,
109 "RTS ERROR: heap unidirectionality broken:" \
109 "RTS ERROR: heap unidirectionality broken:" \
110 "<CON %p> <FIELD %p>\n",
110 "<CON %p> <FIELD %p>\n",
111 heap_item, ptr);
111 heap_item, ptr);
rts/idris_heap.h+2−2
@@ -8,7 +8,7 @@ typedef struct {
8 char* heap; // Point to bottom of heap
8 char* heap; // Point to bottom of heap
9 char* end; // Point to top of heap
9 char* end; // Point to top of heap
10 size_t size; // Size of _next_ heap. Size of current heap is /end - heap/.
10 size_t size; // Size of _next_ heap. Size of current heap is /end - heap/.
11− size_t growth; // Quantity of heap growth in bytes.
11+ size_t growth; // Quantity of heap growth in bytes.
12
12
13 char* old;
13 char* old;
14} Heap;
14} Heap;
@@ -23,7 +23,7 @@ void heap_check_all(Heap * heap);
23// Should be used _between_ gc's.
23// Should be used _between_ gc's.
24#define HEAP_CHECK(vm) heap_check_all(&(vm->heap));
24#define HEAP_CHECK(vm) heap_check_all(&(vm->heap));
25#else
25#else
26−#define HEAP_CHECK(vm)
26+#define HEAP_CHECK(vm)
27#endif // IDRIS_DEBUG
27#endif // IDRIS_DEBUG
28
28
29#endif // _IDRIS_HEAP_H
29#endif // _IDRIS_HEAP_H
rts/idris_net.c+21−24
@@ -38,15 +38,15 @@ void idrnet_free(void* ptr) {
38}
38}
39
39
40// We call this from quite a few functions. Given a textual host and an int port,
40// We call this from quite a few functions. Given a textual host and an int port,
41−// populates a struct addrinfo.
41+// populates a struct addrinfo.
42−int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
42+int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
43 int family, int socket_type) {
43 int family, int socket_type) {
44
44
45 struct addrinfo hints;
45 struct addrinfo hints;
46 // Convert port into string
46 // Convert port into string
47 char str_port[8];
47 char str_port[8];
48 sprintf(str_port, "%d", port);
48 sprintf(str_port, "%d", port);
49−
49+
50 // Set up hints structure
50 // Set up hints structure
51 memset(&hints, 0, sizeof(hints)); // zero out hints
51 memset(&hints, 0, sizeof(hints)); // zero out hints
52 hints.ai_family = family;
52 hints.ai_family = family;
@@ -57,7 +57,7 @@ int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
57 if (strlen(host) == 0) {
57 if (strlen(host) == 0) {
58 hints.ai_flags = AI_PASSIVE; // fill in IP automatically
58 hints.ai_flags = AI_PASSIVE; // fill in IP automatically
59 return getaddrinfo(NULL, str_port, &hints, address_res);
59 return getaddrinfo(NULL, str_port, &hints, address_res);
60− }
60+ }
61 return getaddrinfo(host, str_port, &hints, address_res);
61 return getaddrinfo(host, str_port, &hints, address_res);
62
62
63}
63}
@@ -75,7 +75,7 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
75 //freeaddrinfo(address_res);
75 //freeaddrinfo(address_res);
76 //printf("Lib err: bind\n");
76 //printf("Lib err: bind\n");
77 return -1;
77 return -1;
78− }
78+ }
79 return 0;
79 return 0;
80}
80}
81
81
@@ -111,7 +111,7 @@ char* idrnet_sockaddr_ipv4(void* sockaddr) {
111
111
112int idrnet_sockaddr_ipv4_port(void* sockaddr) {
112int idrnet_sockaddr_ipv4_port(void* sockaddr) {
113 struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
113 struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
114− return ((int) ntohs(addr->sin_port));
114+ return ((int) ntohs(addr->sin_port));
115}
115}
116
116
117void* idrnet_create_sockaddr() {
117void* idrnet_create_sockaddr() {
@@ -141,13 +141,13 @@ int idrnet_send_buf(int sockfd, void* data, int len) {
141}
141}
142
142
143void* idrnet_recv(int sockfd, int len) {
143void* idrnet_recv(int sockfd, int len) {
144− idrnet_recv_result* res_struct =
144+ idrnet_recv_result* res_struct =
145 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
145 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
146 char* buf = malloc(len + 1);
146 char* buf = malloc(len + 1);
147 memset(buf, 0, len + 1);
147 memset(buf, 0, len + 1);
148 int recv_res = recv(sockfd, buf, len, 0);
148 int recv_res = recv(sockfd, buf, len, 0);
149 res_struct->result = recv_res;
149 res_struct->result = recv_res;
150−
150+
151 if (recv_res > 0) { // Data was received
151 if (recv_res > 0) { // Data was received
152 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
152 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
153 }
153 }
@@ -172,7 +172,7 @@ char* idrnet_get_recv_payload(void* res_struct) {
172}
172}
173
173
174void idrnet_free_recv_struct(void* res_struct) {
174void idrnet_free_recv_struct(void* res_struct) {
175− idrnet_recv_result* i_res_struct =
175+ idrnet_recv_result* i_res_struct =
176 (idrnet_recv_result*) res_struct;
176 (idrnet_recv_result*) res_struct;
177 if (i_res_struct->payload != NULL) {
177 if (i_res_struct->payload != NULL) {
178 free(i_res_struct->payload);
178 free(i_res_struct->payload);
@@ -191,9 +191,9 @@ int idrnet_sendto(int sockfd, char* data, char* host, int port, int family) {
191 int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
191 int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
192 if (addr_res == -1) {
192 if (addr_res == -1) {
193 return -1;
193 return -1;
194− }
194+ }
195
195
196− int send_res = sendto(sockfd, data, strlen(data), 0,
196+ int send_res = sendto(sockfd, data, strlen(data), 0,
197 remote_host->ai_addr, remote_host->ai_addrlen);
197 remote_host->ai_addr, remote_host->ai_addrlen);
198 freeaddrinfo(remote_host);
198 freeaddrinfo(remote_host);
199 return send_res;
199 return send_res;
@@ -206,11 +206,11 @@ int idrnet_sendto_buf(int sockfd, void* buf, int buf_len, char* host, int port,
206 if (addr_res == -1) {
206 if (addr_res == -1) {
207 //printf("lib err: sendto getaddrinfo \n");
207 //printf("lib err: sendto getaddrinfo \n");
208 return -1;
208 return -1;
209− }
209+ }
210
210
211 buf_htonl(buf, buf_len);
211 buf_htonl(buf, buf_len);
212
212
213− int send_res = sendto(sockfd, buf, buf_len, 0,
213+ int send_res = sendto(sockfd, buf, buf_len, 0,
214 remote_host->ai_addr, remote_host->ai_addrlen);
214 remote_host->ai_addr, remote_host->ai_addrlen);
215 if (send_res == -1) {
215 if (send_res == -1) {
216 perror("lib err: sendto \n");
216 perror("lib err: sendto \n");
@@ -224,13 +224,13 @@ int idrnet_sendto_buf(int sockfd, void* buf, int buf_len, char* host, int port,
224void* idrnet_recvfrom(int sockfd, int len) {
224void* idrnet_recvfrom(int sockfd, int len) {
225/*
225/*
226 * int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
226 * int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
227− struct sockaddr *from, int *fromlen);
227+ struct sockaddr *from, int *fromlen);
228*/
228*/
229 // Allocate the required structures, and nuke them
229 // Allocate the required structures, and nuke them
230− struct sockaddr_storage* remote_addr =
230+ struct sockaddr_storage* remote_addr =
231 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
231 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
232 char* buf = (char*) malloc(len + 1);
232 char* buf = (char*) malloc(len + 1);
233− idrnet_recvfrom_result* ret =
233+ idrnet_recvfrom_result* ret =
234 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
234 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
235 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
235 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
236 memset(buf, 0, len + 1);
236 memset(buf, 0, len + 1);
@@ -240,7 +240,7 @@ void* idrnet_recvfrom(int sockfd, int len) {
240 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
240 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
241 ret->result = recv_res;
241 ret->result = recv_res;
242 // Check for failure...
242 // Check for failure...
243− if (recv_res == -1) {
243+ if (recv_res == -1) {
244 free(buf);
244 free(buf);
245 free(remote_addr);
245 free(remote_addr);
246 } else {
246 } else {
@@ -255,12 +255,11 @@ void* idrnet_recvfrom(int sockfd, int len) {
255 return ret;
255 return ret;
256}
256}
257
257
258−
259void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
258void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
260 // Allocate the required structures, and nuke them
259 // Allocate the required structures, and nuke them
261− struct sockaddr_storage* remote_addr =
260+ struct sockaddr_storage* remote_addr =
262 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
261 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
263− idrnet_recvfrom_result* ret =
262+ idrnet_recvfrom_result* ret =
264 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
263 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
265 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
264 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
266 memset(ret, 0, sizeof(idrnet_recvfrom_result));
265 memset(ret, 0, sizeof(idrnet_recvfrom_result));
@@ -269,7 +268,7 @@ void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
269 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
268 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
270 // Check for failure... But don't free the buffer! Not our job.
269 // Check for failure... But don't free the buffer! Not our job.
271 ret->result = recv_res;
270 ret->result = recv_res;
272− if (recv_res == -1) {
271+ if (recv_res == -1) {
273 free(remote_addr);
272 free(remote_addr);
274 }
273 }
275 // Payload will be NULL -- since it's been put into the user-specified buffer. We
274 // Payload will be NULL -- since it's been put into the user-specified buffer. We
@@ -298,7 +297,7 @@ void* idrnet_get_recvfrom_sockaddr(void* res_struct) {
298int idrnet_get_recvfrom_port(void* res_struct) {
297int idrnet_get_recvfrom_port(void* res_struct) {
299 idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
298 idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
300 if (recv_struct->remote_addr != NULL) {
299 if (recv_struct->remote_addr != NULL) {
301− struct sockaddr_in* remote_addr_in =
300+ struct sockaddr_in* remote_addr_in =
302 (struct sockaddr_in*) recv_struct->remote_addr;
301 (struct sockaddr_in*) recv_struct->remote_addr;
303 return ((int) ntohs(remote_addr_in->sin_port));
302 return ((int) ntohs(remote_addr_in->sin_port));
304 }
303 }
@@ -316,5 +315,3 @@ void idrnet_free_recvfrom_struct(void* res_struct) {
316 }
315 }
317 }
316 }
318}
317}
319−
320−
rts/idris_stdfgn.c+5−5
@@ -25,13 +25,13 @@ void fileClose(void* h) {
25}
25}
26
26
27int fileEOF(void* h) {
27int fileEOF(void* h) {
28− FILE* f = (FILE*)h;
28+ FILE* f = (FILE*)h;
29− return feof(f);
29+ return feof(f);
30}
30}
31
31
32int fileError(void* h) {
32int fileError(void* h) {
33− FILE* f = (FILE*)h;
33+ FILE* f = (FILE*)h;
34− return ferror(f);
34+ return ferror(f);
35}
35}
36
36
37int idris_writeStr(void* h, char* str) {
37int idris_writeStr(void* h, char* str) {
@@ -107,5 +107,5 @@ VAL idris_mkFileError(VM* vm) {
107}
107}
108
108
109void idris_forceGC(void* vm) {
109void idris_forceGC(void* vm) {
110− idris_gc((VM*)vm);
110+ idris_gc((VM*)vm);
111}
111}
rts/windows/idris_net.c+6−7
@@ -12,7 +12,7 @@
12#include <Ws2tcpip.h>
12#include <Ws2tcpip.h>
13
13
14
14
15−// inet_ntop isn't defined in the old mingw delivered with GHC,
15+// inet_ntop isn't defined in the old mingw delivered with GHC,
16// so put the prototype here, cribbed from a newer version of it
16// so put the prototype here, cribbed from a newer version of it
17WINSOCK_API_LINKAGE LPCSTR WSAAPI InetNtopA(INT Family, PVOID pAddr,
17WINSOCK_API_LINKAGE LPCSTR WSAAPI InetNtopA(INT Family, PVOID pAddr,
18 LPSTR pStringBuf, size_t StringBufSize);
18 LPSTR pStringBuf, size_t StringBufSize);
@@ -50,7 +50,7 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
50 // Convert port into string
50 // Convert port into string
51 char str_port[8];
51 char str_port[8];
52 sprintf(str_port, "%d", port);
52 sprintf(str_port, "%d", port);
53−
53+
54 if (!check_init()) {
54 if (!check_init()) {
55 return -1;
55 return -1;
56 }
56 }
@@ -73,7 +73,7 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
73 int bind_res = bind(sockfd, address_res->ai_addr, address_res->ai_addrlen);
73 int bind_res = bind(sockfd, address_res->ai_addr, address_res->ai_addrlen);
74 if (bind_res == -1) {
74 if (bind_res == -1) {
75 return -1;
75 return -1;
76− }
76+ }
77
77
78 return 0;
78 return 0;
79}
79}
@@ -152,13 +152,13 @@ void* idrnet_recv(int sockfd, int len) {
152 if (!check_init()) {
152 if (!check_init()) {
153 return NULL;
153 return NULL;
154 }
154 }
155− idrnet_recv_result* res_struct =
155+ idrnet_recv_result* res_struct =
156 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
156 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
157
157
158 char* buf = malloc(len + 1);
158 char* buf = malloc(len + 1);
159 int recv_res = recv(sockfd, buf, len, 0);
159 int recv_res = recv(sockfd, buf, len, 0);
160 res_struct->result = recv_res;
160 res_struct->result = recv_res;
161−
161+
162 if (recv_res > 0) { // Data was received
162 if (recv_res > 0) { // Data was received
163 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
163 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
164 }
164 }
@@ -182,7 +182,7 @@ char* idrnet_get_recv_payload(void* res_struct) {
182}
182}
183
183
184void idrnet_free_recv_struct(void* res_struct) {
184void idrnet_free_recv_struct(void* res_struct) {
185− idrnet_recv_result* i_res_struct =
185+ idrnet_recv_result* i_res_struct =
186 (idrnet_recv_result*) res_struct;
186 (idrnet_recv_result*) res_struct;
187 if (i_res_struct->payload != NULL) {
187 if (i_res_struct->payload != NULL) {
188 free(i_res_struct->payload);
188 free(i_res_struct->payload);
@@ -193,4 +193,3 @@ void idrnet_free_recv_struct(void* res_struct) {
193int idrnet_errno() {
193int idrnet_errno() {
194 return errno;
194 return errno;
195}
195}
196−
rts/windows/idris_stdfgn.c+24−5
@@ -26,13 +26,13 @@ void fileClose(void* h) {
26}
26}
27
27
28int fileEOF(void* h) {
28int fileEOF(void* h) {
29− FILE* f = (FILE*)h;
29+ FILE* f = (FILE*)h;
30− return feof(f);
30+ return feof(f);
31}
31}
32
32
33int fileError(void* h) {
33int fileError(void* h) {
34− FILE* f = (FILE*)h;
34+ FILE* f = (FILE*)h;
35− return ferror(f);
35+ return ferror(f);
36}
36}
37
37
38int idris_writeStr(void* h, char* str) {
38int idris_writeStr(void* h, char* str) {
@@ -77,6 +77,25 @@ VAL idris_time() {
77 return MKBIGI(t);
77 return MKBIGI(t);
78}
78}
79
79
80+VAL idris_mkFileError(VM* vm) {
81+ VAL result;
82+ switch(errno) {
83+ // Make sure this corresponds to the FileError structure in
84+ // Prelude.File
85+ case ENOENT:
86+ idris_constructor(result, vm, 2, 0, 0);
87+ break;
88+ case EACCES:
89+ idris_constructor(result, vm, 3, 0, 0);
90+ break;
91+ default:
92+ idris_constructor(result, vm, 4, 1, 0);
93+ idris_setConArg(result, 0, MKINT((intptr_t)errno));
94+ break;
95+ }
96+ return result;
97+}
98+
80void idris_forceGC(void* vm) {
99void idris_forceGC(void* vm) {
81− idris_gc((VM*)vm);
100+ idris_gc((VM*)vm);
82}
101}