Merge branch 'next'
diff --git a/Makefile b/Makefile
index bd6cf51..0da5a1a 100644
--- a/Makefile
+++ b/Makefile
@@ -135,25 +135,29 @@
 
 VERSION=$(shell echo `grep -e PKG_MAJOR -e PKG_MINOR CONFIG | grep -v = | awk '{print $$3}'` | awk '{print $$1"."$$2}')
 
-.PHONY: all clean install uninstall diet dist
+.PHONY: all clean install uninstall dist
 
 all: $(LIBRARY) $(ARCHIVE) $(PKGCFGF)
 	$(MAKE) -C tests
 	$(INSTALL_DATA) lib$(LIBNAME).$(EXT) tests
 
-$(LIBRARY): diet $(LIBOBJ)
+$(LIBRARY): $(LIBOBJ)
 	$(CC) $(LDFLAGS) $(LIBOBJ) -o $(LIBRARY)
 
-# generate include/diet.h
-diet:
-	@echo "#ifndef CAPSTONE_DIET_H" > include/diet.h
+$(LIBOBJ): include/diet.h
+
+# auto-generate include/diet.h
+include/diet.h:
+	@echo "// File auto-generated by Makefile for Capstone framework. DO NOT MODIFY!" > include/diet.h
+	@echo "" >> include/diet.h
+	@echo "#ifndef CAPSTONE_DIET_H" >> include/diet.h
 	@echo "#define CAPSTONE_DIET_H" >> include/diet.h
 	@echo "" >> include/diet.h
-	@echo "// File auto-generated by Makefile for Capstone framework. DO NOT MODIFY!" >> include/diet.h
-	@echo "" >> include/diet.h
 ifneq (,$(findstring yes,$(CAPSTONE_DIET)))
+	@echo "// Capstone is in DIET mode" >> include/diet.h
 	@echo "#define CAPSTONE_DIET" >> include/diet.h
 else
+	@echo "// Capstone is in standard mode (NOT diet)" >> include/diet.h
 	@echo "#undef CAPSTONE_DIET" >> include/diet.h
 endif
 	@echo "" >> include/diet.h
@@ -192,6 +196,7 @@
 clean:
 	rm -f $(LIBOBJ) lib$(LIBNAME).*
 	rm -f $(PKGCFGF)
+	rm -f include/diet.h
 	$(MAKE) -C bindings/python clean
 	$(MAKE) -C bindings/java clean
 	$(MAKE) -C bindings/ocaml clean
diff --git a/bindings/java/capstone/Capstone.java b/bindings/java/capstone/Capstone.java
index 413d342..0c9d313 100644
--- a/bindings/java/capstone/Capstone.java
+++ b/bindings/java/capstone/Capstone.java
@@ -204,7 +204,7 @@
     public NativeLong cs_disasm_ex(NativeLong handle, byte[] code, NativeLong code_len,
         long addr, NativeLong count, PointerByReference insn);
     public void cs_free(Pointer p, NativeLong count);
-    public int cs_close(NativeLong handle);
+    public int cs_close(NativeLongByReference handle);
     public int cs_option(NativeLong handle, int option, NativeLong optionValue);
 
     public String cs_reg_name(NativeLong csh, int id);
@@ -321,7 +321,7 @@
   }
 
   protected void finalize() {
-    cs.cs_close(ns.csh);
+    cs.cs_close(ns.handleRef);
   }
 
   public CsInsn[] disasm(byte[] code, long address) {
diff --git a/bindings/python/capstone/__init__.py b/bindings/python/capstone/__init__.py
index bdc4634..9777165 100644
--- a/bindings/python/capstone/__init__.py
+++ b/bindings/python/capstone/__init__.py
@@ -1 +1 @@
-from capstone import Cs, CsError, cs_disasm_quick, cs_disasm_lite, cs_version, cs_support, CS_API_MAJOR, CS_API_MINOR, CS_ARCH_ARM, CS_ARCH_ARM64, CS_ARCH_MIPS, CS_ARCH_X86, CS_ARCH_PPC, CS_ARCH_ALL, CS_MODE_LITTLE_ENDIAN, CS_MODE_ARM, CS_MODE_THUMB, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT, CS_OPT_SYNTAX_INTEL, CS_OPT_SYNTAX_ATT, CS_OPT_SYNTAX_NOREGNAME, CS_OPT_DETAIL, CS_OPT_ON, CS_OPT_OFF, CS_MODE_16, CS_MODE_32, CS_MODE_64, CS_MODE_BIG_ENDIAN, CS_MODE_MICRO, CS_MODE_N64, CS_SUPPORT_DIET
+from capstone import Cs, CsError, cs_disasm_quick, cs_disasm_lite, cs_version, cs_version_bind, cs_support, CS_API_MAJOR, CS_API_MINOR, CS_ARCH_ARM, CS_ARCH_ARM64, CS_ARCH_MIPS, CS_ARCH_X86, CS_ARCH_PPC, CS_ARCH_ALL, CS_MODE_LITTLE_ENDIAN, CS_MODE_ARM, CS_MODE_THUMB, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT, CS_OPT_SYNTAX_INTEL, CS_OPT_SYNTAX_ATT, CS_OPT_SYNTAX_NOREGNAME, CS_OPT_DETAIL, CS_OPT_ON, CS_OPT_OFF, CS_MODE_16, CS_MODE_32, CS_MODE_64, CS_MODE_BIG_ENDIAN, CS_MODE_MICRO, CS_MODE_N64, CS_SUPPORT_DIET
diff --git a/bindings/python/capstone/capstone.py b/bindings/python/capstone/capstone.py
index 77a7990..cbbe4d9 100644
--- a/bindings/python/capstone/capstone.py
+++ b/bindings/python/capstone/capstone.py
@@ -8,6 +8,7 @@
 
     'cs_disasm_quick',
     'cs_version',
+    'cs_version_bind',
     'cs_support',
 
     'CS_API_MAJOR',
@@ -201,7 +202,7 @@
 _setup_prototype(_cs, "cs_disasm_ex", ctypes.c_size_t, ctypes.c_size_t, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t, \
         ctypes.c_uint64, ctypes.c_size_t, ctypes.POINTER(ctypes.POINTER(_cs_insn)))
 _setup_prototype(_cs, "cs_free", None, ctypes.c_void_p, ctypes.c_size_t)
-_setup_prototype(_cs, "cs_close", ctypes.c_int, ctypes.c_size_t)
+_setup_prototype(_cs, "cs_close", ctypes.c_int, ctypes.POINTER(ctypes.c_size_t))
 _setup_prototype(_cs, "cs_reg_name", ctypes.c_char_p, ctypes.c_size_t, ctypes.c_uint)
 _setup_prototype(_cs, "cs_insn_name", ctypes.c_char_p, ctypes.c_size_t, ctypes.c_uint)
 _setup_prototype(_cs, "cs_op_count", ctypes.c_int, ctypes.c_size_t, ctypes.POINTER(_cs_insn), ctypes.c_uint)
@@ -222,6 +223,7 @@
         return _cs.cs_strerror(self.errno)
 
 
+# return the core's version
 def cs_version():
     major = ctypes.c_int()
     minor = ctypes.c_int()
@@ -229,6 +231,11 @@
     return (major.value, minor.value, combined)
 
 
+# return the binding's version
+def cs_version_bind():
+    return (CS_API_MAJOR, CS_API_MINOR, (CS_API_MAJOR << 8) + CS_API_MINOR)
+
+
 def cs_support(query):
     return _cs.cs_support(query)
 
@@ -270,7 +277,7 @@
         return
         yield
 
-    status = _cs.cs_close(csh)
+    status = _cs.cs_close(ctypes.byref(csh))
     if status != CS_ERR_OK:
         raise CsError(status)
 
@@ -311,7 +318,7 @@
         return
         yield
 
-    status = _cs.cs_close(csh)
+    status = _cs.cs_close(ctypes.byref(csh))
     if status != CS_ERR_OK:
         raise CsError(status)
 
@@ -519,7 +526,7 @@
 
     def __del__(self):
         if self.csh:
-            status = _cs.cs_close(self.csh)
+            status = _cs.cs_close(ctypes.byref(self.csh))
             if status != CS_ERR_OK:
                 raise CsError(status)
 
diff --git a/config.mk b/config.mk
index 3b27ec3..1ab2da0 100644
--- a/config.mk
+++ b/config.mk
@@ -1,5 +1,5 @@
 ################################################################################
-# Specify which archs you want to compile in.
+# Specify which archs you want to compile in. By default, we build all archs.
 # DO NOT touch the line below.
 CAPSTONE_ARCHS =
 
diff --git a/cs.c b/cs.c
index 4f561a1..f36a120 100644
--- a/cs.c
+++ b/cs.c
@@ -81,7 +81,7 @@
 				(1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
 				(1 << CS_ARCH_PPC));
 
-	if (query < CS_ARCH_MAX)
+	if ((unsigned int)query < CS_ARCH_MAX)
 		return all_arch & (1 << query);
 
 	if (query == CS_SUPPORT_DIET) {
@@ -177,25 +177,16 @@
 	}
 }
 
-cs_err cs_close(csh handle)
+cs_err cs_close(csh *handle)
 {
-	if (!handle)
+	if (*handle == 0)
+		// invalid handle
 		return CS_ERR_CSH;
 
-	struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
+	struct cs_struct *ud = (struct cs_struct *)(*handle);
 
-	switch (ud->arch) {
-		case CS_ARCH_X86:
-			break;
-		case CS_ARCH_ARM:
-		case CS_ARCH_MIPS:
-		case CS_ARCH_ARM64:
-		case CS_ARCH_PPC:
-			cs_mem_free(ud->printer_info);
-			break;
-		default:	// unsupported architecture
-			return CS_ERR_HANDLE;
-	}
+	if (ud->printer_info)
+		cs_mem_free(ud->printer_info);
 
 	// arch_destroy[ud->arch](ud);
 
@@ -203,6 +194,10 @@
 	memset(ud, 0, sizeof(*ud));
 	cs_mem_free(ud);
 
+	// invalidate this handle by ZERO out its value.
+	// this is to make sure it is unusable after cs_close()
+	*handle = 0;
+
 	return CS_ERR_OK;
 }
 
diff --git a/include/capstone.h b/include/capstone.h
index a233ce3..c926e4f 100644
--- a/include/capstone.h
+++ b/include/capstone.h
@@ -232,12 +232,14 @@
  cached memory, thus access to any Capstone API after cs_close() might crash
  your application.
 
- @handle: handle returned by cs_open()
+ In fact,this API invalidate @handle by ZERO out its value (i.e *handle = 0).
+
+ @handle: pointer to a handle returned by cs_open()
 
  @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
  for detailed error).
 */
-cs_err cs_close(csh handle);
+cs_err cs_close(csh *handle);
 
 /*
  Set option for disassembling engine at runtime
diff --git a/tests/test.c b/tests/test.c
index 1bdf6b2..9f32e08 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -189,7 +189,7 @@
 
 		printf("\n");
 
-		cs_close(handle);
+		cs_close(&handle);
 	}
 }
 
diff --git a/tests/test_arm.c b/tests/test_arm.c
index 60e7dad..6bb8969 100644
--- a/tests/test_arm.c
+++ b/tests/test_arm.c
@@ -226,7 +226,7 @@
 
 		printf("\n");
 
-		cs_close(handle);
+		cs_close(&handle);
 	}
 }
 
diff --git a/tests/test_arm64.c b/tests/test_arm64.c
index 1f8e435..8b44e17 100644
--- a/tests/test_arm64.c
+++ b/tests/test_arm64.c
@@ -184,7 +184,7 @@
 
 		printf("\n");
 
-		cs_close(handle);
+		cs_close(&handle);
 	}
 }
 
diff --git a/tests/test_detail.c b/tests/test_detail.c
index 786a8bc..a320ba1 100644
--- a/tests/test_detail.c
+++ b/tests/test_detail.c
@@ -218,7 +218,7 @@
 
 		printf("\n");
 
-		cs_close(handle);
+		cs_close(&handle);
 	}
 }
 
diff --git a/tests/test_mips.c b/tests/test_mips.c
index e4eb0c3..fb6ea96 100644
--- a/tests/test_mips.c
+++ b/tests/test_mips.c
@@ -131,7 +131,7 @@
 
 		printf("\n");
 
-		cs_close(handle);
+		cs_close(&handle);
 	}
 }
 
diff --git a/tests/test_ppc.c b/tests/test_ppc.c
index 393eea2..548e0c5 100644
--- a/tests/test_ppc.c
+++ b/tests/test_ppc.c
@@ -123,7 +123,7 @@
 
 		printf("\n");
 
-		cs_close(handle);
+		cs_close(&handle);
 	}
 }
 
diff --git a/tests/test_x86.c b/tests/test_x86.c
index 3ab2583..6086c58 100644
--- a/tests/test_x86.c
+++ b/tests/test_x86.c
@@ -201,7 +201,7 @@
 
 		printf("\n");
 
-		cs_close(handle);
+		cs_close(&handle);
 	}
 }