]>
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.
24 static struct memgroup
*mg_first
= NULL
;
25 struct memgroup
**mg_insert
= &mg_first
;
27 DEFINE_MGROUP(LIB
, "libfrr")
28 DEFINE_MTYPE(LIB
, TMP
, "Temporary memory")
29 DEFINE_MTYPE(LIB
, PREFIX_FLOWSPEC
, "Prefix Flowspec")
31 static inline void mt_count_alloc(struct memtype
*mt
, size_t size
)
35 atomic_fetch_add_explicit(&mt
->n_alloc
, 1, memory_order_relaxed
);
37 oldsize
= atomic_load_explicit(&mt
->size
, memory_order_relaxed
);
39 oldsize
= atomic_exchange_explicit(&mt
->size
, size
,
40 memory_order_relaxed
);
41 if (oldsize
!= 0 && oldsize
!= size
&& oldsize
!= SIZE_VAR
)
42 atomic_store_explicit(&mt
->size
, SIZE_VAR
,
43 memory_order_relaxed
);
46 static inline void mt_count_free(struct memtype
*mt
)
49 atomic_fetch_sub_explicit(&mt
->n_alloc
, 1, memory_order_relaxed
);
52 static inline void *mt_checkalloc(struct memtype
*mt
, void *ptr
, size_t size
)
54 if (__builtin_expect(ptr
== NULL
, 0)) {
56 /* malloc(0) is allowed to return NULL */
57 memory_oom(size
, mt
->name
);
61 mt_count_alloc(mt
, size
);
65 void *qmalloc(struct memtype
*mt
, size_t size
)
67 return mt_checkalloc(mt
, malloc(size
), size
);
70 void *qcalloc(struct memtype
*mt
, size_t size
)
72 return mt_checkalloc(mt
, calloc(size
, 1), size
);
75 void *qrealloc(struct memtype
*mt
, void *ptr
, size_t size
)
79 return mt_checkalloc(mt
, ptr
? realloc(ptr
, size
) : malloc(size
), size
);
82 void *qstrdup(struct memtype
*mt
, const char *str
)
84 return str
? mt_checkalloc(mt
, strdup(str
), strlen(str
) + 1) : NULL
;
87 void qfree(struct memtype
*mt
, void *ptr
)
94 int qmem_walk(qmem_walk_fn
*func
, void *arg
)
100 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
{
116 static int qmem_exit_walker(void *arg
, struct memgroup
*mg
, struct memtype
*mt
)
118 struct exit_dump_args
*eda
= arg
;
122 "%s: showing active allocations in "
124 eda
->prefix
, mg
->name
);
126 } else if (mt
->n_alloc
) {
129 snprintf(size
, sizeof(size
), "%10zu", mt
->size
);
130 fprintf(eda
->fp
, "%s: memstats: %-30s: %6zu * %s\n",
131 eda
->prefix
, mt
->name
, mt
->n_alloc
,
132 mt
->size
== SIZE_VAR
? "(variably sized)" : size
);
137 int log_memstats(FILE *fp
, const char *prefix
)
139 struct exit_dump_args eda
= {.fp
= fp
, .prefix
= prefix
, .error
= 0};
140 qmem_walk(qmem_exit_walker
, &eda
);