瀏覽代碼

fix: 修复thread的内存问题

SongZihuan 3 年之前
父節點
當前提交
375117abec
共有 5 個文件被更改,包括 23 次插入28 次删除
  1. 1 1
      include/core/code.h
  2. 2 2
      include/core/thread.h
  3. 1 7
      src/core/code.c
  4. 10 9
      src/core/thread.c
  5. 9 9
      test/src/run_code.c

+ 1 - 1
include/core/code.h

@@ -30,7 +30,7 @@ AFUN_CORE_EXPORT void freeAllCode(af_Code *bt);
 /* 代码块 相关操作 */
 AFUN_CORE_EXPORT af_Code *pushCode(af_Code **base, af_Code *next);
 AFUN_CORE_EXPORT af_Code *copyAllCode(af_Code *base, FilePath *path);
-AFUN_CORE_EXPORT af_Code *copyCode(af_Code *base, FilePath *path);
+AFUN_CORE_EXPORT af_Code *copyCode(af_Code *base);
 AFUN_CORE_EXPORT bool writeAllCode(af_Code *bt, FILE *file);
 AFUN_CORE_EXPORT bool readAllCode(af_Code **bt, FilePath path, FILE *file);
 

+ 2 - 2
include/core/thread.h

@@ -2,8 +2,8 @@
 #define AFUN_THREAD_H
 
 AFUN_CORE_EXPORT af_Environment *
-startRunThread(af_Environment *env, af_VarSpace *vs, af_Code *code, bool free_code, bool derive_tmp,
+startRunThread(af_Environment *env, af_VarSpace *vs, af_Code *code, bool not_copy_code, bool derive_tmp,
                bool derive_guardian, bool derive_lr, bool enable);
-AFUN_CORE_EXPORT void startRunThread_(af_Environment *env, af_Code *code, bool free_code);
+AFUN_CORE_EXPORT void startRunThread_(af_Environment *env, af_Code *code, bool not_copy_code);
 
 #endif //AFUN_THREAD_H

+ 1 - 7
src/core/code.c

@@ -163,7 +163,7 @@ af_Code *copyAllCode(af_Code *base, FilePath *path) {
  * 函数名: copyCode
  * 目标: 拷贝 code, 并为末尾的code设置合适的code_end
  */
-af_Code *copyCode(af_Code *base, FilePath *path) {
+af_Code *copyCode(af_Code *base){
     af_Code *dest = NULL;
     af_Code **pdest = &dest;
     af_Code *prev = NULL;
@@ -197,12 +197,6 @@ af_Code *copyCode(af_Code *base, FilePath *path) {
             layer -= base->code_end;
         }
     }
-
-    if (dest != NULL && path != NULL) {
-        free(dest->path);
-        dest->path = pathCopy(path);
-    }
-
     return dest;
 }
 

+ 10 - 9
src/core/thread.c

@@ -6,7 +6,7 @@
 struct EnvCode {
     af_Environment *env;
     af_Code *code;
-    bool free_code;
+    bool not_copy_code;
 };
 
 static void *runThread(void *ec);
@@ -18,7 +18,7 @@ static void *runThread(void *ec);
  * @param vs 压入的变量空间
  * @param code 执行的代码
  */
-af_Environment *startRunThread(af_Environment *env, af_VarSpace *vs, af_Code *code, bool free_code, bool derive_tmp,
+af_Environment *startRunThread(af_Environment *env, af_VarSpace *vs, af_Code *code, bool not_copy_code, bool derive_tmp,
                                bool derive_guardian, bool derive_lr, bool enable){
     af_Environment *base = env->base;
     af_Environment *new = deriveEnvironment(derive_tmp, derive_guardian, derive_lr, enable, base);
@@ -34,16 +34,16 @@ af_Environment *startRunThread(af_Environment *env, af_VarSpace *vs, af_Code *co
     gc_delReference(vs, base);
 
     if (enable)  // 如果未Enable, 则暂时不启动线程
-        startRunThread_(new, code, free_code);
+        startRunThread_(new, code, not_copy_code);
     return new;
 }
 
 
-void startRunThread_(af_Environment *env, af_Code *code, bool free_code){
+void startRunThread_(af_Environment *env, af_Code *code, bool not_copy_code){
     struct EnvCode *ec = calloc(1, sizeof(struct EnvCode));
     ec->env = env;
     ec->code = code;
-    ec->free_code = free_code;
+    ec->not_copy_code = not_copy_code;
 
     pthread_t id;
     pthread_create(&id, NULL, runThread, ec);
@@ -54,17 +54,18 @@ void startRunThread_(af_Environment *env, af_Code *code, bool free_code){
 static void *runThread(void *ec) {
     af_Environment *env = ((struct EnvCode *)ec)->env;
     af_Code *code = ((struct EnvCode *)ec)->code;
-    bool free_code = ((struct EnvCode *)ec)->free_code;
+    bool not_copy_code = ((struct EnvCode *)ec)->not_copy_code;
     free(ec);
 
+    if (!not_copy_code)  // “非-不要复制代码” 即 “要复制代码”
+        code = copyCode(code);
+
     writeInfoLog(aFunCoreLogger, "Thread start");
     iterCode(code, 0, env);
 
     writeInfoLog(aFunCoreLogger, "Thread free");
     freeEnvironment(env);
-
-    if (free_code)
-        freeAllCode(code);
+    freeAllCode(code);
 
     writeInfoLog(aFunCoreLogger, "Thread end");
     return NULL;

+ 9 - 9
test/src/run_code.c

@@ -1137,15 +1137,15 @@ INIT_ERROR:
         printf("popGuardian: %d\n\n", re);
     }
 
-//    {  // TODO-szh 设置 多线程 code 的释放模式
-//        printf("TAG V: [Thread]\n");
-//        af_Code *bt1 = makeElementCode("object", 0, 1, "Tagv.aun");
-//
-//        startRunThread(env, NULL, bt1, false, true, true, true, true);
-//        runCodeFromMemory(bt1, 0, env);
-//        freeAllCode(bt1);
-//        printf("\n");
-//    }
+    {
+        printf("TAG V: [Thread]\n");
+        af_Code *bt1 = makeElementCode("object", 0, 1, "Tagv.aun");
+
+        startRunThread(env, NULL, bt1, false, true, true, true, true);
+        runCodeFromMemory(bt1, 0, env);
+        freeAllCode(bt1);
+        printf("\n");
+    }
 
     // 错误用例