~vladh/peony

peony/src/memory.cpp -rw-r--r-- 2.8 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#include "logs.hpp"
#include "util.hpp"
#include "constants.hpp"
#include "memory.hpp"
#include "intrinsics.hpp"


namespace memory {
  pny_internal void reset_memory_pool(MemoryPool *pool) {
    #if USE_MEMORY_DEBUG_LOGS
      logs::info("Resetting memory pool");
    #endif
    pool->used = 0;
    pool->n_items = 0;
  }


  pny_internal void zero_out_memory_pool(MemoryPool *pool) {
    memset(pool->memory, 0, pool->size);
    pool->used = 0;
    pool->n_items = 0;
  }
}


void* memory::push(
  MemoryPool *pool,
  size_t item_size,
  const char *item_debug_name
) {
  // If we had just init'd an empty pool, let's just give it some size.
  if (pool->size == 0) {
    pool->size = util::mb_to_b(256);
  }

  // If we haven't allocated anything in the pool, let's allocate something now.
  if (pool->memory == nullptr) {
    #if USE_MEMORY_DEBUG_LOGS
      logs::info(
        "Allocating memory pool: %.2fMB (%dB)",
        util::b_to_mb((real64)pool->size),
        pool->size
      );
    #endif

    pool->memory = (uint8*)calloc(1, pool->size);
    if (!pool->memory) {
      logs::fatal("Could not allocate memory. Buy more RAM!");
      assert(false); // A little hint for the compiler
    }

    assert(pool->memory);
  }
  assert(pool->used + item_size <= pool->size);

  #if USE_MEMORYPOOL_ITEM_DEBUG
    assert(pool->n_items < MAX_N_MEMORYPOOL_ITEMS);
    pool->item_debug_names[pool->n_items] = item_debug_name;
    pool->item_debug_sizes[pool->n_items] = item_size;
  #endif

  void *new_memory = pool->memory + pool->used;
  pool->used += item_size;
  pool->n_items++;

  #if USE_MEMORY_DEBUG_LOGS
    logs::info(
      "Pusing to memory pool: %.2fMB (%dB) for %s, now at %.2fMB (%dB)",
      util::b_to_mb((real64)item_size),
      item_size, item_debug_name,
      util::b_to_mb((real64)pool->used),
      pool->used
    );
  #endif

  return new_memory;
}


void memory::print_memory_pool(MemoryPool *pool) {
  logs::info("MemoryPool:");
  logs::info("  Used: %.2fMB (%dB)", util::b_to_mb((uint32)pool->used), pool->used);
  logs::info("  Size: %.2fMB (%dB)", util::b_to_mb((uint32)pool->size), pool->size);
  logs::info("  Items:");
  if (pool->n_items == 0) {
    logs::info("    (none)");
  }
  #if USE_MEMORYPOOL_ITEM_DEBUG
    for (uint32 idx = 0; idx < pool->n_items; idx++) {
      logs::info(
        "    %02d. %s, %.2fMB (%dB)",
        idx,
        pool->item_debug_names[idx],
        util::b_to_mb((real64)pool->item_debug_sizes[idx]),
        pool->item_debug_sizes[idx]
      );
    }
  #endif
}


void memory::destroy_memory_pool(MemoryPool *memory_pool) {
  #if USE_MEMORY_DEBUG_LOGS
    logs::info("destroy_memory_pool");
  #endif
  reset_memory_pool(memory_pool);
  free(memory_pool->memory);
}