2016-03-27 11:49:47 +02:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
Copyright (C) 2015 the OpenMoHAA team
|
|
|
|
|
|
|
|
This file is part of OpenMoHAA source code.
|
|
|
|
|
|
|
|
OpenMoHAA source code is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
OpenMoHAA source code is distributed in the hope that it will be
|
|
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OpenMoHAA source code; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// mem_tempalloc.cpp: Fast temporary memory manager
|
|
|
|
|
2023-06-17 01:24:20 +02:00
|
|
|
#include "mem_tempalloc.h"
|
2016-03-27 11:49:47 +02:00
|
|
|
|
2023-01-29 20:59:31 +01:00
|
|
|
#ifdef GAME_DLL
|
2023-07-05 21:23:39 +02:00
|
|
|
# include "../fgame/g_local.h"
|
2023-01-29 20:59:31 +01:00
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
# define MEM_TempAllocate(x) gi.Malloc(x)
|
|
|
|
# define MEM_TempFree(x) gi.Free(x)
|
2023-04-30 20:36:22 +02:00
|
|
|
|
|
|
|
#elif defined(CGAME_DLL)
|
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
# include "../cgame/cg_local.h"
|
2023-04-30 20:36:22 +02:00
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
# define MEM_TempAllocate(x) cgi.Malloc(x)
|
|
|
|
# define MEM_TempFree(x) cgi.Free(x)
|
2023-04-30 20:36:22 +02:00
|
|
|
|
2023-01-29 20:59:31 +01:00
|
|
|
#else
|
2023-07-05 21:23:39 +02:00
|
|
|
# include "qcommon.h"
|
2023-01-29 20:59:31 +01:00
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
# define MEM_TempAllocate(x) Z_Malloc(x)
|
|
|
|
# define MEM_TempFree(x) Z_Free(x)
|
2023-01-29 20:59:31 +01:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 23:12:26 +01:00
|
|
|
class tempBlock_t
|
|
|
|
{
|
|
|
|
public:
|
2023-07-05 21:23:39 +02:00
|
|
|
void *GetData();
|
|
|
|
void *GetData(size_t pos);
|
2023-02-01 23:12:26 +01:00
|
|
|
|
|
|
|
public:
|
2023-07-05 21:23:39 +02:00
|
|
|
tempBlock_t *prev;
|
2023-02-01 23:12:26 +01:00
|
|
|
};
|
|
|
|
|
2016-03-27 11:49:47 +02:00
|
|
|
MEM_TempAlloc::MEM_TempAlloc()
|
|
|
|
{
|
2023-02-04 19:56:06 +01:00
|
|
|
m_CurrentMemoryBlock = nullptr;
|
2023-07-05 21:23:39 +02:00
|
|
|
m_CurrentMemoryPos = 0;
|
|
|
|
m_BlockSize = 0;
|
|
|
|
m_LastPos = 0;
|
2016-03-27 11:49:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
void *MEM_TempAlloc::Alloc(size_t len)
|
2016-03-27 11:49:47 +02:00
|
|
|
{
|
2023-02-04 19:56:06 +01:00
|
|
|
if (m_CurrentMemoryBlock && m_CurrentMemoryPos + len <= m_BlockSize) {
|
2023-07-05 21:23:39 +02:00
|
|
|
void *data = m_CurrentMemoryBlock->GetData(m_CurrentMemoryPos);
|
|
|
|
m_LastPos = m_CurrentMemoryPos;
|
2023-02-04 19:56:06 +01:00
|
|
|
m_CurrentMemoryPos += len;
|
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
return CreateBlock(len);
|
|
|
|
}
|
2023-02-01 23:12:26 +01:00
|
|
|
}
|
2016-03-27 11:49:47 +02:00
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
void *MEM_TempAlloc::Alloc(size_t len, size_t alignment)
|
2023-02-01 23:12:26 +01:00
|
|
|
{
|
2023-02-04 19:56:06 +01:00
|
|
|
if (m_CurrentMemoryBlock) {
|
|
|
|
if (m_CurrentMemoryPos % alignment != 0) {
|
|
|
|
m_CurrentMemoryPos += alignment - m_CurrentMemoryPos % alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_CurrentMemoryPos + len <= m_BlockSize) {
|
2023-07-05 21:23:39 +02:00
|
|
|
void *data = m_CurrentMemoryBlock->GetData(m_CurrentMemoryPos);
|
|
|
|
m_LastPos = m_CurrentMemoryPos;
|
2023-02-04 19:56:06 +01:00
|
|
|
m_CurrentMemoryPos += len;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CreateBlock(len);
|
2016-03-27 11:49:47 +02:00
|
|
|
}
|
|
|
|
|
2023-02-04 19:56:06 +01:00
|
|
|
void MEM_TempAlloc::FreeAll(void)
|
2016-03-27 11:49:47 +02:00
|
|
|
{
|
2023-02-04 19:56:06 +01:00
|
|
|
while (m_CurrentMemoryBlock) {
|
2023-07-05 21:23:39 +02:00
|
|
|
tempBlock_t *prev_block = m_CurrentMemoryBlock->prev;
|
2023-02-04 19:56:06 +01:00
|
|
|
MEM_TempFree(m_CurrentMemoryBlock);
|
|
|
|
m_CurrentMemoryBlock = prev_block;
|
|
|
|
}
|
2016-03-27 11:49:47 +02:00
|
|
|
}
|
2023-02-01 23:12:26 +01:00
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
void *MEM_TempAlloc::CreateBlock(size_t len)
|
2023-02-01 23:12:26 +01:00
|
|
|
{
|
2023-02-04 19:56:06 +01:00
|
|
|
m_CurrentMemoryPos = len;
|
|
|
|
|
|
|
|
// allocate a new block
|
2023-07-05 21:23:39 +02:00
|
|
|
tempBlock_t *prev_block = m_CurrentMemoryBlock;
|
|
|
|
m_CurrentMemoryBlock = (tempBlock_t *)MEM_TempAllocate(sizeof(tempBlock_t) + Q_max(m_BlockSize, len));
|
2023-02-04 19:56:06 +01:00
|
|
|
m_CurrentMemoryBlock->prev = prev_block;
|
|
|
|
return m_CurrentMemoryBlock->GetData();
|
2023-02-01 23:12:26 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
void *tempBlock_t::GetData()
|
|
|
|
{
|
|
|
|
return (void *)(this + 1);
|
|
|
|
}
|
2023-02-01 23:12:26 +01:00
|
|
|
|
2023-07-05 21:23:39 +02:00
|
|
|
void *tempBlock_t::GetData(size_t pos)
|
|
|
|
{
|
|
|
|
return (uint8_t *)(this + 1) + pos;
|
|
|
|
}
|