Index: trunk/Mars/mcore/zofits.h
===================================================================
--- trunk/Mars/mcore/zofits.h	(revision 17778)
+++ trunk/Mars/mcore/zofits.h	(revision 17779)
@@ -8,5 +8,4 @@
  *      Author: lyard
  */
-
 #include "ofits.h"
 #include "huffman.h"
@@ -16,4 +15,6 @@
 #ifdef HAVE_BOOST_THREAD
 #include <boost/thread.hpp>
+#else
+#include <unistd.h>
 #endif
 
@@ -89,5 +90,5 @@
 public:
         /// static setter for the default number of threads to use. -1 means all available physical cores
-        static uint32_t DefaultNumThreads(const uint32_t &_n=-2) { static uint32_t n=0; if (int32_t(_n)>=-1) n=_n; return n; }
+        static uint32_t DefaultNumThreads(const uint32_t &_n=-2) { static uint32_t n=0; if (int32_t(_n)>-2) n=_n; return n; }
         static uint32_t DefaultMaxMemory(const uint32_t &_n=0) { static uint32_t n=1000000; if (_n>0) n=_n; return n; }
         static uint32_t DefaultMaxNumTiles(const uint32_t &_n=0) { static uint32_t n=1000; if (_n>0) n=_n; return n; }
@@ -119,7 +120,7 @@
             fWriteToDiskQueue(std::bind(&zofits::WriteBufferToDisk, this, std::placeholders::_1), false)
         {
-            open(fname);
             InitMemberVariables(numTiles, rowPerTile, size_t(maxUsableMem)*1000);
             SetNumThreads(DefaultNumThreads());
+            open(fname);
         }
 
@@ -131,7 +132,7 @@
             fWriteToDiskQueue(std::bind(&zofits::WriteBufferToDisk, this, std::placeholders::_1), false)
         {
-            open(fname);
             InitMemberVariables(numTiles, rowPerTile, size_t(maxUsableMem)*1000);
             SetNumThreads(DefaultNumThreads());
+            open(fname);
         }
 
@@ -576,5 +577,5 @@
                 return false;
 
-            const size_t size = SizeFromType(typechar);
+            const size_t size = FITS::SizeFromType(typechar);
 
             Table::Column col;
@@ -589,5 +590,5 @@
             fRealColumns.emplace_back(col, comp);
 
-            SetStr("ZFORM"+std::to_string((long long int)(fRealColumns.size())), std::to_string((long long int)(cnt))+typechar, "format of "+name+" "+CommentFromType(typechar));
+            SetStr("ZFORM"+std::to_string((long long int)(fRealColumns.size())), std::to_string((long long int)(cnt))+typechar, "format of "+name+" "+FITS::CommentFromType(typechar));
             SetStr("ZCTYP"+std::to_string((long long int)(fRealColumns.size())), "FACT", "Compression type: FACT");
 
@@ -614,4 +615,6 @@
 #else
             unsigned int num_available_cores = std::thread::hardware_concurrency();
+            if (num_available_cores == 0)
+                num_available_cores = sysconf(_SC_NPROCESSORS_ONLN);
 #endif
             // could not detect number of available cores from system properties...
@@ -637,7 +640,10 @@
         void reallocateBuffers()
         {
-            const size_t chunk_size = fRealRowWidth*fNumRowsPerTile + fRealColumns.size()*sizeof(FITS::BlockHeader) + sizeof(FITS::TileHeader) + 8; //+8 for checksuming;
+            size_t total_block_head_size = 0;
+            for (auto it=fRealColumns.begin(); it!=fRealColumns.end(); it++)
+                total_block_head_size += it->block_head.getSizeOnDisk();
+
+            const size_t chunk_size = fRealRowWidth*fNumRowsPerTile + total_block_head_size + sizeof(FITS::TileHeader) + 8; //+8 for checksuming;
             fMemPool.setChunkSize(chunk_size);
-
             fSmartBuffer = fMemPool.malloc();
             fRawSumBuffer.resize(fRealRowWidth + 4-fRealRowWidth%4); //for checksuming
