]>
git.proxmox.com Git - mirror_frr.git/blob - lib/memory.c
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 static struct memgroup
*mg_first
= NULL
;
24 struct memgroup
**mg_insert
= &mg_first
;
26 DEFINE_MGROUP(LIB
, "libzebra")
27 DEFINE_MTYPE(LIB
, TMP
, "Temporary memory")
30 mt_count_alloc (struct memtype
*mt
, size_t size
)
36 else if (mt
->size
!= size
)
41 mt_count_free (struct memtype
*mt
)
47 mt_checkalloc (struct memtype
*mt
, void *ptr
, size_t size
)
49 if (__builtin_expect(ptr
== NULL
, 0))
51 memory_oom (size
, mt
->name
);
54 mt_count_alloc (mt
, size
);
59 qmalloc (struct memtype
*mt
, size_t size
)
61 return mt_checkalloc (mt
, malloc (size
), size
);
65 qcalloc (struct memtype
*mt
, size_t size
)
67 return mt_checkalloc (mt
, calloc (size
, 1), size
);
71 qrealloc (struct memtype
*mt
, void *ptr
, size_t size
)
75 return mt_checkalloc (mt
, ptr
? realloc (ptr
, size
) : malloc (size
), size
);
79 qstrdup (struct memtype
*mt
, const char *str
)
81 return mt_checkalloc (mt
, strdup (str
), strlen (str
) + 1);
85 qfree (struct memtype
*mt
, void *ptr
)
93 qmem_walk (qmem_walk_fn
*func
, void *arg
)
99 for (mg
= mg_first
; mg
; mg
= mg
->next
)
101 if ((rv
= func (arg
, mg
, NULL
)))
103 for (mt
= mg
->types
; mt
; mt
= mt
->next
)
104 if ((rv
= func (arg
, mg
, mt
)))
110 struct exit_dump_args
117 qmem_exit_walker (void *arg
, struct memgroup
*mg
, struct memtype
*mt
)
119 struct exit_dump_args
*eda
= arg
;
123 fprintf (stderr
, "%s: showing active allocations in memory group %s\n",
124 eda
->prefix
, mg
->name
);
126 else if (mt
->n_alloc
)
130 snprintf (size
, sizeof (size
), "%10zu", mt
->size
);
131 fprintf (stderr
, "%s: memstats: %-30s: %6zu * %s\n",
132 eda
->prefix
, mt
->name
, mt
->n_alloc
,
133 mt
->size
== SIZE_VAR
? "(variably sized)" : size
);
139 log_memstats_stderr (const char *prefix
)
141 struct exit_dump_args eda
= { .prefix
= prefix
, .error
= 0 };
142 qmem_walk (qmem_exit_walker
, &eda
);