Put free functions into common header / source file.

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

View File

@ -8,7 +8,7 @@ project(libabacus)
add_compile_options(-pedantic -Wall) add_compile_options(-pedantic -Wall)
add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c src/ref_vec.c src/ref_trie.c src/basetype.c src/value.c src/custom.c src/interpreter.c src/function_list.c) add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c src/ref_vec.c src/ref_trie.c src/basetype.c src/value.c src/custom.c src/interpreter.c src/function_list.c src/free_functions.c)
add_executable(libabacus src/main.c) add_executable(libabacus src/main.c)
add_executable(interactive src/interactive.c) add_executable(interactive src/interactive.c)
add_subdirectory(external/liblex) add_subdirectory(external/liblex)

43
include/free_functions.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef LIBABACUS_FREE_FUNCTIONS_H
#define LIBABACUS_FREE_FUNCTIONS_H
/**
* Free functions. Because a lot of the reference
* counting operations require free functions,
* and redeclaring them in mutliple files makes no
* sense (also, it doesn't link :^) ), we
* put them all here.
*/
/**
* Frees a libab_function.
* @param func the function to free.
*/
void free_function(void* func);
/**
* Frees a libab_function_list.
* @param func_list the function list to free.
*/
void free_function_list(void* func_list);
/**
* Frees a unit. This is a no-op.
* @param unit the unit to free.
*/
void free_unit(void* unit);
/**
* Frees a parsetype.
* @param parsetype the parsetype to free.
*/
void free_parsetype(void* parsetype);
/**
* Frees a table.
* @param table the table to free.
*/
void free_table(void* table);
/**
* Frees a value.
* @param value the value to free.
*/
void free_value(void* value);
#endif

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

View File

@ -5,29 +5,16 @@
#include "util.h" #include "util.h"
#include "value.h" #include "value.h"
#include <stdlib.h> #include <stdlib.h>
#include "free_functions.h"
void _free_function_list(void* function_list) { static libab_basetype _basetype_function_list = {free_function_list, NULL, 0};
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_param _basetype_function_params[] = {{BT_LIST, NULL}}; 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}; _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*)); 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* scope) {
libab_ref function_ref; libab_ref function_ref;
libab_result result = 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); libab_ref_null(into);
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
libab_ref_free(into); libab_ref_free(into);

View File

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