changeset 507:67453c7d623e

Clean up malloc() and friends to xmalloc/xzmalloc/xstrdup, and remove redundant if(null) and memset() occurrences.
author Rob Landley <rob@landley.net>
date Sat, 10 Nov 2007 17:58:25 -0600
parents 68a80edf5e12
children 651cfa5d202a
files tcc.c tccasm.c tccelf.c
diffstat 3 files changed, 43 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/tcc.c	Sat Nov 10 13:37:05 2007 -0600
+++ b/tcc.c	Sat Nov 10 17:58:25 2007 -0600
@@ -174,32 +174,30 @@
     return 1;
 }
 
-static void *tcc_malloc(unsigned long size)
+static void *xmalloc(unsigned long size)
 {
     void *ptr = malloc(size);
     if (!ptr && size) error("memory full");
     return ptr;
 }
 
-static void *tcc_mallocz(unsigned long size)
-{
-    void *ptr;
-    ptr = tcc_malloc(size);
+static void *xzmalloc(unsigned long size)
+{
+    void *ptr = xmalloc(size);
     memset(ptr, 0, size);
     return ptr;
 }
 
-static inline void *tcc_realloc(void *ptr, unsigned long size)
+static inline void *xrealloc(void *ptr, unsigned long size)
 {
     void *ptr1 = realloc(ptr, size);
     if (!ptr1 && size) error("memory full");
     return ptr1;
 }
 
-static char *tcc_strdup(const char *str)
-{
-    char *ptr;
-    ptr = tcc_malloc(strlen(str) + 1);
+static char *xstrdup(const char *str)
+{
+    char *ptr = xmalloc(strlen(str) + 1);
     strcpy(ptr, str);
     return ptr;
 }
@@ -219,22 +217,20 @@
             nb_alloc = 1;
         else
             nb_alloc = nb * 2;
-        pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
-        if (!pp)
-            error("memory full");
+        pp = xrealloc(pp, nb_alloc * sizeof(void *));
         *ptab = pp;
     }
     pp[nb++] = data;
     *nb_ptr = nb;
 }
 
-/* symbol allocator */
+/* Allocate a batch of new symbol structures. */
 static Sym *__sym_malloc(void)
 {
     Sym *sym_pool, *sym, *last_sym;
     int i;
 
-    sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
+    sym_pool = xmalloc(SYM_POOL_NB * sizeof(Sym));
 
     last_sym = sym_free_first;
     sym = sym_pool;
@@ -247,6 +243,8 @@
     return last_sym;
 }
 
+// Allocate a symbol structure by popping most recently freed one off a linked
+// list, and malloc() a bunch of new ones at once if the list is empty.
 static inline Sym *sym_malloc(void)
 {
     Sym *sym;
@@ -257,6 +255,8 @@
     return sym;
 }
 
+// Push symbol onto the free list.
+
 static inline void sym_free(Sym *sym)
 {
     sym->next = sym_free_first;
@@ -267,7 +267,7 @@
 {
     Section *sec;
 
-    sec = tcc_mallocz(sizeof(Section) + strlen(name));
+    sec = xzmalloc(sizeof(Section) + strlen(name));
     strcpy(sec->name, name);
     sec->sh_type = sh_type;
     sec->sh_flags = sh_flags;
@@ -312,9 +312,7 @@
         size = 1;
     while (size < new_size)
         size = size * 2;
-    data = tcc_realloc(sec->data, size);
-    if (!data)
-        error("memory full");
+    data = xrealloc(sec->data, size);
     memset(data + sec->data_allocated, 0, size - sec->data_allocated);
     sec->data = data;
     sec->data_allocated = size;
@@ -581,13 +579,11 @@
     /* expand token table if needed */
     i = tok_ident - TOK_IDENT;
     if ((i % TOK_ALLOC_INCR) == 0) {
-        ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
-        if (!ptable)
-            error("memory full");
+        ptable = xrealloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
         table_ident = ptable;
     }
 
-    ts = tcc_malloc(sizeof(TokenSym) + len);
+    ts = xmalloc(sizeof(TokenSym) + len);
     table_ident[i] = ts;
     ts->tok = tok_ident++;
     ts->sym_define = NULL;
@@ -641,9 +637,7 @@
         size = 8; /* no need to allocate a too small first string */
     while (size < new_size)
         size = size * 2;
-    data = tcc_realloc(cstr->data_allocated, size);
-    if (!data)
-        error("memory full");
+    data = xrealloc(cstr->data_allocated, size);
     cstr->data_allocated = data;
     cstr->size_allocated = size;
     cstr->data = data;
@@ -947,11 +941,7 @@
     fd = open(filename, O_RDONLY | O_BINARY);
     if (fd < 0)
         return NULL;
-    bf = tcc_malloc(sizeof(BufferedFile));
-    if (!bf) {
-        close(fd);
-        return NULL;
-    }
+    bf = xmalloc(sizeof(BufferedFile));
     bf->fd = fd;
     bf->buf_ptr = bf->buffer;
     bf->buf_end = bf->buffer;
@@ -1444,9 +1434,7 @@
     } else {
         len = s->allocated_len * 2;
     }
-    str = tcc_realloc(s->str, len * sizeof(int));
-    if (!str)
-        error("memory full");
+    str = xrealloc(s->str, len * sizeof(int));
     s->allocated_len = len;
     s->str = str;
     return str;
@@ -1854,9 +1842,7 @@
 #ifdef INC_DEBUG
     printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
 #endif
-    e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
-    if (!e)
-        return;
+    e = xmalloc(sizeof(CachedInclude) + strlen(filename));
     e->type = type;
     strcpy(e->filename, filename);
     e->ifndef_macro = ifndef_macro;
@@ -8579,9 +8565,7 @@
     bf->fd = -1;
     /* XXX: avoid copying */
     len = strlen(str);
-    buf = tcc_malloc(len + 1);
-    if (!buf)
-        return -1;
+    buf = xmalloc(len + 1);
     memcpy(buf, str, len);
     buf[len] = CH_EOB;
     bf->buf_ptr = buf;
@@ -8695,7 +8679,7 @@
         s = s1->sections[i];
         if (s->sh_flags & SHF_ALLOC) {
             if (s->sh_type == SHT_NOBITS)
-                s->data = tcc_mallocz(s->data_offset);
+                s->data = xzmalloc(s->data_offset);
             s->sh_addr = (unsigned long)s->data;
         }
     }
@@ -8773,7 +8757,7 @@
     TokenSym *ts;
     int i, c;
 
-    s = tcc_state = tcc_mallocz(sizeof(TCCState));
+    s = tcc_state = xzmalloc(sizeof(TCCState));
     tcc_state->output_type = TCC_OUTPUT_MEMORY;
 
     /* init isidnum table */
@@ -8935,7 +8919,7 @@
 {
     char *pathname1;
     
-    pathname1 = tcc_strdup(pathname);
+    pathname1 = xstrdup(pathname);
     dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
     return 0;
 }
@@ -8944,7 +8928,7 @@
 {
     char *pathname1;
     
-    pathname1 = tcc_strdup(pathname);
+    pathname1 = xstrdup(pathname);
     dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
     return 0;
 }
@@ -9073,7 +9057,7 @@
 {
     char *pathname1;
     
-    pathname1 = tcc_strdup(pathname);
+    pathname1 = xstrdup(pathname);
     dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
     return 0;
 }
@@ -9431,7 +9415,7 @@
         while (*str != '\0' && !is_space(*str))
             str++;
         len = str - s1;
-        arg = tcc_malloc(len + 1);
+        arg = xmalloc(len + 1);
         memcpy(arg, s1, len);
         arg[len] = '\0';
         dynarray_add((void ***)&argv, &argc, arg);
--- a/tccasm.c	Sat Nov 10 13:37:05 2007 -0600
+++ b/tccasm.c	Sat Nov 10 17:58:25 2007 -0600
@@ -700,8 +700,7 @@
     BufferedFile *bf, *saved_file;
     int saved_parse_flags, *saved_macro_ptr;
 
-    bf = tcc_malloc(sizeof(BufferedFile));
-    memset(bf, 0, sizeof(BufferedFile));
+    bf = xzmalloc(sizeof(BufferedFile));
     bf->fd = -1;
     bf->buf_ptr = str;
     bf->buf_end = str + len;
@@ -831,7 +830,7 @@
             }
             if (tok != TOK_STR)
                 expect("string constant");
-            op->constraint = tcc_malloc(tokc.cstr->size);
+            op->constraint = xmalloc(tokc.cstr->size);
             strcpy(op->constraint, tokc.cstr->data);
             next();
             skip('(');
--- a/tccelf.c	Sat Nov 10 13:37:05 2007 -0600
+++ b/tccelf.c	Sat Nov 10 17:58:25 2007 -0600
@@ -321,8 +321,8 @@
     int type, sym_index;
 
     nb_syms = s->data_offset / sizeof(Elf32_Sym);
-    new_syms = tcc_malloc(nb_syms * sizeof(Elf32_Sym));
-    old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
+    new_syms = xmalloc(nb_syms * sizeof(Elf32_Sym));
+    old_to_new_syms = xmalloc(nb_syms * sizeof(int));
 
     /* first pass for local symbols */
     p = (Elf32_Sym *)s->data;
@@ -664,9 +664,7 @@
         n = 1;
         while (index >= n)
             n *= 2;
-        tab = tcc_realloc(s1->got_offsets, n * sizeof(unsigned long));
-        if (!tab)
-            error("memory full");
+        tab = xrealloc(s1->got_offsets, n * sizeof(unsigned long));
         s1->got_offsets = tab;
         memset(s1->got_offsets + s1->nb_got_offsets, 0,
                (n - s1->nb_got_offsets) * sizeof(unsigned long));
@@ -1274,7 +1272,7 @@
                 int nb_syms;
                 /* shared library case : we simply export all the global symbols */
                 nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
-                s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms);
+                s1->symtab_to_dynsym = xzmalloc(sizeof(int) * nb_syms);
                 for(sym = (Elf32_Sym *)symtab_section->data + 1; 
                     sym < sym_end;
                     sym++) {
@@ -1322,7 +1320,7 @@
     shnum = s1->nb_sections;
 
     /* this array is used to reorder sections in the output file */
-    section_order = tcc_malloc(sizeof(int) * shnum);
+    section_order = xmalloc(sizeof(int) * shnum);
     section_order[0] = 0;
     sh_order_index = 1;
     
@@ -1366,7 +1364,7 @@
     }
 
     /* allocate program segment headers */
-    phdr = tcc_mallocz(phnum * sizeof(Elf32_Phdr));
+    phdr = xzmalloc(phnum * sizeof(Elf32_Phdr));
         
     if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
         file_offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);
@@ -1758,7 +1756,7 @@
 {
     void *data;
 
-    data = tcc_malloc(size);
+    data = xmalloc(size);
     lseek(fd, file_offset, SEEK_SET);
     read(fd, data, size);
     return data;
@@ -1807,7 +1805,7 @@
     /* read sections */
     shdr = load_data(fd, file_offset + ehdr.e_shoff, 
                      sizeof(Elf32_Shdr) * ehdr.e_shnum);
-    sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
+    sm_table = xzmalloc(sizeof(SectionMergeInfo) * ehdr.e_shnum);
     
     /* load section names */
     sh = &shdr[ehdr.e_shstrndx];
@@ -1925,7 +1923,7 @@
     sm = sm_table;
 
     /* resolve symbols */
-    old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
+    old_to_new_syms = xzmalloc(nb_syms * sizeof(int));
 
     sym = symtab + 1;
     for(i = 1; i < nb_syms; i++, sym++) {
@@ -2038,7 +2036,7 @@
     const uint8_t *ar_index;
     Elf32_Sym *sym;
 
-    data = tcc_malloc(size);
+    data = xmalloc(size);
     if (read(fd, data, size) != size)
         goto fail;
     nsyms = get_be32(data);
@@ -2201,7 +2199,7 @@
     //    printf("loading dll '%s'\n", soname);
 
     /* add the dll and its level */
-    dllref = tcc_malloc(sizeof(DLLReference) + strlen(soname));
+    dllref = xmalloc(sizeof(DLLReference) + strlen(soname));
     dllref->level = level;
     strcpy(dllref->name, soname);
     dynarray_add((void ***)&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);