Merge "Move native bridge library in system/core"
diff --git a/adb/commandline.c b/adb/commandline.c
index ccf158c..356c0db 100644
--- a/adb/commandline.c
+++ b/adb/commandline.c
@@ -1602,9 +1602,10 @@
     return 1;
 }
 
+#define MAX_ARGV_LENGTH 16
 static int do_cmd(transport_type ttype, char* serial, char *cmd, ...)
 {
-    char *argv[16];
+    char *argv[MAX_ARGV_LENGTH];
     int argc;
     va_list ap;
 
@@ -1621,7 +1622,9 @@
     }
 
     argv[argc++] = cmd;
-    while((argv[argc] = va_arg(ap, char*)) != 0) argc++;
+    while(argc < MAX_ARGV_LENGTH &&
+        (argv[argc] = va_arg(ap, char*)) != 0) argc++;
+    assert(argc < MAX_ARGV_LENGTH);
     va_end(ap);
 
 #if 0
diff --git a/adb/usb_osx.c b/adb/usb_osx.c
index 45ce444..5efb23b 100644
--- a/adb/usb_osx.c
+++ b/adb/usb_osx.c
@@ -197,7 +197,8 @@
         kr = (*dev)->GetDeviceProduct(dev, &product);
         kr = (*dev)->GetLocationID(dev, &locationId);
         if (kr == 0) {
-            snprintf(devpathBuf, sizeof(devpathBuf), "usb:%lX", locationId);
+            snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X",
+	             (unsigned int)locationId);
             devpath = devpathBuf;
         }
         kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
diff --git a/debuggerd/tombstone.cpp b/debuggerd/tombstone.cpp
index 2b9d093..32f0eca 100755
--- a/debuggerd/tombstone.cpp
+++ b/debuggerd/tombstone.cpp
@@ -64,6 +64,7 @@
     case SIGFPE:
     case SIGILL:
     case SIGSEGV:
+    case SIGTRAP:
       return true;
     default:
       return false;
@@ -350,16 +351,12 @@
     return;
   }
 
-  bool is_running = (si.si_code == SI_USER);
+  bool has_fault_address = signal_has_si_addr(si.si_signo);
   uintptr_t addr = reinterpret_cast<uintptr_t>(si.si_addr);
-  addr &= ~0xfff;     // round to 4K page boundary
-  if (!is_running && addr == 0) {    // null-pointer deref
-    return;
-  }
 
-  _LOG(log, logtype::MAPS, "\nmemory map: %s\n", is_running? "" : "(fault address prefixed with --->)");
+  _LOG(log, logtype::MAPS, "\nmemory map: %s\n", has_fault_address ? "(fault address prefixed with --->)" : "");
 
-  if(!is_running && (addr < map->begin()->start)) {
+  if (has_fault_address && (addr < map->begin()->start)) {
     _LOG(log, logtype::MAPS, "--->Fault address falls at %" PRIPTR " before any mapped regions\n", addr);
   }
 
@@ -369,10 +366,10 @@
       _LOG(log, logtype::MAPS, "--->Fault address falls at %" PRIPTR " between mapped regions\n", addr);
     }
     prev = it;
-    bool in_map = !is_running && (addr >= (*it).start) && (addr < (*it).end);
+    bool in_map = has_fault_address && (addr >= (*it).start) && (addr < (*it).end);
     dump_map(log, &*it, in_map);
   }
-  if (!is_running && (addr >= (*prev).end)) {
+  if (has_fault_address && (addr >= (*prev).end)) {
     _LOG(log, logtype::MAPS, "--->Fault address falls at %" PRIPTR " after any mapped regions\n", addr);
   }
 }
diff --git a/fastboot/usb_osx.c b/fastboot/usb_osx.c
index 0f55e0d..0b6c515 100644
--- a/fastboot/usb_osx.c
+++ b/fastboot/usb_osx.c
@@ -328,7 +328,8 @@
         ERR("GetLocationId");
         goto error;
     }
-    snprintf(handle->info.device_path, sizeof(handle->info.device_path), "usb:%lX", locationId);
+    snprintf(handle->info.device_path, sizeof(handle->info.device_path),
+             "usb:%" PRIu32 "X", (unsigned int)locationId);
 
     kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
 
diff --git a/include/utils/RefBase.h b/include/utils/RefBase.h
index cbfe13a..ac1ac6c 100644
--- a/include/utils/RefBase.h
+++ b/include/utils/RefBase.h
@@ -484,7 +484,8 @@
                 TYPE::renameRefId(d[i].get(), &s[i], &d[i]);
             }
         public:
-            Renamer(sp<TYPE>* d, sp<TYPE> const* s) : s(s), d(d) { }
+            Renamer(sp<TYPE>* d, sp<TYPE> const* s) : d(d), s(s) { }
+            virtual ~Renamer() { }
         };
 
         memmove(d, s, n*sizeof(sp<TYPE>));
@@ -503,7 +504,8 @@
                 TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]);
             }
         public:
-            Renamer(wp<TYPE>* d, wp<TYPE> const* s) : s(s), d(d) { }
+            Renamer(wp<TYPE>* d, wp<TYPE> const* s) : d(d), s(s) { }
+            virtual ~Renamer() { }
         };
 
         memmove(d, s, n*sizeof(wp<TYPE>));
diff --git a/include/ziparchive/zip_archive.h b/include/ziparchive/zip_archive.h
index 1877494..27a9796 100644
--- a/include/ziparchive/zip_archive.h
+++ b/include/ziparchive/zip_archive.h
@@ -130,10 +130,10 @@
 /*
  * Start iterating over all entries of a zip file. The order of iteration
  * is not guaranteed to be the same as the order of elements
- * in the central directory but is stable for a given zip file. |cookie|
- * must point to a writeable memory location, and will be set to the value
- * of an opaque cookie which can be used to make one or more calls to
- * Next.
+ * in the central directory but is stable for a given zip file. |cookie| will
+ * contain the value of an opaque cookie which can be used to make one or more
+ * calls to Next. All calls to StartIteration must be matched by a call to
+ * EndIteration to free any allocated memory.
  *
  * This method also accepts an optional prefix to restrict iteration to
  * entry names that start with |prefix|.
@@ -152,6 +152,12 @@
 int32_t Next(void* cookie, ZipEntry* data, ZipEntryName *name);
 
 /*
+ * End iteration over all entries of a zip file and frees the memory allocated
+ * in StartIteration.
+ */
+void EndIteration(void* cookie);
+
+/*
  * Uncompress and write an entry to an open file identified by |fd|.
  * |entry->uncompressed_length| bytes will be written to the file at
  * its current offset, and the file will be truncated at the end of
diff --git a/init/bootchart.c b/init/bootchart.c
index f72fcaa..a514261 100644
--- a/init/bootchart.c
+++ b/init/bootchart.c
@@ -119,6 +119,18 @@
     }
 }
 
+static long long
+get_uptime_jiffies()
+{
+    char       buff[64];
+    long long  jiffies = 0;
+
+    if (proc_read("/proc/uptime", buff, sizeof(buff)) > 0)
+        jiffies = 100LL*strtod(buff,NULL);
+
+    return jiffies;
+}
+
 static void
 log_header(void)
 {
@@ -185,22 +197,11 @@
 do_log_uptime(FileBuff  log)
 {
     char  buff[65];
-    int   fd, ret, len;
+    int   len;
 
-    fd = open("/proc/uptime",O_RDONLY);
-    if (fd >= 0) {
-        int  ret;
-        ret = unix_read(fd, buff, 64);
-        close(fd);
-        buff[64] = 0;
-        if (ret >= 0) {
-            long long  jiffies = 100LL*strtod(buff,NULL);
-            int        len;
-            snprintf(buff,sizeof(buff),"%lld\n",jiffies);
-            len = strlen(buff);
-            file_buff_write(log, buff, len);
-        }
-    }
+    snprintf(buff,sizeof(buff),"%lld\n",get_uptime_jiffies());
+    len = strlen(buff);
+    file_buff_write(log, buff, len);
 }
 
 static void
@@ -376,3 +377,9 @@
     file_buff_done(log_procs);
     acct(NULL);
 }
+
+/* called to get time (in ms) used by bootchart */
+long long  bootchart_gettime( void )
+{
+    return 10LL*get_uptime_jiffies();
+}
diff --git a/init/bootchart.h b/init/bootchart.h
index 39d2d4f..ed65e8a 100644
--- a/init/bootchart.h
+++ b/init/bootchart.h
@@ -26,6 +26,7 @@
 extern int   bootchart_init(void);
 extern int   bootchart_step(void);
 extern void  bootchart_finish(void);
+extern long long  bootchart_gettime(void);
 
 # define BOOTCHART_POLLING_MS   200   /* polling period in ms */
 # define BOOTCHART_DEFAULT_TIME_SEC    (2*60)  /* default polling time in seconds */
diff --git a/init/init.c b/init/init.c
index bd1db7a..99474e6 100644
--- a/init/init.c
+++ b/init/init.c
@@ -65,6 +65,7 @@
 
 #if BOOTCHART
 static int   bootchart_count;
+static long long bootchart_time = 0;
 #endif
 
 static char console[32];
@@ -1147,11 +1148,29 @@
 
 #if BOOTCHART
         if (bootchart_count > 0) {
-            if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
-                timeout = BOOTCHART_POLLING_MS;
-            if (bootchart_step() < 0 || --bootchart_count == 0) {
-                bootchart_finish();
-                bootchart_count = 0;
+            long long current_time;
+            int elapsed_time, remaining_time;
+
+            current_time = bootchart_gettime();
+            elapsed_time = current_time - bootchart_time;
+
+            if (elapsed_time >= BOOTCHART_POLLING_MS) {
+                /* count missed samples */
+                while (elapsed_time >= BOOTCHART_POLLING_MS) {
+                    elapsed_time -= BOOTCHART_POLLING_MS;
+                    bootchart_count--;
+                }
+                /* count may be negative, take a sample anyway */
+                bootchart_time = current_time;
+                if (bootchart_step() < 0 || bootchart_count <= 0) {
+                    bootchart_finish();
+                    bootchart_count = 0;
+                }
+            }
+            if (bootchart_count > 0) {
+                remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
+                if (timeout < 0 || timeout > remaining_time)
+                    timeout = remaining_time;
             }
         }
 #endif
diff --git a/libusbhost/usbhost.c b/libusbhost/usbhost.c
index cd8000a..a3222cf 100644
--- a/libusbhost/usbhost.c
+++ b/libusbhost/usbhost.c
@@ -690,6 +690,6 @@
 int usb_request_cancel(struct usb_request *req)
 {
     struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data);
-    return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, &urb);
+    return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, urb);
 }
 
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index 6ec8f0d..a29eb79 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -899,16 +899,28 @@
 
   IterationHandle* cookie = (IterationHandle*) malloc(sizeof(IterationHandle));
   cookie->position = 0;
-  cookie->prefix = prefix;
   cookie->archive = archive;
   if (prefix != NULL) {
+    cookie->prefix = strdup(prefix);
     cookie->prefix_len = strlen(prefix);
+  } else {
+    cookie->prefix = NULL;
   }
 
   *cookie_ptr = cookie ;
   return 0;
 }
 
+void EndIteration(void* cookie) {
+  if (cookie != NULL) {
+    IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
+    if (handle->prefix != NULL) {
+      free(const_cast<char*>(handle->prefix));
+    }
+    free(cookie);
+  }
+}
+
 int32_t FindEntry(const ZipArchiveHandle handle, const char* entryName,
                   ZipEntry* data) {
   const ZipArchive* archive = (ZipArchive*) handle;
diff --git a/logd/LogTimes.cpp b/logd/LogTimes.cpp
index e7e3ec2..ea4e8c8 100644
--- a/logd/LogTimes.cpp
+++ b/logd/LogTimes.cpp
@@ -33,7 +33,6 @@
         , mRelease(false)
         , mError(false)
         , threadRunning(false)
-        , threadTriggered(true)
         , mReader(reader)
         , mLogMask(logMask)
         , mPid(pid)
@@ -45,7 +44,9 @@
         , mStart(start)
         , mNonBlock(nonBlock)
         , mEnd(CLOCK_MONOTONIC)
-{ }
+{
+        pthread_cond_init(&threadTriggeredCondition, NULL);
+}
 
 void LogTimeEntry::startReader_Locked(void) {
     pthread_attr_t attr;
@@ -74,7 +75,6 @@
 
     lock();
 
-    me->threadRunning = false;
     if (me->mNonBlock) {
         me->error_Locked();
     }
@@ -103,6 +103,7 @@
         client->decRef();
     }
 
+    me->threadRunning = false;
     me->decRef_Locked();
 
     unlock();
@@ -118,7 +119,7 @@
     SocketClient *client = me->mClient;
     if (!client) {
         me->error();
-        pthread_exit(NULL);
+        return NULL;
     }
 
     LogBuffer &logbuf = me->mReader.logbuf();
@@ -127,12 +128,7 @@
 
     lock();
 
-    me->threadTriggered = true;
-
-    while(me->threadTriggered && !me->isError_Locked()) {
-
-        me->threadTriggered = false;
-
+    while (me->threadRunning && !me->isError_Locked()) {
         log_time start = me->mStart;
 
         unlock();
@@ -142,24 +138,21 @@
         }
         start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
 
+        lock();
+
         if (start == LogBufferElement::FLUSH_ERROR) {
-            me->error();
+            me->error_Locked();
         }
 
-        if (me->mNonBlock) {
-            lock();
+        if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) {
             break;
         }
 
-        sched_yield();
-
-        lock();
+        pthread_cond_wait(&me->threadTriggeredCondition, &timesLock);
     }
 
     unlock();
 
-    pthread_exit(NULL);
-
     pthread_cleanup_pop(true);
 
     return NULL;
diff --git a/logd/LogTimes.h b/logd/LogTimes.h
index beaf646..0bfa7a2 100644
--- a/logd/LogTimes.h
+++ b/logd/LogTimes.h
@@ -31,7 +31,7 @@
     bool mRelease;
     bool mError;
     bool threadRunning;
-    bool threadTriggered;
+    pthread_cond_t threadTriggeredCondition;
     pthread_t mThread;
     LogReader &mReader;
     static void *threadStart(void *me);
@@ -63,12 +63,16 @@
     bool runningReader_Locked(void) const {
         return threadRunning || mRelease || mError || mNonBlock;
     }
-    void triggerReader_Locked(void) { threadTriggered = true; }
+    void triggerReader_Locked(void) {
+        pthread_cond_signal(&threadTriggeredCondition);
+    }
+
     void triggerSkip_Locked(unsigned int skip) { skipAhead = skip; }
 
     // Called after LogTimeEntry removed from list, lock implicitly held
     void release_Locked(void) {
         mRelease = true;
+        pthread_cond_signal(&threadTriggeredCondition);
         if (mRefCount || threadRunning) {
             return;
         }
@@ -78,7 +82,7 @@
 
     // Called to mark socket in jeopardy
     void error_Locked(void) { mError = true; }
-    void error(void) { lock(); mError = true; unlock(); }
+    void error(void) { lock(); error_Locked(); unlock(); }
 
     bool isError_Locked(void) const { return mRelease || mError; }