From d2320d42e103bad595d9752e774c5902dd4b0ba4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sat, 30 Nov 2019 22:12:51 +0100 Subject: [PATCH] :white_check_mark: Update slice test --- include/gwiondata.h | 2 ++ src/gwion.c | 42 ++++++++++++++++++++++---------------- src/gwiondata.c | 29 +++++++++++++++++++++----- src/lib/shred.c | 15 +++++++------- tests/import/test.log | 15 ++++++++++++++ tests/slice/slice_array.gw | 8 ++++---- 6 files changed, 77 insertions(+), 34 deletions(-) create mode 100644 tests/import/test.log diff --git a/include/gwiondata.h b/include/gwiondata.h index 155879cf..046f15b7 100644 --- a/include/gwiondata.h +++ b/include/gwiondata.h @@ -12,5 +12,7 @@ typedef struct GwionData_ { } GwionData; ANN GwionData* new_gwiondata(MemPool); +ANN GwionData* cpy_gwiondata(MemPool, const GwionData*); ANN void free_gwiondata(const struct Gwion_*); +ANN void free_gwiondata_cpy(const MemPool, GwionData*); #endif diff --git a/src/gwion.c b/src/gwion.c index bad13366..4bb4c9db 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -11,7 +11,6 @@ #include "compile.h" #include "object.h" // fork_clean #include "pass.h" // fork_clean -#include "specialid.h" // fork_clean #include "shreduler_private.h" ANN static void driver_arg(const Gwion gwion, Driver *di) { @@ -44,6 +43,14 @@ ANN static inline void gwion_compile(const Gwion gwion, const Vector v) { compile_filename(gwion, (m_str)vector_at(v, i)); } +ANN static void gwion_cleaner(const Gwion gwion) { + if(!gwion->type[et_shred]) + return; + const VM_Code code = new_vm_code(gwion->mp, NULL, 0, ae_flag_builtin, "in code dtor"); + gwion->vm->cleaner_shred = new_vm_shred(gwion->mp, code); + vm_add_shred(gwion->vm, gwion->vm->cleaner_shred); +} + ANN VM* gwion_cpy(const VM* src) { const Gwion gwion = mp_calloc(src->gwion->mp, Gwion); gwion->vm = new_vm(src->gwion->mp, 0); @@ -51,7 +58,7 @@ ANN VM* gwion_cpy(const VM* src) { gwion->vm->bbq->si = soundinfo_cpy(src->gwion->mp, src->bbq->si); gwion->emit = src->gwion->emit; gwion->env = src->gwion->env; - gwion->data = src->gwion->data; + gwion->data = cpy_gwiondata(src->gwion->mp, src->gwion->data); gwion->st = src->gwion->st; gwion->mp = src->gwion->mp; gwion->type = src->gwion->type; @@ -95,36 +102,35 @@ ANN void gwion_run(const Gwion gwion) { vm->bbq->driver->run(vm, vm->bbq); } -ANN static void fork_clean2(const Vector v) { +ANN static void gwion_end_child(const VM_Shred shred, const Gwion gwion); + +ANN static inline void free_gwion_cpy(const Gwion gwion, const VM_Shred shred) { + gwion_end_child(shred, gwion); + free_vm(gwion->vm); + free_gwiondata_cpy(gwion->mp, gwion->data); + mp_free(gwion->mp, Gwion, gwion); +} + +ANN static void fork_clean2(const VM_Shred shred, const Vector v) { for(m_uint i = 0; i < vector_size(v); ++i) { VM* vm = (VM*)vector_at(v, i); - const Gwion gwion = vm->gwion; - free_vm(vm); - mp_free(gwion->mp, Gwion, gwion); + free_gwion_cpy(vm->gwion, shred); } vector_release(v); } -ANN static void gwion_end_child(const Gwion gwion) { +ANN static void gwion_end_child(const VM_Shred shred, const Gwion gwion) { MUTEX_LOCK(gwion->vm->shreduler->mutex); if(gwion->data->child.ptr) - fork_clean(gwion->vm->cleaner_shred, &gwion->data->child); + fork_clean(shred, &gwion->data->child); if(gwion->data->child2.ptr) - fork_clean2(&gwion->data->child2); + fork_clean2(shred, &gwion->data->child2); MUTEX_UNLOCK(gwion->vm->shreduler->mutex); } -ANN static void gwion_cleaner(const Gwion gwion) { - if(!gwion->type[et_shred]) - return; - const VM_Code code = new_vm_code(gwion->mp, NULL, 0, ae_flag_builtin, "in code dtor"); - gwion->vm->cleaner_shred = new_vm_shred(gwion->mp, code); - vm_add_shred(gwion->vm, gwion->vm->cleaner_shred); -} - ANN void gwion_end(const Gwion gwion) { gwion_cleaner(gwion); - gwion_end_child(gwion); + gwion_end_child(gwion->vm->cleaner_shred, gwion); free_env(gwion->env); if(gwion->vm->cleaner_shred) free_vm_shred(gwion->vm->cleaner_shred); diff --git a/src/gwiondata.c b/src/gwiondata.c index 9e617dbb..533496a9 100644 --- a/src/gwiondata.c +++ b/src/gwiondata.c @@ -10,18 +10,38 @@ #include "specialid.h" #include "pass.h" -ANN GwionData* new_gwiondata(MemPool mp) { +ANN static inline GwionData* gwiondata(MemPool mp) { struct GwionData_ *data = mp_calloc(mp, GwionData); + MUTEX_SETUP(data->mutex); + return data; +} + +ANN GwionData* new_gwiondata(MemPool mp) { + GwionData *data = gwiondata(mp); map_init(&data->freearg); map_init(&data->id); vector_init(&data->reserved); data->passes = new_passes(mp); - MUTEX_SETUP(data->mutex); return data; } +ANN GwionData* cpy_gwiondata(MemPool mp, const GwionData* src) { + GwionData *data = gwiondata(mp); + data->freearg = src->freearg; + data->id = src->id; + data->reserved = src->reserved; + data->plug = src->plug; + data->passes = src->passes; + return data; +} + +ANN void free_gwiondata_cpy(const MemPool mp, GwionData *data) { + MUTEX_CLEANUP(data->mutex); + mp_free(mp, GwionData, data); +} + ANN void free_gwiondata(const struct Gwion_ *gwion) { - struct GwionData_ *data = gwion->data; + GwionData *data = gwion->data; map_release(&data->freearg); for(m_uint i = 0; i < map_size(&data->id); ++i) mp_free(gwion->mp, SpecialId, (struct SpecialId_*)map_at(&data->id, i)); @@ -30,6 +50,5 @@ ANN void free_gwiondata(const struct Gwion_ *gwion) { free_passes(data->passes); if(data->plug) free_plug(gwion); - MUTEX_CLEANUP(data->mutex); - mp_free(gwion->mp, GwionData, data); + free_gwiondata_cpy(gwion->mp, data); } diff --git a/src/lib/shred.c b/src/lib/shred.c index a2c23124..fd430fba 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -135,7 +135,8 @@ describe_path_and_dir(, s->info->name) describe_path_and_dir(_code, s->code->name) static DTOR(shred_dtor) { - free_vm_shred(ME(o)); + if(ME(o)) + free_vm_shred(ME(o)); } static MFUN(shred_lock) { @@ -148,9 +149,12 @@ static MFUN(shred_unlock) { static DTOR(fork_dtor) { THREAD_JOIN(FORK_THREAD(o)); + const VM *vm = FORK_ORIG(o); if(*(m_int*)(o->data + o_fork_done)) { - vector_rem2(&FORK_ORIG(o)->gwion->data->child, (vtype)o); - vector_add(&FORK_ORIG(o)->gwion->data->child2, (vtype)ME(o)->info->vm); + vector_rem2(&vm->gwion->data->child, (vtype)o); + if(!vm->gwion->data->child2.ptr) + vector_init(&vm->gwion->data->child2); + vector_add(&vm->gwion->data->child2, (vtype)ME(o)->info->vm); } } @@ -210,17 +214,14 @@ static ANN void* fork_run(void* data) { ANN void fork_launch(const VM* vm, const M_Object o, const m_uint sz) { o->ref += 1; - if(!vm->gwion->data->child.ptr) { + if(!vm->gwion->data->child.ptr) vector_init(&vm->gwion->data->child); - vector_init(&vm->gwion->data->child2); - } vector_add(&vm->gwion->data->child, (vtype)o); FORK_ORIG(o) = (VM*)vm; FORK_RETSIZE(o) = sz; THREAD_CREATE(FORK_THREAD(o), fork_run, o); } - ANN void fork_clean(const VM_Shred shred, const Vector v) { for(m_uint i = 0; i < vector_size(v); ++i) { const M_Object o = (M_Object)vector_at(v, i); diff --git a/tests/import/test.log b/tests/import/test.log new file mode 100644 index 00000000..23d7fa2b --- /dev/null +++ b/tests/import/test.log @@ -0,0 +1,15 @@ +op_already_imported.gw ==964533== Memcheck, a memory error detector +==964533== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. +==964533== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info +==964533== Command: ./../../gwion -p. -m dummy -d dummy op_already_imported.gw +==964533== Parent PID: 963027 +==964533== +==964533== +==964533== HEAP SUMMARY: +==964533== in use at exit: 0 bytes in 0 blocks +==964533== total heap usage: 869 allocs, 869 frees, 17,602,040 bytes allocated +==964533== +==964533== All heap blocks were freed -- no leaks are possible +==964533== +==964533== For lists of detected and suppressed errors, rerun with: -s +==964533== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/tests/slice/slice_array.gw b/tests/slice/slice_array.gw index d2d85867..5c07a731 100644 --- a/tests/slice/slice_array.gw +++ b/tests/slice/slice_array.gw @@ -1,4 +1,4 @@ -[1,2,3] @=> auto i; -i[1:2]; -i[:2]; -i[2: -1]; +<<< [1,2,3] @=> auto i >>>; +<<< i[1:2] >>>; +<<< i[:2] >>>; +<<< i[2: -1] >>>; -- 2.43.0