Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
708 B

  1. #ifndef __MEMORYTEMPLATES_H__
  2. #define __MEMORYTEMPLATES_H__
  3. template <class T>
  4. inline HRESULT SHLocalAlloc(ULONG cb, T **ppv)
  5. {
  6. *ppv = (T *) LocalAlloc(LPTR, cb);
  7. return *ppv ? S_OK : E_OUTOFMEMORY;
  8. }
  9. template <class T>
  10. inline HRESULT SHCoAlloc(ULONG cb, T **ppv)
  11. {
  12. *ppv = (T *) CoTaskMemAlloc(cb);
  13. return *ppv ? S_OK : E_OUTOFMEMORY;
  14. }
  15. template <class T>
  16. class CSmartCoTaskMem
  17. {
  18. protected:
  19. T *p;
  20. public:
  21. CSmartCoTaskMem() { p = NULL; }
  22. ~CSmartCoTaskMem() { if (p) CoTaskMemFree(p); }
  23. T** operator&()
  24. {
  25. ASSERT(p==NULL);
  26. return &p;
  27. }
  28. operator T*() { return p; }
  29. };
  30. #endif // __MEMORYTEMPLATES_H__