Use the updated free_json and profile it
This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
			
		||||
#include "json/json_entities.h"
 | 
			
		||||
#include "aliases.h"
 | 
			
		||||
#include "profiler/ids.h"
 | 
			
		||||
#include "profiler/timer.h"
 | 
			
		||||
#include "json/dstring.h"
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
@@ -110,58 +112,90 @@ void print_json(const jentity_t *entity, u32 indent) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void free_json(jentity_t **entity) {
 | 
			
		||||
  if (!(*entity)) {
 | 
			
		||||
void free_json(jentity_t **root) {
 | 
			
		||||
  if (!(*root)) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  jentity_t *entt_ptr = *entity;
 | 
			
		||||
  SAMPLE_START(PROFILER_ID_FREE_JSON, "FREE JSON");
 | 
			
		||||
 | 
			
		||||
  jentity_t *current = *root;
 | 
			
		||||
  jentity_t *temp = NULL;
 | 
			
		||||
  dstr_t *key = NULL;
 | 
			
		||||
  const jval_t *value = NULL;
 | 
			
		||||
  jval_t *value = NULL;
 | 
			
		||||
 | 
			
		||||
  if (entt_ptr->type == JENTITY_SINGLE) {
 | 
			
		||||
    value = &(entt_ptr->value);
 | 
			
		||||
  } else {
 | 
			
		||||
    key = entt_ptr->pair.key;
 | 
			
		||||
    value = &(entt_ptr->pair.value);
 | 
			
		||||
  }
 | 
			
		||||
  while (current) {
 | 
			
		||||
    if (current->parent) {
 | 
			
		||||
      // Move the beginning pointer of the collection to the next child
 | 
			
		||||
 | 
			
		||||
  if (key) {
 | 
			
		||||
    dstr_free(&(entt_ptr->pair.key));
 | 
			
		||||
  }
 | 
			
		||||
      // TODO (Abdelrahman): This part gets repeated for some elements. Try to
 | 
			
		||||
      // avoid that repetition
 | 
			
		||||
 | 
			
		||||
  if (!value) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
      jentity_t *parent = current->parent;
 | 
			
		||||
      jcoll_t *collection = NULL;
 | 
			
		||||
 | 
			
		||||
  switch (value->type) {
 | 
			
		||||
  case JVAL_COLLECTION:
 | 
			
		||||
    if (!(value->collection)) {
 | 
			
		||||
      if (parent->type == JENTITY_SINGLE) {
 | 
			
		||||
        collection = parent->value.collection;
 | 
			
		||||
      } else {
 | 
			
		||||
        collection = parent->pair.value.collection;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (collection) {
 | 
			
		||||
        collection->begin = current->next;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (current->type == JENTITY_SINGLE) {
 | 
			
		||||
      key = NULL;
 | 
			
		||||
      value = &(current->value);
 | 
			
		||||
    } else {
 | 
			
		||||
      key = current->pair.key;
 | 
			
		||||
      value = &(current->pair.value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (key) {
 | 
			
		||||
      dstr_free(&(current->pair.key));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!value) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (value->collection->begin) {
 | 
			
		||||
      free_json(&(value->collection->begin));
 | 
			
		||||
    if (value->type == JVAL_COLLECTION) {
 | 
			
		||||
      if (!(value->collection->begin)) {
 | 
			
		||||
        // Once all children of the collection has been freed, free the memory
 | 
			
		||||
        // allocated to the collection and the entity that holds it
 | 
			
		||||
 | 
			
		||||
        free(value->collection);
 | 
			
		||||
 | 
			
		||||
        temp = current;
 | 
			
		||||
 | 
			
		||||
        current = current->next != NULL ? current->next : current->parent;
 | 
			
		||||
 | 
			
		||||
        free(temp);
 | 
			
		||||
        temp = NULL;
 | 
			
		||||
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      current = value->collection->begin;
 | 
			
		||||
    } else {
 | 
			
		||||
      if (value->type == JVAL_STRING) {
 | 
			
		||||
        dstr_free(&(value->string));
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      temp = current;
 | 
			
		||||
 | 
			
		||||
      current = current->next != NULL ? current->next : current->parent;
 | 
			
		||||
 | 
			
		||||
      free(temp);
 | 
			
		||||
      temp = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    free(value->collection);
 | 
			
		||||
 | 
			
		||||
    break;
 | 
			
		||||
  case JVAL_STRING:
 | 
			
		||||
    dstr_free(&(entt_ptr->pair.value.string));
 | 
			
		||||
 | 
			
		||||
    break;
 | 
			
		||||
  default:
 | 
			
		||||
    break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (entt_ptr->next) {
 | 
			
		||||
    free_json(&(entt_ptr->next));
 | 
			
		||||
  }
 | 
			
		||||
  *root = NULL;
 | 
			
		||||
 | 
			
		||||
  free(*entity);
 | 
			
		||||
  *entity = NULL;
 | 
			
		||||
  SAMPLE_END(PROFILER_ID_FREE_JSON);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
jcoll_t *get_collection_from_entity(const jentity_t *entity) {
 | 
			
		||||
 
 | 
			
		||||
@@ -114,6 +114,8 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
 | 
			
		||||
  free(point_pairs);
 | 
			
		||||
 | 
			
		||||
  free_json(&root);
 | 
			
		||||
 | 
			
		||||
  SAMPLE_END(PROFILER_ID_TEAR_DOWN);
 | 
			
		||||
 | 
			
		||||
  PROFILE_END;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user