Allocator.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "il2cpp-config.h"
  2. #include "Allocator.h"
  3. static allocate_func s_AllocatorFunc;
  4. static free_func s_ReleaseFunc;
  5. extern "C"
  6. {
  7. void register_allocator(allocate_func allocator, free_func release)
  8. {
  9. s_AllocatorFunc = allocator;
  10. s_ReleaseFunc = release;
  11. }
  12. void free_memory(void* memory)
  13. {
  14. Allocator::Free(memory);
  15. }
  16. }
  17. void* Allocator::Allocate(size_t size)
  18. {
  19. IL2CPP_ASSERT(s_AllocatorFunc);
  20. return s_AllocatorFunc(size);
  21. }
  22. void Allocator::Free(void* memory)
  23. {
  24. IL2CPP_ASSERT(s_ReleaseFunc);
  25. s_ReleaseFunc(memory);
  26. }
  27. char* Allocator::CopyToAllocatedStringBuffer(const std::string& input)
  28. {
  29. size_t size = input.size();
  30. char* buffer = (char*)Allocator::Allocate(size + 1);
  31. input.copy(buffer, size);
  32. buffer[size] = '\0';
  33. return buffer;
  34. }
  35. char* Allocator::CopyToAllocatedStringBuffer(const char* input)
  36. {
  37. size_t size = strlen(input);
  38. char* buffer = (char*)Allocator::Allocate(size + 1);
  39. strcpy(buffer, input);
  40. return buffer;
  41. }
  42. void Allocator::CopyStringVectorToNullTerminatedArray(const std::vector<std::string>& input, void*** output)
  43. {
  44. if (output != NULL)
  45. {
  46. size_t numberOfAddresses = input.size();
  47. *output = (void**)Allocate(sizeof(void*) * (numberOfAddresses + 1));
  48. for (size_t i = 0; i < numberOfAddresses; ++i)
  49. (*output)[i] = CopyToAllocatedStringBuffer(input[i].c_str());
  50. (*output)[numberOfAddresses] = NULL;
  51. }
  52. }
  53. void Allocator::CopyDataVectorToNullTerminatedArray(const std::vector<void*>& input, void*** output, int32_t elementSize)
  54. {
  55. if (output != NULL)
  56. {
  57. size_t numberOfEntries = input.size();
  58. *output = (void**)Allocate(sizeof(void*) * (numberOfEntries + 1));
  59. for (size_t i = 0; i < numberOfEntries; ++i)
  60. {
  61. (*output)[i] = (void*)Allocate(elementSize);
  62. memcpy((*output)[i], input[i], elementSize);
  63. }
  64. (*output)[numberOfEntries] = NULL;
  65. }
  66. }