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.

xmalloc.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*\
  19. * xmalloc.c
  20. *
  21. * Shamelessly ripped from openssh and xcalloc added
  22. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  23. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  24. * All rights reserved
  25. * Versions of malloc and friends that check their results, and never return
  26. * failure (they call fatal if they encounter an error).
  27. *
  28. * Changes:
  29. *
  30. * xstrndup() added. AlpT
  31. * xfree() modified to _xfree(). AlpT
  32. * xzalloc(size_t size) added.
  33. \*/
  34. #include <sys/types.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "xmalloc.h"
  38. #include "log.h"
  39. #ifndef USE_DMALLOC
  40. void *
  41. xmalloc(size_t size)
  42. {
  43. void *ptr;
  44. if (!size)
  45. fatal("xmalloc: zero size");
  46. ptr = malloc(size);
  47. if (!ptr)
  48. fatal("xmalloc: out of memory (allocating %lu bytes)",
  49. (u_long) size);
  50. return ptr;
  51. }
  52. /*
  53. * xzalloc
  54. *
  55. * Mallocs `size' bytes and sets them to zero
  56. */
  57. void *
  58. xzalloc(size_t size)
  59. {
  60. void *ptr;
  61. ptr = xmalloc(size);
  62. memset(ptr, 0, size);
  63. return ptr;
  64. }
  65. void *
  66. xcalloc(size_t nmemb, size_t size)
  67. {
  68. void *ptr;
  69. if (!size || !nmemb)
  70. fatal("xcalloc: zero size");
  71. ptr = calloc(nmemb, size);
  72. if (!ptr)
  73. fatal("xcalloc: out of memory (allocating %lu bytes * %lu blocks)",
  74. (u_long) size, (u_long) nmemb);
  75. return ptr;
  76. }
  77. void *
  78. xrealloc(void *ptr, size_t new_size)
  79. {
  80. void *new_ptr;
  81. if (!new_size)
  82. fatal("xrealloc: zero size");
  83. if (!ptr)
  84. new_ptr = malloc(new_size);
  85. else
  86. new_ptr = realloc(ptr, new_size);
  87. if (!new_ptr)
  88. fatal("xrealloc: out of memory (new_size %lu bytes)",
  89. (u_long) new_size);
  90. return new_ptr;
  91. }
  92. void
  93. _xfree(void *ptr)
  94. {
  95. if (!ptr)
  96. fatal("xfree: NULL pointer given as argument");
  97. free(ptr);
  98. }
  99. char *
  100. xstrndup(const char *str, size_t n)
  101. {
  102. size_t len;
  103. char *cp;
  104. len = strlen(str) + 1;
  105. if (len > n && n > 0)
  106. len = n;
  107. cp = xmalloc(len);
  108. strncpy(cp, str, len);
  109. return cp;
  110. }
  111. char *
  112. xstrdup(const char *str)
  113. {
  114. return xstrndup(str, 0);
  115. }
  116. #endif /*USE_DMALLOC */