OSDN Git Service

am beeeb788: am 05fa5fd5: Merge "Simplify merges of the annotation code."
[android-x86/dalvik.git] / vm / StdioConverter.cpp
index 45c2668..f420c4d 100644 (file)
 #define kFilenoStdout   1
 #define kFilenoStderr   2
 
-/*
- * Hold our replacement stdout/stderr.
- */
-struct StdPipes {
-    int stdoutPipe[2];
-    int stderrPipe[2];
-};
-
 #define kMaxLine    512
 
 /*
@@ -57,41 +49,35 @@ static bool readAndLog(int fd, BufferedData* data, const char* tag);
  */
 bool dvmStdioConverterStartup()
 {
-    StdPipes* pipeStorage;
-
     gDvm.haltStdioConverter = false;
 
     dvmInitMutex(&gDvm.stdioConverterLock);
     pthread_cond_init(&gDvm.stdioConverterCond, NULL);
 
-    pipeStorage = (StdPipes*) malloc(sizeof(StdPipes));
-    if (pipeStorage == NULL)
-        return false;
-
-    if (pipe(pipeStorage->stdoutPipe) != 0) {
-        LOGW("pipe failed: %s", strerror(errno));
+    if (pipe(gDvm.stdoutPipe) != 0) {
+        ALOGW("pipe failed: %s", strerror(errno));
         return false;
     }
-    if (pipe(pipeStorage->stderrPipe) != 0) {
-        LOGW("pipe failed: %s", strerror(errno));
+    if (pipe(gDvm.stderrPipe) != 0) {
+        ALOGW("pipe failed: %s", strerror(errno));
         return false;
     }
 
-    if (dup2(pipeStorage->stdoutPipe[1], kFilenoStdout) != kFilenoStdout) {
-        LOGW("dup2(1) failed: %s", strerror(errno));
+    if (dup2(gDvm.stdoutPipe[1], kFilenoStdout) != kFilenoStdout) {
+        ALOGW("dup2(1) failed: %s", strerror(errno));
         return false;
     }
-    close(pipeStorage->stdoutPipe[1]);
-    pipeStorage->stdoutPipe[1] = -1;
+    close(gDvm.stdoutPipe[1]);
+    gDvm.stdoutPipe[1] = -1;
 #ifdef HAVE_ANDROID_OS
     /* don't redirect stderr on sim -- logs get written there! */
     /* (don't need this on the sim anyway) */
-    if (dup2(pipeStorage->stderrPipe[1], kFilenoStderr) != kFilenoStderr) {
-        LOGW("dup2(2) failed: %d %s", errno, strerror(errno));
+    if (dup2(gDvm.stderrPipe[1], kFilenoStderr) != kFilenoStderr) {
+        ALOGW("dup2(2) failed: %d %s", errno, strerror(errno));
         return false;
     }
-    close(pipeStorage->stderrPipe[1]);
-    pipeStorage->stderrPipe[1] = -1;
+    close(gDvm.stderrPipe[1]);
+    gDvm.stderrPipe[1] = -1;
 #endif
 
 
@@ -101,12 +87,11 @@ bool dvmStdioConverterStartup()
     dvmLockMutex(&gDvm.stdioConverterLock);
 
     if (!dvmCreateInternalThread(&gDvm.stdioConverterHandle,
-                "Stdio Converter", stdioConverterThreadStart, pipeStorage))
-    {
-        free(pipeStorage);
+                                 "Stdio Converter",
+                                 stdioConverterThreadStart,
+                                 NULL)) {
         return false;
     }
-    /* new thread owns pipeStorage */
 
     while (!gDvm.stdioConverterReady) {
         dvmWaitCond(&gDvm.stdioConverterCond, &gDvm.stdioConverterLock);
@@ -132,7 +117,7 @@ void dvmStdioConverterShutdown()
     printf("Shutting down\n");
     fflush(stdout);
 
-    LOGD("Joining stdio converter...");
+    ALOGD("Joining stdio converter...");
     pthread_join(gDvm.stdioConverterHandle, NULL);
 }
 
@@ -143,9 +128,6 @@ void dvmStdioConverterShutdown()
  */
 static void* stdioConverterThreadStart(void* arg)
 {
-    StdPipes* pipeStorage = (StdPipes*) arg;
-    BufferedData* stdoutData;
-    BufferedData* stderrData;
     int cc;
 
     /* tell the main thread that we're ready */
@@ -161,8 +143,8 @@ static void* stdioConverterThreadStart(void* arg)
     /*
      * Allocate read buffers.
      */
-    stdoutData = (BufferedData*) malloc(sizeof(*stdoutData));
-    stderrData = (BufferedData*) malloc(sizeof(*stderrData));
+    BufferedData* stdoutData = new BufferedData;
+    BufferedData* stderrData = new BufferedData;
     stdoutData->count = stderrData->count = 0;
 
     /*
@@ -173,45 +155,44 @@ static void* stdioConverterThreadStart(void* arg)
         int maxFd, fdCount;
 
         FD_ZERO(&readfds);
-        FD_SET(pipeStorage->stdoutPipe[0], &readfds);
-        FD_SET(pipeStorage->stderrPipe[0], &readfds);
-        maxFd = MAX(pipeStorage->stdoutPipe[0], pipeStorage->stderrPipe[0]);
+        FD_SET(gDvm.stdoutPipe[0], &readfds);
+        FD_SET(gDvm.stderrPipe[0], &readfds);
+        maxFd = MAX(gDvm.stdoutPipe[0], gDvm.stderrPipe[0]);
 
         fdCount = select(maxFd+1, &readfds, NULL, NULL, NULL);
 
         if (fdCount < 0) {
             if (errno != EINTR) {
-                LOGE("select on stdout/stderr failed");
+                ALOGE("select on stdout/stderr failed");
                 break;
             }
-            LOGD("Got EINTR, ignoring");
+            ALOGD("Got EINTR, ignoring");
         } else if (fdCount == 0) {
-            LOGD("WEIRD: select returned zero");
+            ALOGD("WEIRD: select returned zero");
         } else {
             bool err = false;
-            if (FD_ISSET(pipeStorage->stdoutPipe[0], &readfds)) {
-                err |= !readAndLog(pipeStorage->stdoutPipe[0], stdoutData,
+            if (FD_ISSET(gDvm.stdoutPipe[0], &readfds)) {
+                err |= !readAndLog(gDvm.stdoutPipe[0], stdoutData,
                     "stdout");
             }
-            if (FD_ISSET(pipeStorage->stderrPipe[0], &readfds)) {
-                err |= !readAndLog(pipeStorage->stderrPipe[0], stderrData,
+            if (FD_ISSET(gDvm.stderrPipe[0], &readfds)) {
+                err |= !readAndLog(gDvm.stderrPipe[0], stderrData,
                     "stderr");
             }
 
             /* probably EOF; give up */
             if (err) {
-                LOGW("stdio converter got read error; shutting it down");
+                ALOGW("stdio converter got read error; shutting it down");
                 break;
             }
         }
     }
 
-    close(pipeStorage->stdoutPipe[0]);
-    close(pipeStorage->stderrPipe[0]);
+    close(gDvm.stdoutPipe[0]);
+    close(gDvm.stderrPipe[0]);
 
-    free(pipeStorage);
-    free(stdoutData);
-    free(stderrData);
+    delete stdoutData;
+    delete stderrData;
 
     /* change back for shutdown sequence */
     dvmChangeStatus(NULL, THREAD_RUNNING);
@@ -232,11 +213,11 @@ static bool readAndLog(int fd, BufferedData* data, const char* tag)
     want = kMaxLine - data->count;
     actual = read(fd, data->buf + data->count, want);
     if (actual <= 0) {
-        LOGW("read %s: (%d,%d) failed (%d): %s",
+        ALOGW("read %s: (%d,%d) failed (%d): %s",
             tag, fd, want, (int)actual, strerror(errno));
         return false;
     } else {
-        //LOGI("read %s: %d at %d", tag, actual, data->count);
+        //ALOGI("read %s: %d at %d", tag, actual, data->count);
     }
     data->count += actual;
 
@@ -250,8 +231,8 @@ static bool readAndLog(int fd, BufferedData* data, const char* tag)
     for (i = data->count; i > 0; i--, cp++) {
         if (*cp == '\n' || (*cp == '\r' && i != 0 && *(cp+1) != '\n')) {
             *cp = '\0';
-            //LOGW("GOT %d at %d '%s'", cp - start, start - data->buf, start);
-            LOG(LOG_INFO, tag, "%s", start);
+            //ALOGW("GOT %d at %d '%s'", cp - start, start - data->buf, start);
+            ALOG(LOG_INFO, tag, "%s", start);
             start = cp+1;
         }
     }
@@ -261,7 +242,7 @@ static bool readAndLog(int fd, BufferedData* data, const char* tag)
      */
     if (start == data->buf && data->count == kMaxLine) {
         data->buf[kMaxLine] = '\0';
-        LOG(LOG_INFO, tag, "%s!", start);
+        ALOG(LOG_INFO, tag, "%s!", start);
         start = cp + kMaxLine;
     }