Descent3/mem/mem.h

142 lines
3.8 KiB
C
Raw Permalink Normal View History

/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
--- HISTORICAL COMMENTS FOLLOW ---
2024-04-16 03:43:29 +00:00
* $Logfile: /DescentIII/Main/lib/mem.h $
* $Revision: 18 $
* $Date: 3/20/00 12:26p $
* $Author: Matt $
*
* Memory header.
*
* $Log: /DescentIII/Main/lib/mem.h $
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 18 3/20/00 12:26p Matt
* Merge of Duane's post-1.3 changes.
* Added declaration
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 17 10/21/99 9:27p Jeff
* B.A. Macintosh code merge
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 16 8/10/99 5:12p Jeff
* added a debug function to dump the current state of the mem library to
* file
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 15 3/15/99 11:24a Kevin
* Improved memory leak detection for mem_strdup and turned on debugging
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 14 10/13/98 3:42p Kevin
* bug fixes
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 13 10/13/98 9:25a Kevin
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 12 10/12/98 8:39p Kevin
* removed mprintf's and fixed some smallish bugs
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 11 10/12/98 11:54a Kevin
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 10 10/12/98 10:55a Kevin
* Don't use memory lib for editor currently
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 9 10/12/98 10:22a Samir
* made mem_strdup take a const char pointer.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 8 10/09/98 3:32p Kevin
* New memory library
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 7 10/08/98 4:24p Kevin
* Changed code to comply with memory library usage. Always use mem_malloc
* , mem_free and mem_strdup
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 6 10/08/98 2:40p Kevin
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 5 7/31/98 5:44p Samir
* improved memory debugging.
*
* $NoKeywords: $
*/
#ifndef MEM_H
#define MEM_H
#include <cstdlib>
#include <type_traits>
// Memory management debugging
2024-04-16 03:43:29 +00:00
#ifdef MEM_USE_RTL
2024-04-16 18:56:40 +00:00
#define mem_malloc(d) malloc(d) // Use this if your going to run BoundsChecker
#define mem_free(d) free(d)
2024-04-16 03:43:29 +00:00
#define mem_strdup(d) strdup(d)
#define mem_size(d) mem_size_sub(d)
2024-04-16 18:56:40 +00:00
#define mem_realloc(d, e) realloc(d, e)
2024-04-16 03:43:29 +00:00
#else
2024-04-16 18:56:40 +00:00
// Use this if your going to NOT run BoundsChecker
#define mem_malloc(d) mem_malloc_sub(d, __FILE__, __LINE__)
#define mem_free(d) mem_free_sub(d)
2024-04-16 03:43:29 +00:00
#define mem_strdup(d) mem_strdup_sub(d, __FILE__, __LINE__)
#define mem_size(d) mem_size_sub(d)
2024-04-16 18:56:40 +00:00
#define mem_realloc(d, e) mem_realloc_sub(d, e)
2024-04-16 03:43:29 +00:00
#endif
extern bool Mem_low_memory_mode;
2024-04-16 18:56:40 +00:00
extern bool Mem_superlow_memory_mode; // DAJ
2024-04-16 03:43:29 +00:00
// use if you want to manually print out a memory error
2024-04-16 18:56:40 +00:00
#define mem_error() mem_error_msg(__FILE__, __LINE__)
2024-04-16 03:43:29 +00:00
// initializes memory library.
2024-04-16 03:43:29 +00:00
void mem_Init();
// shutsdown memory
void mem_shutdown();
2024-04-16 03:43:29 +00:00
// Returns the number of dynamically allocated bytes
2024-04-16 18:56:40 +00:00
int mem_GetTotalMemoryUsed();
2024-04-16 03:43:29 +00:00
// Allocates a block of memory and returns a pointer to it
2024-04-16 18:56:40 +00:00
void *mem_malloc_sub(int size, const char *file, int line);
2024-04-16 03:43:29 +00:00
// Frees a previously allocated block of memory
2024-04-16 18:56:40 +00:00
void mem_free_sub(void *memblock);
2024-04-16 03:43:29 +00:00
// prints out a memory error message
2024-04-16 18:56:40 +00:00
void mem_error_msg(const char *file, int line, int size = -1);
2024-04-16 03:43:29 +00:00
char *mem_strdup_sub(const char *src, const char *file, int line);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
void *mem_realloc_sub(void *memblock, int size);
2024-04-16 03:43:29 +00:00
int mem_size_sub(void *memblock);
bool mem_dumpmallocstofile(char *filename);
void mem_heapcheck();
2024-04-16 03:43:29 +00:00
// type aware memory allocation
template<typename T> static inline T *mem_rmalloc()
{
static_assert(std::is_trivially_constructible_v<T> && std::is_trivially_destructible_v<T>);
return static_cast<T *>(mem_malloc(sizeof(T)));
}
template<typename T> static inline T *mem_rmalloc(std::size_t nelem)
{
static_assert(std::is_trivially_constructible_v<T> && std::is_trivially_destructible_v<T>);
return static_cast<T *>(mem_malloc(nelem * sizeof(T)));
}
#endif