aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/tourist/foundation/include/foundation/malloc.h
blob: 12b0c743093b1465e15b14cc58b66fd9167bac2c (plain) (blame)
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
#pragma once

#include <algorithm>
#include <concepts>
#include <foundation/types.h>

//------------------------------------------------------------------------------
void            tt_memory_canaries_on();
void            tt_memory_stomp_on();
extern void*    (*tt_malloc_impl)(size_t size, size_t alignment);
extern void     (*tt_free)(void* address);

//------------------------------------------------------------------------------
template <typename T=uint8>
T* tt_malloc(
    size_t size,
    size_t alignment=std::max(alignof(std::max_align_t), alignof(T)))
{
    return (T*)tt_malloc_impl(size, alignment);
}

//------------------------------------------------------------------------------
template <std::default_initializable T>
T* tt_new(size_t count=1)
{
    size_t size = sizeof(T) * count;
    auto* ret = (T*)tt_malloc_impl(size, alignof(T));
    for (T& t : Span<T>(ret, count))
        new (&t) T();
    return ret;
}