Mem.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <kopano/platform.h>
  18. #include <mapispi.h>
  19. #include <mapicode.h>
  20. #include <kopano/Trace.h>
  21. #include "Mem.h"
  22. // We don't want that here
  23. #undef ECAllocateBuffer
  24. LPMALLOC _pmalloc;
  25. LPALLOCATEBUFFER _pfnAllocBuf;
  26. LPALLOCATEMORE _pfnAllocMore;
  27. LPFREEBUFFER _pfnFreeBuf;
  28. HINSTANCE _hInstance;
  29. // This is the same as client-side MAPIFreeBuffer, but uses
  30. // the linked memory routines passed in during MSProviderInit()
  31. // Use the EC* functions to allocate memory that will be
  32. // passed back to the caller through MAPI
  33. HRESULT ECFreeBuffer(void *lpvoid) {
  34. if(_pfnFreeBuf == NULL)
  35. return MAPI_E_CALL_FAILED;
  36. else return _pfnFreeBuf(lpvoid);
  37. }
  38. HRESULT ECAllocateBuffer(ULONG cbSize, void **lpvoid) {
  39. if(_pfnAllocBuf == NULL)
  40. return MAPI_E_CALL_FAILED;
  41. else return _pfnAllocBuf(cbSize, lpvoid);
  42. }
  43. HRESULT ECAllocateMore(ULONG cbSize, void *lpBase, void **lpvoid) {
  44. if(_pfnAllocMore == NULL)
  45. return MAPI_E_CALL_FAILED;
  46. else return _pfnAllocMore(cbSize, lpBase, lpvoid);
  47. }
  48. HRESULT AllocNewMapiObject(ULONG ulUniqueId, ULONG ulObjId, ULONG ulObjType, MAPIOBJECT **lppMapiObject)
  49. {
  50. auto sMapiObject = new MAPIOBJECT;
  51. sMapiObject->ulUniqueId = ulUniqueId;
  52. sMapiObject->ulObjId = ulObjId;
  53. sMapiObject->ulObjType = ulObjType;
  54. *lppMapiObject = sMapiObject;
  55. return hrSuccess;
  56. }
  57. HRESULT FreeMapiObject(MAPIOBJECT *lpsObject)
  58. {
  59. for (const auto &obj : lpsObject->lstChildren)
  60. FreeMapiObject(obj);
  61. if (lpsObject->lpInstanceID)
  62. ECFreeBuffer(lpsObject->lpInstanceID);
  63. delete lpsObject;
  64. return hrSuccess;
  65. }