evas_textblock: allocator use same heap if it is large enough
authorAli Alzyod <ali198724@gmail.com>
Thu, 29 Aug 2019 09:43:00 +0000 (18:43 +0900)
committerYeongjong Lee <yj34.lee@samsung.com>
Mon, 16 Sep 2019 01:23:00 +0000 (10:23 +0900)
Summary:
allocator use same heap if it is large enough

I am also think to move this struct/functionality into common place, I think we can use it in other parts too

Reviewers: smohanty, Hermet, bowonryu, SanghyeonLee

Reviewed By: smohanty, SanghyeonLee

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Differential Revision: https://phab.enlightenment.org/D9770

src/lib/evas/canvas/evas_object_textblock.c

index 4061cf6..2daa600 100644 (file)
@@ -2952,12 +2952,17 @@ typedef struct _Allocator
 {
    char       stack[ALLOCATOR_SIZE];
    char      *heap;
+   size_t     heap_size;
 } Allocator;
 
 static inline void
 _allocator_init(Allocator* allocator)
 {
-   if (allocator) allocator->heap = NULL;
+   if (allocator)
+     {
+        allocator->heap = NULL;
+        allocator->heap_size = 0;
+     }
 }
 
 static inline void
@@ -2967,6 +2972,7 @@ _allocator_reset(Allocator* allocator)
      {
         free(allocator->heap);
         allocator->heap = NULL;
+        allocator->heap_size = 0;
      }
 }
 
@@ -2983,11 +2989,24 @@ _allocator_make_string(Allocator* allocator, const char* str, size_t size)
         return allocator->stack;
      }
    //fallback to heap
-   if (allocator->heap) free(allocator->heap);
+   if (allocator->heap && allocator->heap_size < (size + 1))
+     {
+        free(allocator->heap);
+        allocator->heap = NULL;
+        allocator->heap_size = 0;
+     }
 
-   allocator->heap = malloc(size + 1);
+   if (!allocator->heap)
+     {
+        allocator->heap = malloc(size + 1);
+        allocator->heap_size = (size + 1);
+     }
 
-   if (!allocator->heap) return NULL;
+   if (!allocator->heap)
+     {
+        allocator->heap_size = 0;
+        return NULL;
+     }
 
    memcpy(allocator->heap, str, size);
    allocator->heap[size] = '\0';
@@ -3154,8 +3173,9 @@ _format_fill(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const char
           {
              /* immediate - not handled here */
           }
-        _allocator_reset(&allocator);
      }
+
+   _allocator_reset(&allocator);
 }
 
 /**