Skip to content

Commit

Permalink
Changes for map (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe authored Oct 24, 2023
1 parent 06ce01c commit a43566d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
17 changes: 14 additions & 3 deletions include/fuse/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@
typedef struct fuse_map_instance fuse_map_t;

/** @brief Create a new empty map
*
* @param fuse The fuse application
* @param size The initial size of the map
* @param hashfunc The hash function to use for the map. If NULL, then the default hash function is used
* @param file The file from which the map is allocated
* @param line The line in the file from which the map is allocated
* @returns A new empty map, or NULL if memory could not be allocated
*/
fuse_map_t *fuse_map_new_ex(fuse_t *fuse, size_t size, const char *file, int line);
fuse_map_t *fuse_map_new_ex(fuse_t *fuse, size_t size, size_t (*hashfunc)(void *), const char *file, int line);

#ifdef DEBUG
#define fuse_map_new(self, size) \
(fuse_map_new_ex((self), (size), __FILE__, __LINE__))
(fuse_map_new_ex((self), (size), NULL, __FILE__, __LINE__))
#else

/** @brief Create a new empty map
*
* @param fuse The fuse application
* @param size The initial size of the map
* @returns A new empty map, or NULL if memory could not be allocated
*/
#define fuse_map_new(self, size) \
(fuse_map_new_ex((self), (size), 0, 0))
(fuse_map_new_ex((self), (size), NULL, 0, 0))
#endif

/** @brief Deallocate a map
Expand Down
4 changes: 2 additions & 2 deletions src/fuse/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS

fuse_map_t *fuse_map_new_ex(fuse_t *fuse, size_t size, const char *file, int line)
fuse_map_t *fuse_map_new_ex(fuse_t *fuse, size_t size, size_t (*hashfunc)(void *), const char *file, int line)
{
assert(fuse);
assert(size > 0);
Expand All @@ -25,7 +25,7 @@ fuse_map_t *fuse_map_new_ex(fuse_t *fuse, size_t size, const char *file, int lin
instance->count = 0;
instance->size = size;
instance->nodes = (struct fuse_map_node *)(instance + 1);
instance->hashfunc = NULL;
instance->hashfunc = hashfunc;

// Return success
return instance;
Expand Down
4 changes: 2 additions & 2 deletions src/fuse/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define FUSE_PRIVATE_MAP_H
#include <fuse/fuse.h>

/* @brief Represents an instance of a map
/** @brief Represents an instance of a map
*/
struct fuse_map_instance
{
Expand All @@ -15,7 +15,7 @@ struct fuse_map_instance
size_t (*hashfunc)(void *); ///< The hash function for the map
};

/* @brief Represents a node in a map
/** @brief Represents a node in a map
*/
struct fuse_map_node
{
Expand Down
33 changes: 17 additions & 16 deletions tests/map/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@ int TEST_001()
return fuse_destroy(fuse);
}

int TEST_002()
int TEST_002(uint64_t sz)
{
fuse_debugf(NULL, "Creating a fuse application\n");
fuse_t *fuse = fuse_new(FUSE_FLAG_DEBUG);
assert(fuse);

// Create a map
fuse_debugf(NULL, "Creating a map of 10 items\n");
fuse_map_t *map = fuse_map_new(fuse, 10);
fuse_debugf(NULL, "Creating a map of %lu items\n",sz);
fuse_map_t *map = fuse_map_new(fuse, sz);
assert(map);

// Add 10 items
for (int i = 1; i <= 10; i++)
// Add items
for (uint64_t i = 1; i <= sz; i++)
{
fuse_debugf(NULL, "Adding item %d=> %d\n", i, i);
fuse_debugf(NULL, "Adding item %lu=> %lu\n", i, i);
assert(fuse_map_set(map, (void *)i, (void *)i));
assert(fuse_map_stats(map, NULL) == i);
}

// Get 10 items
for (int i = 1; i <= 10; i++)
// Get items
for (uint64_t i = 1; i <= sz; i++)
{
void* j = fuse_map_get(map, (void *)i);
void *j = fuse_map_get(map, (void *)i);
assert(j == (void *)i);
fuse_debugf(NULL, "Getting item %d => %d\n", i, j);
fuse_debugf(NULL, "Getting item %lu => %lu\n", i, j);
}

// Deleting 10 items
for (int i = 1; i <= 10; i++)
// Deleting items
for (uint64_t i = 1; i <= sz; i++)
{
assert(fuse_map_set(map, (void *)i, 0));
void* j = fuse_map_get(map, (void *)i);
fuse_debugf(NULL, "Deleting item %d => %d (should be zero)\n", i, j);
void *j = fuse_map_get(map, (void *)i);
fuse_debugf(NULL, "Deleting item %lu => %lu (should be zero)\n", i, j);
assert(j == 0);
assert(fuse_map_stats(map, NULL) == 10 - i);
assert(fuse_map_stats(map, NULL) == sz - i);
}

// Destroy the map
Expand All @@ -75,7 +75,8 @@ int TEST_002()
int main()
{
assert(TEST_001() == 0);
assert(TEST_002() == 0);
assert(TEST_002(10) == 0);
assert(TEST_002(100) == 0);

// Return success
return 0;
Expand Down

0 comments on commit a43566d

Please sign in to comment.