Memory.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
  2. #pragma once
  3. #ifndef NV_CORE_MEMORY_H
  4. #define NV_CORE_MEMORY_H
  5. #include "nvcore.h"
  6. #include <stdlib.h> // malloc(), realloc() and free()
  7. #include <string.h> // memset
  8. //#include <stddef.h> // size_t
  9. //#include <new> // new and delete
  10. #define TRACK_MEMORY_LEAKS 0
  11. #if TRACK_MEMORY_LEAKS
  12. #include <vld.h>
  13. #endif
  14. #if NV_CC_GNUC
  15. # define NV_ALIGN_16 __attribute__ ((__aligned__ (16)))
  16. #else
  17. # define NV_ALIGN_16 __declspec(align(16))
  18. #endif
  19. #define NV_OVERRIDE_ALLOC 0
  20. #if NV_OVERRIDE_ALLOC
  21. // Custom memory allocator
  22. extern "C" {
  23. NVCORE_API void * malloc(size_t size);
  24. NVCORE_API void * debug_malloc(size_t size, const char * file, int line);
  25. NVCORE_API void free(void * ptr);
  26. NVCORE_API void * realloc(void * ptr, size_t size);
  27. }
  28. /*
  29. #ifdef _DEBUG
  30. #define new new(__FILE__, __LINE__)
  31. #define malloc(i) debug_malloc(i, __FILE__, __LINE__)
  32. #endif
  33. */
  34. #endif
  35. namespace nv {
  36. NVCORE_API void * aligned_malloc(size_t size, size_t alignment);
  37. NVCORE_API void aligned_free(void * );
  38. // C++ helpers.
  39. template <typename T> NV_FORCEINLINE T * malloc(size_t count) {
  40. return (T *)::malloc(sizeof(T) * count);
  41. }
  42. template <typename T> NV_FORCEINLINE T * realloc(T * ptr, size_t count) {
  43. return (T *)::realloc(ptr, sizeof(T) * count);
  44. }
  45. template <typename T> NV_FORCEINLINE void free(const T * ptr) {
  46. ::free((void *)ptr);
  47. }
  48. template <typename T> NV_FORCEINLINE void zero(T & data) {
  49. memset(&data, 0, sizeof(T));
  50. }
  51. } // nv namespace
  52. #endif // NV_CORE_MEMORY_H