Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stack_allocator.cpp « Memory « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e5b91420270f3f8132f863800e0afb257413c45 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//-----------------------------------------------------------------------------
//           Name: stack_allocator.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "stack_allocator.h"

#include <Utility/assert.h>
#include <Utility/stacktrace.h>

#include <Logging/logdata.h>
#include <Threading/thread_sanity.h>
#include <Memory/allocation.h>

#ifndef NO_ERR
#include <Internal/error.h>
#endif

#include <cstdio>
#include <cstdlib>
#include <stdint.h>

using std::endl;

void* StackAllocator::Alloc(size_t requested_size) {
    current_allocation_count++;
    frame_allocation_count++;
    //AssertMainThread();
#ifdef DEBUG_DISABLE_STACK_ALLOCATOR
	return OG_MALLOC(requested_size);
#else // DEBUG_DISABLE_STACK_ALLOCATOR
    if(stack_block_pts[stack_blocks] + requested_size < size && stack_blocks < kMaxBlocks-2){
        ++stack_blocks;
        stack_block_pts[stack_blocks] = stack_block_pts[stack_blocks-1] + requested_size;
        void* ptr = (void*)((uintptr_t)mem + stack_block_pts[stack_blocks-1]);
        return ptr;
    } else {
        if( stack_block_pts[stack_blocks] + requested_size >= size )
        {
            LOGF << "Unable to stack allocate memory size:" << requested_size << ". Out of memory." << endl;
        }
        else
        {
            LOGF << "Unable to stack allocate memory size:" << requested_size << ". Out of blocks." << endl;
        }
        
        LOGF << GenerateStacktrace() << endl;
        return NULL;
    }
#endif // DEBUG_DISABLE_STACK_ALLOCATOR
}

void StackAllocator::Free(void* ptr) {
    current_allocation_count--;
    //AssertMainThread();
#ifdef DEBUG_DISABLE_STACK_ALLOCATOR
	OG_FREE(ptr);
#else // DEBUG_DISABLE_STACK_ALLOCATOR
    if(stack_blocks){
        --stack_blocks;
        LOG_ASSERT(ptr == (void*)((uintptr_t)mem + stack_block_pts[stack_blocks]));
        if( ptr != (void*)((uintptr_t)mem + stack_block_pts[stack_blocks]) )
        {
            LOGE << "Called fre on something that isn't at the top of the stack" << endl;
            LOGE << "Stacktrace:" << endl << GenerateStacktrace() << endl; 
        }
        
    } else {
#ifndef NO_ERR
        FatalError("Memory stack underflow", "Calling Free() on StackMemoryBlock with no stack elements");
#endif
    }
#endif // DEBUG_DISABLE_STACK_ALLOCATOR
}


uintptr_t StackAllocator::TopBlockSize() {
    switch(stack_blocks){
    case 0:
        return 0;
    default:
        return stack_block_pts[stack_blocks] - stack_block_pts[stack_blocks-1];
    }
}

void StackAllocator::Init(void* p_mem, size_t p_size) {
    for (int i = 0; i < kMaxBlocks; i++) {
        stack_block_pts[i] = 0;
    }
    mem = p_mem;
    size = p_size;
    stack_blocks = 0;
}

void StackAllocator::Dispose() {
    for (int i = 0; i < kMaxBlocks; i++) {
        stack_block_pts[i] = 0;
    }
    mem = 0;
    size = 0;
    stack_blocks = 0;
}

uint64_t StackAllocator::AllocatedAmountMemory() {
    return stack_block_pts[stack_blocks];
}

uint32_t StackAllocator::GetCurrentAllocations() {
    return current_allocation_count;
}

uint32_t StackAllocator::GetAndResetAllocationCount() {
    uint32_t v = frame_allocation_count;
    frame_allocation_count = 0;
    return v;
}