Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / mcr-test / src / edu / tamu / aser / rvtest / allocationvector / AllocateAndFreeThread.java
diff --git a/JMCR-Stable/mcr-test/src/edu/tamu/aser/rvtest/allocationvector/AllocateAndFreeThread.java b/JMCR-Stable/mcr-test/src/edu/tamu/aser/rvtest/allocationvector/AllocateAndFreeThread.java
new file mode 100644 (file)
index 0000000..1abafc1
--- /dev/null
@@ -0,0 +1,85 @@
+package edu.tamu.aser.rvtest.allocationvector;\r
+\r
+/**\r
+ * class TestThread1: Used to run thread which allocates and frees blocks by\r
+ * given AllocationVector object.\r
+ */\r
+public class AllocateAndFreeThread extends Thread {\r
+    /**\r
+     * Reference to class AllocationVector object with which the thread will\r
+     * work.\r
+     */\r
+    private AllocationVector vector = null;\r
+\r
+    /**\r
+     * An array to which the resulting allocated blocks indexes will be stored.\r
+     * It's lenght indicates the number of accesses, which to perform to\r
+     * 'vector'.\r
+     */\r
+    private int[] resultBuf = null;\r
+\r
+    /**\r
+     * Whether to allocate only (0) or free only (1) or allocate and free (2).\r
+     */\r
+    private int mode;\r
+\r
+    /**\r
+     * Constructor: Constructs thread which will work on class AllocationVector\r
+     * object 'vec', to which the thread will perform 'resBuf.length' accesses\r
+     * of allocation and 'resBuf.length' accesses of frees. The resulting\r
+     * allocated blocks indexes will be stored to 'resBuf'.\r
+     * \r
+     * @param vec\r
+     *                class AllocationVector object on which the thread will\r
+     *                work. resBuf Buffer for resulting allocated blocks\r
+     *                indexes, which also indicates the number of access which\r
+     *                to perform to 'vec'. NOTE:\r
+     */\r
+    public AllocateAndFreeThread(AllocationVector vec, int[] resBuf, int mode) {\r
+        if ((vec == null) || (resBuf == null) || (mode < 0 || mode > 2)) {\r
+            throw new IllegalArgumentException();\r
+        }\r
+        vector = vec;\r
+        resultBuf = resBuf;\r
+    }\r
+\r
+    /**\r
+     * Perform 2 * 'resultBuf.length' accesses to 'vector', in the following\r
+     * way: 'resultBuf.length' blocks allocations. 'resultBuf.length' blocks\r
+     * frees. NOTE: If allocation/free block error occurs, function sets\r
+     * resultBuf[0] to -2/-3.\r
+     */\r
+    public void run() {\r
+        try {\r
+\r
+            if (mode == 0 || mode == 2) {\r
+                // Allocating 'resultBuf.length' blocks.\r
+                for (int i = 0; i < resultBuf.length; i++) {\r
+                    // TO RE-INTRODUCE BUG uncomment the following code and\r
+                    // remove\r
+                    // the single call that replaces it\r
+                     resultBuf[i] = vector.getFreeBlockIndex();\r
+                     if (resultBuf[i] != -1) {\r
+                     vector.markAsAllocatedBlock(resultBuf[i]);\r
+                     }\r
+                    //resultBuf[i] = vector.getFreeBlockIndexAndMarkAsAllocated();\r
+                }\r
+            }\r
+\r
+            if (mode == 1 || mode == 2) {\r
+                // Freeing 'resultBuf.length' blocks.\r
+                for (int i = 0; i < resultBuf.length; i++) {\r
+                    if (resultBuf[i] != -1) {\r
+                        vector.markAsFreeBlock(resultBuf[i]);\r
+                    }\r
+                }\r
+            }\r
+        } catch (Exception e) {\r
+            if (e.getMessage().compareTo("Allocation") == 0) {\r
+                resultBuf[0] = -2;\r
+            } else {\r
+                resultBuf[0] = -3;\r
+            }\r
+        }\r
+    }\r
+}\r