slightly cleaner way to test 'NUM.FIELD'
authorMrStrcmp <e0826623@student.tuwien.ac.at>
Tue, 11 May 2010 00:41:51 +0000 (02:41 +0200)
committerMrStrcmp <e0826623@student.tuwien.ac.at>
Tue, 11 May 2010 00:41:51 +0000 (02:41 +0200)
codea/lewurm_11.0
codea/lewurm_11.call

index 6cca5234da27d91be4c5d10f0bf66515e142ea08..ac14b1aa77a4326b499f52e8db27d27829519e33 100644 (file)
@@ -1,5 +1,13 @@
 struct a b c end;
-method wfoo()
-       /* passt die addresse jedes mal? */
-       return 0x600ac0.c;
+
+method try0()
+       return 0x00100000.c;
+end;
+
+method try1()
+       return 0x01000000.c;
+end;
+
+method try2()
+       return 0xA0000000.c;
 end;
index 08c981d46924916a9c37c97a42b694827fb3c42d..067edca4b263016fc46f5d568083e6a570ad07fe 100644 (file)
@@ -1,6 +1,46 @@
-long wfoo(long);
-static long s[3];
-s[0] = 5;
-s[1] = 1;
-s[2] = 1337;
-RET(wfoo(0xcafebabe) == 1337);
+#define TRIES 3
+
+/* ugly. copied from 'bits/mman.h' */
+#define PROT_READ      0x1
+#define PROT_WRITE     0x2
+#define MAP_PRIVATE    0x02
+#define MAP_FIXED      0x10
+#define MAP_ANONYMOUS  0x20
+
+void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
+
+long try0(long);
+long try1(long);
+long try2(long);
+
+/* try these adresses until one is free */
+long *tries[TRIES] = {
+       (long *)0x00100000,
+       (long *)0x01000000,
+       (long *)0xA0000000
+};
+
+int   t    = 0;
+long *addr = NULL;
+
+for (t = 0; t < TRIES; t++) {
+       addr = mmap(tries[t], 4096, PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+
+       if (tries[t] == addr)
+               break;
+}
+
+if ((void *)-1 == addr) {
+       puts("unable to map memory");
+       RET(1); /* keep the statistic clean */
+}
+
+addr[2] = 1337;
+
+switch (t) {
+case 0:        RET(try0(0xcafebabe) == 1337);
+case 1:        RET(try1(0xcafebabe) == 1337);
+case 2:        RET(try2(0xcafebabe) == 1337);
+}
+