2000-07-16 22:18:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-07-21 21:39:16 +00:00
|
|
|
#include <time.h>
|
2004-09-23 17:55:23 +00:00
|
|
|
#include <_heap.h>
|
2000-07-16 22:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-31 00:59:32 +00:00
|
|
|
static unsigned char* V[256];
|
2000-07-16 22:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-31 00:59:32 +00:00
|
|
|
static unsigned char* Alloc (void)
|
2000-07-16 22:18:33 +00:00
|
|
|
/* Allocate a random sized chunk of memory */
|
|
|
|
{
|
|
|
|
/* Determine the size */
|
|
|
|
unsigned char Size = (((unsigned char)rand()) & 0x7F) + 1;
|
|
|
|
|
|
|
|
/* Allocate memory */
|
2022-01-31 00:59:32 +00:00
|
|
|
unsigned char* P = malloc (Size);
|
2000-07-16 22:18:33 +00:00
|
|
|
|
|
|
|
/* Set the string to a defined value. We use the size, since this will
|
2014-06-30 09:10:35 +00:00
|
|
|
** also allow us to retrieve it later.
|
|
|
|
*/
|
2000-07-16 22:18:33 +00:00
|
|
|
if (P) {
|
2013-05-09 11:56:54 +00:00
|
|
|
memset (P, Size, Size);
|
2000-07-16 22:18:33 +00:00
|
|
|
} else {
|
2013-05-09 11:56:54 +00:00
|
|
|
printf ("Could not allocate %u bytes\n", Size);
|
|
|
|
exit (EXIT_FAILURE);
|
2000-07-16 22:18:33 +00:00
|
|
|
}
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-31 00:59:32 +00:00
|
|
|
static void Free (unsigned char* P)
|
2000-07-16 22:18:33 +00:00
|
|
|
/* Check a memory block and free it */
|
|
|
|
{
|
|
|
|
unsigned char I;
|
|
|
|
|
|
|
|
/* Get the size of the block */
|
|
|
|
unsigned char Size = P[0];
|
|
|
|
|
|
|
|
/* Scan the block */
|
|
|
|
for (I = 1; I < Size; ++I) {
|
2013-05-09 11:56:54 +00:00
|
|
|
if (P[I] != Size) {
|
|
|
|
printf ("Scan failed - expected %02X, got %02X\n",
|
|
|
|
Size, P[I]);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2000-07-16 22:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the block */
|
|
|
|
free (P);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void FillArray (void)
|
|
|
|
/* Fill the array with randomly allocated memory chunks */
|
|
|
|
{
|
|
|
|
unsigned char I = 0;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
V[I] = Alloc ();
|
|
|
|
++I;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ShowInfo (void)
|
|
|
|
/* Show heap info */
|
|
|
|
{
|
2000-07-21 21:39:16 +00:00
|
|
|
/* Count free blocks */
|
|
|
|
unsigned Count = 0;
|
2004-09-23 17:55:23 +00:00
|
|
|
register struct freeblock* P = _heapfirst;
|
2000-07-21 21:39:16 +00:00
|
|
|
while (P) {
|
2013-05-09 11:56:54 +00:00
|
|
|
++Count;
|
|
|
|
P = P->next;
|
2000-07-21 21:39:16 +00:00
|
|
|
}
|
|
|
|
printf ("%04X %04X %04X %04X %04X %u\n",
|
2013-05-09 11:56:54 +00:00
|
|
|
_heaporg, _heapptr, _heapend, _heapfirst, _heaplast, Count);
|
2000-07-21 21:39:16 +00:00
|
|
|
|
|
|
|
if (Count) {
|
2013-05-09 11:56:54 +00:00
|
|
|
P = _heapfirst;
|
|
|
|
while (P) {
|
|
|
|
printf ("%04X %04X %04X %04X(%u)\n",
|
|
|
|
(unsigned) P, P[2], P[1], P[0], P[0]);
|
|
|
|
P = P->next;
|
|
|
|
}
|
|
|
|
getchar ();
|
2000-07-21 21:39:16 +00:00
|
|
|
}
|
2000-07-16 22:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Test1 (void)
|
|
|
|
{
|
|
|
|
unsigned char I;
|
|
|
|
FillArray ();
|
|
|
|
for (I = 0; I < 0x80; ++I) {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[0x7F-I]);
|
|
|
|
Free (V[0x80+I]);
|
2000-07-16 22:18:33 +00:00
|
|
|
}
|
|
|
|
ShowInfo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Test2 (void)
|
2000-07-21 21:39:16 +00:00
|
|
|
{
|
|
|
|
unsigned char I;
|
|
|
|
FillArray ();
|
|
|
|
I = 0;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[I]);
|
|
|
|
++I;
|
2000-07-21 21:39:16 +00:00
|
|
|
} while (I != 0);
|
|
|
|
ShowInfo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Test3 (void)
|
|
|
|
{
|
|
|
|
unsigned char I;
|
|
|
|
FillArray ();
|
|
|
|
I = 0;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
--I;
|
|
|
|
Free (V[I]);
|
2000-07-21 21:39:16 +00:00
|
|
|
} while (I != 0);
|
|
|
|
ShowInfo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Test4 (void)
|
2000-07-16 22:18:33 +00:00
|
|
|
{
|
|
|
|
unsigned char I;
|
|
|
|
FillArray ();
|
|
|
|
I = 0;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[I]);
|
|
|
|
I += 2;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 0);
|
|
|
|
I = 1;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[I]);
|
|
|
|
I += 2;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 1);
|
|
|
|
ShowInfo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-07-21 21:39:16 +00:00
|
|
|
static void Test5 (void)
|
2000-07-16 22:18:33 +00:00
|
|
|
{
|
|
|
|
unsigned char I;
|
|
|
|
FillArray ();
|
|
|
|
I = 0;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[I]);
|
|
|
|
I += 2;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 0);
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
V[I] = Alloc ();
|
|
|
|
I += 2;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 0);
|
|
|
|
I = 1;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[I]);
|
|
|
|
I += 2;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 1);
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
V[I] = Alloc ();
|
|
|
|
I += 2;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 1);
|
|
|
|
I = 0;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[I]);
|
|
|
|
++I;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 0);
|
|
|
|
ShowInfo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-07-21 21:39:16 +00:00
|
|
|
static void Test6 (void)
|
2000-07-16 22:18:33 +00:00
|
|
|
{
|
|
|
|
unsigned char I, J;
|
|
|
|
FillArray ();
|
|
|
|
I = J = 0;
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
do {
|
|
|
|
Free (V[I]);
|
|
|
|
V[I] = Alloc ();
|
|
|
|
++I;
|
|
|
|
} while (I != 0);
|
|
|
|
++J;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (J < 5);
|
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
Free (V[I]);
|
|
|
|
++I;
|
2000-07-16 22:18:33 +00:00
|
|
|
} while (I != 0);
|
|
|
|
ShowInfo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
unsigned long T;
|
|
|
|
|
|
|
|
/* Show info at start */
|
|
|
|
ShowInfo ();
|
2020-08-26 13:08:17 +00:00
|
|
|
#if !defined(__APPLE2__)
|
2000-07-16 22:18:33 +00:00
|
|
|
/* Remember the time */
|
|
|
|
T = clock ();
|
2020-08-26 13:08:17 +00:00
|
|
|
#endif
|
2000-07-16 22:18:33 +00:00
|
|
|
/* Do the tests */
|
|
|
|
Test1 ();
|
|
|
|
Test2 ();
|
|
|
|
Test3 ();
|
|
|
|
Test4 ();
|
2000-07-21 21:39:16 +00:00
|
|
|
Test5 ();
|
|
|
|
Test6 ();
|
2000-07-16 22:18:33 +00:00
|
|
|
|
2020-08-26 13:08:17 +00:00
|
|
|
#if !defined(__APPLE2__)
|
2000-07-16 22:18:33 +00:00
|
|
|
/* Calculate the time and print it */
|
|
|
|
T = clock () - T;
|
|
|
|
printf ("Time needed: %lu ticks\n", T);
|
2020-08-26 13:08:17 +00:00
|
|
|
#endif
|
2000-07-16 22:18:33 +00:00
|
|
|
/* Done */
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|