changeset 28:04283ef8f3ca

Free memory when backed previous depth
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Tue, 26 Apr 2016 18:30:46 +0900
parents a416dd4093cf
children 073de2e0c148
files src/insert_verification/akashaCS.c src/insert_verification/akashaLLRBContext.c src/insert_verification/include/akashaLLRBContext.h src/insert_verification/main.c
diffstat 4 files changed, 76 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/insert_verification/akashaCS.c	Tue Apr 26 11:21:47 2016 +0900
+++ b/src/insert_verification/akashaCS.c	Tue Apr 26 18:30:46 2016 +0900
@@ -74,6 +74,32 @@
 /* Code Segments */
 
 __code meta(struct Context* context, enum Code next) {
+    struct Iterator* iter = &context->data[Iter]->iterator;
+
+    switch (context->prev) {
+        case GoToPreviousDepth:
+            if (iter->iteratedPointDataNum == 0) break;
+            if (iter->iteratedPointHeap == NULL) break;
+
+            unsigned int diff =(unsigned long)context->heap - (unsigned long)iter->iteratedPointHeap;
+            memset(iter->iteratedPointHeap, 0, diff);
+            context->dataNum = iter->iteratedPointDataNum;
+            context->heap    = iter->iteratedPointHeap;
+            break;
+        default:
+            break;
+    }
+    switch (next) {
+        case PutAndGoToNextDepth:
+            if (context->prev == GoToPreviousDepth) break;
+            if (iter->previousDepth == NULL)        break;
+            iter->previousDepth->iteratedPointDataNum = context->dataNum;
+            iter->previousDepth->iteratedPointHeap    = context->heap;
+            break;
+        default:
+            break;
+    }
+
     findInvalidPointer(context);
     context->prev = next;
     goto (context->code[next])(context);
--- a/src/insert_verification/akashaLLRBContext.c	Tue Apr 26 11:21:47 2016 +0900
+++ b/src/insert_verification/akashaLLRBContext.c	Tue Apr 26 18:30:46 2016 +0900
@@ -59,12 +59,11 @@
 __code initLLRBContext(struct Context* context, enum Code next) {
     context->codeNum   = Exit;
 
-    context->dataLimit = ALLOCATE_SIZE;
-    context->heapLimit = sizeof(union Data)*context->dataLimit;
+    context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
     context->code      = malloc(sizeof(__code*)*context->codeNum);
-    context->data      = malloc(sizeof(union Data*)*context->dataLimit);
+    context->data      = malloc(sizeof(union Data*)*ALLOCATE_SIZE);
     context->heapStart = malloc(context->heapLimit);
-    memset(context->heapStart, context->heapLimit, 0);
+    memset(context->heapStart, 0, context->heapLimit);
 
 
     context->code[ShowTree]              = showTree_stub;
--- a/src/insert_verification/include/akashaLLRBContext.h	Tue Apr 26 11:21:47 2016 +0900
+++ b/src/insert_verification/include/akashaLLRBContext.h	Tue Apr 26 18:30:46 2016 +0900
@@ -2,7 +2,7 @@
 #include "stack.h"
 
 #define ALLOCATE_SIZE 20000000
-#define LIMIT_OF_VERIFICATION_SIZE 8
+#define LIMIT_OF_VERIFICATION_SIZE 12
 
 enum Code {
     ShowTree,
@@ -71,7 +71,6 @@
     void* heapStart;
     void* heap;
     unsigned long heapLimit;
-    unsigned long dataLimit;
     unsigned long dataNum;
     stack_ptr code_stack;
     stack_ptr node_stack;
@@ -122,6 +121,8 @@
         struct Iterator* previousDepth;
         struct IterElem* head;
         struct IterElem* last;
-        unsigned int iteratedValue;
+        unsigned int  iteratedValue;
+        unsigned long iteratedPointDataNum;
+        void*         iteratedPointHeap;
     } iterator;
 };
--- a/src/insert_verification/main.c	Tue Apr 26 11:21:47 2016 +0900
+++ b/src/insert_verification/main.c	Tue Apr 26 18:30:46 2016 +0900
@@ -7,6 +7,45 @@
 extern void allocator(struct Context* context);
 void showTrace(struct Iterator* iter); //FIXME
 
+void* akashaMalloc(struct Context* context, size_t size) {
+    context->data[++context->dataNum] = context->heap;
+    context->heap += size;
+    return context->data[context->dataNum];
+}
+
+struct Node* nodeDeepCopy(struct Context* newContext, struct Node* oldNode) {
+    if (oldNode == NULL) return NULL;
+
+    struct Node* newNode = akashaMalloc(newContext, sizeof(struct Node));
+    newNode->next  = oldNode->next;
+    newNode->key   = oldNode->key;
+    newNode->value = oldNode->value;
+    newNode->color = oldNode->color;
+
+    if (oldNode->left != NULL)  {
+        newNode->left = nodeDeepCopy(newContext, oldNode->left);
+    } else {
+        newNode->left = NULL;
+    }
+
+    if (oldNode->right != NULL)  {
+        newNode->right = nodeDeepCopy(newContext, oldNode->right);
+    } else {
+        newNode->right = NULL;
+    }
+
+    return newNode;
+}
+
+void treeDeepCopy(struct Context* newContext, struct Tree* newTree, struct Tree* oldTree) {
+    if (oldTree == NULL) return;
+    newTree->next    = oldTree->next;
+    newTree->result  = oldTree->result;
+    newTree->root    = nodeDeepCopy(newContext, oldTree->root);
+    newTree->current = nodeDeepCopy(newContext, oldTree->current);
+    newTree->deleted = nodeDeepCopy(newContext, oldTree->deleted);
+}
+
 /* C functions (TODO: convert to code segment) */
 
 unsigned int min_height(struct Node* node, unsigned int height) {
@@ -97,6 +136,8 @@
 __code putAndGoToNextDepth(struct Context* context, struct Iterator* iter, struct Tree* tree, struct Node* node) {
     node->key           = iter->head->val;
     node->value         = iter->head->val;
+    node->left          = NULL;
+    node->right         = NULL;
     iter->head          = iter->head->next;
     iter->iteratedValue = node->value;
 
@@ -182,7 +223,8 @@
 __code duplicateTree(struct Context* context, struct Allocate* allocate, struct Tree* tree, struct Iterator* iter) {
     // Tree must be non destructive.
     // If you use destructive tree, you must copy tree.
-    iter->previousDepth->tree = tree;
+    iter->previousDepth->tree = akashaMalloc(context, sizeof(struct Tree));
+    treeDeepCopy(context, iter->previousDepth->tree, tree);
     goto meta(context, PutAndGoToNextDepth);
 }
 
@@ -195,7 +237,6 @@
         }
 
         finishedIter = finishedIter->previousDepth;
-        // TODO: cleanup memory
     }
     context->data[Iter] = (union Data*)finishedIter;
     context->data[Tree] = (union Data*)finishedIter->tree;