memory.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright (c) 2006-2020 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "config.h"
  21. #include "memory.h"
  22. #include <stdlib.h>
  23. #ifdef LOVE_WINDOWS
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <malloc.h>
  26. #include <Windows.h>
  27. #else
  28. #include <unistd.h> // Assume POSIX support.
  29. #endif
  30. namespace love
  31. {
  32. bool alignedMalloc(void **mem, size_t size, size_t alignment)
  33. {
  34. #ifdef LOVE_WINDOWS
  35. *mem = _aligned_malloc(size, alignment);
  36. return *mem != nullptr;
  37. #else
  38. return posix_memalign(mem, alignment, size) == 0;
  39. #endif
  40. }
  41. void alignedFree(void *mem)
  42. {
  43. #ifdef LOVE_WINDOWS
  44. _aligned_free(mem);
  45. #else
  46. free(mem);
  47. #endif
  48. }
  49. size_t getPageSize()
  50. {
  51. #ifdef LOVE_WINDOWS
  52. static DWORD size = 0;
  53. if (size == 0)
  54. {
  55. SYSTEM_INFO si;
  56. GetSystemInfo(&si);
  57. size = si.dwPageSize;
  58. }
  59. return (size_t) size;
  60. #else
  61. static const long size = sysconf(_SC_PAGESIZE);
  62. return size > 0 ? (size_t) size : 4096;
  63. #endif
  64. }
  65. size_t alignUp(size_t size, size_t alignment)
  66. {
  67. return (size + alignment - 1) & (~(alignment - 1));
  68. }
  69. } // love