--- embedaddon/quagga/lib/memory.c 2012/10/09 09:22:28 1.1.1.2 +++ embedaddon/quagga/lib/memory.c 2016/11/02 10:09:11 1.1.1.3 @@ -32,7 +32,7 @@ static void alloc_inc (int); static void alloc_dec (int); static void log_memstats(int log_priority); - + static const struct message mstr [] = { { MTYPE_THREAD, "thread" }, @@ -42,7 +42,7 @@ static const struct message mstr [] = { MTYPE_IF, "interface" }, { 0, NULL }, }; - + /* Fatal memory allocation error occured. */ static void __attribute__ ((noreturn)) zerror (const char *fname, int type, size_t size) @@ -108,6 +108,9 @@ zrealloc (int type, void *ptr, size_t size) { void *memory; + if (ptr == NULL) /* is really alloc */ + return zcalloc(type, size); + memory = realloc (ptr, size); if (memory == NULL) zerror ("realloc", type, size); @@ -150,7 +153,7 @@ zstrdup (int type, const char *str) alloc_inc (type); return dup; } - + #ifdef MEMORY_LOG static struct { @@ -259,7 +262,7 @@ alloc_dec (int type) { mstat[type].alloc--; } - + /* Looking up memory status from vty interface. */ #include "vector.h" #include "vty.h" @@ -392,12 +395,11 @@ show_memory_mallinfo (struct vty *vty) } #endif /* HAVE_MALLINFO */ -DEFUN (show_memory_all, - show_memory_all_cmd, - "show memory all", +DEFUN (show_memory, + show_memory_cmd, + "show memory", "Show running system information\n" - "Memory statistics\n" - "All memory statistics\n") + "Memory statistics\n") { struct mlist *ml; int needsep = 0; @@ -416,149 +418,17 @@ DEFUN (show_memory_all, return CMD_SUCCESS; } -ALIAS (show_memory_all, - show_memory_cmd, - "show memory", - "Show running system information\n" - "Memory statistics\n") -DEFUN (show_memory_lib, - show_memory_lib_cmd, - "show memory lib", - SHOW_STR - "Memory statistics\n" - "Library memory\n") -{ - show_memory_vty (vty, memory_list_lib); - return CMD_SUCCESS; -} - -DEFUN (show_memory_zebra, - show_memory_zebra_cmd, - "show memory zebra", - SHOW_STR - "Memory statistics\n" - "Zebra memory\n") -{ - show_memory_vty (vty, memory_list_zebra); - return CMD_SUCCESS; -} - -DEFUN (show_memory_rip, - show_memory_rip_cmd, - "show memory rip", - SHOW_STR - "Memory statistics\n" - "RIP memory\n") -{ - show_memory_vty (vty, memory_list_rip); - return CMD_SUCCESS; -} - -DEFUN (show_memory_ripng, - show_memory_ripng_cmd, - "show memory ripng", - SHOW_STR - "Memory statistics\n" - "RIPng memory\n") -{ - show_memory_vty (vty, memory_list_ripng); - return CMD_SUCCESS; -} - -DEFUN (show_memory_babel, - show_memory_babel_cmd, - "show memory babel", - SHOW_STR - "Memory statistics\n" - "Babel memory\n") -{ - show_memory_vty (vty, memory_list_babel); - return CMD_SUCCESS; -} - -DEFUN (show_memory_bgp, - show_memory_bgp_cmd, - "show memory bgp", - SHOW_STR - "Memory statistics\n" - "BGP memory\n") -{ - show_memory_vty (vty, memory_list_bgp); - return CMD_SUCCESS; -} - -DEFUN (show_memory_ospf, - show_memory_ospf_cmd, - "show memory ospf", - SHOW_STR - "Memory statistics\n" - "OSPF memory\n") -{ - show_memory_vty (vty, memory_list_ospf); - return CMD_SUCCESS; -} - -DEFUN (show_memory_ospf6, - show_memory_ospf6_cmd, - "show memory ospf6", - SHOW_STR - "Memory statistics\n" - "OSPF6 memory\n") -{ - show_memory_vty (vty, memory_list_ospf6); - return CMD_SUCCESS; -} - -DEFUN (show_memory_isis, - show_memory_isis_cmd, - "show memory isis", - SHOW_STR - "Memory statistics\n" - "ISIS memory\n") -{ - show_memory_vty (vty, memory_list_isis); - return CMD_SUCCESS; -} - void memory_init (void) { install_element (RESTRICTED_NODE, &show_memory_cmd); - install_element (RESTRICTED_NODE, &show_memory_all_cmd); - install_element (RESTRICTED_NODE, &show_memory_lib_cmd); - install_element (RESTRICTED_NODE, &show_memory_rip_cmd); - install_element (RESTRICTED_NODE, &show_memory_ripng_cmd); - install_element (RESTRICTED_NODE, &show_memory_babel_cmd); - install_element (RESTRICTED_NODE, &show_memory_bgp_cmd); - install_element (RESTRICTED_NODE, &show_memory_ospf_cmd); - install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd); - install_element (RESTRICTED_NODE, &show_memory_isis_cmd); install_element (VIEW_NODE, &show_memory_cmd); - install_element (VIEW_NODE, &show_memory_all_cmd); - install_element (VIEW_NODE, &show_memory_lib_cmd); - install_element (VIEW_NODE, &show_memory_rip_cmd); - install_element (VIEW_NODE, &show_memory_ripng_cmd); - install_element (VIEW_NODE, &show_memory_babel_cmd); - install_element (VIEW_NODE, &show_memory_bgp_cmd); - install_element (VIEW_NODE, &show_memory_ospf_cmd); - install_element (VIEW_NODE, &show_memory_ospf6_cmd); - install_element (VIEW_NODE, &show_memory_isis_cmd); install_element (ENABLE_NODE, &show_memory_cmd); - install_element (ENABLE_NODE, &show_memory_all_cmd); - install_element (ENABLE_NODE, &show_memory_lib_cmd); - install_element (ENABLE_NODE, &show_memory_zebra_cmd); - install_element (ENABLE_NODE, &show_memory_rip_cmd); - install_element (ENABLE_NODE, &show_memory_ripng_cmd); - install_element (ENABLE_NODE, &show_memory_babel_cmd); - install_element (ENABLE_NODE, &show_memory_bgp_cmd); - install_element (ENABLE_NODE, &show_memory_ospf_cmd); - install_element (ENABLE_NODE, &show_memory_ospf6_cmd); - install_element (ENABLE_NODE, &show_memory_isis_cmd); } - + /* Stats querying from users */ /* Return a pointer to a human friendly string describing * the byte count passed in. E.g: @@ -570,41 +440,28 @@ memory_init (void) const char * mtype_memstr (char *buf, size_t len, unsigned long bytes) { - unsigned int t, g, m, k; - + unsigned int m, k; + /* easy cases */ if (!bytes) return "0 bytes"; if (bytes == 1) return "1 byte"; - - if (sizeof (unsigned long) >= 8) - /* Hacked to make it not warn on ILP32 machines - * Shift will always be 40 at runtime. See below too */ - t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0); - else - t = 0; - g = bytes >> 30; + + /* + * When we pass the 2gb barrier mallinfo() can no longer report + * correct data so it just does something odd... + * Reporting like Terrabytes of data. Which makes users... + * edgy.. yes edgy that's the term for it. + * So let's just give up gracefully + */ + if (bytes > 0x7fffffff) + return "> 2GB"; + m = bytes >> 20; k = bytes >> 10; - - if (t > 10) - { - /* The shift will always be 39 at runtime. - * Just hacked to make it not warn on 'smaller' machines. - * Static compiler analysis should mean no extra code - */ - if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0))) - t++; - snprintf (buf, len, "%4d TiB", t); - } - else if (g > 10) - { - if (bytes & (1 << 29)) - g++; - snprintf (buf, len, "%d GiB", g); - } - else if (m > 10) + + if (m > 10) { if (bytes & (1 << 19)) m++;