changeset 23:dcfd4b848886

Support memory refresh
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Tue, 12 Apr 2016 14:07:58 +0900
parents 5b3b206edea2
children 8ca38f736745
files src/CMakeLists.txt src/insert_verification/CMakeLists.txt src/insert_verification/akashaLLRBContext.c src/insert_verification/allocate.c src/insert_verification/include/akashaLLRBContext.h
diffstat 5 files changed, 136 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Tue Mar 29 17:47:30 2016 +0900
+++ b/src/CMakeLists.txt	Tue Apr 12 14:07:58 2016 +0900
@@ -10,4 +10,3 @@
 
 include_directories(include)
 add_subdirectory(insert_verification)
-
--- a/src/insert_verification/CMakeLists.txt	Tue Mar 29 17:47:30 2016 +0900
+++ b/src/insert_verification/CMakeLists.txt	Tue Apr 12 14:07:58 2016 +0900
@@ -11,9 +11,9 @@
                main.c
                akashaCS.c
                akashaLLRBContext.c
+               allocate.c
 
                ${llrb_path}/llrb.c
-               ${llrb_path}/allocate.c
                ${llrb_path}/compare.c
                ${llrb_path}/stack.c
               )
--- a/src/insert_verification/akashaLLRBContext.c	Tue Mar 29 17:47:30 2016 +0900
+++ b/src/insert_verification/akashaLLRBContext.c	Tue Apr 12 14:07:58 2016 +0900
@@ -56,12 +56,13 @@
 __code initIteratorElem(struct Context* context, struct Allocate* allocate, struct IterElem* prev);
 
 __code initLLRBContext(struct Context* context, enum Code next) {
+    context->codeNum   = Exit;
+
     context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
-    context->code      = malloc(sizeof(__code*)*ALLOCATE_SIZE);
+    context->code      = malloc(sizeof(__code*)*context->codeNum);
     context->data      = malloc(sizeof(union Data*)*ALLOCATE_SIZE);
     context->heapStart = malloc(context->heapLimit);
 
-    context->codeNum = Exit;
 
     context->code[ShowTree]              = showTree_stub;
     context->code[IterateInsertion]      = iterateInsertion_stub;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/insert_verification/allocate.c	Tue Apr 12 14:07:58 2016 +0900
@@ -0,0 +1,130 @@
+#include "llrbContext.h"
+#include <stdio.h>
+
+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);
+}
+
+void iterElemDeepCopy(struct Context* newContext, struct Iterator* newIter, struct Iterator *oldIter) {
+    newIter->head = (struct IterElem*)akashaMalloc(newContext, sizeof(struct IterElem));
+
+    newIter->head->val = oldIter->head->val;
+    struct IterElem *oldElem = oldIter->head;
+    struct IterElem *newElem = newIter->head;
+    while (1) {
+        if (oldElem->next->val == oldIter->head->val) break;
+        newElem->next = akashaMalloc(newContext, sizeof(struct IterElem));
+
+        newElem->next->val = oldElem->next->val;
+        newElem = newElem->next;
+        oldElem = oldElem->next;
+    }
+    newElem->next = newIter->head;
+
+    while (newElem->val != oldIter->last->val) {
+        newElem = newElem->next;
+    }
+    newIter->last = newElem;
+}
+
+void iterDeepCopy(struct Context* newContext, struct Iterator* newIter, struct Iterator* oldIter) {
+    newIter->tree = akashaMalloc(newContext, sizeof(struct Tree));
+    treeDeepCopy(newContext, newIter->tree, oldIter->tree);
+
+    newIter->iteratedValue = oldIter->iteratedValue;
+
+    iterElemDeepCopy(newContext, newIter, oldIter);
+
+    if (oldIter->previousDepth != NULL) {
+        newIter->previousDepth = akashaMalloc(newContext, sizeof(struct Iterator));
+        iterDeepCopy(newContext, newIter->previousDepth, oldIter->previousDepth);
+    } else {
+        newIter->previousDepth = NULL;
+    }
+}
+
+void memoryRefresh(struct Context* oldContext) {
+    struct Context* newContext = (struct Context*)malloc(sizeof(struct Context));
+
+    newContext->heapLimit = oldContext->heapLimit*2;
+    newContext->code      = oldContext->code;
+    newContext->data      = malloc((newContext->heapLimit / sizeof(union Data)) * sizeof(union Data*));
+    newContext->heapStart = malloc(newContext->heapLimit);
+    newContext->codeNum   = Exit;
+    newContext->heap      = newContext->heapStart;
+
+    printf("reallocation! : %u\n", newContext->heapLimit);
+
+    newContext->data[Allocate] = newContext->heap;
+    newContext->heap += sizeof(struct Allocate);
+    newContext->data[Allocate]->allocate = oldContext->data[Allocate]->allocate;
+
+    newContext->data[Tree] = newContext->heap;
+    newContext->heap += sizeof(struct Tree);
+
+    newContext->data[Node] = newContext->heap;
+    newContext->heap += sizeof(struct Node);
+    newContext->data[Node]->node = oldContext->data[Node]->node;
+
+    newContext->data[Iter] = newContext->heap;
+    newContext->heap += sizeof(struct Iterator);
+
+    newContext->dataNum = Iter;
+
+    treeDeepCopy(newContext, &newContext->data[Tree]->tree,     &oldContext->data[Tree]->tree);
+    iterDeepCopy(newContext, &newContext->data[Iter]->iterator, &oldContext->data[Iter]->iterator);
+
+    newContext->node_stack = oldContext->node_stack;
+    newContext->code_stack = oldContext->code_stack;
+    newContext->next       = oldContext->next;
+    free(oldContext->data);
+    *oldContext = *newContext;
+}
+
+void allocator(struct Context* context) {
+    struct Allocate* allocate = &context->data[Allocate]->allocate;
+
+    if ((((long)context->heap - (long)context->heapStart) > (context->heapLimit - allocate->size)) ||
+          (context->dataNum >= (context->heapLimit / sizeof(union Data)))) {
+        memoryRefresh(context);
+    }
+    context->data[++context->dataNum] = context->heap;
+    context->heap += allocate->size;
+    allocate->size = 0;
+}
--- a/src/insert_verification/include/akashaLLRBContext.h	Tue Mar 29 17:47:30 2016 +0900
+++ b/src/insert_verification/include/akashaLLRBContext.h	Tue Apr 12 14:07:58 2016 +0900
@@ -1,8 +1,8 @@
 /* Context definition for llrb example */
 #include "stack.h"
 
-#define ALLOCATE_SIZE 1000000000
-#define LIMIT_OF_VERIFICATION_SIZE 8
+#define ALLOCATE_SIZE 100000
+#define LIMIT_OF_VERIFICATION_SIZE 11
 
 enum Code {
     ShowTree,