Register
Handmade Hero»Forums»Code»advice on "memory management"
87 posts
advice on "memory management"
3 weeks ago Edited by C_Worm on Feb. 8, 2021, 6:43 p.m. Reason: Initial post
Hey, im trying to come up with some kind of custom allocator for my engine but its hard to forsee the limitations of it, pros / cons etc....
I've provided some pseudo-code here on what it does.

I would like to here some opinions about it and if you think its a good way to keep going with it or if taking the wrong approach, maybe if
you can see anything that would become problems.

Im using opengl for rendering so vtx_buffer here is just a big block of memory allocated on the stack as a big float array, that's sent to opengl via glBufferData.
The CreateQuad just assigns values to 36 vertices (1 quad) and then advances a vtx_marker to next "quad start"
in the vtx_buffer.

Every fram it resets the vtx_marker to the beginning of where the marker was just before the game-loop starts.

thanks!


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  

struct mem
{
   vtx_buffer;
   vtx_marker;
   vtx_marker_after_init;
}



        q (quad) = 36 vtx's

  	    vtx_marker  | 
  			        V

  vtx_buffer 		|---------------------------|

               vtx_marker  |
  				           V
  q1 = CreateQuad	|--q1--|--------------------|  

  		              vtx_marker  |
  					              V
  q2 = CreateQuad	|--q1--|--q2--|-------------|  
 
  

  mem.vtx_marker_after_init = mem.vtx_marker  (Must set this marker else "quads" before game-loop will be overwritten in the game-loop)


  while(running)
  {
  	!!! Must reset the vtx_marker to where it was before the game-loop
	    else there will be a vtx_buffer overflow since CreateQuad increments 
	    the vtx_buffer index * 36 every frame.

	// Reset vtx_marker
  	mem.vtx_marker = mem.vtx_marker_after_init

		         vtx_marker   |
				              V
  	vtx_buffer 	|--q1--|--q2--|-------------| (has to start after where the last quad was written before game-loop)


  	                         vtx_marker  |
  					                     V
	
  	q3 = CreateQuad	|--q1--|--q2--|--q3--|------|  


  }

  CreateQuad(vtx_buffer, pos, tex_pos, etc....)
  {
  	vtx = mem.vtx_marker_after_init + mem.vtx_marker

	assign vtx_buffer[vtx + (0 - 35)]  (36 vtx's per quad)

	mem.vtx_marker += 36

  }


Simon Anciaux
957 posts
advice on "memory management"
2 weeks, 6 days ago Edited by Simon Anciaux on Feb. 9, 2021, 4:53 p.m. Reason: 3 not 4
C_Worm
its hard to forsee the limitations of it, pros / cons etc...

In my experience, you should just do anything minimal/simple that does the job, and when you encounter an issue, address it. Spending time at the beginning on things you think may happen in the future is most likely a waste of time, because you don't actually have any information on the issue and are hoping you addressed it (if it happens at all).

The allocator you are describing is just an array where you reset the index each frame. Totally fine.

A detail in the wording of your post: 1 quad is 4 vertices and each vertex as 3 attributes:
- position, 3 components;
- uv, 2 components;
- color, 4 components;

For a total of 36 floats (or 144 bytes).