/contrib/groff/src/libs/libgroff/new.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 71 lines · 44 code · 7 blank · 20 comment · 7 complexity · 69c5aaef2d9501a5f77bfcca2e2f4dc8 MD5 · raw file

  1. /* Copyright (C) 1989, 1990, 1991, 1992, 2001, 2003, 2004
  2. Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.com)
  4. This file is part of groff.
  5. groff is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with groff; see the file COPYING. If not, write to the Free Software
  15. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include "lib.h"
  17. #include <stddef.h>
  18. #include <stdlib.h>
  19. #include "posix.h"
  20. #include "nonposix.h"
  21. extern "C" const char *program_name;
  22. static void ewrite(const char *s)
  23. {
  24. write(2, s, strlen(s));
  25. }
  26. void *operator new(size_t size)
  27. {
  28. // Avoid relying on the behaviour of malloc(0).
  29. if (size == 0)
  30. size++;
  31. #ifdef COOKIE_BUG
  32. char *p = (char *)malloc(unsigned(size + 8));
  33. #else /* not COOKIE_BUG */
  34. char *p = (char *)malloc(unsigned(size));
  35. #endif /* not COOKIE_BUG */
  36. if (p == 0) {
  37. if (program_name) {
  38. ewrite(program_name);
  39. ewrite(": ");
  40. }
  41. ewrite("out of memory\n");
  42. _exit(-1);
  43. }
  44. #ifdef COOKIE_BUG
  45. ((unsigned *)p)[1] = 0;
  46. return p + 8;
  47. #else /* not COOKIE_BUG */
  48. return p;
  49. #endif /* not COOKIE_BUG */
  50. }
  51. void operator delete(void *p)
  52. {
  53. #ifdef COOKIE_BUG
  54. if (p)
  55. free((void *)((char *)p - 8));
  56. #else
  57. if (p)
  58. free(p);
  59. #endif /* COOKIE_BUG */
  60. }