struct来存储/ proc / <pid> / maps </pid>中的记录

时间:2012-12-08 13:28:10

标签: c linux

我使用下一个结构来存储来自/ proc // maps的信息:

typedef struct
{
    unsigned long vm_start;
    unsigned long vm_end;
    unsigned long vm_flags;
    unsigned long vm_pgoff;
    dev_t vm_dev;
    unsigned long vm_ino;
    unsigned long vm_size;
    int vm_shares;
} map_t;

一个向量:

typedef struct
{
    map_t *arr;
    int size;
    int reserved;
} map_vector_t;

当我从文件中读取记录时,我找到了一个共享的等效区域:

int index = mem_mapping_find_shared(vector, map);
if (index != -1) {
    vector->arr[index].vm_shares++;
    continue;
}

map.vm_shares = 0;
int mem_mapping_find_shared(map_vector_t *vector, map_t map)
{
    int index = -1;

    for (int i = 0; index == -1 && i < vector->size; i++) {
        if (mem_mapping_is_shareable(vector->arr[i], map)) {
            index = i;
        }
    }

    return index;
}

但代码太慢了。我想我应该使用哈希表,对吗?我可以使用哪种哈希函数?

1 个答案:

答案 0 :(得分:0)

也许对某人有用:

unsigned long long mem_mapping_hash(const map_t *map)
{
    unsigned long long h = map->vm_pgoff;
    h = h * PRIME + map->vm_dev;
    h = h * PRIME + map->vm_ino;
    h = h * PRIME + map->vm_size;

    return h % PRIME;
}