#include <sys/mman.h>
#include <fcntl.h>
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>

const char *path="/squid-testRock_Store_i.sho";

using namespace std;
int main(int argc, char ** argv) {
    if (argc < 2) {
        cout << "Need size arg\n";
        throw("Need size arg");
    }
    cout  << "start" << endl;
    int fd = shm_open(path, O_RDWR|O_CREAT,
        S_IRUSR|S_IWUSR);
    cout << "fd: " << fd << endl;
    if (fd < 0) {
        cout << "shm_open error: " << strerror(errno) << endl;
        throw("no go");
    }
    int sz = stoi(argv[1]);
    cout << "sz: " << sz << endl;
    int rv = ftruncate(fd, sz);
    if ( rv ) {
        cout << "error in ftruncate: " << strerror(errno) << endl;
        throw("ftruncate");
    }
    cout << "mmap\n";
    void * map = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    cout << "mem at " << map << endl;
    shm_unlink(path);
    cout << "ok!" << endl;
    return 0;
}
