Can I resize Linux shared memory with shmctl?

后端 未结 3 2070
情话喂你
情话喂你 2021-02-20 03:03

I have a C++ application that allocates shared memory on a Linux system via shmget(2). The data that I store in the shared memory grows periodically, and I\'d like to resize th

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-20 03:50

    It seems for me that you can write your own memory manager for your purpose. The conception is quite simple:

    1. You have a shared memory block which size is N bytes;
    2. Allocate new block of shared memory with 2*N size;
    3. Copy memory from one block to another;
    4. Free the old shared memory block;
    5. Wrap the #2-4 into some routine and use it;

    I'm afraid we have nothing more to do with that. This is how std::vector is implemented. And void *realloc() in most of cases will return you the pointer to the new block of memory (but not to the extended old block).

提交回复
热议问题