-#include "src\lib\lib_head.h"
+/* Project 16 Source Code~
+ * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
+ *
+ * This file is part of Project 16.
+ *
+ * Project 16 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Project 16 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>, or
+ * write to the Free Software Foundation, Inc., 51 Franklin Street,
+ * Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include "src/lib/lib_head.h"
\r
/* local function */\r
-void wait(clock_t wait);\r
+void wait(clock_t wait);
+void* AllocateLargestFreeBlock(size_t* Size);
+size_t GetFreeSize(void);
+long int filesize(FILE *fp);\r
\r
/* Function: Wait **********************************************************\r
*\r
\r
goal = wait + clock();\r
while((goal > clock()) && !kbhit()) ;\r
-} /* End of wait */
\ No newline at end of file
+} /* End of wait */
+
+void* AllocateLargestFreeBlock(size_t* Size)
+{
+ size_t s0, s1;
+ void* p;
+
+ s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
+
+ while (s0 && (p = malloc(s0)) == NULL)
+ s0 >>= 1;
+
+ if (p)
+ free(p);
+
+ s1 = s0 >> 1;
+
+ while (s1)
+ {
+ if ((p = malloc(s0 + s1)) != NULL)
+ {
+ s0 += s1;
+ free(p);
+ }
+ s1 >>= 1;
+ }
+
+ while (s0 && (p = malloc(s0)) == NULL)
+ s0 ^= s0 & -s0;
+
+ *Size = s0;
+ return p;
+}
+
+size_t GetFreeSize(void)
+{
+ size_t total = 0;
+ void* pFirst = NULL;
+ void* pLast = NULL;
+
+ for (;;)
+ {
+ size_t largest;
+ void* p = AllocateLargestFreeBlock(&largest);
+
+ if (largest < sizeof(void*))
+ {
+ if (p != NULL)
+ free(p);
+ break;
+ }
+
+ *(void**)p = NULL;
+
+ total += largest;
+
+ if (pFirst == NULL)
+ pFirst = p;
+
+ if (pLast != NULL)
+ *(void**)pLast = p;
+
+ pLast = p;
+ }
+
+ while (pFirst != NULL)
+ {
+ void* p = *(void**)pFirst;
+ free(pFirst);
+ pFirst = p;
+ }
+
+ return total;
+}
+
+long int
+filesize(FILE *fp)\r
+{\r
+ long int save_pos, size_of_file;\r
+\r
+ save_pos = ftell(fp);\r
+ fseek(fp, 0L, SEEK_END);\r
+ size_of_file = ftell(fp);\r
+ fseek(fp, save_pos, SEEK_SET);\r
+ return(size_of_file);\r
+}