os161-1.99
 All Data Structures
vm-mix1.c
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <unistd.h>
00005 
00006 /* This is creating a program that has
00007  * a few more text pages than the average
00008  * program we usually have.
00009  */
00010 
00011 // #define DEBUG
00012 
00013 extern void     call_all();
00014 void write_data(unsigned int array[], unsigned int start);
00015 void read_data(unsigned int array[], unsigned int start, const char *array_name);
00016 void print_data(unsigned int array[]);
00017 void do_work(unsigned int start);
00018 
00019 #define PAGE_SIZE           (4096)
00020 #define DATA_BYTES          (3 * 1024 * 1024)
00021 #define PAGES               (DATA_BYTES / PAGE_SIZE)
00022 #define ELEM_SIZE           (sizeof(unsigned int))
00023 #define ELEMS               ((PAGE_SIZE * PAGES / sizeof(unsigned int)) / 2)
00024 #define ELEMS_PER_PAGE      (PAGE_SIZE / ELEM_SIZE)
00025 #define NUM_REFS            (2)
00026 
00027 #define STACK_PAGES_USED    (9)
00028 #define STACK_ARRAY_ELEMS   (PAGE_SIZE * STACK_PAGES_USED / sizeof(unsigned int))
00029 
00030 unsigned int init[] = {
00031   0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
00032  10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
00033  20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
00034  30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
00035  40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
00036  50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
00037 };
00038 
00039 #define INIT_ARRAY_ELEMS     (sizeof(init) / sizeof(int))
00040 
00041 unsigned int array1[ELEMS];
00042 unsigned int array2[ELEMS];
00043 
00044 void
00045 write_data(unsigned int array[], unsigned int start)
00046 {
00047         unsigned int ref = 0;
00048         unsigned int i = 0;
00049 
00050         for (ref = 0; ref < NUM_REFS-1; ref++) {
00051                 for (i=0; i<ELEMS; i++) {
00052       array[i] = start + (i / ELEMS_PER_PAGE);
00053                 }
00054         }
00055 }
00056 
00057 void
00058 read_data(unsigned int array[], unsigned int start, const char *array_name)
00059 {
00060   unsigned int ref = 0;
00061         unsigned int i = 0;
00062 
00063         for (ref = 0; ref < NUM_REFS-1; ref++) {
00064                 for (i=0; i<ELEMS; i++) {
00065                         if (array[i] != (start + (i / ELEMS_PER_PAGE))) {
00066                                 printf("FAILED in file %s at line %d %s[%d] = %u != %u ref = %d\n", 
00067                 __FILE__, __LINE__, array_name, i, array[i], i, ref+1);
00068                                 exit(1);
00069                         }
00070                 }
00071   }
00072 }
00073 
00074 void
00075 print_data(unsigned int array[])
00076 {
00077         unsigned int i = 0;
00078   unsigned int count = 0;
00079 
00080         for (i=0; i<ELEMS; i+= (ELEMS_PER_PAGE)) {
00081                 printf("[%10u] = %10u ", i, array[i]);
00082     if (((count+1) % 4) == 0) {
00083       printf("\n");
00084     }
00085     count++;
00086         }
00087   printf("\n");
00088 }
00089 
00090 void
00091 do_work(unsigned int start)
00092 {
00093         unsigned int stack_array[STACK_ARRAY_ELEMS];
00094         unsigned int i = 0;
00095   unsigned int array1_start = start;
00096   unsigned int array2_start = start + (ELEMS / (ELEMS_PER_PAGE)) + 10;
00097 
00098   printf("Checking uninitialized array1\n");
00099         /* check the uninitialized array1 before initialization */
00100         for (i=0; i<ELEMS; i++) {
00101                 if (array1[i] != 0) {
00102                         printf("FAILED in file %s at line %d: array1[%d] = %u != %d\n", __FILE__, __LINE__, i, array1[i], 0);
00103                         exit(1);
00104                 }
00105         }
00106 
00107   printf("Checking uninitialized array2\n");
00108         /* check the uninitialized array2 before initialization */
00109         for (i=0; i<ELEMS; i++) {
00110                 if (array2[i] != 0) {
00111                         printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
00112                         exit(1);
00113                 }
00114         }
00115 
00116         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
00117                 stack_array[i] = i * 1000;
00118         }
00119 
00120         for (i=0; i<2; i++) {
00121           call_all();
00122           write_data(array1, array1_start);
00123           call_all();
00124     printf("Checking initialized array1\n");
00125           read_data(array1, array1_start, "array1");
00126         }
00127 
00128         /* check the uninitialized array2 again before initialization */
00129   printf("Checking initialized array2 again\n");
00130         for (i=0; i<ELEMS; i++) {
00131                 if (array2[i] != 0) {
00132                         printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
00133                         exit(1);
00134                 }
00135         }
00136 
00137   printf("Checking initialized stack_array\n");
00138         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
00139                 if (stack_array[i] != i * 1000) {
00140                         printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
00141                         exit(1);
00142                 }
00143         }
00144 
00145   printf("Checking initialized init\n");
00146         /* check the initialized array */
00147         for (i=0; i<INIT_ARRAY_ELEMS; i++) {
00148                 if (init[i] != i) {
00149                         printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
00150                         exit(1);
00151                 }
00152         }
00153 
00154         for (i=0; i<2; i++) {
00155           call_all();
00156           write_data(array2, array2_start);
00157           call_all();
00158     printf("Checking initialized array2\n");
00159           read_data(array2, array2_start, "array2");
00160         }
00161 
00162   printf("Checking initialized stack_array\n");
00163         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
00164                 if (stack_array[i] != i * 1000) {
00165                         printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
00166                         exit(1);
00167                 }
00168         }
00169 
00170         /* check the initialized array */
00171   printf("Checking initialized init\n");
00172         for (i=0; i<INIT_ARRAY_ELEMS; i++) {
00173                 if (init[i] != i) {
00174                         printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
00175                         exit(1);
00176                 }
00177         }
00178 
00179 
00180   printf("Checking initialized array1 for the last time\n");
00181         read_data(array1, array1_start, "array1");
00182   printf("Checking initialized array2 for the last time\n");
00183         read_data(array2, array2_start, "array2");
00184 
00185 
00186         printf("SUCCEEDED\n");
00187 }
00188 
00189 int
00190 main()
00191 {
00192 
00193 #ifdef DEBUG
00194   printf("PAGE_SIZE = %d\n", PAGE_SIZE);
00195   printf("DATA_BYTES = %d\n", DATA_BYTES);
00196   printf("ELEMS = %d\n", ELEMS);
00197   printf("ELEMS_PER_PAGE = %d\n", ELEMS_PER_PAGE);
00198   printf("PAGES = %d\n", PAGES);
00199   printf("Array elements = %d\n", ELEMS);
00200   printf("Pages per array = %d\n", ((ELEMS * sizeof(unsigned int)) / PAGE_SIZE));
00201 #endif
00202 
00203   do_work(1);
00204 #ifdef DEBUG
00205         printf("array 1\n");
00206         print_data(array1);
00207         printf("array 2\n");
00208         print_data(array2);
00209 #endif
00210   exit(0);
00211 }
00212 
 All Data Structures