nvidia-texture-tools/src/nvcore/Memory.h

77 lines
1.7 KiB
C
Raw Normal View History

// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
#pragma once
#ifndef NV_CORE_MEMORY_H
#define NV_CORE_MEMORY_H
#include "nvcore.h"
#include <stdlib.h> // malloc(), realloc() and free()
2018-02-06 02:55:07 +00:00
#include <string.h> // memset
2012-04-30 23:02:23 +00:00
//#include <stddef.h> // size_t
#if NV_OS_LINUX
#include <malloc.h> // memalign()
#endif
2012-04-30 23:02:23 +00:00
//#include <new> // new and delete
2018-02-06 02:55:07 +00:00
#define TRACK_MEMORY_LEAKS 0
#if TRACK_MEMORY_LEAKS
#include <vld.h>
#endif
2011-09-27 17:48:46 +00:00
#if NV_CC_GNUC
# define NV_ALIGN_16 __attribute__ ((__aligned__ (16)))
#else
# define NV_ALIGN_16 __declspec(align(16))
#endif
#define NV_OVERRIDE_ALLOC 0
#if NV_OVERRIDE_ALLOC
// Custom memory allocator
extern "C" {
NVCORE_API void * malloc(size_t size);
NVCORE_API void * debug_malloc(size_t size, const char * file, int line);
NVCORE_API void free(void * ptr);
NVCORE_API void * realloc(void * ptr, size_t size);
}
/*
#ifdef _DEBUG
#define new new(__FILE__, __LINE__)
#define malloc(i) debug_malloc(i, __FILE__, __LINE__)
#endif
*/
#endif
2010-10-09 07:45:48 +00:00
namespace nv {
2018-02-06 02:55:07 +00:00
NVCORE_API void * aligned_malloc(size_t size, size_t alignment);
NVCORE_API void aligned_free(void * );
2010-10-09 07:45:48 +00:00
// C++ helpers.
2011-09-27 17:48:46 +00:00
template <typename T> NV_FORCEINLINE T * malloc(size_t count) {
2010-10-09 07:45:48 +00:00
return (T *)::malloc(sizeof(T) * count);
}
2011-09-27 17:48:46 +00:00
template <typename T> NV_FORCEINLINE T * realloc(T * ptr, size_t count) {
2010-10-09 07:45:48 +00:00
return (T *)::realloc(ptr, sizeof(T) * count);
}
2011-09-27 17:48:46 +00:00
template <typename T> NV_FORCEINLINE void free(const T * ptr) {
2010-10-21 18:44:10 +00:00
::free((void *)ptr);
2010-10-09 07:45:48 +00:00
}
2014-11-04 17:49:29 +00:00
template <typename T> NV_FORCEINLINE void zero(T & data) {
memset(&data, 0, sizeof(T));
}
2010-10-09 07:45:48 +00:00
} // nv namespace
#endif // NV_CORE_MEMORY_H