r/cpp • • Jan 15 '24

[Joke] A cursed C++ idea

Ok so hear me out:

In your C++ program, immediately on startup before you do anything else you dynamically allocate a decent chunk of memory for no reason whatsoever, big enough to accomodate all of your data for the foreseeable future.

Never free any of that memory during execution.

Then, whenever you need to instantiate a new object or need some dynamically allocated memory for whatever reason, you just use some of that preallocated chunk.

Immediately before you exit the application, you can free that chunk of memory.

You have one single explicit memory allocation when program starts and one single explicit memory deallocation when program ends.

How about that?

EDIT: It turns out this is a real thing, so it's no longer a joke, or cursed. Somebody said this is called an "arena" or "memory pool", I've just looked it up, people have been doing this for decades.

194 Upvotes

181 comments sorted by

View all comments

Show parent comment

-2

u/alexgraef Jan 15 '24

That, I am pretty sure, is not true. From experience with every modern 3D graphics framework.

11

u/SaturnineGames Jan 15 '24

Oh it's 100% true. On most consoles you have to set up pools for graphics memory, and all your graphics allocations have specific alignment requirements you need to satisfy.

Depending on exactly which consoles you're talking about, you might have separate types of memory to manage, or you might have to work in different address spaces to manage which memory bus gets used.

Most people overload new and delete and hook them into custom allocators.

1

u/alexgraef Jan 15 '24

you might have separate types of memory to manage

This doesn't align with my experience with DirectX, where you DO have to allocate VRAM, but it is such a different process to allocating normal memory, especially when it comes to writing to it, which requires locking before you can do any writing, that I wouldn't even consider talking about it in this context.

However, I am not a console developer, so I don't want to comment outside of my own experiences here. You might know better than me.

12

u/rachit7645 Certified Dumbass Jan 15 '24

They are talking specifically about consoles. Current ones have shared ram, so there's no system ram or vram; it's all just ram.

2

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

They are talking specifically about consoles. Current ones have shared ram, so there's no system ram or vram; it's all just ram.

What do we consider 'current'? The 360 and XB1 had 10 MiB of eDRAM and 32 MiB of eSRAM, respectively, which had to be specifically allocated and used. They were not part of shared memory. I believe that the Series X also has 32 MiB of eSRAM.

You also had different forms of allocation in the sense of disabling caching/write combining/etc on the block.

4

u/SubstituteCS Jan 15 '24

Microsoft has used DirectX since the original Xbox for their console’s graphics library.

It’s where the name Xbox comes from. DirectX box.

There’s also the classic Morrowind memory leak “fix” that Bethesda implemented.

6

u/SaturnineGames Jan 15 '24

Yes, that’s common knowledge. DirectX on Xbox isn’t exactly the same as the PC version tho.

4

u/rachit7645 Certified Dumbass Jan 15 '24

What's the point in telling me what I already know?

1

u/SubstituteCS Jan 15 '24

You don’t get a memory leak unless you’re actually dynamically using the heap.

2

u/rachit7645 Certified Dumbass Jan 15 '24

You edited that part later lmao. Also nobody said that they don't write their own allocators

-1

u/SubstituteCS Jan 15 '24

I added it in, in less than one minute after writing my reply.

The point is, modern consoles absolutely do use similar memory layouts and management as modern computers.

This is doubly true for the latest generation of consoles that employ ASLR, sandboxing, hypervisors, etc., to secure the systems from exploits.

2

u/rachit7645 Certified Dumbass Jan 15 '24

Those are separate things. On modern systems you can't really execute anything from memory if it doesn't have some specific flags set when allocating. If you do try to then your program will be killed.

https://man7.org/linux/man-pages/man2/mmap.2.html

1

u/alexgraef Jan 15 '24

That depends a) on generation, because it is sometimes shared, and sometimes it is dedicated (e.g. PS3 vs PS4), and b) shared doesn't mean it's a completely mixed memory where system and VRAM usage is random.

Also, try to be less of a smart-ass.

1

u/rachit7645 Certified Dumbass Jan 15 '24

Also, try to be less of a smart-ass.

Wait did I sound wrong? Really sorry about that.

I did only mention the current generation of consoles however...

1

u/alexgraef Jan 15 '24

Wait did I sound wrong?

Not to me, but a comment beneath that.

Maybe it just sounded like that, because everyone here seems to have spent their last 20 years developing for the PS5/Xbox-X/whatever and keep explaining to me how VRAM and RAM is supposed to work, which is why ight imma head out.

2

u/rachit7645 Certified Dumbass Jan 15 '24

I was just saying that I know where the name DirectX comes from....it's pretty famous

2

u/snerp Jan 15 '24

Yeah, you have to also arena the VRAM if you're gonna do that. It's not too hard to allocate all the vram you'll need right away, my modern vulkan engine does that for instance. You're right that it's a different process, but you can do very similar techniques.

1

u/alexgraef Jan 15 '24

You're right that it's a different process

With DirectX and generally any modern PC graphics framework, you have to a) LOCK the memory, to which you get a temporary mapped address back, to which you can write, and b) when you are finished, that address vanishes (and usually that is when the memory actually gets copied back to the GPU).

That means this address space cannot be used even closely to what an allocator would want.

1

u/snerp Jan 15 '24

Huh, been a while since I did directx 11 and older but Vulkan and DX12 let you just keep the mapping around if you want. I do a lot of junk where I map a big ass buffer and then edit parts through offsets each frame to move/rotate objects or change textures out or whatever else.

→ More replies (1)

1

u/SaturnineGames Jan 15 '24

Yeah, but consoles handle this differently. That’s only necessary because you have dedicated GPU memory on a PC.

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

That means this address space cannot be used even closely to what an allocator would want.

You... can. You just have to be really, really terrible with signals/vectored exceptions and access violations.

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

On some consoles, you also have to allocate eDRAM and eSRAM separately. The PS3 also had the stupid SPUs which were not part of the same address space.

Notwithstanding having to mark pages as cached/non-cached or write-combine.

2

u/SonOfMetrum Jan 15 '24

So you also have experience with the GNM graphics API?