Skip to content

Commit

Permalink
rework pr to not break existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h committed Dec 9, 2024
1 parent 9bef995 commit 736d2e8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
33 changes: 23 additions & 10 deletions src/dice/ffi/metall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,15 @@ metall_manager *open_impl(char const *path) {
return reinterpret_cast<metall_manager *>(manager);
}

metall_manager *metall_open(char const *path) {
return open_impl<metall::open_only>(path);
}

metall_manager *metall_open_read_only(char const *path) {
return open_impl<metall::open_read_only>(path);
}

metall_manager *metall_create(char const *path, size_t capacity) {
template<typename Create>
metall_manager *create_impl(char const *path, Create &&create) {
if (std::filesystem::exists(path)) {
// prevent accidental overwrite
errno = EEXIST;
return nullptr;
}

auto *manager = new metall_manager_t{metall::create_only, path, capacity};
auto *manager = std::invoke(create, path);
if (!manager->check_sanity()) {
delete manager;
errno = ENOTRECOVERABLE;
Expand All @@ -47,6 +40,26 @@ metall_manager *metall_create(char const *path, size_t capacity) {
return reinterpret_cast<metall_manager *>(manager);
}

metall_manager *metall_open(char const *path) {
return open_impl<metall::open_only>(path);
}

metall_manager *metall_open_read_only(char const *path) {
return open_impl<metall::open_read_only>(path);
}

metall_manager *metall_create(char const *path) {
return create_impl(path, [](char const *path) {
return new metall_manager_t{metall::create_only, path};
});
}

metall_manager *metall_create_with_capacity_limit(char const *path, size_t capacity) {
return create_impl(path, [capacity](char const *path) {
return new metall_manager_t{metall::create_only, path, capacity};
});
}

bool metall_is_read_only(metall_manager const *manager) {
return reinterpret_cast<metall_manager_t const *>(manager)->read_only();
}
Expand Down
16 changes: 13 additions & 3 deletions src/dice/ffi/metall.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include <stdbool.h>
#include <stddef.h>
#include <metall/defs.hpp>

#include <metall/logger_interface.h>

#if __has_include(<metall/logger_interface.h>)
#include <metall/logger_interface.h>
Expand Down Expand Up @@ -53,12 +54,21 @@ metall_manager *metall_open_read_only(char const *path);
/**
* @brief Attempts to create a metall datastore at path
* @param path path at which to create a datastore
* @param capacity maximum capacity for metall manager, for default value use METALL_DEFAULT_CAPACITY
* @return true on success, false on failure. On failure, sets errno to one of the following values:
* - EEXIST if the given path already exists
* - ENOTRECOVERABLE if the datastore could not be created for some other reason
*/
metall_manager *metall_create(char const *path, size_t capacity);
metall_manager *metall_create(char const *path);

/**
* @brief Attempts to create a metall datastore at path
* @param path path at which to create a datastore
* @param capacity maximum capacity for metall manager
* @return true on success, false on failure. On failure, sets errno to one of the following values:
* - EEXIST if the given path already exists
* - ENOTRECOVERABLE if the datastore could not be created for some other reason
*/
metall_manager *metall_create_with_capacity_limit(char const *path, size_t capacity);

/**
* @brief Returns true if the metall manager was opened as read-only
Expand Down
18 changes: 16 additions & 2 deletions tests/tests_Sanity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST_SUITE("metall-ffi") {
std::string const snap_path = path + "-snap";

{
metall_manager *manager = metall_create(path.c_str(), METALL_DEFAULT_CAPACITY);
metall_manager *manager = metall_create(path.c_str());
if (manager == nullptr) {
FAIL("failed to create: ", strerror(errno));
}
Expand Down Expand Up @@ -98,7 +98,7 @@ TEST_SUITE("metall-ffi") {

TEST_CASE("prevent open same datastore twice") {
std::string const path = "/tmp/" + std::to_string(std::random_device{}());
metall_manager *manager = metall_create(path.c_str(), METALL_DEFAULT_CAPACITY);
metall_manager *manager = metall_create(path.c_str());
if (manager == nullptr) {
FAIL("failed to create datastore: ", strerror(errno));
}
Expand All @@ -114,4 +114,18 @@ TEST_SUITE("metall-ffi") {
metall_close(manager);
CHECK(metall_remove(path.c_str()));
}

TEST_CASE("capacity limit") {
std::string const path = "/tmp/" + std::to_string(std::random_device{}());
metall_manager *manager = metall_create_with_capacity_limit(path.c_str(), 1);
if (manager == nullptr) {
FAIL("failed to create datastore: ", strerror(errno));
}

auto ret = metall_malloc(manager, "aaa", 1'000'000'000);
if (ret != nullptr) {
metall_free(manager, "aaa");
FAIL("Did not refuse to allocate");
}
}
}

0 comments on commit 736d2e8

Please sign in to comment.