Put free functions into common header / source file.

This commit is contained in:
2018-06-15 20:38:19 -07:00
parent 1158b29c1b
commit 695fbed235
6 changed files with 89 additions and 41 deletions

31
src/free_functions.c Normal file
View File

@@ -0,0 +1,31 @@
#include "free_functions.h"
#include "custom.h"
#include "function_list.h"
#include "parsetype.h"
#include "table.h"
#include "value.h"
#include <stdlib.h>
void free_function(void* func) {
libab_function_free(func);
free(func);
}
void free_function_list(void* function_list) {
libab_function_list_free(function_list);
free(function_list);
}
void free_unit(void* unit) {
}
void free_parsetype(void* parsetype) {
libab_parsetype_free(parsetype);
free(parsetype);
}
void free_table(void* table) {
libab_table_free(table);
free(table);
}
void free_value(void* value) {
libab_value_free(value);
free(value);
}

View File

@@ -1,6 +1,7 @@
#include "libabacus.h"
#include "util.h"
#include "value.h"
#include "free_functions.h"
libab_result libab_interpreter_init(libab_interpreter* intr, libab* ab) {
libab_result result;
@@ -159,11 +160,6 @@ libab_result _interpreter_compare_types(libab_ref* left_type,
return result;
}
void _free_parsetype(void* parsetype) {
libab_parsetype_free(parsetype);
free(parsetype);
}
/**
* Copies a type, substituting type parameters for their copies
* from the parameter trie.
@@ -208,9 +204,9 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type,
}
if (result == LIBAB_SUCCESS) {
result = libab_ref_new(into, copy, _free_parsetype);
result = libab_ref_new(into, copy, free_parsetype);
if (result != LIBAB_SUCCESS) {
_free_parsetype(copy);
free_parsetype(copy);
}
}
}

View File

@@ -5,29 +5,16 @@
#include "util.h"
#include "value.h"
#include <stdlib.h>
#include "free_functions.h"
void _free_function_list(void* function_list) {
libab_function_list_free(function_list);
free(function_list);
}
static libab_basetype _basetype_function_list = {_free_function_list, NULL, 0};
void _free_function(void* function) {
libab_function_free(function);
free(function);
}
static libab_basetype _basetype_function_list = {free_function_list, NULL, 0};
static libab_basetype_param _basetype_function_params[] = {{BT_LIST, NULL}};
static libab_basetype _basetype_function = {_free_function,
static libab_basetype _basetype_function = {free_function,
_basetype_function_params, 1};
void _free_unit(void* unit) {
}
static libab_basetype _basetype_unit = { _free_unit, NULL, 0 };
static libab_basetype _basetype_unit = { free_unit, NULL, 0 };
libab_result _prepare_types(libab* ab, void (*free_function)(void*));
@@ -167,7 +154,7 @@ libab_result _create_value_function_internal(libab_ref* into, libab_ref* type,
libab_ref* scope) {
libab_ref function_ref;
libab_result result =
libab_create_function_internal(&function_ref, _free_function, func, scope);
libab_create_function_internal(&function_ref, free_function, func, scope);
libab_ref_null(into);
if (result == LIBAB_SUCCESS) {
libab_ref_free(into);

View File

@@ -2,6 +2,7 @@
#include "value.h"
#include <stdarg.h>
#include <stdlib.h>
#include "free_functions.h"
libab_result libab_convert_lex_result(liblex_result to_convert) {
libab_result result = LIBAB_SUCCESS;
@@ -130,21 +131,16 @@ libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
return result;
}
void _free_table(void* data) {
libab_table_free(data);
free(data);
}
libab_result libab_create_table(libab_ref* into, libab_ref* parent) {
libab_table* table;
libab_result result = LIBAB_SUCCESS;
if ((table = malloc(sizeof(*table)))) {
libab_table_init(table);
libab_table_set_parent(table, parent);
result = libab_ref_new(into, table, _free_table);
result = libab_ref_new(into, table, free_table);
if (result != LIBAB_SUCCESS) {
_free_table(table);
free_table(table);
}
} else {
result = LIBAB_MALLOC;
@@ -156,21 +152,16 @@ libab_result libab_create_table(libab_ref* into, libab_ref* parent) {
return result;
}
void _free_value(void* value) {
libab_value_free(value);
free(value);
}
libab_result libab_create_value_ref(libab_ref* into, libab_ref* data,
libab_ref* type) {
libab_value* value;
libab_result result = LIBAB_SUCCESS;
if ((value = malloc(sizeof(*value)))) {
libab_value_init_ref(value, data, type);
result = libab_ref_new(into, value, _free_value);
result = libab_ref_new(into, value, free_value);
if (result != LIBAB_SUCCESS) {
_free_value(value);
free_value(value);
}
} else {
result = LIBAB_MALLOC;
@@ -194,7 +185,7 @@ libab_result libab_create_value_raw(libab_ref* into, void* data,
}
if (result == LIBAB_SUCCESS) {
result = libab_ref_new(into, value, _free_value);
result = libab_ref_new(into, value, free_value);
if (result != LIBAB_SUCCESS) {
libab_value_free(value);
}