File: memory.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (63 lines) | stat: -rw-r--r-- 1,241 bytes parent folder | download | duplicates (2)
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

#pragma once

#include <cstddef>
#include <cstdlib>

#include "globalincs/pstypes.h"

namespace memory
{
	struct quiet_alloc_t { quiet_alloc_t(){} };
	extern const quiet_alloc_t quiet_alloc;

	void out_of_memory();
}

inline void *vm_malloc(size_t size, const memory::quiet_alloc_t &)
{ return std::malloc(size); }

inline void *vm_malloc(size_t size)
{
	auto ptr = vm_malloc(size, memory::quiet_alloc);

	if (ptr == NULL)
	{
		memory::out_of_memory();
	}

	return ptr;
}

inline void vm_free(void *ptr)
{ std::free(ptr); }

inline void *vm_realloc(void *ptr, size_t size, const memory::quiet_alloc_t &)
{ return std::realloc(ptr, size); }

inline void *vm_realloc(void *ptr, size_t size)
{
	auto ret_ptr = vm_realloc(ptr, size, memory::quiet_alloc);

	if (ret_ptr == NULL)
	{
		memory::out_of_memory();
	}

	return ret_ptr;
}

// For use with unique_ptr
template <typename T>
struct VmFreeDeleter
{
	void operator()(T* const p) const
	{
		vm_free(p);
	}
};

// Managed unique pointer for data allocated using vm_malloc.  Never use with data allocated with new or malloc;
// allocation and deallocation must always be performed using matching operations.
template<typename T>
using SCP_vm_unique_ptr = std::unique_ptr<T, VmFreeDeleter<T>>;