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
34 struct message mstr [] =
36 { MTYPE_THREAD, "thread" },
37 { MTYPE_THREAD_MASTER, "thread_master" },
38 { MTYPE_VECTOR, "vector" },
39 { MTYPE_VECTOR_INDEX, "vector_index" },
40 { MTYPE_IF, "interface" },
46 /* Fatal memory allocation error occured. */
48 zerror (const char *fname, int type, size_t size)
50 fprintf (stderr, "%s : can't allocate memory for `%s' size %d\n",
51 fname, lookup (mstr, type), (int) size);
56 /* Memory allocation. */
58 zmalloc (int type, size_t size)
62 memory = malloc (size);
65 zerror ("malloc", type, size);
71 /* Memory allocation with num * size with cleared. */
73 zcalloc (int type, size_t size)
77 memory = calloc (1, size);
80 zerror ("calloc", type, size);
87 /* Memory reallocation. */
89 zrealloc (int type, void *ptr, size_t size)
93 memory = realloc (ptr, size);
96 zerror ("realloc", type, size);
103 zfree (int type, void *ptr)
105 #ifdef BRCM_RIP_DEBUG
111 /* String duplication. */
113 zstrdup (int type, char *str)
118 #ifdef BRCM_RIP_DEBUG
120 zerror ("strdup", type, strlen (str));
131 unsigned long t_malloc;
132 unsigned long c_malloc;
133 unsigned long t_calloc;
134 unsigned long c_calloc;
135 unsigned long t_realloc;
136 unsigned long t_free;
137 unsigned long c_strdup;
141 mtype_log (char *func, void *memory, const char *file, int line, int type)
143 #ifdef BRCM_RIP_DEBUG
144 zlog_info ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
149 mtype_zmalloc (const char *file, int line, int type, size_t size)
153 mstat[type].c_malloc++;
154 mstat[type].t_malloc++;
156 memory = zmalloc (type, size);
157 mtype_log ("zmalloc", memory, file, line, type);
163 mtype_zcalloc (const char *file, int line, int type, size_t size)
167 mstat[type].c_calloc++;
168 mstat[type].t_calloc++;
170 memory = zcalloc (type, size);
171 mtype_log ("xcalloc", memory, file, line, type);
177 mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
181 /* Realloc need before allocated pointer. */
182 mstat[type].t_realloc++;
184 memory = zrealloc (type, ptr, size);
186 mtype_log ("xrealloc", memory, file, line, type);
191 /* Important function. */
193 mtype_zfree (const char *file, int line, int type, void *ptr)
195 mstat[type].t_free++;
197 mtype_log ("xfree", ptr, file, line, type);
203 mtype_zstrdup (const char *file, int line, int type, char *str)
207 mstat[type].c_strdup++;
209 memory = zstrdup (type, str);
211 mtype_log ("xstrdup", memory, file, line, type);
221 #endif /* MTPYE_LOG */
223 #ifdef BRCM_RIP_DEBUG
224 /* Increment allocation counter. */
231 /* Decrement allocation counter. */
239 #ifdef BRCM_CMD_SUPPORT
241 /* Looking up memory status from vty interface. */
246 /* For pretty printng of memory allocate information. */
253 struct memory_list memory_list_lib[] =
255 { MTYPE_TMP, "Temporary memory" },
256 { MTYPE_ROUTE_TABLE, "Route table " },
257 { MTYPE_ROUTE_NODE, "Route node " },
258 { MTYPE_RIB, "RIB " },
259 { MTYPE_NEXTHOP, "Nexthop " },
260 { MTYPE_LINK_LIST, "Link List " },
261 { MTYPE_LINK_NODE, "Link Node " },
262 { MTYPE_HASH, "Hash " },
263 { MTYPE_HASH_BACKET, "Hash Bucket " },
264 { MTYPE_ACCESS_LIST, "Access List " },
265 { MTYPE_ACCESS_LIST_STR, "Access List Str " },
266 { MTYPE_ACCESS_FILTER, "Access Filter " },
267 { MTYPE_PREFIX_LIST, "Prefix List " },
268 { MTYPE_PREFIX_LIST_STR, "Prefix List Str " },
269 { MTYPE_PREFIX_LIST_ENTRY, "Prefix List Entry "},
270 { MTYPE_ROUTE_MAP, "Route map " },
271 { MTYPE_ROUTE_MAP_NAME, "Route map name " },
272 { MTYPE_ROUTE_MAP_INDEX, "Route map index " },
273 { MTYPE_ROUTE_MAP_RULE, "Route map rule " },
274 { MTYPE_ROUTE_MAP_RULE_STR, "Route map rule str" },
275 { MTYPE_DESC, "Command desc " },
276 { MTYPE_BUFFER, "Buffer " },
277 { MTYPE_BUFFER_DATA, "Buffer data " },
278 { MTYPE_STREAM, "Stream " },
279 { MTYPE_KEYCHAIN, "Key chain " },
280 { MTYPE_KEY, "Key " },
281 { MTYPE_VTY, "VTY " },
285 struct memory_list memory_list_bgp[] =
287 { MTYPE_BGP_PEER, "BGP peer" },
288 { MTYPE_ATTR, "BGP attribute" },
289 { MTYPE_AS_PATH, "BGP aspath" },
290 { MTYPE_AS_SEG, "BGP aspath seg" },
291 { MTYPE_AS_STR, "BGP aspath str" },
293 { MTYPE_BGP_TABLE, "BGP table" },
294 { MTYPE_BGP_NODE, "BGP node" },
295 { MTYPE_BGP_ADVERTISE_ATTR, "BGP adv attr" },
296 { MTYPE_BGP_ADVERTISE, "BGP adv" },
297 { MTYPE_BGP_ADJ_IN, "BGP adj in" },
298 { MTYPE_BGP_ADJ_OUT, "BGP adj out" },
300 { MTYPE_AS_LIST, "BGP AS list" },
301 { MTYPE_AS_FILTER, "BGP AS filter" },
302 { MTYPE_AS_FILTER_STR, "BGP AS filter str" },
304 { MTYPE_COMMUNITY, "community" },
305 { MTYPE_COMMUNITY_VAL, "community val" },
306 { MTYPE_COMMUNITY_STR, "community str" },
308 { MTYPE_ECOMMUNITY, "extcommunity" },
309 { MTYPE_ECOMMUNITY_VAL, "extcommunity val" },
310 { MTYPE_ECOMMUNITY_STR, "extcommunity str" },
312 { MTYPE_COMMUNITY_LIST, "community-list" },
313 { MTYPE_COMMUNITY_LIST_NAME, "community-list name" },
314 { MTYPE_COMMUNITY_LIST_ENTRY, "community-list entry" },
315 { MTYPE_COMMUNITY_LIST_CONFIG, "community-list config" },
317 { MTYPE_CLUSTER, "Cluster list" },
318 { MTYPE_CLUSTER_VAL, "Cluster list val" },
320 { MTYPE_TRANSIT, "BGP transit attr" },
321 { MTYPE_TRANSIT_VAL, "BGP transit val" },
323 { MTYPE_BGP_DISTANCE, "BGP distance" },
324 { MTYPE_BGP_NEXTHOP_CACHE, "BGP nexthop" },
325 { MTYPE_BGP_CONFED_LIST, "BGP confed list" },
326 { MTYPE_PEER_UPDATE_SOURCE, "peer update if" },
327 { MTYPE_BGP_DAMP_INFO, "Dampening info" },
328 { MTYPE_BGP_REGEXP, "BGP regexp" },
332 struct memory_list memory_list_rip[] =
334 { MTYPE_RIP, "RIP structure " },
335 { MTYPE_RIP_INFO, "RIP route info " },
336 { MTYPE_RIP_INTERFACE, "RIP interface " },
337 { MTYPE_RIP_PEER, "RIP peer " },
338 { MTYPE_RIP_OFFSET_LIST, "RIP offset list " },
339 { MTYPE_RIP_DISTANCE, "RIP distance " },
343 struct memory_list memory_list_ospf[] =
345 { MTYPE_OSPF_TOP, "OSPF top " },
346 { MTYPE_OSPF_AREA, "OSPF area " },
347 { MTYPE_OSPF_AREA_RANGE, "OSPF area range " },
348 { MTYPE_OSPF_NETWORK, "OSPF network " },
350 { MTYPE_OSPF_NEIGHBOR_STATIC,"OSPF static nbr " },
351 #endif /* NBMA_ENABLE */
352 { MTYPE_OSPF_IF, "OSPF interface " },
353 { MTYPE_OSPF_NEIGHBOR, "OSPF neighbor " },
354 { MTYPE_OSPF_ROUTE, "OSPF route " },
355 { MTYPE_OSPF_TMP, "OSPF tmp mem " },
356 { MTYPE_OSPF_LSA, "OSPF LSA " },
357 { MTYPE_OSPF_LSA_DATA, "OSPF LSA data " },
358 { MTYPE_OSPF_LSDB, "OSPF LSDB " },
359 { MTYPE_OSPF_PACKET, "OSPF packet " },
360 { MTYPE_OSPF_FIFO, "OSPF FIFO queue " },
361 { MTYPE_OSPF_VERTEX, "OSPF vertex " },
362 { MTYPE_OSPF_NEXTHOP, "OSPF nexthop " },
363 { MTYPE_OSPF_PATH, "OSPF path " },
364 { MTYPE_OSPF_VL_DATA, "OSPF VL data " },
365 { MTYPE_OSPF_CRYPT_KEY, "OSPF crypt key " },
366 { MTYPE_OSPF_EXTERNAL_INFO, "OSPF ext. info " },
367 { MTYPE_OSPF_DISTANCE, "OSPF distance " },
368 { MTYPE_OSPF_IF_INFO, "OSPF if info " },
369 { MTYPE_OSPF_IF_PARAMS, "OSPF if params " },
373 struct memory_list memory_list_ospf6[] =
375 { MTYPE_OSPF6_TOP, "OSPF6 top " },
376 { MTYPE_OSPF6_AREA, "OSPF6 area " },
377 { MTYPE_OSPF6_IF, "OSPF6 interface " },
378 { MTYPE_OSPF6_NEIGHBOR, "OSPF6 neighbor " },
379 { MTYPE_OSPF6_ROUTE, "OSPF6 route " },
380 { MTYPE_OSPF6_PREFIX, "OSPF6 prefix " },
381 { MTYPE_OSPF6_MESSAGE, "OSPF6 message " },
382 { MTYPE_OSPF6_LSA, "OSPF6 LSA " },
383 { MTYPE_OSPF6_LSA_SUMMARY, "OSPF6 LSA summary " },
384 { MTYPE_OSPF6_LSDB, "OSPF6 LSA database" },
385 { MTYPE_OSPF6_VERTEX, "OSPF6 vertex " },
386 { MTYPE_OSPF6_SPFTREE, "OSPF6 SPF tree " },
387 { MTYPE_OSPF6_NEXTHOP, "OSPF6 nexthop " },
388 { MTYPE_OSPF6_EXTERNAL_INFO,"OSPF6 ext. info " },
389 { MTYPE_OSPF6_OTHER, "OSPF6 other " },
393 struct memory_list memory_list_separator[] =
400 show_memory_vty (struct vty *vty, struct memory_list *list)
402 struct memory_list *m;
404 for (m = list; m->index >= 0; m++)
406 vty_out (vty, "-----------------------------\r\n");
408 vty_out (vty, "%-22s: %5ld\r\n", m->format, mstat[m->index].alloc);
411 DEFUN (show_memory_all,
414 "Show running system information\n"
415 "Memory statistics\n"
416 "All memory statistics\n")
418 show_memory_vty (vty, memory_list_lib);
419 show_memory_vty (vty, memory_list_separator);
420 show_memory_vty (vty, memory_list_rip);
421 show_memory_vty (vty, memory_list_separator);
422 show_memory_vty (vty, memory_list_ospf);
423 show_memory_vty (vty, memory_list_separator);
424 show_memory_vty (vty, memory_list_ospf6);
425 show_memory_vty (vty, memory_list_separator);
426 show_memory_vty (vty, memory_list_bgp);
431 ALIAS (show_memory_all,
434 "Show running system information\n"
435 "Memory statistics\n")
437 DEFUN (show_memory_lib,
441 "Memory statistics\n"
444 show_memory_vty (vty, memory_list_lib);
448 DEFUN (show_memory_rip,
452 "Memory statistics\n"
455 show_memory_vty (vty, memory_list_rip);
459 DEFUN (show_memory_bgp,
463 "Memory statistics\n"
466 show_memory_vty (vty, memory_list_bgp);
470 DEFUN (show_memory_ospf,
471 show_memory_ospf_cmd,
474 "Memory statistics\n"
477 show_memory_vty (vty, memory_list_ospf);
481 DEFUN (show_memory_ospf6,
482 show_memory_ospf6_cmd,
485 "Memory statistics\n"
488 show_memory_vty (vty, memory_list_ospf6);
491 #endif /* BRCM_CMD_SUPPORT */
496 #ifdef BRCM_CMD_SUPPORT
497 install_element (VIEW_NODE, &show_memory_cmd);
498 install_element (VIEW_NODE, &show_memory_all_cmd);
499 install_element (VIEW_NODE, &show_memory_lib_cmd);
500 install_element (VIEW_NODE, &show_memory_rip_cmd);
501 install_element (VIEW_NODE, &show_memory_bgp_cmd);
502 install_element (VIEW_NODE, &show_memory_ospf_cmd);
503 install_element (VIEW_NODE, &show_memory_ospf6_cmd);
505 install_element (ENABLE_NODE, &show_memory_cmd);
506 install_element (ENABLE_NODE, &show_memory_all_cmd);
507 install_element (ENABLE_NODE, &show_memory_lib_cmd);
508 install_element (ENABLE_NODE, &show_memory_rip_cmd);
509 install_element (ENABLE_NODE, &show_memory_bgp_cmd);
510 install_element (ENABLE_NODE, &show_memory_ospf_cmd);
511 install_element (ENABLE_NODE, &show_memory_ospf6_cmd);