Changeset 1234 for trunk/lib/lang/lang.c


Ignore:
Timestamp:
07/11/11 01:57:16 (23 months ago)
Author:
reyalp
Message:

memory saving patch from philmoz in http://chdk.setepontos.com/index.php?topic=650.msg70056#msg70056

  • changed some struct values from int to short (conf.c, gui_menu.h, platform.h)
  • gui code cleanup plus some helper functions in gui_draw
  • changed palette display under Miscellaneous stuff menu to use the same UI as the color picker in Visual settings (better and consistent UI, plus removes a chunk of code)
  • packed version of font_8x16_uni.h default font, plus new tool to create packed version (this is done during the build so if changes are need to the font they can be done in font_8x16_uni.h).
  • Change 'strings' pointer table in lang.c to an offset table (halves table size).
  • Changes lang.c to use one buffer for the strings instead of allocating a new buffer for each string,
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/lang/lang.c

    r515 r1234  
    44//------------------------------------------------------------------- 
    55 
    6 static char **strings = NULL; 
     6// Array of offsets to each string stored in the 'sbuf' array 
     7static unsigned short *strings = NULL; 
    78static int count = 0; 
     9 
     10// Strings stored in one large umalloc'ed array rather than individually (reduces memory overhead) 
     11// Previously when loading a language file a block of memory was allocated to read the file 
     12// then another block was allocated for each string in the data read from the file, then the original 
     13// block was freed. This version uses the same buffer to read the file and store the strings. 
     14static char *sbuf = NULL;    
     15static int sbuflen = 0; 
    816 
    917//------------------------------------------------------------------- 
     
    1119    int i; 
    1220 
    13     if (strings) { 
    14        for (i=0; i<count; ++i) 
    15            if (strings[i]) ufree(strings[i]); 
     21    // Free old buffer (should not happen since this is only called once at startup) 
     22    if (strings) 
     23    { 
    1624       ufree(strings); 
     25       strings = 0; 
    1726       count = 0; 
    1827    } 
    1928 
     29    // Free old buffer (should not happen since this is only called once at startup) 
     30    if (sbuf) 
     31    { 
     32        ufree(sbuf); 
     33        sbuf = 0; 
     34        sbuflen = 0; 
     35    } 
     36 
     37    // Allocate offset buffer 
    2038    ++num; 
    21     strings = umalloc(num*sizeof(char*)); 
     39    strings = umalloc(num*sizeof(unsigned short)); 
    2240    if (strings) { 
    23         memset(strings, 0, num*sizeof(char*)); 
     41        memset(strings, 0, num*sizeof(unsigned short)); 
    2442        count = num; 
    2543    } 
    26  
    2744} 
    2845 
    2946//------------------------------------------------------------------- 
    30 static void lang_add_string(int num, const char *str) { 
     47// Add a string to the buffer. String is cleaned up to convert special 
     48// characters and the offset of the string in 'sbuf' is stored in the 
     49// strings array. 
     50static void lang_add_string(int num, char *str) { 
    3151    int f=0; 
    3252    char *p; 
    3353 
    34     if (strings && num<count) { 
    35        if (strings[num]) ufree(strings[num]); 
    36        p = strings[num] = umalloc(strlen(str)+1); 
     54    if (num<count) { 
     55       p = str; 
     56       strings[num] = (unsigned short)(str - sbuf); 
    3757       if (p) { 
    3858           for (; *str; ++str) { 
     
    5575 
    5676//------------------------------------------------------------------- 
    57 void lang_load_from_mem(char *buf) { 
     77// Parse the 'sbuf' memory and build the strings offset array 
     78void lang_load_from_sbuf() 
     79{ 
    5880    char *p, *s, *e; 
    5981    int i; 
    6082     
    61     e = buf-1; 
     83    e = sbuf-1; 
    6284    while(e) { 
    6385        p = e+1; 
     
    82104 
    83105//------------------------------------------------------------------- 
     106// Allocate a new 'sbuf' array if needed. 
     107// If the existing one is large enough use it instead of getting a new block of memory. 
     108int alloc_sbuf(int len) 
     109{ 
     110    if (len > sbuflen) 
     111    { 
     112        if (sbuf) ufree(sbuf); 
     113        sbuf = umalloc(len); 
     114        if (sbuf) 
     115        { 
     116            sbuflen = len; 
     117        } 
     118    } 
     119    return (sbuf != 0); 
     120} 
     121//------------------------------------------------------------------- 
     122// Load the default language data from memory. 
     123void lang_load_from_mem(char *buf) { 
     124    if (alloc_sbuf(strlen(buf)+1)) 
     125    { 
     126        memcpy(sbuf,buf,sbuflen); 
     127        lang_load_from_sbuf(); 
     128    } 
     129} 
     130 
     131//------------------------------------------------------------------- 
     132// Load language data from a file. 
    84133void lang_load_from_file(const char *filename) { 
    85134    int f, size; 
     
    91140        size = (stat((char*)filename, &st)==0)?st.st_size:0; 
    92141        if (size) { 
    93             buf = umalloc(size+1); 
    94             if (buf) { 
    95                 size = read(f, buf, size); 
    96                 buf[size]=0; 
    97                 lang_load_from_mem(buf); 
    98                 ufree(buf); 
     142            if (alloc_sbuf(size+1)) 
     143            { 
     144                size = read(f, sbuf, size); 
     145                sbuf[size]=0; 
     146                lang_load_from_sbuf(); 
    99147            } 
    100148        } 
     
    104152 
    105153//------------------------------------------------------------------- 
     154// Return the string corresponding to the 'str' index. 
    106155char* lang_str(int str) { 
    107156    if (str && str<0x1000) { 
    108         return (strings && str<count && strings[str])?strings[str]:""; 
     157        return (strings && str<count && strings[str])?sbuf+strings[str]:""; 
    109158    } else { // not ID, just char* 
    110159        return (char*)str; 
Note: See TracChangeset for help on using the changeset viewer.