]>
git.proxmox.com Git - mirror_frr.git/blob - lib/memory.c
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
29 static struct memgroup
*mg_first
= NULL
;
30 struct memgroup
**mg_insert
= &mg_first
;
32 DEFINE_MGROUP(LIB
, "libzebra")
33 DEFINE_MTYPE(LIB
, TMP
, "Temporary memory")
36 mt_count_alloc (struct memtype
*mt
, size_t size
)
42 else if (mt
->size
!= size
)
47 mt_count_free (struct memtype
*mt
)
53 mt_checkalloc (struct memtype
*mt
, void *ptr
, size_t size
)
55 if (__builtin_expect(ptr
== NULL
, 0))
57 memory_oom (size
, mt
->name
);
60 mt_count_alloc (mt
, size
);
65 qmalloc (struct memtype
*mt
, size_t size
)
67 return mt_checkalloc (mt
, malloc (size
), size
);
71 qcalloc (struct memtype
*mt
, size_t size
)
73 return mt_checkalloc (mt
, calloc (size
, 1), size
);
77 qrealloc (struct memtype
*mt
, void *ptr
, size_t size
)
81 return mt_checkalloc (mt
, ptr
? realloc (ptr
, size
) : malloc (size
), size
);
85 qstrdup (struct memtype
*mt
, const char *str
)
87 return mt_checkalloc (mt
, strdup (str
), strlen (str
) + 1);
91 qfree (struct memtype
*mt
, void *ptr
)
99 qmem_walk (qmem_walk_fn
*func
, void *arg
)
105 for (mg
= mg_first
; mg
; mg
= mg
->next
)
107 if ((rv
= func (arg
, mg
, NULL
)))
109 for (mt
= mg
->types
; mt
; mt
= mt
->next
)
110 if ((rv
= func (arg
, mg
, mt
)))
116 struct exit_dump_args
123 qmem_exit_walker (void *arg
, struct memgroup
*mg
, struct memtype
*mt
)
125 struct exit_dump_args
*eda
= arg
;
129 fprintf (stderr
, "%s: showing active allocations in memory group %s\n",
130 eda
->prefix
, mg
->name
);
132 else if (mt
->n_alloc
)
136 snprintf (size
, sizeof (size
), "%10zu", mt
->size
);
137 fprintf (stderr
, "%s: memstats: %-30s: %6zu * %s\n",
138 eda
->prefix
, mt
->name
, mt
->n_alloc
,
139 mt
->size
== SIZE_VAR
? "(variably sized)" : size
);
145 log_memstats_stderr (const char *prefix
)
147 struct exit_dump_args eda
= { .prefix
= prefix
, .error
= 0 };
148 qmem_walk (qmem_exit_walker
, &eda
);