C/C++编程:用于进程间通信的map

1060 篇文章 297 订阅
# 项目地址

shm_container

使用

  1. 加入头文件到项目中
    在这里插入图片描述

  2. 编写makefile

include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${CMAKE_CURRENT_SOURCE_DIR}/include/shmc)

target_link_libraries(${PROJECT_NAME} rt)

测试hashmap

使用1


#include <shmc/shm_hash_map.h>


namespace {
    constexpr const char * kShmKey = "keyeddeee";
    constexpr size_t kKeyNum = 100000;
    constexpr size_t kNodeSize = 32;
    constexpr size_t kNodeNum = 2000000;
}
int main(int argc, char **argv)
{
    shmc::ShmHashMap<size_t, shmc::POSIX> hash_map_;
    shmc::POSIX alloc_;

    shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel level, const char *s){
       fprintf(stderr, "[%d] %s", level, s);
    });
    alloc_.Unlink(kShmKey + std::string("0"));
    alloc_.Unlink(kShmKey + std::string("1"));



    hash_map_.InitForWrite(kShmKey, kKeyNum, kNodeSize, kNodeNum);
    hash_map_.Insert(1000, "hello");
    std::string out;
    hash_map_.Find(1000, &out);
    assert(out == "hello");

    hash_map_.Insert(10000, "world");
    hash_map_.Find(10000, &out);
    assert(out == "world");

    hash_map_.Replace(10000, "replace");
    hash_map_.Find(10000, &out);
    assert(out == "replace");

    hash_map_.Erase(10000);
    hash_map_.Find(10000, &out);

    alloc_.Unlink(kShmKey + std::string("0"));
    alloc_.Unlink(kShmKey + std::string("1"));
}

读写


#include <shmc/shm_hash_map.h>


namespace {
    constexpr const char * kShmKey = "keyeddeee";
    constexpr size_t kKeyNum = 100000;
    constexpr size_t kNodeSize = 32;
    constexpr size_t kNodeNum = 2000000;
}
int main(int argc, char **argv)
{
    shmc::ShmHashMap<const char *, shmc::POSIX> hash_map_;
    shmc::POSIX alloc_;

    shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel level, const char *s){
       fprintf(stderr, "[%d] %s", level, s);
    });
    alloc_.Unlink(kShmKey + std::string("0"));
    alloc_.Unlink(kShmKey + std::string("1"));

    if(!hash_map_.InitForWrite(kShmKey, kKeyNum, kNodeSize, kNodeNum) ){
        printf("init failure\n");
        exit(-1);
    }

    const char * key = "6666";
    int len = 1000;
    int buf[len];
    for (int i = 0; i < len; i++) {
        buf[i] = i;
    }


    if(!hash_map_.Insert(key, buf, sizeof(buf))){
        printf("hash_map_.Insert failed\n");
        exit(-1);
    }


    std::string out;
    hash_map_.Find(key, &out);
    printf("%lu, %lu\n", sizeof(buf), out.size());
    const int* out_buf = reinterpret_cast<const int*>(out.data());
    for (int i = 0; i < len; i++) {
        if (i != out_buf[i]) {
            printf("error, %d, %d\n", i, out_buf[i]);
            exit(-1);
        }
    }

    

    printf("finally hashmap size = %zu\n", hash_map_.size());
    alloc_.Unlink(kShmKey + std::string("0"));
    alloc_.Unlink(kShmKey + std::string("1"));

}

travel


#include <shmc/shm_hash_map.h>


namespace {
    constexpr const char * kShmKey = "keyeddeee";
    constexpr size_t kKeyNum = 100000;
    constexpr size_t kNodeSize = 32;
    constexpr size_t kNodeNum = 2000000;
}
int main(int argc, char **argv)
{
    shmc::ShmHashMap<size_t , shmc::POSIX> hash_map_;
    shmc::POSIX alloc_;

    shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel level, const char *s){
       fprintf(stderr, "[%d] %s", level, s);
    });
    alloc_.Unlink(kShmKey + std::string("0"));
    alloc_.Unlink(kShmKey + std::string("1"));

    if(!hash_map_.InitForWrite(kShmKey, kKeyNum, kNodeSize, kNodeNum) ){
        printf("init failure\n");
        exit(-1);
    }

    for (size_t i = 0; i < 10; i++) {
        char buf[20];
        snprintf(buf, sizeof(buf), "%lu", i);
        if(!hash_map_.Insert(i, buf, sizeof(buf))){
            printf("hash_map_.Insert failed\n");
            exit(-1);
        }
    }

    // travel all
    size_t sum = 0;
    bool flag = hash_map_.Travel([&sum](const size_t& key, const std::string& val){
        size_t val_n = strtoul(val.c_str(), NULL, 0);
        if (val_n != key){
            printf("1. strtoul failed, %d, %d\n", val_n, key);
            exit(-1);
        }
        sum += key;
    });
    if (!flag){
        printf("1. Travel failure\n");
        exit(-1);
    }
    if(sum != 45UL){
        printf("1. sum = %zu", sum);
        exit(-1);
    }

    // travel with step 1
    sum = 0;
    typename decltype(hash_map_)::TravelPos pos;
    do{
        if (!hash_map_.Travel(&pos, 1, [&sum](const size_t& key, const std::string& val) {
            size_t val_n = strtoul(val.c_str(), NULL, 0);
            if (val_n != key){
                printf("2. strtoul failed, %d, %d\n", val_n, key);
                exit(-1);
            }
            sum += key;})){
            printf("2. Travel failure\n");
            exit(-1);
        }
    }while (!pos.at_origin());
    if(sum != 45UL){
        printf("2. sum = %zu", sum);
        exit(-1);
    }

    
    printf("finally hashmap size = %zu\n", hash_map_.size());
    alloc_.Unlink(kShmKey + std::string("0"));
    alloc_.Unlink(kShmKey + std::string("1"));

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值