Sign in to follow this  
arbuckle911

Unity custom memory allocator

Recommended Posts

I'm going to be writing a custom memory allocator, and I wanted to run my idea by the community first. I've never written one before but the fact that I want to benefit from the speed this kind of thing can offer I decided it would be a good idea. Basically I want to write a C++ class Called MemoryPool. On creation MemoryPool will run memory = new char[sizeInByes]; where memory is defined as char *memory; During usage that memory chunk will be divided on the basis of what types it's going to store. For example, I'd like to use it for octree and scene object management. So I would do: On level load:
MemoryPool pool(neededBytes);  //creates the block of memory
pool.CreateBank(sizeof(StaticMapObject), numObjects);  //Sets a pointer to a place in memory where numObjects of StaticMapObjects can live in the char array. A counter returns sucessive pointers as they are requested.
pool.CreateBank(sizeof(OctreeNode), numNodes); //Same as other, but for OctreeNodes and in another place in the pool.


Here are my questions: Is it a good idea to use a char array? Should I use an unsigned char array? is malloc better for this application? should the object "banks" be closely packed together? would it matter if the first bank had a 100 meg empty space between it and the next bank? If the spacing between bank affects speed, how can I reduce the space between the banks? How should I handle deleted objects? make a list of pointers to locations of deleted ojects for reuse? That about sums it up. If anybody knows of any resources that do this already please let me know. Also if there are any optimizations I could make here I would be glad if they were pointed out. Thanks in advance.

Share this post


Link to post
Share on other sites
Quote:
Original post by arbuckle911
Is it a good idea to use a char array? Should I use an unsigned char array?
is malloc better for this application?

It's a good idea to use operator new for the particular datatype. That's the only way to ensure proper alignment.
Quote:
should the object "banks" be closely packed together? would it matter if the first bank had a 100 meg empty space between it and the next bank?

It doesn't really matter, assuming a fairly large number of objects. Small object pooled allocators aren't great at cache coherence no matter what you do.

Quote:
If anybody knows of any resources that do this already please let me know.

Check out boost::pool. There is very little reason for anyone to implement their own fixed-size pool allocator.

Share this post


Link to post
Share on other sites
Quote:

It's a good idea to use operator new for the particular datatype. That's the only way to ensure proper alignment.

Ok but, what if I want to make SURE that all the data is in one chunk? Are you saying it would be best to make an array for each object type? If there is an array for each object type, wouldn't there be fragmentation between each array?

Share this post


Link to post
Share on other sites
Quote:
Original post by arbuckle911
Ok but, what if I want to make SURE that all the data is in one chunk? Are you saying it would be best to make an array for each object type? If there is an array for each object type, wouldn't there be fragmentation between each array?

Hmm? A fixed size pool allocator only works on objects of a single size, which pretty much means objects of a single type.

Share this post


Link to post
Share on other sites
ok, I did some reading I get what you mean now. I'll probly just end up creating a "Factory" class that just dishes whatever class I need out from a fixed size array.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627664
    • Total Posts
      2978522
  • Similar Content

    • By Kurupted Playz
      Hey, I'm looking for some people that can help me with a game that I'm making that is a 3d platformer.
      I mostly need someone for character design and animation,  I can do some coding but if you can do coding as well you're still welcome because I'm still not the greatest at coding just yet.
      if you need to contact me add me on discord @ Kurupted#1206
       
    • By Jcyshadow97
      Hi,guys.I m working on a Fantasy RPG.Currently i m work alone on the project.I really need someone can make UI stuff.If someone can handle it please feel free to contact me on email: 270514974@libero.it.
      Thank you guys and sorry for my english.
       
       



    • By STRATUM the Game
      Hey, everyone! This is my first post here.
      I would like to know what you think about my project called STRATUM. It's a 2D platformer that is heavily based on storytelling and boss fighting while trekking through the world.

      Everything in STRATUM takes place in the first century AD, in a world that wraps our own universe, called  The Stratum. A parallel Universe that is the home of the Christian deities . In this game you will play as a Dacian warrior, unfamiliar with everything in this world, you’ll get to know and understand The Stratum together with him.
      The main thing that I want with STRATUM is to reinvent the known lore and history of the Christian deities and realms. 
      The story is unconventional, it plays down a lot of the mysticism of Hell or Heaven and it gives it a more rational view while keeping the fantastic in it. What do I mean by that? Well, think about Hell. What do you know about it? It's a bad place where bad people go, right? Well, that's not the case in STRATUM. I don't want to describe such a world. In STRATUM, there is a reason for everything, especially for the way Hell is what it is in the game. "Hell" is called The Black Stratum in the game.
      This world is not very different from Earth, but it is governed by different natural laws.
      The story will also involve the reason why this world entered in touch with ours.

       
      What do you think about all that I said? Would you be interested in such a game? I have to say that everything is just a work of fiction made with my imagination. I do not want to offend anyone's beliefs.
      I want this to be a one man game. I have been working alone on it (this was my decision from the beginning) from art to effects to programming to music to sound effects to everything.
      I also have a youtube video HERE if you want to see the way the game moves and the way my music sounds.
      Please, any kind of feedback will be highly appreciated. If you have something bad to say, do it, don't keep it for yourself only. I want to hear anything that you don't like about my project.
       
    • By LimeJuice
      Hi, it's my first post on this forum and I would like to share the game I am working on at the moment.
      Graphics have been made with Blender3D using Cycle as a renderer and I am using Unity3D. It's a 2D game, one touch side-scrolling game for iOS and Android.
      Here some pictures, and you can have a look to the gameplay on this video :
      Feedbacks ?
      And if you want to try it, send me your email and I will add you to the beta tester list!
       
       








    • By Kirill Kot
      An adventure indie game with quests in a beautiful, bright world. Characters with unique traits, goals, and benefits. Active gameplay will appeal to players found of interactivity, especially lovers of quests and investigations.
      Available on:
      Gameroom (just open the web page and relax)
      AppStore
      GooglePlay
      WindowsPhone

  • Popular Now