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.

45 lines
1.9 KiB

  1. /****************************************************************************************[XAlloc.h]
  2. Copyright (c) 2009-2010, Niklas Sorensson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. associated documentation files (the "Software"), to deal in the Software without restriction,
  5. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies or
  9. substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  11. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  13. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  14. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. **************************************************************************************************/
  16. #ifndef Minisat_XAlloc_h
  17. #define Minisat_XAlloc_h
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. namespace Minisat {
  21. //=================================================================================================
  22. // Simple layer on top of malloc/realloc to catch out-of-memory situtaions and provide some typing:
  23. class OutOfMemoryException{};
  24. static inline void* xrealloc(void *ptr, size_t size)
  25. {
  26. void* mem = realloc(ptr, size);
  27. if (mem == NULL && errno == ENOMEM){
  28. throw OutOfMemoryException();
  29. }else
  30. return mem;
  31. }
  32. //=================================================================================================
  33. }
  34. #endif