From f7cfd0f8339bcfa13a89a7ce0a1c005c77bf05c7 Mon Sep 17 00:00:00 2001 From: Ali Alzyod Date: Thu, 29 Aug 2019 18:43:00 +0900 Subject: [PATCH] evas_textblock: allocator use same heap if it is large enough 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 | 30 +++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/lib/evas/canvas/evas_object_textblock.c b/src/lib/evas/canvas/evas_object_textblock.c index 433597fa27..5f8ae177fc 100644 --- a/src/lib/evas/canvas/evas_object_textblock.c +++ b/src/lib/evas/canvas/evas_object_textblock.c @@ -2897,12 +2897,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 @@ -2912,6 +2917,7 @@ _allocator_reset(Allocator* allocator) { free(allocator->heap); allocator->heap = NULL; + allocator->heap_size = 0; } } @@ -2928,11 +2934,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'; @@ -3099,8 +3118,9 @@ _format_fill(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const char { /* immediate - not handled here */ } - _allocator_reset(&allocator); } + + _allocator_reset(&allocator); } /**