paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Memory management routine |
| 2 | Copyright (C) 1998 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #ifndef _ZEBRA_MEMORY_H |
| 22 | #define _ZEBRA_MEMORY_H |
| 23 | |
David Lamparter | 80a21dc | 2012-05-08 13:32:12 +0200 | [diff] [blame] | 24 | #define array_size(ar) (sizeof(ar) / sizeof(ar[0])) |
| 25 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 26 | /* For pretty printing of memory allocate information. */ |
| 27 | struct memory_list |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | { |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 29 | int index; |
| 30 | const char *format; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 33 | struct mlist { |
| 34 | struct memory_list *list; |
| 35 | const char *name; |
| 36 | }; |
| 37 | |
paul | a7bca0f | 2005-04-27 12:44:54 +0000 | [diff] [blame] | 38 | #include "lib/memtypes.h" |
paul | e1e53ed | 2005-04-22 13:44:17 +0000 | [diff] [blame] | 39 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 40 | extern struct mlist mlists[]; |
| 41 | |
| 42 | /* #define MEMORY_LOG */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | #ifdef MEMORY_LOG |
| 44 | #define XMALLOC(mtype, size) \ |
| 45 | mtype_zmalloc (__FILE__, __LINE__, (mtype), (size)) |
| 46 | #define XCALLOC(mtype, size) \ |
| 47 | mtype_zcalloc (__FILE__, __LINE__, (mtype), (size)) |
| 48 | #define XREALLOC(mtype, ptr, size) \ |
| 49 | mtype_zrealloc (__FILE__, __LINE__, (mtype), (ptr), (size)) |
| 50 | #define XFREE(mtype, ptr) \ |
David Ward | 0917f7e | 2010-01-13 20:10:56 +0300 | [diff] [blame] | 51 | do { \ |
| 52 | mtype_zfree (__FILE__, __LINE__, (mtype), (ptr)); \ |
| 53 | ptr = NULL; } \ |
| 54 | while (0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | #define XSTRDUP(mtype, str) \ |
| 56 | mtype_zstrdup (__FILE__, __LINE__, (mtype), (str)) |
| 57 | #else |
| 58 | #define XMALLOC(mtype, size) zmalloc ((mtype), (size)) |
Baruch Siach | 9ed99f0 | 2016-08-21 09:23:05 +0300 | [diff] [blame^] | 59 | #define XCALLOC(mtype, size) zzcalloc ((mtype), (size)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | #define XREALLOC(mtype, ptr, size) zrealloc ((mtype), (ptr), (size)) |
Paul Jakma | 8ce5cfd | 2006-06-15 12:41:02 +0000 | [diff] [blame] | 61 | #define XFREE(mtype, ptr) do { \ |
| 62 | zfree ((mtype), (ptr)); \ |
| 63 | ptr = NULL; } \ |
| 64 | while (0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 65 | #define XSTRDUP(mtype, str) zstrdup ((mtype), (str)) |
| 66 | #endif /* MEMORY_LOG */ |
| 67 | |
| 68 | /* Prototypes of memory function. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 69 | extern void *zmalloc (int type, size_t size); |
Baruch Siach | 9ed99f0 | 2016-08-21 09:23:05 +0300 | [diff] [blame^] | 70 | extern void *zzcalloc (int type, size_t size); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 71 | extern void *zrealloc (int type, void *ptr, size_t size); |
| 72 | extern void zfree (int type, void *ptr); |
| 73 | extern char *zstrdup (int type, const char *str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 74 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 75 | extern void *mtype_zmalloc (const char *file, int line, int type, size_t size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | |
David Ward | 0917f7e | 2010-01-13 20:10:56 +0300 | [diff] [blame] | 77 | extern void *mtype_zcalloc (const char *file, int line, int type, size_t size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 79 | extern void *mtype_zrealloc (const char *file, int line, int type, void *ptr, |
| 80 | size_t size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 82 | extern void mtype_zfree (const char *file, int line, int type, |
| 83 | void *ptr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 85 | extern char *mtype_zstrdup (const char *file, int line, int type, |
| 86 | const char *str); |
| 87 | extern void memory_init (void); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 88 | extern void log_memstats_stderr (const char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 90 | /* return number of allocations outstanding for the type */ |
| 91 | extern unsigned long mtype_stats_alloc (int); |
| 92 | |
| 93 | /* Human friendly string for given byte count */ |
| 94 | #define MTYPE_MEMSTR_LEN 20 |
| 95 | extern const char *mtype_memstr (char *, size_t, unsigned long); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | #endif /* _ZEBRA_MEMORY_H */ |