Enlightenment CVS committal
Author : dj2
Project : e17
Module : libs/ewl
Dir : e17/libs/ewl/src/lib
Modified Files:
ewl_text.c ewl_text.h
Log Message:
- allocate extra space when we grab more so we can cut back on the number of
allocations
===================================================================
RCS file: /cvsroot/enlightenment/e17/libs/ewl/src/lib/ewl_text.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -3 -r1.20 -r1.21
--- ewl_text.c 26 Aug 2005 21:56:23 -0000 1.20
+++ ewl_text.c 27 Aug 2005 14:48:33 -0000 1.21
@@ -6,6 +6,9 @@
* on the btree */
#define EWL_TEXT_BTREE_CONDENSE_COUNT 5
+/* how much do we extend the text by when we need more space? */
+#define EWL_TEXT_EXTEND_VAL 4096
+
/*
* TODO
* - need a way to handle fonts that aren't in the theme .edj
@@ -394,13 +397,14 @@
}
t->text = NULL;
t->length = 0;
+ t->total_size = 0;
t->cursor_position = 0;
-
}
else if (!t->text)
{
t->text = strdup(text);
t->length = strlen(text);
+ t->total_size = t->length + 1;
if (!t->current_context)
t->current_context = ewl_text_context_default_create(t);
@@ -411,27 +415,35 @@
}
else
{
- char *new = NULL;
-
- len = strlen(text);
if (!t->current_context)
t->current_context = ewl_text_context_default_create(t);
- new = malloc(sizeof(char) * (t->length + len + 1));
+ len = strlen(text);
+ if ((t->length + len + 1) >= t->total_size)
+ {
+ int extend;
+
+ extend = len;
+ if (extend < EWL_TEXT_EXTEND_VAL)
+ extend = EWL_TEXT_EXTEND_VAL;
+
+ t->text = realloc(t->text, (t->length + extend + 1) *
sizeof(char));
+ t->total_size += extend + 1;
+ }
+
if (idx == 0)
- sprintf(new, "%s%s", text, t->text);
+ {
+ memmove(t->text + len, t->text, t->length);
+ memcpy(t->text, text, len);
+ }
else if (idx == t->length)
- sprintf(new, "%s%s", t->text, text);
+ strncat(t->text, text, len);
else
{
- memcpy(new, t->text, idx);
- memcpy(new + idx, text, len);
- memcpy(new + idx + len, t->text + idx, t->length - idx);
+ memmove(t->text + idx + len, t->text + idx, t->length -
idx);
+ memcpy(t->text + idx, text, len);
}
- FREE(t->text);
- t->text = new;
- t->length = t->length + len;
- t->text[t->length] = '\0';
+ t->length += len;
ewl_text_btree_text_context_insert(t->formatting,
t->current_context, idx, len);
t->cursor_position = idx + len;
@@ -484,7 +496,7 @@
*(old + t->cursor_position) = '\0';
ptr = old + t->cursor_position + length;
- t->text = malloc(sizeof(char) * (t->length + 1));
+ t->text = calloc((t->length + 1), sizeof(char));
snprintf(t->text, (t->length + 1), "%s%s", ((old) ? old : ""),
((ptr) ? ptr : ""));
IF_FREE(old);
}
===================================================================
RCS file: /cvsroot/enlightenment/e17/libs/ewl/src/lib/ewl_text.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -3 -r1.11 -r1.12
--- ewl_text.h 26 Aug 2005 21:56:23 -0000 1.11
+++ ewl_text.h 27 Aug 2005 14:48:33 -0000 1.12
@@ -48,6 +48,7 @@
char *text; /**< The text itself */
unsigned int length; /**< The length of the text */
+ unsigned int total_size; /**< The total size we've
alloc'd for text */
unsigned int cursor_position; /**< The cursor position */
Ewl_Text_Context *current_context; /**< The current formatting
context */
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
enlightenment-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs