guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 200/437: Add a simple memory management wrapper.


From: Andy Wingo
Subject: [Guile-commits] 200/437: Add a simple memory management wrapper.
Date: Mon, 2 Jul 2018 05:14:17 -0400 (EDT)

wingo pushed a commit to branch lightning
in repository guile.

commit c39def9dce7aafe3f05e2266b2b06e83e3df9efc
Author: pcpa <address@hidden>
Date:   Fri Mar 29 12:10:36 2013 -0300

    Add a simple memory management wrapper.
    
        * lib/jit_memory.c: Implement a simple memory allocation wrapper
        to allow overriding calls to malloc/calloc/realloc/free, as well
        as ensuring all memory containing pointers is zero or points to
        allocated memory.
    
        * include/lightning.h, include/lightning/jit_private.h: Definitions
        for the memory allocation wrapper.
    
        * lib/Makefile.am: Update for new jit_memory.c file.
    
        * lib/jit_arm.c, lib/jit_disasm.c, lib/jit_mips.c, lib/jit_note.c,
        lib/jit_ppc.c, lib/jit_sparc.c, lib/jit_x86.c, lib/lightning.c:
        Use the new memory allocation wrapper code.
---
 ChangeLog                       |  16 +++++
 include/lightning.h             |  11 ++++
 include/lightning/jit_private.h |   4 ++
 lib/Makefile.am                 |   1 +
 lib/jit_arm.c                   |  28 ++++-----
 lib/jit_disasm.c                |  15 ++---
 lib/jit_memory.c                | 102 ++++++++++++++++++++++++++++++++
 lib/jit_mips.c                  |  19 +++---
 lib/jit_note.c                  |  27 +++++----
 lib/jit_ppc.c                   |  26 ++++----
 lib/jit_sparc.c                 |  17 +++---
 lib/jit_x86.c                   |  19 +++---
 lib/lightning.c                 | 128 ++++++++++++++++++----------------------
 13 files changed, 264 insertions(+), 149 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 82beb65..9d46844 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2013-03-29 Paulo Andrade <address@hidden>
+
+       * lib/jit_memory.c: Implement a simple memory allocation wrapper
+       to allow overriding calls to malloc/calloc/realloc/free, as well
+       as ensuring all memory containing pointers is zero or points to
+       allocated memory.
+
+       * include/lightning.h, include/lightning/jit_private.h: Definitions
+       for the memory allocation wrapper.
+
+       * lib/Makefile.am: Update for new jit_memory.c file.
+
+       * lib/jit_arm.c, lib/jit_disasm.c, lib/jit_mips.c, lib/jit_note.c,
+       lib/jit_ppc.c, lib/jit_sparc.c, lib/jit_x86.c, lib/lightning.c:
+       Use the new memory allocation wrapper code.
+
 2013-03-22 Paulo Andrade <address@hidden>
 
        * configure.ac, include/lightning/jit_private.h, lib/lightning.c:
diff --git a/include/lightning.h b/include/lightning.h
index 6844373..a5c7cc8 100644
--- a/include/lightning.h
+++ b/include/lightning.h
@@ -747,6 +747,10 @@ typedef enum {
     jit_code_x86_retval_f,     jit_code_x86_retval_d,
 } jit_code_t;
 
+typedef void* (*jit_alloc_func_ptr)    (size_t);
+typedef void* (*jit_realloc_func_ptr)  (void*, size_t);
+typedef void  (*jit_free_func_ptr)     (void*);
+
 /*
  * Prototypes
  */
@@ -882,4 +886,11 @@ extern jit_node_t *_jit_new_node_pwd(jit_state_t*, 
jit_code_t,
 #define jit_disassemble()              _jit_disassemble(_jit)
 extern void _jit_disassemble(jit_state_t*);
 
+extern void jit_set_memory_functions(jit_alloc_func_ptr,
+                                    jit_realloc_func_ptr,
+                                    jit_free_func_ptr);
+extern void jit_get_memory_functions(jit_alloc_func_ptr*,
+                                    jit_realloc_func_ptr*,
+                                    jit_free_func_ptr*);
+
 #endif /* _lightning_h */
diff --git a/include/lightning/jit_private.h b/include/lightning/jit_private.h
index e73d3c5..02847ea 100644
--- a/include/lightning/jit_private.h
+++ b/include/lightning/jit_private.h
@@ -481,6 +481,10 @@ extern jit_bool_t 
_jit_get_note(jit_state_t*,jit_uint8_t*,char**,char**,int*);
 #define jit_annotate()         _jit_annotate(_jit)
 extern void _jit_annotate(jit_state_t*);
 
+extern void jit_alloc(jit_pointer_t*, jit_word_t);
+extern void jit_realloc(jit_pointer_t*, jit_word_t, jit_word_t);
+void jit_free(jit_pointer_t*);
+
 /*
  * Externs
  */
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 3b33ce3..a723e73 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -18,6 +18,7 @@ liblightning_LTLIBRARIES = liblightning.la
 liblightningdir = $(libdir)
 liblightning_la_SOURCES =      \
        jit_disasm.c            \
+       jit_memory.c            \
        jit_note.c              \
        jit_print.c             \
        lightning.c
diff --git a/lib/jit_arm.c b/lib/jit_arm.c
index 14af57c..103307f 100644
--- a/lib/jit_arm.c
+++ b/lib/jit_arm.c
@@ -215,11 +215,9 @@ _jit_prolog(jit_state_t *_jit)
     jit_regset_set_ui(_jitc->regsav, 0);
     offset = _jitc->functions.offset;
     if (offset >= _jitc->functions.length) {
-       _jitc->functions.ptr = realloc(_jitc->functions.ptr,
-                                     (_jitc->functions.length + 16) *
-                                     sizeof(jit_function_t));
-       memset(_jitc->functions.ptr + _jitc->functions.length, 0,
-              16 * sizeof(jit_function_t));
+       jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
+                   _jitc->functions.length * sizeof(jit_function_t),
+                   (_jitc->functions.length + 16) * sizeof(jit_function_t));
        _jitc->functions.length += 16;
     }
     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@@ -234,7 +232,8 @@ _jit_prolog(jit_state_t *_jit)
     else
        _jitc->function->self.aoff = 0;
     _jitc->function->self.call = jit_call_default;
-    _jitc->function->regoff = calloc(_jitc->reglen, sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
+             _jitc->reglen * sizeof(jit_int32_t));
 
     _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
     jit_link(_jitc->function->prolog);
@@ -1748,11 +1747,10 @@ _flush_consts(jit_state_t *_jit)
 
     if (_jitc->data_info.ptr) {
        if (_jitc->data_info.offset >= _jitc->data_info.length) {
-           _jitc->data_info.ptr = realloc(_jitc->data_info.ptr,
-                                         (_jitc->data_info.length + 1024) *
-                                         sizeof(jit_data_info_t));
-           memset(_jitc->data_info.ptr + _jitc->data_info.length, 0,
-                  1024 * sizeof(jit_data_info_t));
+           jit_realloc((jit_pointer_t *)&_jitc->data_info.ptr,
+                       _jitc->data_info.length * sizeof(jit_data_info_t),
+                       (_jitc->data_info.length + 1024) *
+                       sizeof(jit_data_info_t));
            _jitc->data_info.length += 1024;
        }
        _jitc->data_info.ptr[_jitc->data_info.offset].code = word;
@@ -1796,11 +1794,9 @@ _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t 
*node)
     assert(!(flag & jit_flag_patch));
     kind |= arm_patch_node;
     if (_jitc->patches.offset >= _jitc->patches.length) {
-       _jitc->patches.ptr = realloc(_jitc->patches.ptr,
-                                   (_jitc->patches.length + 1024) *
-                                   sizeof(jit_patch_t));
-       memset(_jitc->patches.ptr + _jitc->patches.length, 0,
-              1024 * sizeof(jit_patch_t));
+       jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
+                   _jitc->patches.length * sizeof(jit_patch_t),
+                   (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
        _jitc->patches.length += 1024;
     }
     _jitc->patches.ptr[_jitc->patches.offset].kind = kind;
diff --git a/lib/jit_disasm.c b/lib/jit_disasm.c
index 27edd4c..6366ac7 100644
--- a/lib/jit_disasm.c
+++ b/lib/jit_disasm.c
@@ -111,7 +111,8 @@ jit_init_debug(void)
            else
                dyn_storage = 0;
 
-           disasm_symbols = malloc(sym_storage + dyn_storage);
+           jit_alloc((jit_pointer_t *)&disasm_symbols,
+                     (sym_storage + dyn_storage) * sizeof(asymbol *));
            sym_count = bfd_canonicalize_symtab(disasm_bfd, disasm_symbols);
            assert(sym_count >= 0);
            if (dyn_storage) {
@@ -132,10 +133,10 @@ jit_init_debug(void)
                                                            sym_count,
                                                            &disasm_synthetic);
            if (disasm_num_synthetic > 0) {
-               disasm_symbols = realloc(disasm_symbols,
-                                        sym_storage + dyn_storage +
-                                        disasm_num_synthetic *
-                                        sizeof(asymbol *));
+               jit_realloc((jit_pointer_t *)&disasm_symbols,
+                           (sym_storage + dyn_storage) * sizeof(asymbol *),
+                           (sym_storage + dyn_storage + disasm_num_synthetic) *
+                           sizeof(asymbol *));
                for (offset = 0; offset < disasm_num_synthetic; offset++)
                    disasm_symbols[disasm_num_symbols++] =
                        disasm_synthetic + offset;
@@ -165,9 +166,9 @@ jit_finish_debug(void)
 {
 #if DISASSEMBLER
     if (disasm_synthetic)
-       free(disasm_synthetic);
+       jit_free((jit_pointer_t *)&disasm_synthetic);
     if (disasm_symbols)
-       free(disasm_symbols);
+       jit_free((jit_pointer_t *)&disasm_symbols);
 #endif
 }
 
diff --git a/lib/jit_memory.c b/lib/jit_memory.c
new file mode 100644
index 0000000..ef2624f
--- /dev/null
+++ b/lib/jit_memory.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2013  Free Software Foundation, Inc.
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * Authors:
+ *     Paulo Cesar Pereira de Andrade
+ */
+
+#include <lightning.h>
+#include <lightning/jit_private.h>
+
+/*
+ * Prototypes
+ */
+static void *jit_default_alloc_func(size_t);
+static void *jit_default_realloc_func(void*, size_t);
+static void jit_default_free_func(void *);
+
+/*
+ * Initialization
+ */
+static jit_alloc_func_ptr jit_alloc_ptr = jit_default_alloc_func;
+static jit_realloc_func_ptr jit_realloc_ptr = jit_default_realloc_func;
+static jit_free_func_ptr jit_free_ptr = jit_default_free_func;
+
+/*
+ * Implementation
+ */
+void
+jit_set_memory_functions(jit_alloc_func_ptr alloc_ptr,
+                        jit_realloc_func_ptr realloc_ptr,
+                        jit_free_func_ptr free_ptr)
+{
+    if (alloc_ptr == NULL)
+       alloc_ptr = jit_default_alloc_func;
+    if (realloc_ptr == NULL)
+       realloc_ptr = jit_default_realloc_func;
+    if (free_ptr == NULL)
+       free_ptr = jit_default_free_func;
+    jit_alloc_ptr = alloc_ptr;
+    jit_realloc_ptr = realloc_ptr;
+    jit_free_ptr = free_ptr;
+}
+
+void
+jit_get_memory_functions(jit_alloc_func_ptr *alloc_ptr,
+                        jit_realloc_func_ptr *realloc_ptr,
+                        jit_free_func_ptr *free_ptr)
+{
+    *alloc_ptr = jit_alloc_ptr;
+    *realloc_ptr = jit_realloc_ptr;
+    *free_ptr = jit_free_ptr;
+}
+
+void
+jit_alloc(jit_pointer_t *ptr, jit_word_t size)
+{
+    *ptr = (*jit_alloc_ptr)(size);
+    memset(*ptr, 0, size);
+}
+
+void
+jit_realloc(jit_pointer_t *ptr, jit_word_t old_size, jit_word_t new_size)
+{
+    *ptr = (*jit_realloc_ptr)(*ptr, new_size);
+    if (old_size < new_size)
+       memset((jit_int8_t*)*ptr + old_size, 0, new_size - old_size);
+}
+
+void
+jit_free(jit_pointer_t *ptr)
+{
+    (*jit_free_ptr)(*ptr);
+    *ptr = NULL;
+}
+
+static void *
+jit_default_alloc_func(size_t size)
+{
+    return (malloc(size));
+}
+
+static void *
+jit_default_realloc_func(void *ptr, size_t size)
+{
+    return (realloc(ptr, size));
+}
+
+static void
+jit_default_free_func(void *ptr)
+{
+    free(ptr);
+}
diff --git a/lib/jit_mips.c b/lib/jit_mips.c
index 3806b4f..0d92756 100644
--- a/lib/jit_mips.c
+++ b/lib/jit_mips.c
@@ -124,11 +124,9 @@ _jit_prolog(jit_state_t *_jit)
     jit_regset_set_ui(_jitc->regsav, 0);
     offset = _jitc->functions.offset;
     if (offset >= _jitc->functions.length) {
-       _jitc->functions.ptr = realloc(_jitc->functions.ptr,
-                                     (_jitc->functions.length + 16) *
-                                     sizeof(jit_function_t));
-       memset(_jitc->functions.ptr + _jitc->functions.length, 0,
-              16 * sizeof(jit_function_t));
+       jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
+                   _jitc->functions.length * sizeof(jit_function_t),
+                   (_jitc->functions.length + 16) * sizeof(jit_function_t));
        _jitc->functions.length += 16;
     }
     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@@ -136,7 +134,8 @@ _jit_prolog(jit_state_t *_jit)
     _jitc->function->self.argi = _jitc->function->self.argf =
        _jitc->function->self.aoff = _jitc->function->self.alen = 0;
     _jitc->function->self.call = jit_call_default;
-    _jitc->function->regoff = calloc(_jitc->reglen, sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
+             _jitc->reglen * sizeof(jit_int32_t));
 
     _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
     jit_link(_jitc->function->prolog);
@@ -1317,11 +1316,9 @@ _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t 
*node)
        flag = node->u.n->flag;
     assert(!(flag & jit_flag_patch));
     if (_jitc->patches.offset >= _jitc->patches.length) {
-       _jitc->patches.ptr = realloc(_jitc->patches.ptr,
-                                   (_jitc->patches.length + 1024) *
-                                   sizeof(jit_patch_t));
-       memset(_jitc->patches.ptr + _jitc->patches.length, 0,
-              1024 * sizeof(jit_patch_t));
+       jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
+                   _jitc->patches.length * sizeof(jit_patch_t),
+                   (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
        _jitc->patches.length += 1024;
     }
     _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
diff --git a/lib/jit_note.c b/lib/jit_note.c
index 6b30a02..cedabd9 100644
--- a/lib/jit_note.c
+++ b/lib/jit_note.c
@@ -134,7 +134,7 @@ _jit_annotate(jit_state_t *_jit)
        length = sizeof(jit_line_t) * note->length;
        assert(_jitc->note.base + length < _jit->data.ptr + _jit->data.length);
        memcpy(_jitc->note.base, note->lines, length);
-       free(note->lines);
+       jit_free((jit_pointer_t *)&note->lines);
        note->lines = (jit_line_t *)_jitc->note.base;
        _jitc->note.base += length;
     }
@@ -148,13 +148,13 @@ _jit_annotate(jit_state_t *_jit)
            assert(_jitc->note.base + length <
                   _jit->data.ptr + _jit->data.length);
            memcpy(_jitc->note.base, line->linenos, length);
-           free(line->linenos);
+           jit_free((jit_pointer_t *)&line->linenos);
            line->linenos = (jit_int32_t *)_jitc->note.base;
            _jitc->note.base += length;
            assert(_jitc->note.base + length <
                   _jit->data.ptr + _jit->data.length);
            memcpy(_jitc->note.base, line->offsets, length);
-           free(line->offsets);
+           jit_free((jit_pointer_t *)&line->offsets);
            line->offsets = (jit_int32_t *)_jitc->note.base;
            _jitc->note.base += length;
        }
@@ -190,10 +190,12 @@ _jit_set_note(jit_state_t *_jit, jit_note_t *note,
        else {
            /* line or offset changed */
            if ((line->length & 15) == 0) {
-               line->linenos = realloc(line->linenos, (line->length + 17) *
-                                       sizeof(jit_int32_t));
-               line->offsets = realloc(line->offsets, (line->length + 17) *
-                                       sizeof(jit_int32_t));
+               jit_realloc((jit_pointer_t *)&line->linenos,
+                           line->length * sizeof(jit_int32_t),
+                           (line->length + 17) * sizeof(jit_int32_t));
+               jit_realloc((jit_pointer_t *)&line->offsets,
+                           line->length * sizeof(jit_int32_t),
+                           (line->length + 17) * sizeof(jit_int32_t));
            }
            if (index < note->length) {
                memmove(line->linenos + index + 1, line->linenos + index,
@@ -268,10 +270,11 @@ new_line(jit_int32_t index, jit_note_t *note,
     jit_line_t         *line;
 
     if (note->lines == NULL)
-       note->lines = malloc(16 * sizeof(jit_line_t));
+       jit_alloc((jit_pointer_t *)&note->lines, 16 * sizeof(jit_line_t));
     else if ((note->length & 15) == 15)
-       note->lines = realloc(note->lines,
-                             (note->length + 17) * sizeof(jit_line_t));
+       jit_realloc((jit_pointer_t *)&note->lines,
+                   note->length * sizeof(jit_line_t),
+                   (note->length + 17) * sizeof(jit_line_t));
 
     if (index < note->length)
        memmove(note->lines + index + 1, note->lines + index,
@@ -281,9 +284,9 @@ new_line(jit_int32_t index, jit_note_t *note,
 
     line->file = file;
     line->length = 1;
-    line->linenos = malloc(16 * sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&line->linenos, 16 * sizeof(jit_int32_t));
     line->linenos[0] = lineno;
-    line->offsets = malloc(16 * sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&line->offsets, 16 * sizeof(jit_int32_t));
     line->offsets[0] = offset;
 }
 
diff --git a/lib/jit_ppc.c b/lib/jit_ppc.c
index 79e4a32..ec51882 100644
--- a/lib/jit_ppc.c
+++ b/lib/jit_ppc.c
@@ -135,11 +135,9 @@ _jit_prolog(jit_state_t *_jit)
     jit_regset_set_ui(_jitc->regsav, 0);
     offset = _jitc->functions.offset;
     if (offset >= _jitc->functions.length) {
-       _jitc->functions.ptr = realloc(_jitc->functions.ptr,
-                                     (_jitc->functions.length + 16) *
-                                     sizeof(jit_function_t));
-       memset(_jitc->functions.ptr + _jitc->functions.length, 0,
-              16 * sizeof(jit_function_t));
+       jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
+                   _jitc->functions.length * sizeof(jit_function_t),
+                   (_jitc->functions.length + 16) * sizeof(jit_function_t));
        _jitc->functions.length += 16;
     }
     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@@ -149,7 +147,8 @@ _jit_prolog(jit_state_t *_jit)
     /* float conversion */
     _jitc->function->self.aoff = alloca_offset - 8;
     _jitc->function->self.call = jit_call_default;
-    _jitc->function->regoff = calloc(_jitc->reglen, sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
+             _jitc->reglen * sizeof(jit_int32_t));
 
     _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
     jit_link(_jitc->function->prolog);
@@ -1281,9 +1280,10 @@ _emit_code(jit_state_t *_jit)
                     * a common practice as first jit instruction */
                    if (_jitc->prolog.offset >= _jitc->prolog.length) {
                        _jitc->prolog.length += 16;
-                       _jitc->prolog.ptr = realloc(_jitc->prolog.ptr,
-                                                   _jitc->prolog.length *
-                                                   sizeof(jit_word_t));
+                       jit_realloc((jit_pointer_t *)&_jitc->prolog.ptr,
+                                   (_jitc->prolog.length - 16) *
+                                   sizeof(jit_word_t),
+                                   _jitc->prolog.length * sizeof(jit_word_t));
                    }
                    _jitc->prolog.ptr[_jitc->prolog.offset++] = _jit->pc.w;
                    /* function descriptor */
@@ -1401,11 +1401,9 @@ _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t 
*node)
        flag = node->u.n->flag;
     assert(!(flag & jit_flag_patch));
     if (_jitc->patches.offset >= _jitc->patches.length) {
-       _jitc->patches.ptr = realloc(_jitc->patches.ptr,
-                                   (_jitc->patches.length + 1024) *
-                                   sizeof(jit_patch_t));
-       memset(_jitc->patches.ptr + _jitc->patches.length, 0,
-              1024 * sizeof(jit_patch_t));
+       jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
+                   _jitc->patches.length * sizeof(jit_patch_t),
+                   (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
        _jitc->patches.length += 1024;
     }
     _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
diff --git a/lib/jit_sparc.c b/lib/jit_sparc.c
index b21c70a..43072a2 100644
--- a/lib/jit_sparc.c
+++ b/lib/jit_sparc.c
@@ -109,11 +109,9 @@ _jit_prolog(jit_state_t *_jit)
     jit_regset_set_ui(_jitc->regsav, 0);
     offset = _jitc->functions.offset;
     if (offset >= _jitc->functions.length) {
-       _jitc->functions.ptr = realloc(_jitc->functions.ptr,
-                                     (_jitc->functions.length + 16) *
-                                     sizeof(jit_function_t));
-       memset(_jitc->functions.ptr + _jitc->functions.length, 0,
-              16 * sizeof(jit_function_t));
+       jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
+                   _jitc->functions.length * sizeof(jit_function_t),
+                   (_jitc->functions.length + 16) * sizeof(jit_function_t));
        _jitc->functions.length += 16;
     }
     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@@ -123,7 +121,8 @@ _jit_prolog(jit_state_t *_jit)
     /* float conversion */
     _jitc->function->self.aoff = -8;
      _jitc->function->self.call = jit_call_default;
-    _jitc->function->regoff = calloc(_jitc->reglen, sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
+             _jitc->reglen * sizeof(jit_int32_t));
 
     _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
     jit_link(_jitc->function->prolog);
@@ -1163,9 +1162,9 @@ _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t 
*node)
        flag = node->u.n->flag;
     assert(!(flag & jit_flag_patch));
     if (_jitc->patches.offset >= _jitc->patches.length) {
-       _jitc->patches.ptr = realloc(_jitc->patches.ptr,
-                                   (_jitc->patches.length + 1024) *
-                                   sizeof(jit_patch_t));
+       jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
+                   _jitc->patches.length * sizeof(jit_patch_t),
+                   (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
        memset(_jitc->patches.ptr + _jitc->patches.length, 0,
               1024 * sizeof(jit_patch_t));
        _jitc->patches.length += 1024;
diff --git a/lib/jit_x86.c b/lib/jit_x86.c
index a3e7d33..daf826f 100644
--- a/lib/jit_x86.c
+++ b/lib/jit_x86.c
@@ -298,11 +298,9 @@ _jit_prolog(jit_state_t *_jit)
     jit_regset_set_ui(_jitc->regsav, 0);
     offset = _jitc->functions.offset;
     if (offset >= _jitc->functions.length) {
-       _jitc->functions.ptr = realloc(_jitc->functions.ptr,
-                                     (_jitc->functions.length + 16) *
-                                     sizeof(jit_function_t));
-       memset(_jitc->functions.ptr + _jitc->functions.length, 0,
-              16 * sizeof(jit_function_t));
+       jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
+                   _jitc->functions.length * sizeof(jit_function_t),
+                   (_jitc->functions.length + 16) * sizeof(jit_function_t));
        _jitc->functions.length += 16;
     }
     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@@ -312,7 +310,8 @@ _jit_prolog(jit_state_t *_jit)
     /* sse/x87 conversion */
     _jitc->function->self.aoff = -8;
     _jitc->function->self.call = jit_call_default;
-    _jitc->function->regoff = calloc(_jitc->reglen, sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
+             _jitc->reglen * sizeof(jit_int32_t));
 
     _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
     jit_link(_jitc->function->prolog);
@@ -1695,11 +1694,9 @@ _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t 
*node)
        flag = node->u.n->flag;
     assert(!(flag & jit_flag_patch));
     if (_jitc->patches.offset >= _jitc->patches.length) {
-       _jitc->patches.ptr = realloc(_jitc->patches.ptr,
-                                   (_jitc->patches.length + 1024) *
-                                   sizeof(jit_patch_t));
-       memset(_jitc->patches.ptr + _jitc->patches.length, 0,
-              1024 * sizeof(jit_patch_t));
+       jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
+                   _jitc->patches.length * sizeof(jit_patch_t),
+                   (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
        _jitc->patches.length += 1024;
     }
     _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
diff --git a/lib/lightning.c b/lib/lightning.c
index 66fcca7..7c2dcad 100644
--- a/lib/lightning.c
+++ b/lib/lightning.c
@@ -59,8 +59,6 @@ static void _bmp_clear(jit_state_t*);
     memset(_jitc->blockmask.ptr, 0,                                    \
           _jitc->blockmask.length * sizeof(jit_word_t))
 
-static void _bmp_zero(jit_state_t*);
-
 #define bmp_set(bit)                   _bmp_set(_jit, bit)
 static void _bmp_set(jit_state_t*, jit_word_t);
 
@@ -361,16 +359,15 @@ _jit_data(jit_state_t *_jit, jit_pointer_t data,
        size = (_jit->data.length + length + 4096) & - 4095;
        assert(size >= _jit->data.length);
        if (_jit->data.ptr == NULL)
-           _jit->data.ptr = calloc(1, size);
-       else {
-           _jit->data.ptr = realloc(_jit->data.ptr, size);
-           memset(_jit->data.ptr + _jit->data.length, 0,
-                  size - _jit->data.length);
-       }
+           jit_alloc((jit_pointer_t *)&_jit->data.ptr, size);
+       else
+           jit_realloc((jit_pointer_t *)&_jit->data.ptr,
+                       _jit->data.length, size);
        _jit->data.length = size;
     }
     if (_jitc->data.table == NULL)
-       _jitc->data.table = calloc(_jitc->data.size = 16, sizeof(jit_node_t*));
+       jit_alloc((jit_pointer_t *)&_jitc->data.table,
+                 (_jitc->data.size = 16) * sizeof(jit_node_t*));
 
     key = hash_data(data, length) & (_jitc->data.size - 1);
     node = _jitc->data.table[key];
@@ -415,7 +412,8 @@ _jit_data(jit_state_t *_jit, jit_pointer_t data,
            jit_node_t   *next;
            jit_node_t   *temp;
 
-           hash = calloc(_jitc->data.size << 1, sizeof(jit_node_t*));
+           jit_alloc((jit_pointer_t *)&hash,
+                     (_jitc->data.size << 1) * sizeof(jit_node_t*));
            for (i = 0; i < _jitc->data.size; i++) {
                temp = _jitc->data.table[i];
                for (; temp; temp = next) {
@@ -426,7 +424,7 @@ _jit_data(jit_state_t *_jit, jit_pointer_t data,
                    hash[key] = temp;
                }
            }
-           free(_jitc->data.table);
+           jit_free((jit_pointer_t *)&_jitc->data.table);
            _jitc->data.table = hash;
            _jitc->data.size <<= 1;
        }
@@ -442,16 +440,16 @@ _new_pool(jit_state_t *_jit)
     jit_int32_t                 offset;
 
     if (_jitc->pool.offset >= _jitc->pool.length) {
-       jit_node_t      **ptr;
-       jit_int32_t       length;
+       jit_int32_t      length;
 
        length = _jitc->pool.length + 16;
-       ptr = realloc(_jitc->pool.ptr, length * sizeof(jit_node_t));
-       memset(ptr + _jitc->pool.length, 0, 16 * sizeof(jit_node_t));
-       _jitc->pool.ptr = ptr;
+       jit_realloc((jit_pointer_t *)&_jitc->pool.ptr,
+                   _jitc->pool.length * sizeof(jit_node_t *),
+                   length * sizeof(jit_node_t *));
        _jitc->pool.length = length;
     }
-   _jitc->pool.ptr[_jitc->pool.offset] = calloc(sizeof(jit_node_t), 1024);
+    jit_alloc((jit_pointer_t *)(_jitc->pool.ptr + _jitc->pool.offset),
+             sizeof(jit_node_t) * 1024);
     list = _jitc->pool.ptr[_jitc->pool.offset];
     for (offset = 1; offset < 1024; offset++, list++)
        list->next = list + 1;
@@ -523,15 +521,15 @@ static void
 _bmp_init(jit_state_t *_jit)
 {
     _jitc->blockmask.length = 16;
-    _jitc->blockmask.ptr = calloc(sizeof(jit_word_t), _jitc->blockmask.length);
+    jit_alloc((jit_pointer_t *)&_jitc->blockmask.ptr,
+             sizeof(jit_word_t) * _jitc->blockmask.length);
 }
 
 static void
 _bmp_clear(jit_state_t *_jit)
 {
     _jitc->blockmask.length = 0;
-    free(_jitc->blockmask.ptr);
-    _jitc->blockmask.ptr = NULL;
+    jit_free((jit_pointer_t *)&_jitc->blockmask.ptr);
 }
 
 static void
@@ -543,10 +541,9 @@ _bmp_set(jit_state_t *_jit, jit_word_t bit)
     boff = 1LL << (bit & (__WORDSIZE - 1));
     if (woff >= _jitc->blockmask.length) {
        jit_word_t      length = (woff + 16) & -16;
-       _jitc->blockmask.ptr = realloc(_jitc->blockmask.ptr,
-                                      length * sizeof(jit_word_t));
-       memset(_jitc->blockmask.ptr + _jitc->blockmask.length,
-              0, (length - _jitc->blockmask.length) * sizeof(jit_word_t));
+       jit_realloc((jit_pointer_t *)&_jitc->blockmask.ptr,
+                   _jitc->blockmask.length * sizeof(jit_word_t),
+                   length * sizeof(jit_word_t));
        _jitc->blockmask.length = length;
     }
     _jitc->blockmask.ptr[woff] |= boff;
@@ -580,8 +577,8 @@ jit_new_state(void)
 {
     jit_state_t                *_jit;
 
-    _jit = calloc(1, sizeof(jit_state_t));
-    _jitc = calloc(1, sizeof(jit_compiler_t));
+    jit_alloc((jit_pointer_t *)&_jit, sizeof(jit_state_t));
+    jit_alloc((jit_pointer_t *)&_jitc, sizeof(jit_compiler_t));
     jit_regset_new(_jitc->regarg);
     jit_regset_new(_jitc->regsav);
     jit_regset_new(_jitc->reglive);
@@ -590,21 +587,24 @@ jit_new_state(void)
 
     jit_init();
 
-    _jitc->spill = calloc(_jitc->reglen, sizeof(jit_node_t*));
-    _jitc->gen = calloc(_jitc->reglen, sizeof(jit_int32_t));
-    _jitc->values = calloc(_jitc->reglen, sizeof(jit_value_t));
-
-    _jitc->patches.ptr = calloc(_jitc->patches.length = 1024,
-                               sizeof(jit_patch_t));
-    _jitc->functions.ptr = calloc(_jitc->functions.length = 16,
-                                 sizeof(jit_function_t));
-    _jitc->pool.ptr = calloc(_jitc->pool.length = 16,
-                            sizeof(jit_node_t*));
-    _jitc->blocks.ptr = calloc(_jitc->blocks.length = 16,
-                              sizeof(jit_block_t));
+    jit_alloc((jit_pointer_t *)&_jitc->spill,
+             _jitc->reglen * sizeof(jit_node_t*));
+    jit_alloc((jit_pointer_t *)&_jitc->gen,
+             _jitc->reglen * sizeof(jit_int32_t));
+    jit_alloc((jit_pointer_t *)&_jitc->values,
+             _jitc->reglen * sizeof(jit_value_t));
+
+    jit_alloc((jit_pointer_t *)&_jitc->patches.ptr,
+             (_jitc->patches.length = 1024) * sizeof(jit_patch_t));
+    jit_alloc((jit_pointer_t *)&_jitc->functions.ptr,
+             (_jitc->functions.length = 16) * sizeof(jit_function_t));
+    jit_alloc((jit_pointer_t *)&_jitc->pool.ptr,
+             (_jitc->pool.length = 16) * sizeof(jit_node_t*));
+    jit_alloc((jit_pointer_t *)&_jitc->blocks.ptr,
+             (_jitc->blocks.length = 16) * sizeof(jit_block_t));
 #if __arm__ && DISASSEMBLER
-    _jitc->data_info.ptr = calloc(_jitc->data_info.length = 1024,
-                                 sizeof(jit_data_info_t));
+    jit_alloc((jit_pointer_t *)&_jitc->data_info.ptr,
+             (_jitc->data_info.length = 1024) * sizeof(jit_data_info_t));
 #endif
 
     /* allocate at most one extra note in case jit_name() is
@@ -627,37 +627,29 @@ _jit_clear_state(jit_state_t *_jit)
 
     bmp_clear();
 
-    free(_jitc->data.table);
-    _jitc->data.table = NULL;
+    jit_free((jit_pointer_t *)&_jitc->data.table);
     _jitc->data.size = _jitc->data.count = 0;
 
-    free(_jitc->spill);
-    _jitc->spill = NULL;
-    free(_jitc->gen);
-    _jitc->gen = NULL;
-    free(_jitc->values);
-    _jitc->values = NULL;
+    jit_free((jit_pointer_t *)&_jitc->spill);
+    jit_free((jit_pointer_t *)&_jitc->gen);
+    jit_free((jit_pointer_t *)&_jitc->values);
 
-    free(_jitc->blocks.ptr);
-    _jitc->blocks.ptr = NULL;
+    jit_free((jit_pointer_t *)&_jitc->blocks.ptr);
 
-    free(_jitc->patches.ptr);
-    _jitc->patches.ptr = NULL;
+    jit_free((jit_pointer_t *)&_jitc->patches.ptr);
     _jitc->patches.offset = _jitc->patches.length = 0;
 
     for (offset = 0; offset < _jitc->functions.offset; offset++) {
        function = _jitc->functions.ptr + offset;
-       free(function->regoff);
-       function->regoff = NULL;
+       jit_free((jit_pointer_t *)&function->regoff);
     }
-    free(_jitc->functions.ptr);
+    jit_free((jit_pointer_t *)&_jitc->functions.ptr);
     _jitc->functions.offset = _jitc->functions.length = 0;
     _jitc->function = NULL;
 
-    for (offset = 0; offset < _jitc->pool.length; offset++)
-       free(_jitc->pool.ptr[offset]);
-    free(_jitc->pool.ptr);
-    _jitc->pool.ptr = NULL;
+    for (offset = 0; offset < _jitc->pool.offset; offset++)
+       jit_free((jit_pointer_t *)(_jitc->pool.ptr + offset));
+    jit_free((jit_pointer_t *)&_jitc->pool.ptr);
     _jitc->pool.offset = _jitc->pool.length = 0;
     _jitc->list = NULL;
 
@@ -666,16 +658,14 @@ _jit_clear_state(jit_state_t *_jit)
     _jitc->note.base = NULL;
 
 #if __arm__ && DISASSEMBLER
-    free(_jitc->data_info.ptr);
-    _jitc->data_info.ptr = NULL;
+    jit_free((jit_pointer_t *)&_jitc->data_info.ptr);
 #endif
 
 #if __powerpc64__
-    free(_jitc->prolog.ptr);
-    _jitc->prolog.ptr = NULL;
+    jit_free((jit_pointer_t *)&_jitc->prolog.ptr);
 #endif
 
-    free(_jitc);
+    jit_free((jit_pointer_t *)&_jitc);
 }
 
 void
@@ -683,7 +673,7 @@ _jit_destroy_state(jit_state_t *_jit)
 {
     munmap(_jit->code.ptr, _jit->code.length);
     munmap(_jit->data.ptr, _jit->data.length);
-    free(_jit);
+    jit_free((jit_pointer_t *)&_jit);
 }
 
 jit_node_t *
@@ -893,9 +883,9 @@ _jit_link(jit_state_t *_jit, jit_node_t *node)
        jit_word_t        length;
 
        length = _jitc->blocks.length + 16;
-       block = realloc(_jitc->blocks.ptr, length * sizeof(jit_block_t));
-       memset(block + _jitc->blocks.length, 0, 16 * sizeof(jit_block_t));
-       _jitc->blocks.ptr = block;
+       jit_realloc((jit_pointer_t *)&_jitc->blocks.ptr,
+                   _jitc->blocks.length * sizeof(jit_block_t),
+                   length * sizeof(jit_block_t));
        _jitc->blocks.length = length;
     }
     block = _jitc->blocks.ptr + _jitc->blocks.offset;
@@ -1316,7 +1306,7 @@ _jit_optimize(jit_state_t *_jit)
               MAP_PRIVATE | MAP_ANON, -1, 0);
     assert(ptr != MAP_FAILED);
     memcpy(ptr, _jit->data.ptr, _jitc->data.offset);
-    free(_jit->data.ptr);
+    jit_free((jit_pointer_t *)&_jit->data.ptr);
     _jit->data.ptr = ptr;
 
     /* to be filled with note contents once offsets are known */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]