+#ifdef MEMTEST_32BIT
+#define ULONG_ONEZERO 0xaaaaaaaaUL
+#define ULONG_ZEROONE 0x55555555UL
+#else
+#define ULONG_ONEZERO 0xaaaaaaaaaaaaaaaaUL
+#define ULONG_ZEROONE 0x5555555555555555UL
+#endif
+
+static struct winsize ws;
+size_t progress_printed; /* Printed chars in screen-wide progress bar. */
+size_t progress_full; /* How many chars to write to fill the progress bar. */
+
+void memtest_progress_start(char *title, int pass) {
+ int j;
+
+ printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
+ /* Fill with dots. */
+ for (j = 0; j < ws.ws_col*(ws.ws_row-2); j++) printf(".");
+ printf("Please keep the test running several minutes per GB of memory.\n");
+ printf("Also check http://www.memtest86.com/ and http://pyropus.ca/software/memtester/");
+ printf("\x1b[H\x1b[2K"); /* Cursor home, clear current line. */
+ printf("%s [%d]\n", title, pass); /* Print title. */
+ progress_printed = 0;
+ progress_full = ws.ws_col*(ws.ws_row-3);
+ fflush(stdout);
+}
+
+void memtest_progress_end(void) {
+ printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
+}
+
+void memtest_progress_step(size_t curr, size_t size, char c) {
+ size_t chars = ((unsigned long long)curr*progress_full)/size, j;
+
+ for (j = 0; j < chars-progress_printed; j++) {
+ printf("%c",c);
+ progress_printed++;
+ }
+ fflush(stdout);
+}
+
+/* Test that addressing is fine. Every location is populated with its own
+ * address, and finally verified. This test is very fast but may detect
+ * ASAP big issues with the memory subsystem. */
+void memtest_addressing(unsigned long *l, size_t bytes) {
+ unsigned long words = bytes/sizeof(unsigned long);
+ unsigned long j, *p;
+
+ /* Fill */
+ p = l;
+ for (j = 0; j < words; j++) {
+ *p = (unsigned long)p;
+ p++;
+ if ((j & 0xffff) == 0) memtest_progress_step(j,words*2,'A');
+ }
+ /* Test */
+ p = l;
+ for (j = 0; j < words; j++) {
+ if (*p != (unsigned long)p) {
+ printf("\n*** MEMORY ADDRESSING ERROR: %p contains %lu\n",
+ (void*) p, *p);
+ exit(1);
+ }
+ p++;
+ if ((j & 0xffff) == 0) memtest_progress_step(j+words,words*2,'A');
+ }
+}
+