Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1NEh93-0005pL-QK for mharc-grub-devel@gnu.org; Sun, 29 Nov 2009 05:36:45 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NEh91-0005nu-HG for grub-devel@gnu.org; Sun, 29 Nov 2009 05:36:43 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NEh8w-0005ko-IU for grub-devel@gnu.org; Sun, 29 Nov 2009 05:36:42 -0500 Received: from [199.232.76.173] (port=47392 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NEh8w-0005kl-93 for grub-devel@gnu.org; Sun, 29 Nov 2009 05:36:38 -0500 Received: from 197.red-80-32-81.staticip.rima-tde.net ([80.32.81.197]:49060 helo=mail.pina.cat) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NEh8u-0000ts-EW for grub-devel@gnu.org; Sun, 29 Nov 2009 05:36:38 -0500 Received: from pinux (82-45-164-87.cable.ubr06.hari.blueyonder.co.uk [82.45.164.87]) by mail.pina.cat (Postfix) with ESMTP id C45EB288FBA3F for ; Sun, 29 Nov 2009 11:36:27 +0100 (CET) Received: by pinux (Postfix, from userid 1000) id 6B9BED800A; Sun, 29 Nov 2009 10:36:25 +0000 (GMT) Date: Sun, 29 Nov 2009 10:36:25 +0000 From: Carles Pina i Estany To: grub-devel@gnu.org Message-ID: <20091129103625.GA31541@pina.cat> References: <20091129004950.GA7856@pina.cat> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="OgqxwSJOaUobr8KG" Content-Disposition: inline In-Reply-To: <20091129004950.GA7856@pina.cat> User-Agent: Mutt/1.5.20 (2009-06-14) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: Re: gettext.c patch: reuse memory X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 Nov 2009 10:36:43 -0000 --OgqxwSJOaUobr8KG Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, Find attached a new version of the same patch (same approach). Cheers, -- Carles Pina i Estany http://pinux.info --OgqxwSJOaUobr8KG Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="ChangeLog.memory" 2009-11-29 Carles Pina i Estany * gettext/gettext.c: Include `'. Define grub_gettext_msg, grub_gettext_msg_list. (grub_gettext_gettranslation_from_position): Return static const char * and not static char * (grub_gettext_translate): Add the translated strings into a list, returns from the list if existing there. (grub_gettext_delete_list): Delete the list. (grub_gettext_env_write_lang): Call grub_gettext_delete_list changes the language. (GRUB_MOD_FINI): Delete the list. --OgqxwSJOaUobr8KG Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="gettext_memory2.patch" === modified file 'gettext/gettext.c' --- gettext/gettext.c 2009-11-24 21:42:14 +0000 +++ gettext/gettext.c 2009-11-29 10:34:01 +0000 @@ -17,6 +17,7 @@ * along with GRUB. If not, see . */ +#include #include #include #include @@ -33,7 +34,6 @@ http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html . */ - static grub_file_t fd_mo; static int grub_gettext_offsetoriginal; @@ -41,6 +41,16 @@ static int grub_gettext_max; static const char *(*grub_gettext_original) (const char *s); +struct grub_gettext_msg +{ + struct grub_gettext_msg *next; + const char *name; + + const char *translated; +}; + +struct grub_gettext_msg *grub_gettext_msg_list = NULL; + #define GETTEXT_MAGIC_NUMBER 0 #define GETTEXT_FILE_FORMAT 4 #define GETTEXT_NUMBER_OF_STRINGS 8 @@ -79,7 +89,7 @@ grub_gettext_getstring_from_offset (grub translation[length] = '\0'; } -static char * +static const char * grub_gettext_gettranslation_from_position (int position) { int offsettranslation; @@ -130,9 +140,18 @@ static const char * grub_gettext_translate (const char *orig) { char *current_string; - char *ret; + const char *ret; int min, max, current; + int found = 0; + + struct grub_gettext_msg *cur; + + cur = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_gettext_msg_list), + orig); + + if (cur) + return cur->translated; if (fd_mo == 0) return orig; @@ -142,7 +161,7 @@ grub_gettext_translate (const char *orig current = (max + min) / 2; - while (current != min && current != max) + while (current != min && current != max && found == 0) { current_string = grub_gettext_getstring_from_position (current); @@ -160,13 +179,31 @@ grub_gettext_translate (const char *orig else if (grub_strcmp (current_string, orig) == 0) { grub_free (current_string); - return grub_gettext_gettranslation_from_position (current); + found = 1; } current = (max + min) / 2; } - ret = grub_malloc (grub_strlen (orig) + 1); - grub_strcpy (ret, orig); + ret = found ? grub_gettext_gettranslation_from_position (current) : orig; + + if (found) + { + cur = grub_zalloc (sizeof (*cur)); + + if (cur) + { + cur->name = grub_strdup (orig); + if (cur->name) + { + cur->translated = ret; + grub_list_push (GRUB_AS_LIST_P (&grub_gettext_msg_list), + GRUB_AS_LIST (cur)); + } + } + else + grub_errno = GRUB_ERR_NONE; + } + return ret; } @@ -259,12 +296,26 @@ grub_gettext_init_ext (const char *lang) } } +static void +grub_gettext_delete_list () +{ + struct grub_gettext_msg *item; + + while ((item = + grub_list_pop (GRUB_AS_LIST_P (&grub_gettext_msg_list))) != 0) + { + // Don't delete the translated message because could be in use. + } +} + static char * grub_gettext_env_write_lang (struct grub_env_var *var __attribute__ ((unused)), const char *val) { grub_gettext_init_ext (val); + grub_gettext_delete_list (); + return grub_strdup (val); } @@ -307,5 +358,7 @@ GRUB_MOD_FINI (gettext) if (fd_mo != 0) grub_file_close (fd_mo); + grub_gettext_delete_list (); + grub_gettext = grub_gettext_original; } --OgqxwSJOaUobr8KG--