Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move to system malloc to avoid overhead

  • Loading branch information...
commit 7ee6a25377a9fab1b8b67b7c125de49a689eb833 1 parent f20db99
@evanmcc evanmcc authored
Showing with 31 additions and 33 deletions.
  1. +31 −33 c_src/bitcask_nifs.c
View
64 c_src/bitcask_nifs.c
@@ -266,13 +266,13 @@ ERL_NIF_TERM bitcask_nifs_keydir_new0(ErlNifEnv* env, int argc, const ERL_NIF_TE
{
// First, setup a resource for our handle
bitcask_keydir_handle* handle = enif_alloc_resource_compat(env,
- bitcask_keydir_RESOURCE,
- sizeof(bitcask_keydir_handle));
+ bitcask_keydir_RESOURCE,
+ sizeof(bitcask_keydir_handle));
memset(handle, '\0', sizeof(bitcask_keydir_handle));
// Now allocate the actual keydir instance. Because it's unnamed/shared, we'll
// leave the name and lock portions null'd out
- bitcask_keydir* keydir = enif_alloc_compat(env, sizeof(bitcask_keydir));
+ bitcask_keydir* keydir = malloc(sizeof(bitcask_keydir));
memset(keydir, '\0', sizeof(bitcask_keydir));
keydir->entries = kh_init(entries);
keydir->fstats = kh_init(fstats);
@@ -319,7 +319,7 @@ ERL_NIF_TERM bitcask_nifs_keydir_new1(ErlNifEnv* env, int argc, const ERL_NIF_TE
{
// No such keydir, create a new one and add to the globals list. Make sure
// to allocate enough room for the name.
- keydir = enif_alloc_compat(env, sizeof(bitcask_keydir) + name_sz + 1);
+ keydir = malloc(sizeof(bitcask_keydir) + name_sz + 1);
memset(keydir, '\0', sizeof(bitcask_keydir) + name_sz + 1);
strncpy(keydir->name, name, name_sz + 1);
@@ -339,8 +339,8 @@ ERL_NIF_TERM bitcask_nifs_keydir_new1(ErlNifEnv* env, int argc, const ERL_NIF_TE
// Setup a resource for the handle
bitcask_keydir_handle* handle = enif_alloc_resource_compat(env,
- bitcask_keydir_RESOURCE,
- sizeof(bitcask_keydir_handle));
+ bitcask_keydir_RESOURCE,
+ sizeof(bitcask_keydir_handle));
memset(handle, '\0', sizeof(bitcask_keydir_handle));
handle->keydir = keydir;
ERL_NIF_TERM result = enif_make_resource(env, handle);
@@ -385,7 +385,7 @@ static void update_fstats(ErlNifEnv* env, bitcask_keydir* keydir,
if (itr == kh_end(keydir->fstats))
{
// Need to initialize new entry and add to the table
- entry = enif_alloc_compat(env, sizeof(bitcask_fstats_entry));
+ entry = malloc(sizeof(bitcask_fstats_entry));
memset(entry, '\0', sizeof(bitcask_fstats_entry));
entry->file_id = file_id;
@@ -445,12 +445,12 @@ static khiter_t get_entries_hash(ErlNifEnv* env, entries_hash_t *hash, ErlNifBin
}
else
{
- bitcask_keydir_entry* e = enif_alloc_compat(env, sizeof(bitcask_keydir_entry) +
+ bitcask_keydir_entry* e = malloc(sizeof(bitcask_keydir_entry) +
key->size);
e->key_sz = key->size;
memcpy(e->key, key->data, key->size);
itr = kh_get(entries, hash, e);
- enif_free_compat(env, e);
+ free(e);
}
if (itr != kh_end(hash))
@@ -508,9 +508,8 @@ static bitcask_keydir_entry* add_entry(ErlNifEnv* env, bitcask_keydir* keydir,
entries_hash_t* hash,
ErlNifBinary* key, bitcask_keydir_entry* entry)
{
- bitcask_keydir_entry* new_entry = enif_alloc_compat(env,
- sizeof(bitcask_keydir_entry) +
- key->size);
+ bitcask_keydir_entry* new_entry = malloc(sizeof(bitcask_keydir_entry) +
+ key->size);
new_entry->file_id = entry->file_id;
new_entry->total_sz = entry->total_sz;
new_entry->offset = entry->offset;
@@ -813,7 +812,7 @@ ERL_NIF_TERM bitcask_nifs_keydir_remove(ErlNifEnv* env, int argc, const ERL_NIF_
if (keydir->pending == NULL)
{
remove_entry(env, keydir, itr, entry);
- enif_free_compat(env, entry);
+ free(entry);
}
// If found an entry in the pending hash, convert it to a tombstone
else if (keydir->pending == hash)
@@ -858,13 +857,13 @@ ERL_NIF_TERM bitcask_nifs_keydir_copy(ErlNifEnv* env, int argc, const ERL_NIF_TE
LOCK(keydir);
bitcask_keydir_handle* new_handle = enif_alloc_resource_compat(env,
- bitcask_keydir_RESOURCE,
- sizeof(bitcask_keydir_handle));
+ bitcask_keydir_RESOURCE,
+ sizeof(bitcask_keydir_handle));
memset(handle, '\0', sizeof(bitcask_keydir_handle));
// Now allocate the actual keydir instance. Because it's unnamed/shared, we'll
// leave the name and lock portions null'd out
- bitcask_keydir* new_keydir = enif_alloc_compat(env, sizeof(bitcask_keydir));
+ bitcask_keydir* new_keydir = malloc(sizeof(bitcask_keydir));
new_handle->keydir = new_keydir;
memset(new_keydir, '\0', sizeof(bitcask_keydir));
new_keydir->entries = kh_init(entries);
@@ -880,7 +879,7 @@ ERL_NIF_TERM bitcask_nifs_keydir_copy(ErlNifEnv* env, int argc, const ERL_NIF_TE
{
bitcask_keydir_entry* curr = kh_key(keydir->entries, itr);
size_t new_sz = sizeof(bitcask_keydir_entry) + curr->key_sz;
- bitcask_keydir_entry* new = enif_alloc_compat(env, new_sz);
+ bitcask_keydir_entry* new = malloc(new_sz);
memcpy(new, curr, new_sz);
kh_put_set(entries, new_keydir->entries, new);
}
@@ -895,7 +894,7 @@ ERL_NIF_TERM bitcask_nifs_keydir_copy(ErlNifEnv* env, int argc, const ERL_NIF_TE
{
bitcask_keydir_entry* curr = kh_key(keydir->pending, itr);
size_t new_sz = sizeof(bitcask_keydir_entry) + curr->key_sz;
- bitcask_keydir_entry* new = enif_alloc_compat(env, new_sz);
+ bitcask_keydir_entry* new = malloc(new_sz);
memcpy(new, curr, new_sz);
kh_put_set(entries, new_keydir->pending, new);
}
@@ -908,8 +907,7 @@ ERL_NIF_TERM bitcask_nifs_keydir_copy(ErlNifEnv* env, int argc, const ERL_NIF_TE
if (kh_exist(keydir->fstats, itr))
{
bitcask_fstats_entry* curr_f = kh_val(keydir->fstats, itr);
- bitcask_fstats_entry* new_f = enif_alloc_compat(env,
- sizeof(bitcask_fstats_entry));
+ bitcask_fstats_entry* new_f = malloc(sizeof(bitcask_fstats_entry));
memcpy(new_f, curr_f, sizeof(bitcask_fstats_entry));
kh_put2(fstats, new_keydir->fstats, new_f->file_id, new_f);
}
@@ -1009,11 +1007,11 @@ ERL_NIF_TERM bitcask_nifs_keydir_itr(ErlNifEnv* env, int argc, const ERL_NIF_TER
size_t size = keydir->pending_awaken_size * sizeof(keydir->pending_awaken[0]);
if (keydir->pending_awaken == NULL)
{
- keydir->pending_awaken = enif_alloc_compat(env, size);
+ keydir->pending_awaken = malloc(size);
}
else
{
- keydir->pending_awaken = enif_realloc_compat(env, keydir->pending_awaken, size);
+ keydir->pending_awaken = realloc(keydir->pending_awaken, size);
}
}
enif_self(env, &keydir->pending_awaken[keydir->pending_awaken_count]);
@@ -1214,8 +1212,8 @@ ERL_NIF_TERM bitcask_nifs_lock_acquire(ErlNifEnv* env, int argc, const ERL_NIF_T
// Successfully opened the file -- setup a resource to track the FD.
unsigned int filename_sz = strlen(filename) + 1;
bitcask_lock_handle* handle = enif_alloc_resource_compat(env, bitcask_lock_RESOURCE,
- sizeof(bitcask_lock_handle) +
- filename_sz);
+ sizeof(bitcask_lock_handle) +
+ filename_sz);
handle->fd = fd;
handle->is_write_lock = is_write_lock;
strncpy(handle->filename, filename, filename_sz);
@@ -1717,7 +1715,7 @@ static void merge_pending_entries(ErlNifEnv* env, bitcask_keydir* keydir)
/* nop - stats were not updated when tombstone written for
** empty entry
*/
- enif_free_compat(env, pending_entry);
+ free(pending_entry);
}
/* entries: empty, pending:value */
else
@@ -1740,14 +1738,14 @@ static void merge_pending_entries(ErlNifEnv* env, bitcask_keydir* keydir)
if (is_pending_tombstone(pending_entry))
{
remove_entry(env, keydir, ent_itr, entries_entry);
- enif_free_compat(env, entries_entry);
+ free(entries_entry);
}
/* entries: present, pending:value */
else
{
update_entry(env, keydir, entries_entry, pending_entry);
}
- enif_free_compat(env, pending_entry);
+ free(pending_entry);
}
}
}
@@ -1763,7 +1761,7 @@ static void merge_pending_entries(ErlNifEnv* env, bitcask_keydir* keydir)
keydir->pending_start = 0;
if (keydir->pending_awaken != NULL)
{
- enif_free_compat(env, keydir->pending_awaken);
+ free(keydir->pending_awaken);
}
keydir->pending_awaken = NULL;
keydir->pending_awaken_count = 0;
@@ -1792,7 +1790,7 @@ static void lock_release(bitcask_lock_handle* handle)
}
}
-static void free_keydir(ErlNifEnv* env, bitcask_keydir* keydir)
+static void free_keydir(bitcask_keydir* keydir)
{
// Delete all the entries in the hash table, which also has the effect of
// freeing up all resources associated with the table.
@@ -1803,7 +1801,7 @@ static void free_keydir(ErlNifEnv* env, bitcask_keydir* keydir)
if (kh_exist(keydir->entries, itr))
{
current_entry = kh_key(keydir->entries, itr);
- enif_free_compat(env, current_entry);
+ free(current_entry);
}
}
@@ -1816,7 +1814,7 @@ static void free_keydir(ErlNifEnv* env, bitcask_keydir* keydir)
if (kh_exist(keydir->fstats, itr))
{
curr_f = kh_val(keydir->fstats, itr);
- enif_free_compat(env, curr_f);
+ free(curr_f);
}
}
@@ -1878,7 +1876,7 @@ static void bitcask_nifs_keydir_resource_cleanup(ErlNifEnv* env, void* arg)
enif_mutex_destroy(keydir->mutex);
}
- free_keydir(env, keydir);
+ free_keydir(keydir);
}
}
@@ -1937,7 +1935,7 @@ static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
0);
// Initialize shared keydir hashtable
- bitcask_priv_data* priv = enif_alloc_compat(env, sizeof(bitcask_priv_data));
+ bitcask_priv_data* priv = malloc(sizeof(bitcask_priv_data));
priv->global_keydirs = kh_init(global_keydirs);
priv->global_keydirs_lock = enif_mutex_create("bitcask_global_handles_lock");
*priv_data = priv;
Please sign in to comment.
Something went wrong with that request. Please try again.