memory.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Memory management routine
  2. Copyright (C) 1998 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #ifndef _ZEBRA_MEMORY_H
  17. #define _ZEBRA_MEMORY_H
  18. /* For pretty printing of memory allocate information. */
  19. struct memory_list
  20. {
  21. int index;
  22. const char *format;
  23. };
  24. struct mlist {
  25. struct memory_list *list;
  26. const char *name;
  27. };
  28. #include "lib/memtypes.h"
  29. extern struct mlist mlists[];
  30. /* #define MEMORY_LOG */
  31. #ifdef MEMORY_LOG
  32. #define XMALLOC(mtype, size) \
  33. mtype_zmalloc (__FILE__, __LINE__, (mtype), (size))
  34. #define XCALLOC(mtype, size) \
  35. mtype_zcalloc (__FILE__, __LINE__, (mtype), (size))
  36. #define XREALLOC(mtype, ptr, size) \
  37. mtype_zrealloc (__FILE__, __LINE__, (mtype), (ptr), (size))
  38. #define XFREE(mtype, ptr) \
  39. do { \
  40. mtype_zfree (__FILE__, __LINE__, (mtype), (ptr)); \
  41. ptr = NULL; } \
  42. while (0)
  43. #define XSTRDUP(mtype, str) \
  44. mtype_zstrdup (__FILE__, __LINE__, (mtype), (str))
  45. #else
  46. #define XMALLOC(mtype, size) zmalloc ((mtype), (size))
  47. #define XCALLOC(mtype, size) zcalloc ((mtype), (size))
  48. #define XREALLOC(mtype, ptr, size) zrealloc ((mtype), (ptr), (size))
  49. #define XFREE(mtype, ptr) do { \
  50. zfree ((mtype), (ptr)); \
  51. ptr = NULL; } \
  52. while (0)
  53. #define XSTRDUP(mtype, str) zstrdup ((mtype), (str))
  54. #endif /* MEMORY_LOG */
  55. /* Prototypes of memory function. */
  56. extern void *zmalloc (int type, size_t size);
  57. extern void *zcalloc (int type, size_t size);
  58. extern void *zrealloc (int type, void *ptr, size_t size);
  59. extern void zfree (int type, void *ptr);
  60. extern char *zstrdup (int type, const char *str);
  61. extern void *mtype_zmalloc (const char *file, int line, int type, size_t size);
  62. extern void *mtype_zcalloc (const char *file, int line, int type, size_t size);
  63. extern void *mtype_zrealloc (const char *file, int line, int type, void *ptr,
  64. size_t size);
  65. extern void mtype_zfree (const char *file, int line, int type,
  66. void *ptr);
  67. extern char *mtype_zstrdup (const char *file, int line, int type,
  68. const char *str);
  69. extern void memory_init (void);
  70. extern void log_memstats_stderr (const char *);
  71. /* return number of allocations outstanding for the type */
  72. extern unsigned long mtype_stats_alloc (int);
  73. /* Human friendly string for given byte count */
  74. #define MTYPE_MEMSTR_LEN 20
  75. extern const char *mtype_memstr (char *, size_t, unsigned long);
  76. #endif /* _ZEBRA_MEMORY_H */