sped up run binary... realloc sux
authorAndrew Waterman <waterman@parcad.millennium.berkeley.edu>
Tue, 1 Dec 2009 04:45:29 +0000 (20:45 -0800)
committerAndrew Waterman <waterman@parcad.millennium.berkeley.edu>
Tue, 1 Dec 2009 04:45:29 +0000 (20:45 -0800)
user/apps/parlib/run_binary.c
user/parlib/inc/parlib.h
user/parlib/src/syscall.c

index 2f2e6bd..43548a8 100644 (file)
@@ -10,8 +10,8 @@
 
 extern char * readline(const char *prompt);
 
+#define MALLOC_SIZE     1048576
 #define READ_SIZE       1024
-uint8_t* binary_buf;
 
 static void fd_error() {
        fprintf(stderr, "Error: Unable to run remote binary (fd error): %s\n", strerror(errno));
@@ -22,13 +22,13 @@ static void malloc_error() {
 }
 
 static void read_error(void* buf, int fd) {
-       free(binary_buf);
+       free(buf);
        close(fd);
        fprintf(stderr, "Error: Unable to run remote binary (read error): %s\n", strerror(errno));
 }
 
 static void realloc_error(void* buf, int fd) {
-       free(binary_buf);
+       free(buf);
        close(fd);
        fprintf(stderr, "Error: Unable to run remote binary: No more memory available!\n");
 }
@@ -47,17 +47,22 @@ void run_binary()
        //free(file_name);
        if(fd < 0) { fd_error(); return; };
        
-       int iters = 0;
        int total_bytes_read = 0;
        int bytes_read = 0;
-       binary_buf = NULL;
+       int bufsz = 0;
+       void* binary_buf = NULL;
        
        while(1) {
-               total_bytes_read += bytes_read; 
-               void* temp_buf = realloc(binary_buf, READ_SIZE*(++iters));
-               if(temp_buf == NULL) { realloc_error(binary_buf, fd); return; } 
-               binary_buf = temp_buf;
+               if(total_bytes_read+READ_SIZE > bufsz)
+               {
+                       void* temp_buf = realloc(binary_buf,bufsz+MALLOC_SIZE);
+                       if(temp_buf == NULL) { realloc_error(binary_buf, fd); return; }
+                       binary_buf = temp_buf;
+                       bufsz += MALLOC_SIZE;
+               }
+
                bytes_read = read(fd, binary_buf+total_bytes_read, READ_SIZE);
+               total_bytes_read += bytes_read;
                if(bytes_read < 0) { read_error(binary_buf, fd); return; }
                if(bytes_read == 0) break;
        }
index 7fbc6df..f935f80 100644 (file)
@@ -42,6 +42,6 @@ ssize_t     sys_shared_page_alloc(void *COUNT(PGSIZE) *addr, pid_t p2,
                                   int p1_flags, int p2_flags);
 ssize_t     sys_shared_page_free(void *COUNT(PGSIZE) addr, pid_t p2);
 ssize_t     sys_resource_req(int type, size_t amount, uint32_t flags);
-
+void        sys_reboot();
 
 #endif // !ROS_INC_PARLIB_H
index 87dc89c..5e70d2d 100644 (file)
@@ -89,3 +89,7 @@ ssize_t sys_resource_req(int type, size_t amount, uint32_t flags)
         return syscall(SYS_resource_req, type, amount, flags, 0, 0);
 }
 
+void sys_reboot()
+{
+       syscall(SYS_reboot,0,0,0,0,0);
+}