on master help sign in
Implement missing idris_mkFileError for windows rts files

- work around error: undefined reference to `idris_mkFileError' - minor code formatting corrections (use 4 spaces, no tailoring spaces)

commit9da8762e6fff77b3c3fe1f1068c91c5be0128a25 authorHeather <heather@live.ru> dateThu 12 Nov 2015, 13:22 (11 years ago) parent913cddb
6 files changed+70−55
rts/idris_heap.c+12−12 rts/idris_heap.h+2−2 rts/idris_net.c+21−24 rts/idris_stdfgn.c+5−5 rts/windows/idris_net.c+6−7 rts/windows/idris_stdfgn.c+24−5
rts/idris_heap.c+12−12
@@ -7,11 +7,11 @@
77
88
99/* 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)
1111{
12− char * mem = malloc(heap_size);
12+ char * mem = malloc(heap_size);
1313 if (mem == NULL) {
14− fprintf(stderr,
14+ fprintf(stderr,
1515 "RTS ERROR: Unable to allocate heap. Requested %zd bytes.\n",
1616 heap_size);
1717 exit(EXIT_FAILURE);
@@ -37,8 +37,8 @@ void alloc_heap(Heap * h, size_t heap_size, size_t growth, char * old)
3737void free_heap(Heap * h) {
3838 free(h->heap);
3939
40− if (h->old != NULL) {
41− free(h->old);
40+ if (h->old != NULL) {
41+ free(h->old);
4242 }
4343}
4444
@@ -47,7 +47,7 @@ void free_heap(Heap * h) {
4747/******************** Heap testing ********************************************/
4848void heap_check_underflow(Heap * heap) {
4949 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",
5151 heap->heap, heap->next);
5252 exit(EXIT_FAILURE);
5353 }
@@ -55,7 +55,7 @@ void heap_check_underflow(Heap * heap) {
5555
5656void heap_check_overflow(Heap * heap) {
5757 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",
5959 heap->next, heap->end);
6060 exit(EXIT_FAILURE);
6161 }
@@ -65,13 +65,13 @@ int is_valid_ref(VAL v) {
6565 return (v != NULL) && !(ISINT(v));
6666}
6767
68−int ref_in_heap(Heap * heap, VAL v) {
68+int ref_in_heap(Heap * heap, VAL v) {
6969 return ((VAL)heap->heap <= v) && (v < (VAL)heap->next);
7070}
7171
7272// Checks three important properties:
7373// 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.
7575// 2. Unidirectionality. (if compact gc)
7676// All references in the heap should be are unidirectional. In other words,
7777// more recently allocated closure can point only to earlier allocated one.
@@ -79,7 +79,7 @@ int ref_in_heap(Heap * heap, VAL v) {
7979//
8080void heap_check_pointers(Heap * heap) {
8181 char* scan = NULL;
82−
82+
8383 size_t item_size = 0;
8484 for(scan = heap->heap; scan < heap->next; scan += item_size) {
8585 item_size = *((size_t*)scan);
@@ -96,7 +96,7 @@ void heap_check_pointers(Heap * heap) {
9696 if (is_valid_ref(ptr)) {
9797 // Check for closure.
9898 if (!ref_in_heap(heap, ptr)) {
99− fprintf(stderr,
99+ fprintf(stderr,
100100 "RTS ERROR: heap closure broken. "\
101101 "<HEAP %p %p %p> <REF %p>\n",
102102 heap->heap, heap->next, heap->end, ptr);
@@ -105,7 +105,7 @@ void heap_check_pointers(Heap * heap) {
105105#if 0 // TODO macro
106106 // Check for unidirectionality.
107107 if (!(ptr < heap_item)) {
108− fprintf(stderr,
108+ fprintf(stderr,
109109 "RTS ERROR: heap unidirectionality broken:" \
110110 "<CON %p> <FIELD %p>\n",
111111 heap_item, ptr);
rts/idris_heap.h+2−2
@@ -8,7 +8,7 @@ typedef struct {
88 char* heap; // Point to bottom of heap
99 char* end; // Point to top of heap
1010 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.
1212
1313 char* old;
1414} Heap;
@@ -23,7 +23,7 @@ void heap_check_all(Heap * heap);
2323// Should be used _between_ gc's.
2424#define HEAP_CHECK(vm) heap_check_all(&(vm->heap));
2525#else
26−#define HEAP_CHECK(vm)
26+#define HEAP_CHECK(vm)
2727#endif // IDRIS_DEBUG
2828
2929#endif // _IDRIS_HEAP_H
rts/idris_net.c+21−24
@@ -38,15 +38,15 @@ void idrnet_free(void* ptr) {
3838}
3939
4040// We call this from quite a few functions. Given a textual host and an int port,
41−// populates a struct addrinfo.
42−int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
41+// populates a struct addrinfo.
42+int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
4343 int family, int socket_type) {
4444
4545 struct addrinfo hints;
4646 // Convert port into string
4747 char str_port[8];
4848 sprintf(str_port, "%d", port);
49−
49+
5050 // Set up hints structure
5151 memset(&hints, 0, sizeof(hints)); // zero out hints
5252 hints.ai_family = family;
@@ -57,7 +57,7 @@ int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
5757 if (strlen(host) == 0) {
5858 hints.ai_flags = AI_PASSIVE; // fill in IP automatically
5959 return getaddrinfo(NULL, str_port, &hints, address_res);
60− }
60+ }
6161 return getaddrinfo(host, str_port, &hints, address_res);
6262
6363}
@@ -75,7 +75,7 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
7575 //freeaddrinfo(address_res);
7676 //printf("Lib err: bind\n");
7777 return -1;
78− }
78+ }
7979 return 0;
8080}
8181
@@ -111,7 +111,7 @@ char* idrnet_sockaddr_ipv4(void* sockaddr) {
111111
112112int idrnet_sockaddr_ipv4_port(void* sockaddr) {
113113 struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
114− return ((int) ntohs(addr->sin_port));
114+ return ((int) ntohs(addr->sin_port));
115115}
116116
117117void* idrnet_create_sockaddr() {
@@ -141,13 +141,13 @@ int idrnet_send_buf(int sockfd, void* data, int len) {
141141}
142142
143143void* idrnet_recv(int sockfd, int len) {
144− idrnet_recv_result* res_struct =
144+ idrnet_recv_result* res_struct =
145145 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
146146 char* buf = malloc(len + 1);
147147 memset(buf, 0, len + 1);
148148 int recv_res = recv(sockfd, buf, len, 0);
149149 res_struct->result = recv_res;
150−
150+
151151 if (recv_res > 0) { // Data was received
152152 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
153153 }
@@ -172,7 +172,7 @@ char* idrnet_get_recv_payload(void* res_struct) {
172172}
173173
174174void idrnet_free_recv_struct(void* res_struct) {
175− idrnet_recv_result* i_res_struct =
175+ idrnet_recv_result* i_res_struct =
176176 (idrnet_recv_result*) res_struct;
177177 if (i_res_struct->payload != NULL) {
178178 free(i_res_struct->payload);
@@ -191,9 +191,9 @@ int idrnet_sendto(int sockfd, char* data, char* host, int port, int family) {
191191 int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
192192 if (addr_res == -1) {
193193 return -1;
194− }
194+ }
195195
196− int send_res = sendto(sockfd, data, strlen(data), 0,
196+ int send_res = sendto(sockfd, data, strlen(data), 0,
197197 remote_host->ai_addr, remote_host->ai_addrlen);
198198 freeaddrinfo(remote_host);
199199 return send_res;
@@ -206,11 +206,11 @@ int idrnet_sendto_buf(int sockfd, void* buf, int buf_len, char* host, int port,
206206 if (addr_res == -1) {
207207 //printf("lib err: sendto getaddrinfo \n");
208208 return -1;
209− }
209+ }
210210
211211 buf_htonl(buf, buf_len);
212212
213− int send_res = sendto(sockfd, buf, buf_len, 0,
213+ int send_res = sendto(sockfd, buf, buf_len, 0,
214214 remote_host->ai_addr, remote_host->ai_addrlen);
215215 if (send_res == -1) {
216216 perror("lib err: sendto \n");
@@ -224,13 +224,13 @@ int idrnet_sendto_buf(int sockfd, void* buf, int buf_len, char* host, int port,
224224void* idrnet_recvfrom(int sockfd, int len) {
225225/*
226226 * int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
227− struct sockaddr *from, int *fromlen);
227+ struct sockaddr *from, int *fromlen);
228228*/
229229 // Allocate the required structures, and nuke them
230− struct sockaddr_storage* remote_addr =
230+ struct sockaddr_storage* remote_addr =
231231 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
232232 char* buf = (char*) malloc(len + 1);
233− idrnet_recvfrom_result* ret =
233+ idrnet_recvfrom_result* ret =
234234 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
235235 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
236236 memset(buf, 0, len + 1);
@@ -240,7 +240,7 @@ void* idrnet_recvfrom(int sockfd, int len) {
240240 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
241241 ret->result = recv_res;
242242 // Check for failure...
243− if (recv_res == -1) {
243+ if (recv_res == -1) {
244244 free(buf);
245245 free(remote_addr);
246246 } else {
@@ -255,12 +255,11 @@ void* idrnet_recvfrom(int sockfd, int len) {
255255 return ret;
256256}
257257
258−
259258void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
260259 // Allocate the required structures, and nuke them
261− struct sockaddr_storage* remote_addr =
260+ struct sockaddr_storage* remote_addr =
262261 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
263− idrnet_recvfrom_result* ret =
262+ idrnet_recvfrom_result* ret =
264263 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
265264 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
266265 memset(ret, 0, sizeof(idrnet_recvfrom_result));
@@ -269,7 +268,7 @@ void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
269268 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
270269 // Check for failure... But don't free the buffer! Not our job.
271270 ret->result = recv_res;
272− if (recv_res == -1) {
271+ if (recv_res == -1) {
273272 free(remote_addr);
274273 }
275274 // 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) {
298297int idrnet_get_recvfrom_port(void* res_struct) {
299298 idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
300299 if (recv_struct->remote_addr != NULL) {
301− struct sockaddr_in* remote_addr_in =
300+ struct sockaddr_in* remote_addr_in =
302301 (struct sockaddr_in*) recv_struct->remote_addr;
303302 return ((int) ntohs(remote_addr_in->sin_port));
304303 }
@@ -316,5 +315,3 @@ void idrnet_free_recvfrom_struct(void* res_struct) {
316315 }
317316 }
318317}
319−
320−
rts/idris_stdfgn.c+5−5
@@ -25,13 +25,13 @@ void fileClose(void* h) {
2525}
2626
2727int fileEOF(void* h) {
28− FILE* f = (FILE*)h;
29− return feof(f);
28+ FILE* f = (FILE*)h;
29+ return feof(f);
3030}
3131
3232int fileError(void* h) {
33− FILE* f = (FILE*)h;
34− return ferror(f);
33+ FILE* f = (FILE*)h;
34+ return ferror(f);
3535}
3636
3737int idris_writeStr(void* h, char* str) {
@@ -107,5 +107,5 @@ VAL idris_mkFileError(VM* vm) {
107107}
108108
109109void idris_forceGC(void* vm) {
110− idris_gc((VM*)vm);
110+ idris_gc((VM*)vm);
111111}
rts/windows/idris_net.c+6−7
@@ -12,7 +12,7 @@
1212#include <Ws2tcpip.h>
1313
1414
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,
1616// so put the prototype here, cribbed from a newer version of it
1717WINSOCK_API_LINKAGE LPCSTR WSAAPI InetNtopA(INT Family, PVOID pAddr,
1818 LPSTR pStringBuf, size_t StringBufSize);
@@ -50,7 +50,7 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
5050 // Convert port into string
5151 char str_port[8];
5252 sprintf(str_port, "%d", port);
53−
53+
5454 if (!check_init()) {
5555 return -1;
5656 }
@@ -73,7 +73,7 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
7373 int bind_res = bind(sockfd, address_res->ai_addr, address_res->ai_addrlen);
7474 if (bind_res == -1) {
7575 return -1;
76− }
76+ }
7777
7878 return 0;
7979}
@@ -152,13 +152,13 @@ void* idrnet_recv(int sockfd, int len) {
152152 if (!check_init()) {
153153 return NULL;
154154 }
155− idrnet_recv_result* res_struct =
155+ idrnet_recv_result* res_struct =
156156 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
157157
158158 char* buf = malloc(len + 1);
159159 int recv_res = recv(sockfd, buf, len, 0);
160160 res_struct->result = recv_res;
161−
161+
162162 if (recv_res > 0) { // Data was received
163163 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
164164 }
@@ -182,7 +182,7 @@ char* idrnet_get_recv_payload(void* res_struct) {
182182}
183183
184184void idrnet_free_recv_struct(void* res_struct) {
185− idrnet_recv_result* i_res_struct =
185+ idrnet_recv_result* i_res_struct =
186186 (idrnet_recv_result*) res_struct;
187187 if (i_res_struct->payload != NULL) {
188188 free(i_res_struct->payload);
@@ -193,4 +193,3 @@ void idrnet_free_recv_struct(void* res_struct) {
193193int idrnet_errno() {
194194 return errno;
195195}
196−
rts/windows/idris_stdfgn.c+24−5
@@ -26,13 +26,13 @@ void fileClose(void* h) {
2626}
2727
2828int fileEOF(void* h) {
29− FILE* f = (FILE*)h;
30− return feof(f);
29+ FILE* f = (FILE*)h;
30+ return feof(f);
3131}
3232
3333int fileError(void* h) {
34− FILE* f = (FILE*)h;
35− return ferror(f);
34+ FILE* f = (FILE*)h;
35+ return ferror(f);
3636}
3737
3838int idris_writeStr(void* h, char* str) {
@@ -77,6 +77,25 @@ VAL idris_time() {
7777 return MKBIGI(t);
7878}
7979
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+
8099void idris_forceGC(void* vm) {
81− idris_gc((VM*)vm);
100+ idris_gc((VM*)vm);
82101}