Enhance the cache-writing policy.
diff --git a/lib/bcc/CacheReader.cpp b/lib/bcc/CacheReader.cpp
index e44f5b5..a820053 100644
--- a/lib/bcc/CacheReader.cpp
+++ b/lib/bcc/CacheReader.cpp
@@ -416,6 +416,7 @@
 
   if (!mpResult->mContext) {
     // Unable to allocate at cached address.  Give up.
+    mIsContextSlotNotAvail = true;
     return false;
 
     // TODO(logan): If relocation is fixed, we should try to allocate the
diff --git a/lib/bcc/CacheReader.h b/lib/bcc/CacheReader.h
index e66503e..fd3516d 100644
--- a/lib/bcc/CacheReader.h
+++ b/lib/bcc/CacheReader.h
@@ -49,10 +49,13 @@
     std::map<std::string,
              std::pair<uint32_t, unsigned char const *> > mDependencies;
 
+    bool mIsContextSlotNotAvail;
+
   public:
     CacheReader()
       : mFile(NULL), mFileSize(0), mpHeader(NULL),
-        mpCachedDependTable(NULL), mpPragmaList(NULL), mpFuncTable(NULL) {
+        mpCachedDependTable(NULL), mpPragmaList(NULL), mpFuncTable(NULL),
+        mIsContextSlotNotAvail(false) {
     }
 
     ~CacheReader();
@@ -66,6 +69,10 @@
 
     ScriptCached *readCacheFile(FileHandle *file, Script *s);
 
+    bool isContextSlotNotAvail() const {
+      return mIsContextSlotNotAvail;
+    }
+
   private:
     bool readHeader();
     bool readStringPool();
diff --git a/lib/bcc/ContextManager.cpp b/lib/bcc/ContextManager.cpp
index 8d173c1..e480480 100644
--- a/lib/bcc/ContextManager.cpp
+++ b/lib/bcc/ContextManager.cpp
@@ -36,6 +36,21 @@
 
 static android::Mutex gContextLock;
 
+
+ssize_t getSlotIndexFromAddress(char *addr) {
+  if (addr >= BCC_CONTEXT_FIXED_ADDR) {
+    size_t offset = (size_t)(addr - BCC_CONTEXT_FIXED_ADDR);
+    if (offset % BCC_CONTEXT_SIZE == 0) {
+      size_t slot = offset / BCC_CONTEXT_SIZE;
+      if (slot < BCC_CONTEXT_SLOT_COUNT) {
+        return slot;
+      }
+    }
+  }
+  return -1;
+}
+
+
 char *allocateContext() {
   android::AutoMutex _l(gContextLock);
 
@@ -80,18 +95,6 @@
   return (char *)result;
 }
 
-static ssize_t getSlotIndexFromAddress(char* addr) {
-  if (addr >= BCC_CONTEXT_FIXED_ADDR) {
-    size_t offset = (size_t)(addr - BCC_CONTEXT_FIXED_ADDR);
-    if (offset % BCC_CONTEXT_SIZE == 0) {
-      size_t slot = offset / BCC_CONTEXT_SIZE;
-      if (slot < BCC_CONTEXT_SLOT_COUNT) {
-        return slot;
-      }
-    }
-  }
-  return -1;
-}
 
 char *allocateContext(char *addr, int imageFd, off_t imageOffset) {
   android::AutoMutex _l(gContextLock);
diff --git a/lib/bcc/ContextManager.h b/lib/bcc/ContextManager.h
index e9a5860..461caff 100644
--- a/lib/bcc/ContextManager.h
+++ b/lib/bcc/ContextManager.h
@@ -32,6 +32,8 @@
 
 namespace bcc {
 
+  extern ssize_t getSlotIndexFromAddress(char *addr);
+
   extern char *allocateContext();
 
   extern char *allocateContext(char *addr, int imageFd, off_t imageOffset);
diff --git a/lib/bcc/Script.cpp b/lib/bcc/Script.cpp
index cb86ef5..23fe9cf 100644
--- a/lib/bcc/Script.cpp
+++ b/lib/bcc/Script.cpp
@@ -23,6 +23,7 @@
 
 #include "CacheReader.h"
 #include "CacheWriter.h"
+#include "ContextManager.h"
 #include "FileHandle.h"
 #include "ScriptCompiled.h"
 #include "ScriptCached.h"
@@ -183,6 +184,7 @@
   // Read cache file
   ScriptCached *cached = reader.readCacheFile(&file, this);
   if (!cached) {
+    mIsContextSlotNotAvail = reader.isContextSlotNotAvail();
     return 1;
   }
 
@@ -250,8 +252,18 @@
   }
 
 #if USE_CACHE
-  // TODO(logan): Write the cache out
-  if (mCachePath && !getBooleanProp("debug.bcc.nocache")) {
+  // Note: If we re-compile the script because the cached context slot not
+  // available, then we don't have to write the cache.
+
+  // Note: If the address of the context is not in the context slot, then
+  // we don't have to cache it.
+
+  char *addr = getContext();
+
+  if (mCachePath &&
+      !mIsContextSlotNotAvail && getSlotIndexFromAddress(addr) >= 0 &&
+      !getBooleanProp("debug.bcc.nocache")) {
+
     FileHandle file;
 
     // Remove the file if it already exists before writing the new file.
diff --git a/lib/bcc/Script.h b/lib/bcc/Script.h
index 64e4803..a8f8c4e 100644
--- a/lib/bcc/Script.h
+++ b/lib/bcc/Script.h
@@ -57,6 +57,8 @@
 
     char const *mCachePath;
 
+    bool mIsContextSlotNotAvail;
+
     // ReadBC
     char const *sourceBC;
     char const *sourceResName;
@@ -76,7 +78,7 @@
 
   public:
     Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
-               mCachePath(NULL),
+               mCachePath(NULL), mIsContextSlotNotAvail(false),
                sourceBC(NULL), sourceResName(NULL), sourceSize(0),
                sourceModule(NULL), libraryBC(NULL), librarySize(0),
                mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {