Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question About Usage #57

Open
unclepaul84 opened this issue Jan 15, 2020 · 6 comments
Open

Question About Usage #57

unclepaul84 opened this issue Jan 15, 2020 · 6 comments
Labels

Comments

@unclepaul84
Copy link

Hello,

Let me apologize in advance for dumb question.

I am trying to figure out a cross-platform way to store and access structs via memory mapped files.

I guess the question is how do I perform operations with mio on types wider than byte or char?

Thanks in Advance

struct Tick
{    
   double Price;
  int Volume;
};

   const auto path = "ticks.bin";
 

      std::error_code error;
  
      mio::mmap_sink rw_mmap = mio::make_mmap_sink(path, 0, mio::map_entire_file, error);
  
      for(Tick *t = rw_mmap->begin(); t != rw_mmap->end(); ++t)
      {
          t->Price = 5; //
      } 
@vimpunk
Copy link
Owner

vimpunk commented Jan 19, 2020

Hello @unclepaul84,

I'm thrilled that you're giving mio a go! Unfortunately serialization is not supported, mio serves as a thin wrapper around the platform memory mapping facilities, which also don't provide this feature, afaik.

This could be a feature on top of mio, a serialization framework, but I think it may be better suited as a separate library to keep separation of concerns clearer.

Happy to accept feedback, of course.

@privefl
Copy link

privefl commented Apr 7, 2020

I would need this too (reading only).
Any idea how to easily implement this on top of 'mio' for this int-double struct?

@privefl
Copy link

privefl commented Apr 7, 2020

What I have currently:

struct indval {
  int i;
  double x;
};

std::error_code error;
mio::mmap_source ro_mmap;
ro_mmap.map(path, error);
const indval * data = reinterpret_cast<const indval*>(ro_mmap.data());
cout << data->i << " // " << data->x << std::endl;

@sykhro
Copy link

sykhro commented Apr 23, 2020

@privefl what you're doing is bound to break because of padding and alignment. you can use Boost.serialization or, if you prefer something smaller, cereal

@spudwa
Copy link

spudwa commented Nov 17, 2022

I'm doing exactly what @privefl suggested and it works. When the original file is created sizeof(indval) bytes is written to disk. This will include any padding bytes added by the compiler. It's just a binary blob of bytes. As long as you use the same padding the bytes on disk and in memory are the same. This is highly non portable of course

@privefl
Copy link

privefl commented Nov 17, 2022

I've finally used two doubles, for safety.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants