SOAPAlloc.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef SOAPALLOC_H
  18. #define SOAPALLOC_H
  19. #include <new>
  20. #include "soapH.h"
  21. namespace KC {
  22. // The automatic soap/non-soap allocator
  23. template<typename Type> Type *s_alloc_nothrow(struct soap *soap, size_t size)
  24. {
  25. return reinterpret_cast<Type *>(soap_malloc(soap, sizeof(Type) * size));
  26. }
  27. template<typename Type> Type *s_alloc_nothrow(struct soap *soap)
  28. {
  29. return reinterpret_cast<Type *>(soap_malloc(soap, sizeof(Type)));
  30. }
  31. template<typename Type> Type *s_alloc(struct soap *soap, size_t size)
  32. {
  33. auto p = reinterpret_cast<Type *>(soap_malloc(soap, sizeof(Type) * size));
  34. if (p == nullptr)
  35. throw std::bad_alloc();
  36. return p;
  37. }
  38. template<typename Type> Type *s_alloc(struct soap *soap)
  39. {
  40. auto p = reinterpret_cast<Type *>(soap_malloc(soap, sizeof(Type)));
  41. if (p == nullptr)
  42. throw std::bad_alloc();
  43. return p;
  44. }
  45. inline char *s_strcpy(struct soap *soap, const char *str) {
  46. char *s = s_alloc<char>(soap, strlen(str)+1);
  47. strcpy(s, str);
  48. return s;
  49. }
  50. inline char *s_memcpy(struct soap *soap, const char *str, unsigned int len) {
  51. char *s = s_alloc<char>(soap, len);
  52. memcpy(s, str, len);
  53. return s;
  54. }
  55. template<typename Type>
  56. inline void s_free(struct soap *soap, Type *p) {
  57. /*
  58. * Horrible implementation detail because gsoap does not expose
  59. * a proper function that is completely symmetric to soap_malloc.
  60. */
  61. if (soap == NULL)
  62. SOAP_FREE(soap, p);
  63. else
  64. soap_dealloc(soap, p);
  65. }
  66. } /* namespace */
  67. #endif