KSMemory.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // KSMemory.h
  3. //
  4. // Created by Karl Stenerud on 2012-01-29.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. /* Utility functions for querying the mach kernel.
  27. */
  28. #ifndef HDR_ksmemory_h
  29. #define HDR_ksmemory_h
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include <stdbool.h>
  34. /** Test if the specified memory is safe to read from.
  35. *
  36. * @param memory A pointer to the memory to test.
  37. * @param byteCount The number of bytes to test.
  38. *
  39. * @return True if the memory can be safely read.
  40. */
  41. bool ksmem_isMemoryReadable(const void* const memory, const int byteCount);
  42. /** Test how much memory is readable from the specified pointer.
  43. *
  44. * @param memory A pointer to the memory to test.
  45. * @param tryByteCount The number of bytes to test.
  46. *
  47. * @return The number of bytes that are readable from that address.
  48. */
  49. int ksmem_maxReadableBytes(const void* const memory, const int tryByteCount);
  50. /** Copy memory safely. If the memory is not accessible, returns false
  51. * rather than crashing.
  52. *
  53. * @param src The source location to copy from.
  54. *
  55. * @param dst The location to copy to.
  56. *
  57. * @param byteCount The number of bytes to copy.
  58. *
  59. * @return true if successful.
  60. */
  61. bool ksmem_copySafely(const void* restrict const src, void* restrict const dst, int byteCount);
  62. /** Copies up to numBytes of data from src to dest, stopping if memory
  63. * becomes inaccessible.
  64. *
  65. * @param src The source location to copy from.
  66. *
  67. * @param dst The location to copy to.
  68. *
  69. * @param byteCount The number of bytes to copy.
  70. *
  71. * @return The number of bytes actually copied.
  72. */
  73. int ksmem_copyMaxPossible(const void* restrict const src, void* restrict const dst, int byteCount);
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif // HDR_ksmemory_h