root/share/dispose.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dispose_type_specifier
  2. dispose_local_variable
  3. dvm_dispose_executable

#include "MEM.h"
#include "DBG.h"
#include "DVM_code.h"

static void dispose_local_variable(int local_variable_count,
                                   DVM_LocalVariable *local_variable_list);

static void
dispose_type_specifier(DVM_TypeSpecifier *type)
{
    int i;

    for (i = 0; i < type->derive_count; i++) {
        switch (type->derive[i].tag) {
        case DVM_FUNCTION_DERIVE:
            dispose_local_variable(type->derive[i].u
                                   .function_d.parameter_count,
                                   type->derive[i].u
                                   .function_d.parameter);
            break;
        default:
            DBG_assert(0, ("derive->tag..%d\n", type->derive[i].tag));
        }
    }
    MEM_free(type->derive);
    MEM_free(type);
}

static void
dispose_local_variable(int local_variable_count,
                       DVM_LocalVariable *local_variable)
{
    int i;

    for (i = 0; i < local_variable_count; i++) {
        MEM_free(local_variable[i].name);
        dispose_type_specifier(local_variable[i].type);
    }
    MEM_free(local_variable);
}

void
dvm_dispose_executable(DVM_Executable *exe)
{
    int i;

    for (i = 0; i < exe->constant_pool_count; i++) {
        if (exe->constant_pool[i].tag == DVM_CONSTANT_STRING) {
            MEM_free(exe->constant_pool[i].u.c_string);
        }
    }
    MEM_free(exe->constant_pool);
    
    for (i = 0; i < exe->global_variable_count; i++) {
        MEM_free(exe->global_variable[i].name);
        dispose_type_specifier(exe->global_variable[i].type);
    }
    MEM_free(exe->global_variable);

    for (i = 0; i < exe->function_count; i++) {
        dispose_type_specifier(exe->function[i].type);
        MEM_free(exe->function[i].name);
        dispose_local_variable(exe->function[i].parameter_count,
                               exe->function[i].parameter);
        if (exe->function[i].is_implemented) {
            dispose_local_variable(exe->function[i].local_variable_count,
                                   exe->function[i].local_variable);
            MEM_free(exe->function[i].code);
        }
    }
    MEM_free(exe->function);
    MEM_free(exe->code);
    MEM_free(exe);
}

/* [<][>][^][v][top][bottom][index][help] */