0

Goal : Have a buffer (fixed size) of memory in which I can store any type of structs. Initialized to 0s

Question : How would it work?

Here is what I was thinking up to now.

void Main()
{
   void* BufferHead = CreatBuffer(1024)
}

void* CreateBuffer(int ByteCount)
{
     void* Head = operator new (std::size_t(ByteCount));
     memset(Head, 0, ByteCount);
     return Head;
}

void* AddAt(int ByteIdx, void* DataToAdd, size_t DataSize )
{        
   return memcpy(BufferHead + ByteIdx, DataToAdd, DataSize);
}

void RemoveAt(int ByteIdx, size_t DataSize)
{
   memset(BufferHead+ByteIdx, 0, DataSize);
}

I did't do any of the "memory fragmentation/management" nor the smart pointer to reset ptr to null. I'd like to start with a solid add add/remove/alloc interface.

EDIT:

Intend purpose of this question is to allow me to store various data types (struct, basic types, objects) of different size in a sequential way so that I can read it forward and backward in a cache friendly way as it will be "processed" between 30 and 60 times a second. I was thinking of iterating through the buffer with a Double Linked List of Node and simply put all the data where it fits at any time in the buffer.

I can use up to c++ 14 features.

Fawar
  • 735
  • 2
  • 11
  • 32
  • 8
    What's wrong with [`std::vector`](http://en.cppreference.com/w/cpp/container/vector)? Or any other [standard container](http://en.cppreference.com/w/cpp/container)? – Some programmer dude Dec 08 '17 at 13:15
  • 2
    ....or with `std::any`, isnt that what you are looking for? – 463035818_is_not_an_ai Dec 08 '17 at 13:15
  • 1
    You can only memcpy PODs into such a buffer. –  Dec 08 '17 at 13:15
  • 1
    For non-POD types you can implement a templated `AddAt` that performs [placement new](http://www.devx.com/tips/Tip/12582) – rustyx Dec 08 '17 at 13:17
  • @Someprogrammerdude I want it to be fixed size, not dynamic resizing? – Fawar Dec 08 '17 at 13:21
  • @tobi303 What would std::any do for me? – Fawar Dec 08 '17 at 13:21
  • @RustyX what's POD types? – Fawar Dec 08 '17 at 13:21
  • 2
    @manni66 That's not true. You can do it with all TriviallyCopyable types, not only PODs. (§ 6.9 [basic.types]) – Jodocus Dec 08 '17 at 13:21
  • BTW I dont think the question is bad (downvotes), maybe the idea is but the question is totally legit... – Fawar Dec 08 '17 at 13:22
  • 2
    You are asking how to solve a problem that has been solved in the 80s and 90s and become part of international standards. I would go with "lacks research effort" here. – nwp Dec 08 '17 at 13:24
  • You can create a `std::vector` with a fixed size, and never use functions that change the size. Or use [`std::array`](http://en.cppreference.com/w/cpp/container/array)? – Some programmer dude Dec 08 '17 at 13:28
  • @nwp I have done my research, but i can't say that I know everything about C++ and have researched the proper keywords/questions... therefore this question. – Fawar Dec 08 '17 at 13:34
  • @Someprogrammerdude If I were to used std::array would the memory allocated be on the stack or the heap? And could I use std::any in that array to put any type (struct, object, base types) of data in there? How would the data be laid out? – Fawar Dec 08 '17 at 13:35
  • 1
    Those questions are not for SO. They are answered by looking at the documentation. [`std::array`](http://en.cppreference.com/w/cpp/container/array) and [`std::any`](http://en.cppreference.com/w/cpp/utility/any). You should also know that objects are stored at the place you put them. Some types need additional dynamic memory. `std::array` doesn't and `std::any` maybe, depending on the size and type of the object it holds. – nwp Dec 08 '17 at 13:39
  • You could allocate memory on the stack or the heap and then use that memory as you choose to place objects. [Here](https://stackoverflow.com/q/8049657/1460794) and [here](https://stackoverflow.com/questions/33722907/hinnants-short-alloc-and-alignment-guarantees) are some examples of this being done on the stack. In C++ it is your memory, you can do whatever you want with it, but you must decide how much you want to keep track of yourself. – wally Dec 08 '17 at 13:39
  • By the way, the existence of objects in C++ is defined by their constructors and destructors, not by the value of bytes being 0 or not. There is no need to initialize the buffer with 0 because the constructor is responsible to set up the type's invariants, which may or may not require setting some bytes to 0. – nwp Dec 08 '17 at 13:48
  • If you are looking for an implementation of heap memory allocation from which you can carve arbitrary sized samples at run time and then return then to the heap when they are deleted, here is an example: https://github.com/sccn/labstreaminglayer/blob/master/LSL/liblsl/src/sample.h#L40-L169 – dmedine Dec 08 '17 at 13:56
  • @Fawar - POD: "Plain Old Data" – 2785528 Dec 08 '17 at 14:10
  • Is this question actually just asking for [placement `new`](http://en.cppreference.com/w/cpp/language/new)? Or do you intend this fixed-size allocator to actually do something not shown (or described)? – Useless Dec 08 '17 at 14:13
  • @Useless I'm trying to build a Realtime Data Scrubber, issue is each data that is going to be scrub will be of different size and need to accessed sequentially (forward, backward) as fast as possible. So I was looking into managing my own block of memory to put data of various format/size and be able to read in it a continous/cache friendly way. Previously we used the union keyword but it's a nightmare to handle with all the new type of data that needs to be scrubbed. – Fawar Dec 08 '17 at 14:30

0 Answers0