memory.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #define array_size(ar) (sizeof(ar) / sizeof(ar[0]))
  19. /* For pretty printing of memory allocate information. */
  20. struct memory_list
  21. {
  22. int index;
  23. const char *format;
  24. };
  25. struct mlist {
  26. struct memory_list *list;
  27. const char *name;
  28. };
  29. #include "lib/memtypes.h"
  30. extern struct mlist mlists[];
  31. /* #define MEMORY_LOG */
  32. #ifdef MEMORY_LOG
  33. #define XMALLOC(mtype, size) \
  34. mtype_zmalloc (__FILE__, __LINE__, (mtype), (size))
  35. #define XCALLOC(mtype, size) \
  36. mtype_zcalloc (__FILE__, __LINE__, (mtype), (size))
  37. #define XREALLOC(mtype, ptr, size) \
  38. mtype_zrealloc (__FILE__, __LINE__, (mtype), (ptr), (size))
  39. #define XFREE(mtype, ptr) \
  40. do { \
  41. mtype_zfree (__FILE__, __LINE__, (mtype), (ptr)); \
  42. (ptr) = NULL; } \
  43. while (0)
  44. #define XSTRDUP(mtype, str) \
  45. mtype_zstrdup (__FILE__, __LINE__, (mtype), (str))
  46. #else
  47. #define XMALLOC(mtype, size) zmalloc ((mtype), (size))
  48. #define XCALLOC(mtype, size) zzcalloc ((mtype), (size))
  49. #define XREALLOC(mtype, ptr, size) zrealloc ((mtype), (ptr), (size))
  50. #define XFREE(mtype, ptr) do { \
  51. zfree ((mtype), (ptr)); \
  52. (ptr) = NULL; } \
  53. while (0)
  54. #define XSTRDUP(mtype, str) zstrdup ((mtype), (str))
  55. #endif /* MEMORY_LOG */
  56. /* Prototypes of memory function. */
  57. extern void *zmalloc (int type, size_t size);
  58. extern void *zzcalloc (int type, size_t size);
  59. extern void *zrealloc (int type, void *ptr, size_t size);
  60. extern void zfree (int type, void *ptr);
  61. extern char *zstrdup (int type, const char *str);
  62. extern void *mtype_zmalloc (const char *file, int line, int type, size_t size);
  63. extern void *mtype_zcalloc (const char *file, int line, int type, size_t size);
  64. extern void *mtype_zrealloc (const char *file, int line, int type, void *ptr,
  65. size_t size);
  66. extern void mtype_zfree (const char *file, int line, int type,
  67. void *ptr);
  68. extern char *mtype_zstrdup (const char *file, int line, int type,
  69. const char *str);
  70. extern void memory_init (void);
  71. extern void log_memstats_stderr (const char *);
  72. /* return number of allocations outstanding for the type */
  73. extern unsigned long mtype_stats_alloc (int);
  74. /* Human friendly string for given byte count */
  75. #define MTYPE_MEMSTR_LEN 20
  76. extern const char *mtype_memstr (char *, size_t, unsigned long);
  77. #endif /* _ZEBRA_MEMORY_H */