blob: 501352993d211b76aad1f4b0bceddf14e9b9d956 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Memory management routine
2 Copyright (C) 1998 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#ifndef _ZEBRA_MEMORY_H
22#define _ZEBRA_MEMORY_H
23
David Lamparter80a21dc2012-05-08 13:32:12 +020024#define array_size(ar) (sizeof(ar) / sizeof(ar[0]))
25
paul2fd2fd52005-04-15 11:47:15 +000026/* For pretty printing of memory allocate information. */
27struct memory_list
paul718e3742002-12-13 20:15:29 +000028{
paul2fd2fd52005-04-15 11:47:15 +000029 int index;
30 const char *format;
paul718e3742002-12-13 20:15:29 +000031};
32
paul2fd2fd52005-04-15 11:47:15 +000033struct mlist {
34 struct memory_list *list;
35 const char *name;
36};
37
paula7bca0f2005-04-27 12:44:54 +000038#include "lib/memtypes.h"
paule1e53ed2005-04-22 13:44:17 +000039
paul2fd2fd52005-04-15 11:47:15 +000040extern struct mlist mlists[];
41
42/* #define MEMORY_LOG */
paul718e3742002-12-13 20:15:29 +000043#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 Ward0917f7e2010-01-13 20:10:56 +030051 do { \
52 mtype_zfree (__FILE__, __LINE__, (mtype), (ptr)); \
53 ptr = NULL; } \
54 while (0)
paul718e3742002-12-13 20:15:29 +000055#define XSTRDUP(mtype, str) \
56 mtype_zstrdup (__FILE__, __LINE__, (mtype), (str))
57#else
58#define XMALLOC(mtype, size) zmalloc ((mtype), (size))
Baruch Siach9ed99f02016-08-21 09:23:05 +030059#define XCALLOC(mtype, size) zzcalloc ((mtype), (size))
paul718e3742002-12-13 20:15:29 +000060#define XREALLOC(mtype, ptr, size) zrealloc ((mtype), (ptr), (size))
Paul Jakma8ce5cfd2006-06-15 12:41:02 +000061#define XFREE(mtype, ptr) do { \
62 zfree ((mtype), (ptr)); \
63 ptr = NULL; } \
64 while (0)
paul718e3742002-12-13 20:15:29 +000065#define XSTRDUP(mtype, str) zstrdup ((mtype), (str))
66#endif /* MEMORY_LOG */
67
68/* Prototypes of memory function. */
paul8cc41982005-05-06 21:25:49 +000069extern void *zmalloc (int type, size_t size);
Baruch Siach9ed99f02016-08-21 09:23:05 +030070extern void *zzcalloc (int type, size_t size);
paul8cc41982005-05-06 21:25:49 +000071extern void *zrealloc (int type, void *ptr, size_t size);
72extern void zfree (int type, void *ptr);
73extern char *zstrdup (int type, const char *str);
paul718e3742002-12-13 20:15:29 +000074
paul8cc41982005-05-06 21:25:49 +000075extern void *mtype_zmalloc (const char *file, int line, int type, size_t size);
paul718e3742002-12-13 20:15:29 +000076
David Ward0917f7e2010-01-13 20:10:56 +030077extern void *mtype_zcalloc (const char *file, int line, int type, size_t size);
paul718e3742002-12-13 20:15:29 +000078
paul8cc41982005-05-06 21:25:49 +000079extern void *mtype_zrealloc (const char *file, int line, int type, void *ptr,
80 size_t size);
paul718e3742002-12-13 20:15:29 +000081
paul8cc41982005-05-06 21:25:49 +000082extern void mtype_zfree (const char *file, int line, int type,
83 void *ptr);
paul718e3742002-12-13 20:15:29 +000084
paul8cc41982005-05-06 21:25:49 +000085extern char *mtype_zstrdup (const char *file, int line, int type,
86 const char *str);
87extern void memory_init (void);
Chris Caputo228da422009-07-18 05:44:03 +000088extern void log_memstats_stderr (const char *);
paul718e3742002-12-13 20:15:29 +000089
Paul Jakma41be32b2006-03-30 13:53:59 +000090/* return number of allocations outstanding for the type */
91extern unsigned long mtype_stats_alloc (int);
92
93/* Human friendly string for given byte count */
94#define MTYPE_MEMSTR_LEN 20
95extern const char *mtype_memstr (char *, size_t, unsigned long);
paul718e3742002-12-13 20:15:29 +000096#endif /* _ZEBRA_MEMORY_H */