2006-03-20 17:12:09 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
|
|
|
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
|
|
|
|
2015-01-23 03:55:04 +00:00
|
|
|
#pragma once
|
2008-04-06 20:23:28 +00:00
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
#include <stdlib.h>
|
2017-06-25 20:46:03 +00:00
|
|
|
#include <type_traits>
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2012-11-11 22:12:51 +00:00
|
|
|
#ifdef __cplusplus
|
2015-01-23 03:55:04 +00:00
|
|
|
#include <memory>
|
2013-12-18 21:47:20 +00:00
|
|
|
#include "dxxsconf.h"
|
2016-03-19 19:08:10 +00:00
|
|
|
#include "dsx-ns.h"
|
2020-05-02 21:18:42 +00:00
|
|
|
#include <utility>
|
2012-11-11 22:12:51 +00:00
|
|
|
|
2010-08-28 01:13:01 +00:00
|
|
|
#define MEM_K 1.5 // Dynamic array growth factor
|
|
|
|
|
2015-06-13 22:42:15 +00:00
|
|
|
#ifdef DEBUG_BIAS_MEMORY_ALLOCATIONS
|
|
|
|
#include "compiler-array.h"
|
|
|
|
#define DXX_DEBUG_BIAS_MEMORY_ALLOCATION (sizeof(array<double, 2>))
|
|
|
|
#else
|
|
|
|
#define DXX_DEBUG_BIAS_MEMORY_ALLOCATION (0)
|
|
|
|
#endif
|
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2015-12-05 22:57:24 +00:00
|
|
|
|
2012-05-20 18:59:08 +00:00
|
|
|
#ifdef DEBUG_MEMORY_ALLOCATIONS
|
2012-09-02 17:16:21 +00:00
|
|
|
void mem_init(void);
|
2012-10-31 15:26:48 +00:00
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
void mem_display_blocks();
|
2015-01-23 03:55:05 +00:00
|
|
|
__attribute_alloc_size(1)
|
|
|
|
__attribute_malloc()
|
|
|
|
void *mem_malloc(size_t size, const char *var, const char *file, unsigned line);
|
2013-12-18 22:13:03 +00:00
|
|
|
void * mem_calloc( size_t nmemb, size_t size, const char * var, const char * filename, unsigned line) __attribute_alloc_size(1,2) __attribute_malloc();
|
2015-01-23 03:55:05 +00:00
|
|
|
__attribute_alloc_size(2)
|
|
|
|
void *mem_realloc(void *buffer, size_t size, const char *var, const char *file, unsigned line);
|
2006-03-20 17:12:09 +00:00
|
|
|
extern void mem_free( void * buffer );
|
|
|
|
|
|
|
|
/* DPH: Changed malloc, etc. to d_malloc. Overloading system calls is very evil and error prone */
|
|
|
|
|
|
|
|
// Checks to see if any blocks are overwritten
|
|
|
|
void mem_validate_heap();
|
|
|
|
|
2013-07-13 04:23:18 +00:00
|
|
|
#define mem_calloc(nmemb,size,var,file,line) ((mem_calloc)((size) ? (nmemb) : 0, (size), (var), (file), (line)))
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2013-07-13 04:23:18 +00:00
|
|
|
#else
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-06-13 22:42:15 +00:00
|
|
|
#ifdef DEBUG_BIAS_MEMORY_ALLOCATIONS
|
|
|
|
#define bias_malloc(SIZE) ({ \
|
|
|
|
auto p = malloc((SIZE) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION); \
|
|
|
|
p ? p + DXX_DEBUG_BIAS_MEMORY_ALLOCATION : p; \
|
|
|
|
})
|
|
|
|
/* Bias calloc wastes a bit extra to keep the math simple */
|
|
|
|
#define bias_calloc(NMEMB,SIZE) ({ \
|
|
|
|
auto p = calloc((NMEMB), (SIZE) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION); \
|
|
|
|
p ? p + DXX_DEBUG_BIAS_MEMORY_ALLOCATION : p; \
|
|
|
|
})
|
|
|
|
#define bias_realloc(PTR,SIZE) ({ \
|
|
|
|
auto p = realloc(reinterpret_cast<char *>(PTR) - DXX_DEBUG_BIAS_MEMORY_ALLOCATION, (SIZE) + DXX_DEBUG_BIAS_MEMORY_ALLOCATION); \
|
|
|
|
p ? p + DXX_DEBUG_BIAS_MEMORY_ALLOCATION : p; \
|
|
|
|
})
|
|
|
|
#define bias_free(PTR) free(reinterpret_cast<char *>(PTR) - DXX_DEBUG_BIAS_MEMORY_ALLOCATION)
|
|
|
|
#else
|
|
|
|
#define bias_malloc malloc
|
|
|
|
#define bias_calloc calloc
|
|
|
|
#define bias_realloc realloc
|
|
|
|
#define bias_free free
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define mem_malloc(size,var,file,line) ((void)var,(void)file,(void)line,bias_malloc((size)))
|
|
|
|
#define mem_calloc(nmemb,size,var,file,line) ((void)var,(void)file,(void)line,bias_calloc((nmemb),(size)))
|
|
|
|
#define mem_realloc(ptr,size,var,file,line) ((void)var,(void)file,(void)line,bias_realloc((ptr),(size)))
|
|
|
|
#define mem_free bias_free
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2012-05-20 18:59:08 +00:00
|
|
|
static inline void mem_init(void)
|
|
|
|
{
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
#endif
|
2008-04-06 20:23:28 +00:00
|
|
|
|
2014-07-25 01:36:12 +00:00
|
|
|
template <typename T>
|
2014-09-07 21:23:03 +00:00
|
|
|
T *MALLOC(T *&r, std::size_t count, const char *var, const char *file, unsigned line)
|
2014-07-25 01:36:12 +00:00
|
|
|
{
|
2017-06-25 20:46:03 +00:00
|
|
|
static_assert(std::is_pod<T>::value, "MALLOC cannot allocate non-POD");
|
2015-01-23 03:55:05 +00:00
|
|
|
return r = reinterpret_cast<T *>(mem_malloc(count * sizeof(T), var, file, line));
|
2014-07-25 01:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2014-09-07 21:23:03 +00:00
|
|
|
T *CALLOC(T *&r, std::size_t count, const char *var, const char *file, unsigned line)
|
2014-07-25 01:36:12 +00:00
|
|
|
{
|
2017-06-25 20:46:03 +00:00
|
|
|
static_assert(std::is_pod<T>::value, "CALLOC cannot allocate non-POD");
|
2014-09-07 21:23:03 +00:00
|
|
|
return r = reinterpret_cast<T *>(mem_calloc(count, sizeof(T), var, file, line));
|
2014-07-25 01:36:12 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:23:18 +00:00
|
|
|
#define d_malloc(size) mem_malloc((size),"Unknown", __FILE__,__LINE__ )
|
|
|
|
#define d_realloc(ptr,size) mem_realloc((ptr),(size),"Unknown", __FILE__,__LINE__ )
|
2014-08-16 04:18:58 +00:00
|
|
|
template <typename T>
|
|
|
|
static inline void d_free(T *&ptr)
|
|
|
|
{
|
2017-06-25 20:46:03 +00:00
|
|
|
static_assert((std::is_same<T, void>::value || std::is_pod<T>::value), "d_free cannot free non-POD");
|
2020-05-02 21:18:42 +00:00
|
|
|
mem_free(std::exchange(ptr, nullptr));
|
2014-08-16 04:18:58 +00:00
|
|
|
}
|
2013-07-13 04:23:18 +00:00
|
|
|
|
2015-01-23 03:55:04 +00:00
|
|
|
template <typename T>
|
|
|
|
class RAIIdmem_deleter
|
2013-12-08 18:22:17 +00:00
|
|
|
{
|
2015-01-23 03:55:04 +00:00
|
|
|
public:
|
|
|
|
void operator()(T *v) const
|
2014-09-07 21:23:03 +00:00
|
|
|
{
|
2015-01-23 03:55:04 +00:00
|
|
|
d_free(v);
|
2013-12-08 18:22:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-28 03:42:53 +00:00
|
|
|
template <typename T>
|
|
|
|
class RAIIdmem_deleter<T[]> : public RAIIdmem_deleter<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T *pointer;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class RAIIdmem : public std::unique_ptr<T, RAIIdmem_deleter<T>>
|
2013-12-08 18:22:17 +00:00
|
|
|
{
|
2015-01-28 03:42:53 +00:00
|
|
|
typedef std::unique_ptr<T, RAIIdmem_deleter<T>> base_ptr;
|
2015-01-23 03:55:04 +00:00
|
|
|
public:
|
2017-07-08 18:17:49 +00:00
|
|
|
static_assert(std::is_pod<typename base_ptr::element_type>::value, "RAIIdmem cannot manage non-POD");
|
2015-01-28 03:42:53 +00:00
|
|
|
DXX_INHERIT_CONSTRUCTORS(RAIIdmem, base_ptr);
|
2013-12-08 18:22:17 +00:00
|
|
|
};
|
|
|
|
|
2015-01-28 03:42:53 +00:00
|
|
|
/* Disallow C-style arrays of known bound. Use RAIIdmem<array<T, N>>
|
|
|
|
* for this case.
|
|
|
|
*/
|
|
|
|
template <typename T, std::size_t N>
|
|
|
|
class RAIIdmem<T[N]>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RAIIdmem() = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
RAIIdmem<T> &MALLOC(RAIIdmem<T> &r, std::size_t count, const char *var, const char *file, unsigned line)
|
2014-09-07 21:23:03 +00:00
|
|
|
{
|
2015-01-28 03:42:53 +00:00
|
|
|
typename RAIIdmem<T>::pointer p;
|
|
|
|
return r.reset(MALLOC<typename RAIIdmem<T>::element_type>(p, count, var, file, line)), r;
|
2014-09-07 21:23:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:36:39 +00:00
|
|
|
template <typename T>
|
|
|
|
RAIIdmem<T[]> &MALLOC(RAIIdmem<T[]> &r, const std::size_t count, const char *const var, const char *const file, const unsigned line)
|
|
|
|
{
|
|
|
|
return MALLOC<T[]>(r, count, var, file, line);
|
|
|
|
}
|
|
|
|
|
2015-01-28 03:42:53 +00:00
|
|
|
template <typename T>
|
|
|
|
void CALLOC(RAIIdmem<T> &r, std::size_t count, const char *var, const char *file, unsigned line)
|
2014-09-07 21:23:03 +00:00
|
|
|
{
|
2015-01-28 03:42:53 +00:00
|
|
|
typename RAIIdmem<T>::pointer p;
|
|
|
|
r.reset(CALLOC<typename RAIIdmem<T>::element_type>(p, count, var, file, line));
|
2014-09-07 21:23:03 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 03:55:05 +00:00
|
|
|
#define MALLOC( var, type, count ) (MALLOC<type>(var, (count),#var, __FILE__,__LINE__ ))
|
2014-09-07 21:23:03 +00:00
|
|
|
#define CALLOC( var, type, count ) (CALLOC<type>(var, (count),#var, __FILE__,__LINE__ ))
|
|
|
|
|
2015-12-05 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-11 22:12:51 +00:00
|
|
|
#endif
|