redmine

switched from tsearch() to t_tree_replace() [with the rest]

... ... @@ -5,9 +5,9 @@ if CLSYNC
bin_PROGRAMS = clsync
clsync_SOURCES = calc.c cluster.c error.c fileutils.c glibex.c \
indexes.c main.c malloc.c rules.c searchex.c stringex.c sync.c \
port-hacks.c privileged.c pthreadex.c calc.h cluster.h \
fileutils.h glibex.h main.h port-hacks.h pthreadex.h searchex.h \
indexes.c main.c malloc.c rules.c stringex.c sync.c \
privileged.c pthreadex.c calc.h cluster.h \
fileutils.h glibex.h main.h port-hacks.h pthreadex.h \
stringex.h sync.h common.h control.h privileged.h rules.h \
syscalls.h
... ...
... ... @@ -22,34 +22,57 @@
#include "glibex.h"
struct keyvalue_copy_arg {
// GHashTable *ht_src;
GHashTable *ht_dst;
union {
GHashTable *ht_dst;
GTree *bt_dst;
};
GDupFunc k_dup_funct;
GDupFunc v_dup_funct;
};
void g_hash_table_foreach_keyvalue_copy(gpointer k, gpointer v, gpointer arg_gp) {
// GHashTable *ht_src = ((struct keyvalue_copy_arg *)arg_gp)->ht_src;
void g_hash_table_dup_item(gpointer k, gpointer v, gpointer arg_gp) {
GHashTable *ht_dst = ((struct keyvalue_copy_arg *)arg_gp)->ht_dst;
GDupFunc k_dup_funct = ((struct keyvalue_copy_arg *)arg_gp)->k_dup_funct;
GDupFunc v_dup_funct = ((struct keyvalue_copy_arg *)arg_gp)->v_dup_funct;
g_hash_table_insert(ht_dst, k_dup_funct(k), v_dup_funct(v));
g_hash_table_insert(ht_dst, k_dup_funct==NULL?NULL:k_dup_funct(k), v_dup_funct==NULL?NULL:v_dup_funct(v));
return;
}
GHashTable *g_hash_table_dup(GHashTable *ht, GHashFunc hash_funct, GEqualFunc key_equal_funct, GDestroyNotify key_destroy_funct, GDestroyNotify value_destroy_funct, GDupFunc key_dup_funct, GDupFunc value_dup_funct) {
GHashTable *ht_dup = g_hash_table_new_full(hash_funct, key_equal_funct, key_destroy_funct, value_destroy_funct);
GHashTable *g_hash_table_dup(GHashTable *src, GHashFunc hash_funct, GEqualFunc key_equal_funct, GDestroyNotify key_destroy_funct, GDestroyNotify value_destroy_funct, GDupFunc key_dup_funct, GDupFunc value_dup_funct) {
GHashTable *dst = g_hash_table_new_full(hash_funct, key_equal_funct, key_destroy_funct, value_destroy_funct);
struct keyvalue_copy_arg arg;
// arg.ht_src = ht;
arg.ht_dst = ht_dup;
arg.ht_dst = dst;
arg.k_dup_funct = key_dup_funct;
arg.v_dup_funct = value_dup_funct;
g_hash_table_foreach(ht, g_hash_table_foreach_keyvalue_copy, &arg);
g_hash_table_foreach(src, g_hash_table_dup_item, &arg);
return ht_dup;
return dst;
}
gboolean g_tree_dup_item(gpointer k, gpointer v, gpointer arg_gp) {
GTree *bt_dst = ((struct keyvalue_copy_arg *)arg_gp)->bt_dst;
GDupFunc k_dup_funct = ((struct keyvalue_copy_arg *)arg_gp)->k_dup_funct;
GDupFunc v_dup_funct = ((struct keyvalue_copy_arg *)arg_gp)->v_dup_funct;
g_tree_replace(bt_dst, k_dup_funct==NULL?NULL:k_dup_funct(k), v_dup_funct==NULL?NULL:v_dup_funct(v));
return FALSE;
}
GTree *g_tree_dup(GTree *src, GCompareDataFunc key_compare_func, gpointer key_compare_data, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func, GDupFunc key_dup_funct, GDupFunc value_dup_funct) {
GTree *dst = g_tree_new_full(key_compare_func, key_compare_data, key_destroy_func, value_destroy_func);
struct keyvalue_copy_arg arg;
arg.bt_dst = dst;
arg.k_dup_funct = key_dup_funct;
arg.v_dup_funct = value_dup_funct;
g_tree_foreach(src, g_tree_dup_item, &arg);
return dst;
}
... ...
... ... @@ -20,4 +20,5 @@
typedef gpointer(*GDupFunc)(gpointer data);
extern GHashTable *g_hash_table_dup(GHashTable *ht, GHashFunc hash_funct, GEqualFunc key_equal_funct, GDestroyNotify key_destroy_funct, GDestroyNotify value_destroy_funct, GDupFunc key_dup_funct, GDupFunc value_dup_funct);
extern GTree *g_tree_dup(GTree *src, GCompareDataFunc key_compare_func, gpointer key_compare_data, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func, GDupFunc key_dup_funct, GDupFunc value_dup_funct);
... ...
This diff is collapsed. Click to expand it.
/*
clsync - file tree sync utility based on inotify/kqueue/bsm
Copyright (C) 2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "port-hacks.h"
#include <stdlib.h>
#include "error.h"
#ifdef tdestroy_UNDEFINED
struct _tcallfunct_tree {
void *data;
struct _tcallfunct_tree *left;
struct _tcallfunct_tree *right;
};
void tcallfunct(void *root, void (*funct)(struct _tcallfunct_tree *node_p, void *arg), void *arg) {
struct _tcallfunct_tree *node_p = root;
if (node_p == NULL)
return;
tcallfunct(node_p->left , funct, arg);
tcallfunct(node_p->right, funct, arg);
funct(node_p, arg);
return;
}
void tdump_node(struct _tcallfunct_tree *node_p, void *arg) {
debug(80, "node_p == %p; node_p->left == %p; node_p->right == %p; node_p->data == %p", node_p, node_p->left, node_p->right, node_p->data);
return;
}
void _tdump(void *root) {
debug(20, "root = %p", root);
tcallfunct(root, tdump_node, NULL);
return;
}
void tdestroy_freenode(struct _tcallfunct_tree *node_p, void *_free_node_funct) {
void (*free_node_funct)(void *node_data) = _free_node_funct;
free_node_funct(node_p->data);
free(node_p);
return;
}
void tdestroy(void *root, void (*free_node)(void *node_data)) {
tcallfunct(root, tdestroy_freenode, free_node);
return;
}
#endif
... ... @@ -67,18 +67,5 @@
# define O_PATH 0
#endif
#define tdestroy_UNDEFINED
#ifdef _GNU_SOURCE
# ifndef __FreeBSD__
# undef tdestroy_UNDEFINED
# endif
#endif
#ifdef tdestroy_UNDEFINED
#define tdump(root) {if(_DEBUG_FORCE) _tdump(root);}
void _tdump(void *root);
extern void tdestroy(void *root, void (*free_node)(void *nodep));
#endif
#endif // __PORT_HACKS_H
... ...
/*
clsync - file tree sync utility based on inotify/kqueue/bsm
Copyright (C) 2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "error.h"
#include "searchex.h"
#include <search.h>
#ifdef __GNUC__
int tdup(void **to, void *from, int (*compar)(const void *, const void *)) {
debug(20, "");
int count;
if (from == NULL)
return 0;
count = 0;
void tdup_item(const void *node_p, const VISIT which, const int depth) {
debug(40, "%p, %i, %i", node_p, which, depth);
switch (which) {
case leaf:
tsearch(*(void **)node_p, to, compar);
count++;
break;
default:
critical("This code shoudn't be reached (%p, %i, %i).", node_p, which, depth);
}
}
twalk(from, tdup_item);
return count;
}
#else
int _tdup_count;
void *_tdup_to;
int (*_tdup_compar)(const void *, const void *);
void tdup_item(const void *node_p, const VISIT which, const int depth) {
debug(40, "%p, %i, %i", node_p, which, depth);
switch (which) {
case preorder:
case leaf:
tsearch(*(void **)node_p, _tdup_to, _tdup_compar);
_tdup_count++;
break;
default:
debug(25, "skipping: %p, %i, %i.", node_p, which, depth);
break;
}
return;
}
int tdup(void **to, void *from, int (*compar)(const void *, const void *)) {
int count;
#ifdef PARANOID
static int lock = 1;
if (!g_atomic_int_dec_and_test(&lock))
critical ("tdup() is not thread-safe function");
#endif
debug(20, "");
if (from == NULL) {
g_atomic_int_inc(&lock);
return 0;
}
_tdup_count = 0;
_tdup_to = to;
_tdup_compar = compar;
twalk(from, tdup_item);
count = _tdup_count;
g_atomic_int_inc(&lock);
return count;
}
#endif
/*
clsync - file tree sync utility based on inotify/kqueue/bsm
Copyright (C) 2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
extern int tdup(void **to, void *from, int (*compar)(const void *, const void *));