Skip to content

Commit 0b90340

Browse files
committed
Lift default allocation methods to static struct definition
1 parent f7a2523 commit 0b90340

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

c_src/sqlite3_nif.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ exqlite_mem_shutdown(void* ptr)
100100
{
101101
}
102102

103+
///
104+
/// Define the default allocation methods
105+
///
106+
static sqlite3_mem_methods default_alloc_methods = {
107+
exqlite_malloc,
108+
exqlite_free,
109+
exqlite_realloc,
110+
exqlite_mem_size,
111+
exqlite_mem_round_up,
112+
exqlite_mem_init,
113+
exqlite_mem_shutdown,
114+
0};
115+
103116
static const char*
104117
get_sqlite3_error_msg(int rc, sqlite3* db)
105118
{
@@ -861,17 +874,7 @@ on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
861874
{
862875
assert(env);
863876

864-
static const sqlite3_mem_methods methods = {
865-
exqlite_malloc,
866-
exqlite_free,
867-
exqlite_realloc,
868-
exqlite_mem_size,
869-
exqlite_mem_round_up,
870-
exqlite_mem_init,
871-
exqlite_mem_shutdown,
872-
0};
873-
874-
sqlite3_config(SQLITE_CONFIG_MALLOC, &methods);
877+
sqlite3_config(SQLITE_CONFIG_MALLOC, &default_alloc_methods);
875878

876879
connection_type = enif_open_resource_type(
877880
env,
@@ -898,14 +901,6 @@ on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
898901
return 0;
899902
}
900903

901-
static void
902-
on_unload(ErlNifEnv* caller_env, void* priv_data)
903-
{
904-
assert(env);
905-
906-
sqlite3_config(SQLITE_CONFIG_MALLOC, NULL);
907-
}
908-
909904
//
910905
// Enable extension loading
911906
//
@@ -965,4 +960,4 @@ static ErlNifFunc nif_funcs[] = {
965960
#define ERL_NIF_INIT_DECL(MODNAME) ErlNifEntry* sqlite3_nif_nif_init(ERL_NIF_INIT_ARGS)
966961
#endif
967962

968-
ERL_NIF_INIT(Elixir.Exqlite.Sqlite3NIF, nif_funcs, on_load, NULL, NULL, on_unload)
963+
ERL_NIF_INIT(Elixir.Exqlite.Sqlite3NIF, nif_funcs, on_load, NULL, NULL, NULL)

0 commit comments

Comments
 (0)