#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <time.h>
+#include <time.h>
/* From _heap.h */
* also allow us to retrieve it later.
*/
if (P) {
- memset (P, Size, Size);
+ memset (P, Size, Size);
} else {
- printf ("Could not allocate %u bytes\n", Size);
+ printf ("Could not allocate %u bytes\n", Size);
+ exit (EXIT_FAILURE);
}
return P;
}
/* Scan the block */
for (I = 1; I < Size; ++I) {
- if (P[I] != Size) {
- printf ("Scan failed - expected %02X, got %02X\n",
- Size, P[I]);
+ if (P[I] != Size) {
+ printf ("Scan failed - expected %02X, got %02X\n",
+ Size, P[I]);
+ exit (EXIT_FAILURE);
}
}
{
unsigned char I = 0;
do {
- V[I] = Alloc ();
- ++I;
+ V[I] = Alloc ();
+ ++I;
} while (I != 0);
}
static void ShowInfo (void)
/* Show heap info */
{
- printf ("%04X %04X %04X %04X %04X\n",
- _horg, _hptr, _hend, _hfirst, _hlast);
+ /* Count free blocks */
+ unsigned Count = 0;
+ unsigned** P = (unsigned**) _hfirst;
+ while (P) {
+ ++Count;
+ P = P[1];
+ }
+ printf ("%04X %04X %04X %04X %04X %u\n",
+ _horg, _hptr, _hend, _hfirst, _hlast, Count);
+
+ if (Count) {
+ P = (unsigned**) _hfirst;
+ while (P) {
+ printf ("%04X %04X %04X %04X(%u)\n",
+ (unsigned) P, P[2], P[1], P[0], P[0]);
+ P = P[1];
+ }
+ getchar ();
+ }
}
static void Test1 (void)
-/* First test */
{
unsigned char I;
FillArray ();
static void Test2 (void)
-/* Second test */
+{
+ unsigned char I;
+ FillArray ();
+ I = 0;
+ do {
+ Free (V[I]);
+ ++I;
+ } while (I != 0);
+ ShowInfo ();
+}
+
+
+
+static void Test3 (void)
+{
+ unsigned char I;
+ FillArray ();
+ I = 0;
+ do {
+ --I;
+ Free (V[I]);
+ } while (I != 0);
+ ShowInfo ();
+}
+
+
+
+static void Test4 (void)
{
unsigned char I;
FillArray ();
-static void Test3 (void)
-/* Third test */
+static void Test5 (void)
{
unsigned char I;
FillArray ();
} while (I != 1);
I = 0;
do {
- Free (V[I]);
+ Free (V[I]);
++I;
} while (I != 0);
ShowInfo ();
-static void Test4 (void)
-/* Fourth test */
+static void Test6 (void)
{
unsigned char I, J;
FillArray ();
Test2 ();
Test3 ();
Test4 ();
+ Test5 ();
+ Test6 ();
/* Calculate the time and print it */
T = clock () - T;