使用glib的哈希表行为 [英] Behaviour of an hashtable using glib

查看:230
本文介绍了使用glib的哈希表行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将卷更新为每个@IP。因此,例如在每个5秒后,我添加每个@IP(i)的V(i)。好吧现在哈希表正常工作,它在每T秒后都会更新。但问题是经过一段时间后,我发现有时候相同的IP地址会在哈希表内重复两次甚至很多次。所以当我关闭这个过程时,我发现同样的@IP重复了太多次。这就像是哈希表或类似的问题。



下面是代码这个函数update_hashTable()非常重要,每隔X秒调用一次我怀疑实际上是内存泄漏。 ..因为我总是为IP @@呼叫 malloc
,但它一直工作......任何想法???

$ p $ int update_hashTable(...){

u_int32_t * a;

... //声明

struct pf_addr * as;


as = ks-> addr [0];

a =(u_int32_t *)malloc(sizeof(u_int32_t));

* a = ntohl(as-> addr32 [0]);

sz = value; //无论如何...一个int例如

if(ReturnValue =(u_int32_t)g_hash_table_lookup(hashtable,a)){

ReturnValue + = sz;
g_hash_table_insert(hashtable,(gpointer)a,gpointer)ReturnValue);
}
else {
g_hash_table_insert(hashtable,(gpointer)a,(gpointer)sz);


解决方案

的确,内存泄漏,但这不是你的问题。问题在于if语句的真实路径简单地重新插入了与同一个键相关的第二个值,这不是您想要的。



这种典型的模式检查是否存在和增量算法通常是类似于

pre $ code> gpointer val = g_hash_table_lookup(hash_table,key);
if(val == NULL){
val = g_malloc0(...);
g_hash_table_insert(hash_table,key,val);
}
* val = / * something * /;

重要的是,一旦有一个指向与某些值相关的值的指针键,你可以直接修改它。



如果这段代码将被多个线程并行执行,那么整个块应该被一个互斥体保护,也许使用GMutex : http://developer.gnome.org/glib/2.28/glib-Threads。 html



gcc提供原子内置内在函数,比如原子地增加值,参见 http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html


I want to update the Volume to each @IP. So that for example after each 5 s I add V(i) of each @IP(i). Ok Now the hash table works fine it keeps updated after every T seconds. But the problem is that after a certain period I find that sometimes the same ip adress is repeated twice or even a lot of times within the hash table. So that when I close the process I find the same @IP repeated too many times. It is like there is a problem with the hash table or something like that.

Here is the code this funcion "update_hashTable()" is so important it is called every X seconds I suspect in fact a memory leak ... because I always call malloc for IP@. but it keeps working ... any idea ???

int update_hashTable( ... ) {

u_int32_t *a;

... //declarations

struct pf_addr *as;


as = ks->addr[0];

a = (u_int32_t*)malloc(sizeof(u_int32_t));

*a = ntohl(as->addr32[0]);

sz = value; // no matter it is... an int for example

if (ReturnValue=(u_int32_t)g_hash_table_lookup(hashtable, a)) {

  ReturnValue +=sz;
  g_hash_table_insert(hashtable, (gpointer)a, gpointer)ReturnValue);
}
else {
  g_hash_table_insert(hashtable, (gpointer)a, (gpointer)sz);
}

解决方案

Indeed, you appear to have a memory leak, but this isn't your problem. The problem is that the true-path of your if statement simply reinserts a second value associated with the same key, which is not what you want.

The typical pattern for this check-if-exists and increment algorithm is usually something like

gpointer val = g_hash_table_lookup(hash_table, key);
if (val == NULL) {
    val = g_malloc0(...);
    g_hash_table_insert(hash_table, key, val);
}
*val = /* something */;

The important thing to take away from this is that once you have a pointer to the value associated with some key, you can simply modify it directly.

If this code will be executed by multiple threads in parallel, then the entire block should be protected by a mutex, perhaps with GMutex: http://developer.gnome.org/glib/2.28/glib-Threads.html

gcc provides atomic builtin intrinsics, say for atomically incrementing the value, see http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html

这篇关于使用glib的哈希表行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆