Skip to content

Commit

Permalink
map: add support for persistent storage
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Mar 27, 2019
1 parent f7c7e4e commit 0e6333d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
53 changes: 25 additions & 28 deletions map.c
Expand Up @@ -28,27 +28,11 @@
#include <stdlib.h>
#include <string.h>
#include "str.h"
#include "map.h"

#include "mem/mem.h"
#include "mem/shm_mem.h"

#define avl_malloc(dest,size,flags) do \
{ \
if(flags & AVLMAP_SHARED) \
(dest) = shm_malloc(size); \
else \
(dest) = pkg_malloc(size); \
} while(0)

#define avl_free(dest,flags) do \
{ \
if(flags & AVLMAP_SHARED) \
shm_free(dest); \
else \
pkg_free(dest); \
} while(0)

#include "mem/rpm_mem.h"
#include "map.h"

#define min(a,b) ((a)<(b))?(a):(b)

Expand All @@ -75,11 +59,24 @@ static int str_cmp(str s1, str s2)
map_t map_create(enum map_flags flags)
{
map_t tree;

avl_malloc(tree, sizeof *tree, flags);

osips_malloc_f m;
osips_free_f f;

if (flags & AVLMAP_PERSISTENT) {
m = rpm_malloc_func;
f = rpm_free_func;
} else if (flags & AVLMAP_SHARED) {
m = shm_malloc_func;
f = shm_free_func;
} else {
m = pkg_malloc_func;
f = pkg_free_func;
}
tree = func_malloc(m, sizeof *tree);
if (tree == NULL)
return NULL;
tree->malloc = m;
tree->free = f;

tree->avl_root = NULL;
tree->flags = flags;
Expand Down Expand Up @@ -136,7 +133,7 @@ void ** map_get( map_t tree, str key)
y = p;
}

avl_malloc( n, sizeof *n, tree->flags );
n = func_malloc(tree->malloc, sizeof *n);

if (n == NULL)
return NULL;
Expand All @@ -147,7 +144,7 @@ void ** map_get( map_t tree, str key)

if( !( tree->flags & AVLMAP_NO_DUPLICATE ) )
{
avl_malloc(key_copy.s, key.len, tree->flags );
key_copy.s = func_malloc(tree->malloc, key.len);
if (!key_copy.s)
return NULL;

Expand Down Expand Up @@ -330,9 +327,9 @@ void * delete_node(map_t tree, struct avl_node * p)
}

if(!( tree->flags & AVLMAP_NO_DUPLICATE ) )
avl_free(p->key.s,tree->flags);
func_free(tree->free, p->key.s);

avl_free(p,tree->flags);
func_free(tree->free, p);

while (q != (struct avl_node *) & tree->avl_root) {
struct avl_node *y = q;
Expand Down Expand Up @@ -484,15 +481,15 @@ void map_destroy( map_t tree, value_destroy_func destroy)
if (destroy != NULL && p->val != NULL)
destroy(p->val);
if( !(tree->flags & AVLMAP_NO_DUPLICATE ) )
avl_free( p->key.s,tree->flags);
avl_free( p, tree->flags );
func_free(tree->free, p->key.s);
func_free(tree->free, p);
} else {
q = p->avl_link[0];
p->avl_link[0] = q->avl_link[1];
q->avl_link[1] = p;
}

avl_free( tree, tree->flags );
func_free(tree->free, tree);
}

int map_size( map_t tree )
Expand Down
9 changes: 8 additions & 1 deletion map.h
Expand Up @@ -24,6 +24,7 @@
*/

#include "str.h"
#include "mem/mem.h"
#include <stddef.h>

#ifndef AVL_H
Expand All @@ -41,7 +42,9 @@ enum map_flags
{
AVLMAP_SHARED = 1, /* determines if the map is to be allocated in
shared or private memory */
AVLMAP_NO_DUPLICATE = 2 /* determines if the map will duplicate added keys*/
AVLMAP_NO_DUPLICATE = 2, /* determines if the map will duplicate added keys*/
AVLMAP_PERSISTENT = 4, /* determines if the map will be stored in
persistent memory */

};

Expand All @@ -52,6 +55,9 @@ typedef struct avl_table {
size_t avl_count; /* Number of items in tree. */
int ret_code;

osips_malloc_f malloc;
osips_free_f free;

} *map_t;

/* Iterator data structure. */
Expand Down Expand Up @@ -98,6 +104,7 @@ typedef int (* process_each_func )(void * param, str key, void * value);
*
* AVLMAP_SHARED -> flag for shared memory
* AVLMAP_NO_DUPLICATE -> flag for key duplication
* AVLMAP_PERSISTENT -> flag for persistent shared memory
*
*/

Expand Down

0 comments on commit 0e6333d

Please sign in to comment.