@@ -49,8 +49,9 @@ VM* init_vm(int stack_size, size_t heap_size,
4949 vm->ret = NULL;
5050 vm->reg1 = NULL;
5151#ifdef HAS_PTHREAD
52− vm->inbox = malloc(1024*sizeof(VAL));
53− memset(vm->inbox, 0, 1024*sizeof(VAL));
52+ vm->inbox = malloc(1024*sizeof(vm->inbox[0]));
53+ assert(vm->inbox);
54+ memset(vm->inbox, 0, 1024*sizeof(vm->inbox[0]));
5455 vm->inbox_end = vm->inbox + 1024;
5556 vm->inbox_write = vm->inbox;
5657 vm->inbox_nextid = 1;
@@ -276,7 +277,8 @@ static VAL mkstrlen(VM* vm, const char * str, size_t len, int outer) {
276277 String * cl = allocStr(vm, len, outer);
277278 // hdr.u8 used to mark a null string
278279 cl->hdr.u8 = str == NULL;
279− memcpy(cl->str, str, len);
280+ if (!cl->hdr.u8)
281+ memcpy(cl->str, str, len);
280282 return (VAL)cl;
281283}
282284
@@ -411,7 +413,7 @@ void dumpVal(VAL v) {
411413 int i;
412414 switch(GETTY(v)) {
413415 case CT_INT:
414− printf("%d ", (int)(GETINT(v)));
416+ printf("%" PRIdPTR " ", GETINT(v));
415417 break;
416418 case CT_CON:
417419 {
@@ -511,7 +513,7 @@ VAL idris_castBitsStr(VM* vm, VAL i) {
511513 case CT_INT: // 8/16 bits
512514 // max length 16 bit unsigned int str 5 chars (65,535)
513515 cl = allocStr(vm, 6, 0);
514− cl->slen = sprintf(cl->str, "%ld", GETBITS16(i));
516+ cl->slen = sprintf(cl->str, "%" PRIu16, (uint16_t)GETBITS16(i));
515517 break;
516518 case CT_BITS32:
517519 // max length 32 bit unsigned int str 10 chars (4,294,967,295)
@@ -965,7 +967,7 @@ Msg* idris_checkInitMessages(VM* vm) {
965967 Msg* msg;
966968
967969 for (msg = vm->inbox; msg < vm->inbox_end && msg->msg != NULL; ++msg) {
968− if (msg->channel_id && 1 == 1) { // init bit set
970+ if ((msg->channel_id & 1) == 1) { // init bit set
969971 return msg;
970972 }
971973 }
@@ -1028,7 +1030,7 @@ Msg* idris_recvMessage(VM* vm) {
10281030
10291031Msg* idris_recvMessageFrom(VM* vm, int channel_id, VM* sender) {
10301032 Msg* msg;
1031− Msg* ret;
1033+ Msg* ret = malloc(sizeof(Msg));
10321034
10331035 struct timespec timeout;
10341036 int status;
@@ -1051,35 +1053,33 @@ Msg* idris_recvMessageFrom(VM* vm, int channel_id, VM* sender) {
10511053 }
10521054 pthread_mutex_unlock(&vm->inbox_block);
10531055
1054− if (msg == NULL) {
1055− fprintf(stderr, "No messages waiting");
1056− exit(-1);
1057− }
1056+ if (msg != NULL) {
1057+ ret->msg = msg->msg;
1058+ ret->sender = msg->sender;
10581059
1059− ret = malloc(sizeof(Msg));
1060− ret->msg = msg->msg;
1061− ret->sender = msg->sender;
1060+ pthread_mutex_lock(&(vm->inbox_lock));
10621061
1063− pthread_mutex_lock(&(vm->inbox_lock));
1064−
1065− // Slide everything down after the message in the inbox,
1066− // Move the inbox_write pointer down, and clear the value at the
1067− // end - O(n) but it's easier since the message from a specific
1068− // sender could be anywhere in the inbox
1069−
1070− for(;msg < vm->inbox_write; ++msg) {
1071− if (msg+1 != vm->inbox_end) {
1072− msg->sender = (msg + 1)->sender;
1073− msg->msg = (msg + 1)->msg;
1074− }
1075− }
1062+ // Slide everything down after the message in the inbox,
1063+ // Move the inbox_write pointer down, and clear the value at the
1064+ // end - O(n) but it's easier since the message from a specific
1065+ // sender could be anywhere in the inbox
10761066
1077− vm->inbox_write->msg = NULL;
1078− vm->inbox_write->sender = NULL;
1079− vm->inbox_write--;
1067+ for(;msg < vm->inbox_write; ++msg) {
1068+ if (msg+1 != vm->inbox_end) {
1069+ msg->sender = (msg + 1)->sender;
1070+ msg->msg = (msg + 1)->msg;
1071+ }
1072+ }
10801073
1081− pthread_mutex_unlock(&(vm->inbox_lock));
1074+ vm->inbox_write->msg = NULL;
1075+ vm->inbox_write->sender = NULL;
1076+ vm->inbox_write--;
10821077
1078+ pthread_mutex_unlock(&(vm->inbox_lock));
1079+ } else {
1080+ fprintf(stderr, "No messages waiting");
1081+ exit(-1);
1082+ }
10831083 return ret;
10841084}
10851085#endif