2 * Memory management routine
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31 struct message mstr
[] =
33 { MTYPE_THREAD
, "thread" },
34 { MTYPE_THREAD_MASTER
, "thread_master" },
35 { MTYPE_VECTOR
, "vector" },
36 { MTYPE_VECTOR_INDEX
, "vector_index" },
37 { MTYPE_IF
, "interface" },
41 /* Fatal memory allocation error occured. */
43 zerror (const char *fname
, int type
, size_t size
)
45 fprintf (stderr
, "%s : can't allocate memory for `%s' size %d\n",
46 fname
, lookup (mstr
, type
), (int) size
);
50 /* Memory allocation. */
52 zmalloc (int type
, size_t size
)
56 memory
= malloc (size
);
59 zerror ("malloc", type
, size
);
66 /* Memory allocation with num * size with cleared. */
68 zcalloc (int type
, size_t size
)
72 memory
= calloc (1, size
);
75 zerror ("calloc", type
, size
);
82 /* Memory reallocation. */
84 zrealloc (int type
, void *ptr
, size_t size
)
88 memory
= realloc (ptr
, size
);
90 zerror ("realloc", type
, size
);
96 zfree (int type
, void *ptr
)
102 /* String duplication. */
104 zstrdup (int type
, char *str
)
110 zerror ("strdup", type
, strlen (str
));
120 unsigned long t_malloc
;
121 unsigned long c_malloc
;
122 unsigned long t_calloc
;
123 unsigned long c_calloc
;
124 unsigned long t_realloc
;
125 unsigned long t_free
;
126 unsigned long c_strdup
;
130 mtype_log (char *func
, void *memory
, const char *file
, int line
, int type
)
132 zlog_info ("%s: %s %p %s %d", func
, lookup (mstr
, type
), memory
, file
, line
);
136 mtype_zmalloc (const char *file
, int line
, int type
, size_t size
)
140 mstat
[type
].c_malloc
++;
141 mstat
[type
].t_malloc
++;
143 memory
= zmalloc (type
, size
);
144 mtype_log ("zmalloc", memory
, file
, line
, type
);
150 mtype_zcalloc (const char *file
, int line
, int type
, size_t size
)
154 mstat
[type
].c_calloc
++;
155 mstat
[type
].t_calloc
++;
157 memory
= zcalloc (type
, size
);
158 mtype_log ("xcalloc", memory
, file
, line
, type
);
164 mtype_zrealloc (const char *file
, int line
, int type
, void *ptr
, size_t size
)
168 /* Realloc need before allocated pointer. */
169 mstat
[type
].t_realloc
++;
171 memory
= zrealloc (type
, ptr
, size
);
173 mtype_log ("xrealloc", memory
, file
, line
, type
);
178 /* Important function. */
180 mtype_zfree (const char *file
, int line
, int type
, void *ptr
)
182 mstat
[type
].t_free
++;
184 mtype_log ("xfree", ptr
, file
, line
, type
);
190 mtype_zstrdup (const char *file
, int line
, int type
, char *str
)
194 mstat
[type
].c_strdup
++;
196 memory
= zstrdup (type
, str
);
198 mtype_log ("xstrdup", memory
, file
, line
, type
);
208 #endif /* MTPYE_LOG */
210 /* Increment allocation counter. */
217 /* Decrement allocation counter. */
224 /* Looking up memory status from vty interface. */
229 /* For pretty printng of memory allocate information. */
236 struct memory_list memory_list_lib
[] =
238 { MTYPE_TMP
, "Temporary memory" },
239 { MTYPE_ROUTE_TABLE
, "Route table " },
240 { MTYPE_ROUTE_NODE
, "Route node " },
241 { MTYPE_RIB
, "RIB " },
242 { MTYPE_NEXTHOP
, "Nexthop " },
243 { MTYPE_LINK_LIST
, "Link List " },
244 { MTYPE_LINK_NODE
, "Link Node " },
245 { MTYPE_HASH
, "Hash " },
246 { MTYPE_HASH_BACKET
, "Hash Bucket " },
247 { MTYPE_ACCESS_LIST
, "Access List " },
248 { MTYPE_ACCESS_LIST_STR
, "Access List Str " },
249 { MTYPE_ACCESS_FILTER
, "Access Filter " },
250 { MTYPE_PREFIX_LIST
, "Prefix List " },
251 { MTYPE_PREFIX_LIST_STR
, "Prefix List Str " },
252 { MTYPE_PREFIX_LIST_ENTRY
, "Prefix List Entry "},
253 { MTYPE_ROUTE_MAP
, "Route map " },
254 { MTYPE_ROUTE_MAP_NAME
, "Route map name " },
255 { MTYPE_ROUTE_MAP_INDEX
, "Route map index " },
256 { MTYPE_ROUTE_MAP_RULE
, "Route map rule " },
257 { MTYPE_ROUTE_MAP_RULE_STR
, "Route map rule str" },
258 { MTYPE_ROUTE_MAP_COMPILED
, "Route map compiled" },
259 { MTYPE_DESC
, "Command desc " },
260 { MTYPE_BUFFER
, "Buffer " },
261 { MTYPE_BUFFER_DATA
, "Buffer data " },
262 { MTYPE_STREAM
, "Stream " },
263 { MTYPE_KEYCHAIN
, "Key chain " },
264 { MTYPE_KEY
, "Key " },
265 { MTYPE_VTY
, "VTY " },
269 struct memory_list memory_list_bgp
[] =
271 { MTYPE_BGP_PEER
, "BGP peer" },
272 { MTYPE_ATTR
, "BGP attribute" },
273 { MTYPE_AS_PATH
, "BGP aspath" },
274 { MTYPE_AS_SEG
, "BGP aspath seg" },
275 { MTYPE_AS_STR
, "BGP aspath str" },
277 { MTYPE_BGP_TABLE
, "BGP table" },
278 { MTYPE_BGP_NODE
, "BGP node" },
279 { MTYPE_BGP_ADVERTISE_ATTR
, "BGP adv attr" },
280 { MTYPE_BGP_ADVERTISE
, "BGP adv" },
281 { MTYPE_BGP_ADJ_IN
, "BGP adj in" },
282 { MTYPE_BGP_ADJ_OUT
, "BGP adj out" },
284 { MTYPE_AS_LIST
, "BGP AS list" },
285 { MTYPE_AS_FILTER
, "BGP AS filter" },
286 { MTYPE_AS_FILTER_STR
, "BGP AS filter str" },
288 { MTYPE_COMMUNITY
, "community" },
289 { MTYPE_COMMUNITY_VAL
, "community val" },
290 { MTYPE_COMMUNITY_STR
, "community str" },
292 { MTYPE_ECOMMUNITY
, "extcommunity" },
293 { MTYPE_ECOMMUNITY_VAL
, "extcommunity val" },
294 { MTYPE_ECOMMUNITY_STR
, "extcommunity str" },
296 { MTYPE_COMMUNITY_LIST
, "community-list" },
297 { MTYPE_COMMUNITY_LIST_NAME
, "community-list name" },
298 { MTYPE_COMMUNITY_LIST_ENTRY
, "community-list entry" },
299 { MTYPE_COMMUNITY_LIST_CONFIG
, "community-list config" },
301 { MTYPE_CLUSTER
, "Cluster list" },
302 { MTYPE_CLUSTER_VAL
, "Cluster list val" },
304 { MTYPE_TRANSIT
, "BGP transit attr" },
305 { MTYPE_TRANSIT_VAL
, "BGP transit val" },
307 { MTYPE_BGP_DISTANCE
, "BGP distance" },
308 { MTYPE_BGP_NEXTHOP_CACHE
, "BGP nexthop" },
309 { MTYPE_BGP_CONFED_LIST
, "BGP confed list" },
310 { MTYPE_PEER_UPDATE_SOURCE
, "peer update if" },
311 { MTYPE_BGP_DAMP_INFO
, "Dampening info" },
312 { MTYPE_BGP_REGEXP
, "BGP regexp" },
316 struct memory_list memory_list_rip
[] =
318 { MTYPE_RIP
, "RIP structure " },
319 { MTYPE_RIP_INFO
, "RIP route info " },
320 { MTYPE_RIP_INTERFACE
, "RIP interface " },
321 { MTYPE_RIP_PEER
, "RIP peer " },
322 { MTYPE_RIP_OFFSET_LIST
, "RIP offset list " },
323 { MTYPE_RIP_DISTANCE
, "RIP distance " },
327 struct memory_list memory_list_ripng
[] =
329 { MTYPE_RIPNG
, "RIPng structure " },
330 { MTYPE_RIPNG_ROUTE
, "RIPng route info" },
331 { MTYPE_RIPNG_AGGREGATE
, "RIPng aggregate " },
332 { MTYPE_RIPNG_PEER
, "RIPng peer " },
333 { MTYPE_RIPNG_OFFSET_LIST
, "RIPng offset lst" },
334 { MTYPE_RIPNG_RTE_DATA
, "RIPng rte data " },
338 struct memory_list memory_list_ospf
[] =
340 { MTYPE_OSPF_TOP
, "OSPF top " },
341 { MTYPE_OSPF_AREA
, "OSPF area " },
342 { MTYPE_OSPF_AREA_RANGE
, "OSPF area range " },
343 { MTYPE_OSPF_NETWORK
, "OSPF network " },
345 { MTYPE_OSPF_NEIGHBOR_STATIC
,"OSPF static nbr " },
346 #endif /* NBMA_ENABLE */
347 { MTYPE_OSPF_IF
, "OSPF interface " },
348 { MTYPE_OSPF_NEIGHBOR
, "OSPF neighbor " },
349 { MTYPE_OSPF_ROUTE
, "OSPF route " },
350 { MTYPE_OSPF_TMP
, "OSPF tmp mem " },
351 { MTYPE_OSPF_LSA
, "OSPF LSA " },
352 { MTYPE_OSPF_LSA_DATA
, "OSPF LSA data " },
353 { MTYPE_OSPF_LSDB
, "OSPF LSDB " },
354 { MTYPE_OSPF_PACKET
, "OSPF packet " },
355 { MTYPE_OSPF_FIFO
, "OSPF FIFO queue " },
356 { MTYPE_OSPF_VERTEX
, "OSPF vertex " },
357 { MTYPE_OSPF_NEXTHOP
, "OSPF nexthop " },
358 { MTYPE_OSPF_PATH
, "OSPF path " },
359 { MTYPE_OSPF_VL_DATA
, "OSPF VL data " },
360 { MTYPE_OSPF_CRYPT_KEY
, "OSPF crypt key " },
361 { MTYPE_OSPF_EXTERNAL_INFO
, "OSPF ext. info " },
362 { MTYPE_OSPF_DISTANCE
, "OSPF distance " },
363 { MTYPE_OSPF_IF_INFO
, "OSPF if info " },
364 { MTYPE_OSPF_IF_PARAMS
, "OSPF if params " },
368 struct memory_list memory_list_ospf6
[] =
370 { MTYPE_OSPF6_TOP
, "OSPF6 top " },
371 { MTYPE_OSPF6_AREA
, "OSPF6 area " },
372 { MTYPE_OSPF6_IF
, "OSPF6 interface " },
373 { MTYPE_OSPF6_NEIGHBOR
, "OSPF6 neighbor " },
374 { MTYPE_OSPF6_ROUTE
, "OSPF6 route " },
375 { MTYPE_OSPF6_PREFIX
, "OSPF6 prefix " },
376 { MTYPE_OSPF6_MESSAGE
, "OSPF6 message " },
377 { MTYPE_OSPF6_LSA
, "OSPF6 LSA " },
378 { MTYPE_OSPF6_LSA_SUMMARY
, "OSPF6 LSA summary " },
379 { MTYPE_OSPF6_LSDB
, "OSPF6 LSA database" },
380 { MTYPE_OSPF6_VERTEX
, "OSPF6 vertex " },
381 { MTYPE_OSPF6_SPFTREE
, "OSPF6 SPF tree " },
382 { MTYPE_OSPF6_NEXTHOP
, "OSPF6 nexthop " },
383 { MTYPE_OSPF6_EXTERNAL_INFO
,"OSPF6 ext. info " },
384 { MTYPE_OSPF6_OTHER
, "OSPF6 other " },
388 struct memory_list memory_list_separator
[] =
395 show_memory_vty (struct vty
*vty
, struct memory_list
*list
)
397 struct memory_list
*m
;
399 for (m
= list
; m
->index
>= 0; m
++)
401 vty_out (vty
, "-----------------------------\r\n");
403 vty_out (vty
, "%-22s: %5ld\r\n", m
->format
, mstat
[m
->index
].alloc
);
406 DEFUN (show_memory_all
,
409 "Show running system information\n"
410 "Memory statistics\n"
411 "All memory statistics\n")
413 show_memory_vty (vty
, memory_list_lib
);
414 show_memory_vty (vty
, memory_list_separator
);
415 show_memory_vty (vty
, memory_list_rip
);
416 show_memory_vty (vty
, memory_list_separator
);
417 show_memory_vty (vty
, memory_list_ripng
);
418 show_memory_vty (vty
, memory_list_separator
);
419 show_memory_vty (vty
, memory_list_ospf
);
420 show_memory_vty (vty
, memory_list_separator
);
421 show_memory_vty (vty
, memory_list_ospf6
);
422 show_memory_vty (vty
, memory_list_separator
);
423 show_memory_vty (vty
, memory_list_bgp
);
428 ALIAS (show_memory_all
,
431 "Show running system information\n"
432 "Memory statistics\n")
434 DEFUN (show_memory_lib
,
438 "Memory statistics\n"
441 show_memory_vty (vty
, memory_list_lib
);
445 DEFUN (show_memory_rip
,
449 "Memory statistics\n"
452 show_memory_vty (vty
, memory_list_rip
);
456 DEFUN (show_memory_ripng
,
457 show_memory_ripng_cmd
,
460 "Memory statistics\n"
463 show_memory_vty (vty
, memory_list_ripng
);
467 DEFUN (show_memory_bgp
,
471 "Memory statistics\n"
474 show_memory_vty (vty
, memory_list_bgp
);
478 DEFUN (show_memory_ospf
,
479 show_memory_ospf_cmd
,
482 "Memory statistics\n"
485 show_memory_vty (vty
, memory_list_ospf
);
489 DEFUN (show_memory_ospf6
,
490 show_memory_ospf6_cmd
,
493 "Memory statistics\n"
496 show_memory_vty (vty
, memory_list_ospf6
);
503 install_element (VIEW_NODE
, &show_memory_cmd
);
504 install_element (VIEW_NODE
, &show_memory_all_cmd
);
505 install_element (VIEW_NODE
, &show_memory_lib_cmd
);
506 install_element (VIEW_NODE
, &show_memory_rip_cmd
);
507 install_element (VIEW_NODE
, &show_memory_ripng_cmd
);
508 install_element (VIEW_NODE
, &show_memory_bgp_cmd
);
509 install_element (VIEW_NODE
, &show_memory_ospf_cmd
);
510 install_element (VIEW_NODE
, &show_memory_ospf6_cmd
);
512 install_element (ENABLE_NODE
, &show_memory_cmd
);
513 install_element (ENABLE_NODE
, &show_memory_all_cmd
);
514 install_element (ENABLE_NODE
, &show_memory_lib_cmd
);
515 install_element (ENABLE_NODE
, &show_memory_rip_cmd
);
516 install_element (ENABLE_NODE
, &show_memory_ripng_cmd
);
517 install_element (ENABLE_NODE
, &show_memory_bgp_cmd
);
518 install_element (ENABLE_NODE
, &show_memory_ospf_cmd
);
519 install_element (ENABLE_NODE
, &show_memory_ospf6_cmd
);