api: API trace improvements

Type: improvement

 * add support for JSON format in API trace
 * add ability to replay JSON API trace in both VPP and VAT2
 * use CRC for backward compatibility check during JSON API replay
 * fix API trace CLI (and remove duplicits)
 * remove custom dump
 * remove vppapitrace.py
 * update docs accordingly

Change-Id: I5294f68bebe6cbe738630f457f3a87720e06486b
Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
Signed-off-by: Ole Troan <ot@cisco.com>
diff --git a/src/vppinfra/cJSON.c b/src/vppinfra/cJSON.c
index 5b26a4b..448435d 100644
--- a/src/vppinfra/cJSON.c
+++ b/src/vppinfra/cJSON.c
@@ -157,7 +157,7 @@
 {
     void *(CJSON_CDECL *allocate)(size_t size);
     void (CJSON_CDECL *deallocate)(void *pointer);
-    void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
+    void *(CJSON_CDECL *reallocate)(void *pointer, size_t new_size, size_t old_size);
 } internal_hooks;
 
 #if defined(_MSC_VER)
@@ -170,16 +170,20 @@
 {
     free(pointer);
 }
-static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
-{
-    return realloc(pointer, size);
-}
 #else
 #define internal_malloc malloc
 #define internal_free free
-#define internal_realloc realloc
 #endif
 
+static void * CJSON_CDECL internal_realloc(void *pointer, size_t new_size,
+    size_t old_size)
+{
+    return realloc(pointer, new_size);
+}
+
+static void *
+cjson_realloc_internal (void *ptr, size_t new_size, size_t old_size);
+
 /* strlen of character literals resolved at compile time */
 #define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
 
@@ -213,7 +217,7 @@
         /* Reset hooks */
         global_hooks.allocate = malloc;
         global_hooks.deallocate = free;
-        global_hooks.reallocate = realloc;
+        global_hooks.reallocate = internal_realloc;
         return;
     }
 
@@ -233,7 +237,11 @@
     global_hooks.reallocate = NULL;
     if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
     {
-        global_hooks.reallocate = realloc;
+        global_hooks.reallocate = internal_realloc;
+    }
+    else
+    {
+        global_hooks.reallocate = cjson_realloc_internal;
     }
 }
 
@@ -435,6 +443,27 @@
     internal_hooks hooks;
 } printbuffer;
 
+static void *
+cjson_realloc_internal (void *ptr, size_t new_size, size_t old_size)
+{
+    size_t copy_size;
+    if (old_size < new_size)
+      copy_size = old_size;
+    else
+      copy_size = new_size;
+
+    unsigned char *newbuffer = global_hooks.allocate(new_size);
+    if (!newbuffer)
+    {
+        global_hooks.deallocate(ptr);
+        return NULL;
+    }
+
+    memcpy (newbuffer, ptr, copy_size);
+    global_hooks.deallocate (ptr);
+    return newbuffer;
+}
+
 /* realloc printbuffer if necessary to have at least "needed" bytes more */
 static unsigned char* ensure(printbuffer * const p, size_t needed)
 {
@@ -486,34 +515,13 @@
         newsize = needed * 2;
     }
 
-    if (p->hooks.reallocate != NULL)
+    newbuffer = p->hooks.reallocate (p->buffer, newsize, p->length);
+    if (newbuffer == NULL)
     {
-        /* reallocate with realloc if available */
-        newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
-        if (newbuffer == NULL)
-        {
-            p->hooks.deallocate(p->buffer);
-            p->length = 0;
-            p->buffer = NULL;
-
-            return NULL;
-        }
-    }
-    else
-    {
-        /* otherwise reallocate manually */
-        newbuffer = (unsigned char*)p->hooks.allocate(newsize);
-        if (!newbuffer)
-        {
-            p->hooks.deallocate(p->buffer);
-            p->length = 0;
-            p->buffer = NULL;
-
-            return NULL;
-        }
-
-	memcpy (newbuffer, p->buffer, p->offset + 1);
-	p->hooks.deallocate (p->buffer);
+        p->hooks.deallocate(p->buffer);
+        p->length = 0;
+        p->buffer = NULL;
+        return NULL;
     }
     p->length = newsize;
     p->buffer = newbuffer;
@@ -1208,7 +1216,7 @@
     /* check if reallocate is available */
     if (hooks->reallocate != NULL)
     {
-        printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
+        printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1, default_buffer_size);
         if (printed == NULL) {
             goto fail;
         }
@@ -3112,3 +3120,8 @@
 {
     global_hooks.deallocate(object);
 }
+
+CJSON_PUBLIC(void *) cJSON_realloc(void *object, size_t new_size, size_t old_size)
+{
+    return global_hooks.reallocate(object, new_size, old_size);
+}