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 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 24 | /* For pretty printing of memory allocate information. */ |
| 25 | struct memory_list |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 26 | { |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 27 | int index; |
| 28 | const char *format; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 31 | struct mlist { |
| 32 | struct memory_list *list; |
| 33 | const char *name; |
| 34 | }; |
| 35 | |
paul | a7bca0f | 2005-04-27 12:44:54 +0000 | [diff] [blame] | 36 | #include "lib/memtypes.h" |
paul | e1e53ed | 2005-04-22 13:44:17 +0000 | [diff] [blame] | 37 | |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 38 | extern struct mlist mlists[]; |
| 39 | |
| 40 | /* #define MEMORY_LOG */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | #ifdef MEMORY_LOG |
| 42 | #define XMALLOC(mtype, size) \ |
| 43 | mtype_zmalloc (__FILE__, __LINE__, (mtype), (size)) |
| 44 | #define XCALLOC(mtype, size) \ |
| 45 | mtype_zcalloc (__FILE__, __LINE__, (mtype), (size)) |
| 46 | #define XREALLOC(mtype, ptr, size) \ |
| 47 | mtype_zrealloc (__FILE__, __LINE__, (mtype), (ptr), (size)) |
| 48 | #define XFREE(mtype, ptr) \ |
| 49 | mtype_zfree (__FILE__, __LINE__, (mtype), (ptr)) |
| 50 | #define XSTRDUP(mtype, str) \ |
| 51 | mtype_zstrdup (__FILE__, __LINE__, (mtype), (str)) |
| 52 | #else |
| 53 | #define XMALLOC(mtype, size) zmalloc ((mtype), (size)) |
| 54 | #define XCALLOC(mtype, size) zcalloc ((mtype), (size)) |
| 55 | #define XREALLOC(mtype, ptr, size) zrealloc ((mtype), (ptr), (size)) |
Paul Jakma | 8ce5cfd | 2006-06-15 12:41:02 +0000 | [diff] [blame^] | 56 | #define XFREE(mtype, ptr) do { \ |
| 57 | zfree ((mtype), (ptr)); \ |
| 58 | ptr = NULL; } \ |
| 59 | while (0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | #define XSTRDUP(mtype, str) zstrdup ((mtype), (str)) |
| 61 | #endif /* MEMORY_LOG */ |
| 62 | |
| 63 | /* Prototypes of memory function. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 64 | extern void *zmalloc (int type, size_t size); |
| 65 | extern void *zcalloc (int type, size_t size); |
| 66 | extern void *zrealloc (int type, void *ptr, size_t size); |
| 67 | extern void zfree (int type, void *ptr); |
| 68 | extern char *zstrdup (int type, const char *str); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 70 | 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] | 71 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 72 | extern void *mtype_zcalloc (const char *file, int line, int type, |
| 73 | size_t num, size_t size); |
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_zrealloc (const char *file, int line, int type, void *ptr, |
| 76 | size_t size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 78 | extern void mtype_zfree (const char *file, int line, int type, |
| 79 | void *ptr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 80 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 81 | extern char *mtype_zstrdup (const char *file, int line, int type, |
| 82 | const char *str); |
| 83 | extern void memory_init (void); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | |
Paul Jakma | 41be32b | 2006-03-30 13:53:59 +0000 | [diff] [blame] | 85 | /* return number of allocations outstanding for the type */ |
| 86 | extern unsigned long mtype_stats_alloc (int); |
| 87 | |
| 88 | /* Human friendly string for given byte count */ |
| 89 | #define MTYPE_MEMSTR_LEN 20 |
| 90 | extern const char *mtype_memstr (char *, size_t, unsigned long); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 91 | #endif /* _ZEBRA_MEMORY_H */ |