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.

128 lines
3.8 KiB

  1. /**
  2. @file
  3. @ingroup util
  4. @brief Interface routines to be placed between a program and the
  5. system memory allocator.
  6. The function pointer MMoutOfMemory() contains a vector to handle a
  7. 'out-of-memory' error (which, by default, points at a simple wrap-up
  8. and exit routine).
  9. @copyright@parblock
  10. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  11. All rights reserved.
  12. Permission is hereby granted, without written agreement and without license
  13. or royalty fees, to use, copy, modify, and distribute this software and its
  14. documentation for any purpose, provided that the above copyright notice and
  15. the following two paragraphs appear in all copies of this software.
  16. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  21. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  23. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  24. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  25. @endparblock
  26. @copyright@parblock
  27. Copyright (c) 1999-2015, Regents of the University of Colorado
  28. All rights reserved.
  29. Redistribution and use in source and binary forms, with or without
  30. modification, are permitted provided that the following conditions
  31. are met:
  32. Redistributions of source code must retain the above copyright
  33. notice, this list of conditions and the following disclaimer.
  34. Redistributions in binary form must reproduce the above copyright
  35. notice, this list of conditions and the following disclaimer in the
  36. documentation and/or other materials provided with the distribution.
  37. Neither the name of the University of Colorado nor the names of its
  38. contributors may be used to endorse or promote products derived from
  39. this software without specific prior written permission.
  40. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  41. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  42. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  43. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  44. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  45. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  46. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  48. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  50. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  51. POSSIBILITY OF SUCH DAMAGE.
  52. @endparblock
  53. */
  54. #include "util.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**
  59. * @brief Global out-of-memory handler.
  60. */
  61. void (*MMoutOfMemory)(size_t) = MMout_of_memory;
  62. #ifdef __cplusplus
  63. }
  64. #endif
  65. /**
  66. * @brief Out of memory for lazy people: flush and exit.
  67. */
  68. void
  69. MMout_of_memory(size_t size)
  70. {
  71. (void) fflush(stdout);
  72. (void) fprintf(stderr,
  73. "\nCUDD: out of memory allocating %" PRIszt " bytes\n",
  74. (size_t) size);
  75. exit(1);
  76. }
  77. /**
  78. * @brief malloc replacement.
  79. */
  80. void *
  81. MMalloc(size_t size)
  82. {
  83. void *p;
  84. if ((p = malloc(size)) == NIL(void)) {
  85. if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
  86. return NIL(void);
  87. }
  88. return p;
  89. }
  90. /**
  91. * @brief realloc replacement.
  92. */
  93. void *
  94. MMrealloc(void *obj, size_t size)
  95. {
  96. void *p;
  97. if ((p = realloc(obj, size)) == NIL(void)) {
  98. if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
  99. return NIL(void);
  100. }
  101. return p;
  102. }