From 5d41cec800dc6100b8181b95a08f7803dac6eeb9 Mon Sep 17 00:00:00 2001 From: "Chris Hall (GMCH)" Date: Tue, 24 Nov 2009 19:54:30 +0000 Subject: Upgrade lib/vector.c & .h This upgrade maintains the existing vector operations, but changes the underlying mechanisms. It then adds a number of new functions, extending the operations on vectors. The "struct vector" is redefined, which affects only some code in lib/command.c -- which pokes around inside what should be treated as a private data structure. Pro tem this is supported by a macro. The constant VECTOR_MIN_SIZE has been removed -- if there is a minimum size that should be enforced by the vector code. New functions include: vector_insert_item -- insert item, moving existing items vector_move_item -- move item from place to place in vector vector_delete_item -- delete item and close up gap vector_reverse -- reverse order of vector vector_push_item -- simple push vector_pop_item -- simple pop vector_insert -- insert 1 or more NULL items, moving existing items vector_delete -- delete 1 or more items and close up gap vector_bsearch -- perform binary search on vector vector_sort -- qsort vector vector_copy_here -- make copy of body of vector vector_move_here -- move body of vector vector_copy_append -- append copy of vector to end of another vector_move_append -- move body of vector to the end of another vector_copy_extract -- copy part of one vector to another vector_move_extract -- move part of one vector to another vector_copy_splice -- copy part of one vector to replace part of another, taking a copy of the replaced part vector_move_splice -- move part of one vector to replace part of another, taking a copy of the replaced part vector_copy_replace -- copy part of one vector to replace part of another vector_move_replace -- move part of one vector to replace part of another vector_discard -- discard body of vector vector_chop -- discard any unused memory in body of vector vector_decant -- decant vector to new body Other files affected: command.c: -- removal of VECTOR_MIN_SIZE -- use of vector_sort() -- use of VECTOR_INDEX to poke around inside vector (removing this is TODO) memtypes.c & memory.c -- updating names and comments for vectors vty.c -- removal of VECTOR_MIN_SIZE memory.h -- added SIZE(t,n) macro -- (sizeof(t) * (n)) --- lib/memory.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'lib/memory.c') diff --git a/lib/memory.c b/lib/memory.c index dc09d8a6..333f59ad 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with GNU Zebra; see the file COPYING. If not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. + * 02111-1307, USA. */ #include @@ -32,22 +32,22 @@ 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" }, { MTYPE_THREAD_MASTER, "thread_master" }, { MTYPE_VECTOR, "vector" }, - { MTYPE_VECTOR_INDEX, "vector_index" }, + { MTYPE_VECTOR_BODY, "vector_index" }, { MTYPE_IF, "interface" }, { 0, NULL }, }; - + /* Fatal memory allocation error occured. */ static void __attribute__ ((noreturn)) zerror (const char *fname, int type, size_t size) { - zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n", + zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n", fname, lookup (mstr, type), (int) size, safe_strerror(errno)); log_memstats(LOG_WARNING); /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since @@ -122,9 +122,9 @@ zstrdup (int type, const char *str) alloc_inc (type); return dup; } - + #ifdef MEMORY_LOG -static struct +static struct { const char *name; long alloc; @@ -187,7 +187,7 @@ mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size) } /* Important function. */ -void +void mtype_zfree (const char *file, int line, int type, void *ptr) { mstat[type].t_free++; @@ -205,13 +205,13 @@ mtype_zstrdup (const char *file, int line, int type, const char *str) mstat[type].c_strdup++; memory = zstrdup (type, str); - + mtype_log ("xstrdup", memory, file, line, type); return memory; } #else -static struct +static struct { char *name; long alloc; @@ -231,7 +231,7 @@ alloc_dec (int type) { mstat[type].alloc--; } - + /* Looking up memory status from vty interface. */ #include "vector.h" #include "vty.h" @@ -329,7 +329,7 @@ show_memory_mallinfo (struct vty *vty) { struct mallinfo minfo = mallinfo(); char buf[MTYPE_MEMSTR_LEN]; - + vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE); vty_out (vty, " Total heap allocated: %s%s", mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena), @@ -373,11 +373,11 @@ DEFUN (show_memory_all, { struct mlist *ml; int needsep = 0; - + #ifdef HAVE_MALLINFO needsep = show_memory_mallinfo (vty); #endif /* HAVE_MALLINFO */ - + for (ml = mlists; ml->list; ml++) { if (needsep) @@ -516,7 +516,7 @@ memory_init (void) 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: @@ -529,13 +529,13 @@ const char * mtype_memstr (char *buf, size_t len, unsigned long bytes) { unsigned int t, g, 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 */ @@ -545,11 +545,11 @@ mtype_memstr (char *buf, size_t len, unsigned long bytes) g = bytes >> 30; 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. + * 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))) @@ -576,7 +576,7 @@ mtype_memstr (char *buf, size_t len, unsigned long bytes) } else snprintf (buf, len, "%ld bytes", bytes); - + return buf; } -- cgit v1.2.3