Dear there,
In the following code, I think the array gchar *sym_names[] is in the fixed memory until the program exit. I'm thinking about moving it inside the function symtable_alloc() as a local variable, thus it will be allocated on the entry and will be released when the function symtable_alloc() returns.
The built hash table has all strings anyway, hence I think it is a bit waste to keep the array gchar *sym_names[] in the fixed memory all the time.
What do you think?
Code:
#include <glib.h>
#include "mathsym-data.h" /* GdkPixbuf RGBA C-Source image dump 1-byte-run-length-encoded */
#include "mathsym.h"
const guint8 *sym_data[]= {
sym_alpha, //in the mathsym-data.h
sym_beta, sym_chi, sym_delta, sym_Delta, sym_epsilon,
sym_eta, sym_gamma, sym_Gamma, sym_iota, sym_kappa,
sym_lambda, sym_Lambda, sym_mu, sym_nu, sym_omega,
sym_Omega, sym_phi, sym_Phi, sym_pi, sym_Pi, sym_psi,
sym_Psi, sym_rho, sym_sigma, sym_Sigma, sym_tau,
sym_theta, sym_Theta, sym_upsilon, sym_Upsilon, sym_varepsilon,
sym_varphi, sym_varpi, sym_varrho, sym_varsigma, sym_vartheta,
sym_xi, sym_Xi, sym_zeta
};
const gchar *sym_names[]= {
"\\alpha", "\\beta", "\\chi", "\\delta", "\\Delta",
"\\epsilon", "\\eta", "\\gamma", "\\Gamma", "\\iota",
"\\kappa", "\\lambda", "\\Lambda", "\\mu", "\\nu",
"\\omega", "\\Omega", "\\phi", "\\Phi", "\\pi",
"\\Pi", "\\psi", "\\Psi", "\\rho", "\\sigma",
"\\Sigma", "\\tau", "\\theta", "\\Theta", "\\upsilon",
"\\Upsilon", "\\varepsilon", "\\varphi", "\\varpi", "\\varrho",
"\\varsigma", "\\vartheta", "\\xi", "\\Xi", "\\zeta"
};
symtable *symtable_alloc(){
//I propose to move the declaration and definition of gchar *sym_names[] here
//to save memory. Am I thinking in the right direction?
GHashTable *table;
table = g_hash_table_new(g_str_hash, g_str_equal);
int i;
for(i=0;i<sizeof(sym_names)/sizeof(gchar *); i++)
{
g_hash_table_insert(table, (gpointer)sym_names[i], (gpointer)sym_data[i]);
}
return (symtable *)table;
}
void symtable_free(symtable *table){
g_hash_table_destroy((GHashTable *)table);
}
guint8 *symtable_lookup(symtable *table, const gchar* latex)
{
return (guint8 *) g_hash_table_lookup((GHashTable *)table, (gpointer)latex);
}