Browse Source

Fixes for Buf, unit test

Adam Ierymenko 5 years ago
parent
commit
547f39bb49
3 changed files with 60 additions and 4 deletions
  1. 1 4
      node/Buf.cpp
  2. 3 0
      node/Buf.hpp
  3. 56 0
      node/Tests.cpp

+ 1 - 4
node/Buf.cpp

@@ -17,9 +17,6 @@
 #define sched_yield() Sleep(0)
 #endif
 
-// Sanity limit on maximum buffer pool size
-#define ZT_BUF_MAX_POOL_SIZE 1024
-
 namespace ZeroTier {
 
 static std::atomic<uintptr_t> s_pool(0);
@@ -84,11 +81,11 @@ void Buf::freePool() noexcept
 			break;
 		sched_yield();
 	}
-	s_allocated.store(0);
 	s_pool.store(0);
 
 	while (bb != 0) {
 		const uintptr_t next = ((Buf *)bb)->__nextInPool;
+		--s_allocated;
 		free((void *)bb);
 		bb = next;
 	}

+ 3 - 0
node/Buf.hpp

@@ -35,6 +35,9 @@
 #define ZT_BUF_MEM_SIZE 0x00004000
 #define ZT_BUF_MEM_MASK 0x00003fffU
 
+// Sanity limit on maximum buffer pool size
+#define ZT_BUF_MAX_POOL_SIZE 1024
+
 namespace ZeroTier {
 
 /**

+ 56 - 0
node/Tests.cpp

@@ -38,6 +38,7 @@
 #include "Hashtable.hpp"
 #include "FCV.hpp"
 #include "SHA512.hpp"
+#include "Defragmenter.hpp"
 
 #include <cstdint>
 #include <cstring>
@@ -433,6 +434,37 @@ extern "C" const char *ZTT_general()
 
 			ZT_T_PRINTF("OK" ZT_EOL_S);
 		}
+
+		{
+			ZT_T_PRINTF("[general] Testing Buf memory pool (basic sanity check)... ");
+			try {
+				std::vector< SharedPtr<Buf> > bufs;
+				Buf::freePool();
+				long cnt = Buf::poolAllocated();
+				for(int i=0;i<(ZT_BUF_MAX_POOL_SIZE + 100);++i)
+					bufs.push_back(SharedPtr<Buf>(new Buf()));
+				cnt += ZT_BUF_MAX_POOL_SIZE + 100;
+				if (Buf::poolAllocated() != cnt) {
+					ZT_T_PRINTF("FAILED" ZT_EOL_S);
+					return "Buf memory pool test failed";
+				}
+				bufs.clear();
+				if (Buf::poolAllocated() != ZT_BUF_MAX_POOL_SIZE) {
+					ZT_T_PRINTF("FAILED" ZT_EOL_S);
+					return "Buf memory pool test failed";
+				}
+				Buf::freePool();
+				cnt -= ZT_BUF_MAX_POOL_SIZE + 100;
+				if (Buf::poolAllocated() != cnt) {
+					ZT_T_PRINTF("FAILED" ZT_EOL_S);
+					return "Buf memory pool test failed";
+				}
+			} catch ( ... ) {
+				ZT_T_PRINTF("FAILED (out of memory)" ZT_EOL_S);
+				return "Buf memory pool test failed: out of memory";
+			}
+			ZT_T_PRINTF("OK" ZT_EOL_S);
+		}
 	} catch (std::exception &e) {
 		ZT_T_PRINTF(ZT_EOL_S "[general] Unexpected exception: %s" ZT_EOL_S,e.what());
 		return e.what();
@@ -672,6 +704,30 @@ extern "C" const char *ZTT_crypto()
 
 extern "C" const char *ZTT_defragmenter()
 {
+#if 0
+	Defragmenter<11> defrag;
+
+/*
+	ZT_ALWAYS_INLINE ResultCode assemble(
+		const uint64_t messageId,
+		FCV< Buf::Slice,MF > &message,
+		SharedPtr<Buf> &fragment,
+		const unsigned int fragmentDataIndex,
+		const unsigned int fragmentDataSize,
+		const unsigned int fragmentNo,
+		const unsigned int totalFragmentsExpected,
+		const int64_t now,
+		const SharedPtr< Path > &via,
+		const unsigned int maxIncomingFragmentsPerPath)
+	{
+*/
+
+	uint64_t messageId = 1;
+	FCV< Buf::Slice,11 > message;
+	for(int kk=0;kk<16;++kk) {
+	}
+
+#endif
 	return nullptr;
 }