1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-01 20:30:28 +02:00

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.
This commit is contained in:
pcpa 2013-03-29 12:10:36 -03:00
parent be9068f2ee
commit c39def9dce
13 changed files with 263 additions and 148 deletions

View file

@ -1,3 +1,19 @@
2013-03-29 Paulo Andrade <pcpa@gnu.org>
* 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 <pcpa@gnu.org> 2013-03-22 Paulo Andrade <pcpa@gnu.org>
* configure.ac, include/lightning/jit_private.h, lib/lightning.c: * configure.ac, include/lightning/jit_private.h, lib/lightning.c:

View file

@ -747,6 +747,10 @@ typedef enum {
jit_code_x86_retval_f, jit_code_x86_retval_d, jit_code_x86_retval_f, jit_code_x86_retval_d,
} jit_code_t; } 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 * 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) #define jit_disassemble() _jit_disassemble(_jit)
extern void _jit_disassemble(jit_state_t*); 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 */ #endif /* _lightning_h */

View file

@ -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) #define jit_annotate() _jit_annotate(_jit)
extern void _jit_annotate(jit_state_t*); 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 * Externs
*/ */

View file

@ -18,6 +18,7 @@ liblightning_LTLIBRARIES = liblightning.la
liblightningdir = $(libdir) liblightningdir = $(libdir)
liblightning_la_SOURCES = \ liblightning_la_SOURCES = \
jit_disasm.c \ jit_disasm.c \
jit_memory.c \
jit_note.c \ jit_note.c \
jit_print.c \ jit_print.c \
lightning.c lightning.c

View file

@ -215,11 +215,9 @@ _jit_prolog(jit_state_t *_jit)
jit_regset_set_ui(_jitc->regsav, 0); jit_regset_set_ui(_jitc->regsav, 0);
offset = _jitc->functions.offset; offset = _jitc->functions.offset;
if (offset >= _jitc->functions.length) { if (offset >= _jitc->functions.length) {
_jitc->functions.ptr = realloc(_jitc->functions.ptr, jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
(_jitc->functions.length + 16) * _jitc->functions.length * sizeof(jit_function_t),
sizeof(jit_function_t)); (_jitc->functions.length + 16) * sizeof(jit_function_t));
memset(_jitc->functions.ptr + _jitc->functions.length, 0,
16 * sizeof(jit_function_t));
_jitc->functions.length += 16; _jitc->functions.length += 16;
} }
_jitc->function = _jitc->functions.ptr + _jitc->functions.offset++; _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@ -234,7 +232,8 @@ _jit_prolog(jit_state_t *_jit)
else else
_jitc->function->self.aoff = 0; _jitc->function->self.aoff = 0;
_jitc->function->self.call = jit_call_default; _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); _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
jit_link(_jitc->function->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.ptr) {
if (_jitc->data_info.offset >= _jitc->data_info.length) { if (_jitc->data_info.offset >= _jitc->data_info.length) {
_jitc->data_info.ptr = realloc(_jitc->data_info.ptr, jit_realloc((jit_pointer_t *)&_jitc->data_info.ptr,
(_jitc->data_info.length + 1024) * _jitc->data_info.length * sizeof(jit_data_info_t),
sizeof(jit_data_info_t)); (_jitc->data_info.length + 1024) *
memset(_jitc->data_info.ptr + _jitc->data_info.length, 0, sizeof(jit_data_info_t));
1024 * sizeof(jit_data_info_t));
_jitc->data_info.length += 1024; _jitc->data_info.length += 1024;
} }
_jitc->data_info.ptr[_jitc->data_info.offset].code = word; _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)); assert(!(flag & jit_flag_patch));
kind |= arm_patch_node; kind |= arm_patch_node;
if (_jitc->patches.offset >= _jitc->patches.length) { if (_jitc->patches.offset >= _jitc->patches.length) {
_jitc->patches.ptr = realloc(_jitc->patches.ptr, jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
(_jitc->patches.length + 1024) * _jitc->patches.length * sizeof(jit_patch_t),
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; _jitc->patches.length += 1024;
} }
_jitc->patches.ptr[_jitc->patches.offset].kind = kind; _jitc->patches.ptr[_jitc->patches.offset].kind = kind;

View file

@ -111,7 +111,8 @@ jit_init_debug(void)
else else
dyn_storage = 0; 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); sym_count = bfd_canonicalize_symtab(disasm_bfd, disasm_symbols);
assert(sym_count >= 0); assert(sym_count >= 0);
if (dyn_storage) { if (dyn_storage) {
@ -132,10 +133,10 @@ jit_init_debug(void)
sym_count, sym_count,
&disasm_synthetic); &disasm_synthetic);
if (disasm_num_synthetic > 0) { if (disasm_num_synthetic > 0) {
disasm_symbols = realloc(disasm_symbols, jit_realloc((jit_pointer_t *)&disasm_symbols,
sym_storage + dyn_storage + (sym_storage + dyn_storage) * sizeof(asymbol *),
disasm_num_synthetic * (sym_storage + dyn_storage + disasm_num_synthetic) *
sizeof(asymbol *)); sizeof(asymbol *));
for (offset = 0; offset < disasm_num_synthetic; offset++) for (offset = 0; offset < disasm_num_synthetic; offset++)
disasm_symbols[disasm_num_symbols++] = disasm_symbols[disasm_num_symbols++] =
disasm_synthetic + offset; disasm_synthetic + offset;
@ -165,9 +166,9 @@ jit_finish_debug(void)
{ {
#if DISASSEMBLER #if DISASSEMBLER
if (disasm_synthetic) if (disasm_synthetic)
free(disasm_synthetic); jit_free((jit_pointer_t *)&disasm_synthetic);
if (disasm_symbols) if (disasm_symbols)
free(disasm_symbols); jit_free((jit_pointer_t *)&disasm_symbols);
#endif #endif
} }

102
lib/jit_memory.c Normal file
View file

@ -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);
}

View file

@ -124,11 +124,9 @@ _jit_prolog(jit_state_t *_jit)
jit_regset_set_ui(_jitc->regsav, 0); jit_regset_set_ui(_jitc->regsav, 0);
offset = _jitc->functions.offset; offset = _jitc->functions.offset;
if (offset >= _jitc->functions.length) { if (offset >= _jitc->functions.length) {
_jitc->functions.ptr = realloc(_jitc->functions.ptr, jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
(_jitc->functions.length + 16) * _jitc->functions.length * sizeof(jit_function_t),
sizeof(jit_function_t)); (_jitc->functions.length + 16) * sizeof(jit_function_t));
memset(_jitc->functions.ptr + _jitc->functions.length, 0,
16 * sizeof(jit_function_t));
_jitc->functions.length += 16; _jitc->functions.length += 16;
} }
_jitc->function = _jitc->functions.ptr + _jitc->functions.offset++; _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.argi = _jitc->function->self.argf =
_jitc->function->self.aoff = _jitc->function->self.alen = 0; _jitc->function->self.aoff = _jitc->function->self.alen = 0;
_jitc->function->self.call = jit_call_default; _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); _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
jit_link(_jitc->function->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; flag = node->u.n->flag;
assert(!(flag & jit_flag_patch)); assert(!(flag & jit_flag_patch));
if (_jitc->patches.offset >= _jitc->patches.length) { if (_jitc->patches.offset >= _jitc->patches.length) {
_jitc->patches.ptr = realloc(_jitc->patches.ptr, jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
(_jitc->patches.length + 1024) * _jitc->patches.length * sizeof(jit_patch_t),
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; _jitc->patches.length += 1024;
} }
_jitc->patches.ptr[_jitc->patches.offset].inst = instr; _jitc->patches.ptr[_jitc->patches.offset].inst = instr;

View file

@ -134,7 +134,7 @@ _jit_annotate(jit_state_t *_jit)
length = sizeof(jit_line_t) * note->length; length = sizeof(jit_line_t) * note->length;
assert(_jitc->note.base + length < _jit->data.ptr + _jit->data.length); assert(_jitc->note.base + length < _jit->data.ptr + _jit->data.length);
memcpy(_jitc->note.base, note->lines, 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; note->lines = (jit_line_t *)_jitc->note.base;
_jitc->note.base += length; _jitc->note.base += length;
} }
@ -148,13 +148,13 @@ _jit_annotate(jit_state_t *_jit)
assert(_jitc->note.base + length < assert(_jitc->note.base + length <
_jit->data.ptr + _jit->data.length); _jit->data.ptr + _jit->data.length);
memcpy(_jitc->note.base, line->linenos, 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; line->linenos = (jit_int32_t *)_jitc->note.base;
_jitc->note.base += length; _jitc->note.base += length;
assert(_jitc->note.base + length < assert(_jitc->note.base + length <
_jit->data.ptr + _jit->data.length); _jit->data.ptr + _jit->data.length);
memcpy(_jitc->note.base, line->offsets, 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; line->offsets = (jit_int32_t *)_jitc->note.base;
_jitc->note.base += length; _jitc->note.base += length;
} }
@ -190,10 +190,12 @@ _jit_set_note(jit_state_t *_jit, jit_note_t *note,
else { else {
/* line or offset changed */ /* line or offset changed */
if ((line->length & 15) == 0) { if ((line->length & 15) == 0) {
line->linenos = realloc(line->linenos, (line->length + 17) * jit_realloc((jit_pointer_t *)&line->linenos,
sizeof(jit_int32_t)); line->length * sizeof(jit_int32_t),
line->offsets = realloc(line->offsets, (line->length + 17) * (line->length + 17) * sizeof(jit_int32_t));
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) { if (index < note->length) {
memmove(line->linenos + index + 1, line->linenos + index, 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; jit_line_t *line;
if (note->lines == NULL) 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) else if ((note->length & 15) == 15)
note->lines = realloc(note->lines, jit_realloc((jit_pointer_t *)&note->lines,
(note->length + 17) * sizeof(jit_line_t)); note->length * sizeof(jit_line_t),
(note->length + 17) * sizeof(jit_line_t));
if (index < note->length) if (index < note->length)
memmove(note->lines + index + 1, note->lines + index, 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->file = file;
line->length = 1; 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->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; line->offsets[0] = offset;
} }

View file

@ -135,11 +135,9 @@ _jit_prolog(jit_state_t *_jit)
jit_regset_set_ui(_jitc->regsav, 0); jit_regset_set_ui(_jitc->regsav, 0);
offset = _jitc->functions.offset; offset = _jitc->functions.offset;
if (offset >= _jitc->functions.length) { if (offset >= _jitc->functions.length) {
_jitc->functions.ptr = realloc(_jitc->functions.ptr, jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
(_jitc->functions.length + 16) * _jitc->functions.length * sizeof(jit_function_t),
sizeof(jit_function_t)); (_jitc->functions.length + 16) * sizeof(jit_function_t));
memset(_jitc->functions.ptr + _jitc->functions.length, 0,
16 * sizeof(jit_function_t));
_jitc->functions.length += 16; _jitc->functions.length += 16;
} }
_jitc->function = _jitc->functions.ptr + _jitc->functions.offset++; _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@ -149,7 +147,8 @@ _jit_prolog(jit_state_t *_jit)
/* float conversion */ /* float conversion */
_jitc->function->self.aoff = alloca_offset - 8; _jitc->function->self.aoff = alloca_offset - 8;
_jitc->function->self.call = jit_call_default; _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); _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
jit_link(_jitc->function->prolog); jit_link(_jitc->function->prolog);
@ -1281,9 +1280,10 @@ _emit_code(jit_state_t *_jit)
* a common practice as first jit instruction */ * a common practice as first jit instruction */
if (_jitc->prolog.offset >= _jitc->prolog.length) { if (_jitc->prolog.offset >= _jitc->prolog.length) {
_jitc->prolog.length += 16; _jitc->prolog.length += 16;
_jitc->prolog.ptr = realloc(_jitc->prolog.ptr, jit_realloc((jit_pointer_t *)&_jitc->prolog.ptr,
_jitc->prolog.length * (_jitc->prolog.length - 16) *
sizeof(jit_word_t)); sizeof(jit_word_t),
_jitc->prolog.length * sizeof(jit_word_t));
} }
_jitc->prolog.ptr[_jitc->prolog.offset++] = _jit->pc.w; _jitc->prolog.ptr[_jitc->prolog.offset++] = _jit->pc.w;
/* function descriptor */ /* function descriptor */
@ -1401,11 +1401,9 @@ _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node)
flag = node->u.n->flag; flag = node->u.n->flag;
assert(!(flag & jit_flag_patch)); assert(!(flag & jit_flag_patch));
if (_jitc->patches.offset >= _jitc->patches.length) { if (_jitc->patches.offset >= _jitc->patches.length) {
_jitc->patches.ptr = realloc(_jitc->patches.ptr, jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
(_jitc->patches.length + 1024) * _jitc->patches.length * sizeof(jit_patch_t),
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; _jitc->patches.length += 1024;
} }
_jitc->patches.ptr[_jitc->patches.offset].inst = instr; _jitc->patches.ptr[_jitc->patches.offset].inst = instr;

View file

@ -109,11 +109,9 @@ _jit_prolog(jit_state_t *_jit)
jit_regset_set_ui(_jitc->regsav, 0); jit_regset_set_ui(_jitc->regsav, 0);
offset = _jitc->functions.offset; offset = _jitc->functions.offset;
if (offset >= _jitc->functions.length) { if (offset >= _jitc->functions.length) {
_jitc->functions.ptr = realloc(_jitc->functions.ptr, jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
(_jitc->functions.length + 16) * _jitc->functions.length * sizeof(jit_function_t),
sizeof(jit_function_t)); (_jitc->functions.length + 16) * sizeof(jit_function_t));
memset(_jitc->functions.ptr + _jitc->functions.length, 0,
16 * sizeof(jit_function_t));
_jitc->functions.length += 16; _jitc->functions.length += 16;
} }
_jitc->function = _jitc->functions.ptr + _jitc->functions.offset++; _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@ -123,7 +121,8 @@ _jit_prolog(jit_state_t *_jit)
/* float conversion */ /* float conversion */
_jitc->function->self.aoff = -8; _jitc->function->self.aoff = -8;
_jitc->function->self.call = jit_call_default; _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); _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
jit_link(_jitc->function->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; flag = node->u.n->flag;
assert(!(flag & jit_flag_patch)); assert(!(flag & jit_flag_patch));
if (_jitc->patches.offset >= _jitc->patches.length) { if (_jitc->patches.offset >= _jitc->patches.length) {
_jitc->patches.ptr = realloc(_jitc->patches.ptr, jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
(_jitc->patches.length + 1024) * _jitc->patches.length * sizeof(jit_patch_t),
sizeof(jit_patch_t)); (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
memset(_jitc->patches.ptr + _jitc->patches.length, 0, memset(_jitc->patches.ptr + _jitc->patches.length, 0,
1024 * sizeof(jit_patch_t)); 1024 * sizeof(jit_patch_t));
_jitc->patches.length += 1024; _jitc->patches.length += 1024;

View file

@ -298,11 +298,9 @@ _jit_prolog(jit_state_t *_jit)
jit_regset_set_ui(_jitc->regsav, 0); jit_regset_set_ui(_jitc->regsav, 0);
offset = _jitc->functions.offset; offset = _jitc->functions.offset;
if (offset >= _jitc->functions.length) { if (offset >= _jitc->functions.length) {
_jitc->functions.ptr = realloc(_jitc->functions.ptr, jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
(_jitc->functions.length + 16) * _jitc->functions.length * sizeof(jit_function_t),
sizeof(jit_function_t)); (_jitc->functions.length + 16) * sizeof(jit_function_t));
memset(_jitc->functions.ptr + _jitc->functions.length, 0,
16 * sizeof(jit_function_t));
_jitc->functions.length += 16; _jitc->functions.length += 16;
} }
_jitc->function = _jitc->functions.ptr + _jitc->functions.offset++; _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
@ -312,7 +310,8 @@ _jit_prolog(jit_state_t *_jit)
/* sse/x87 conversion */ /* sse/x87 conversion */
_jitc->function->self.aoff = -8; _jitc->function->self.aoff = -8;
_jitc->function->self.call = jit_call_default; _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); _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
jit_link(_jitc->function->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; flag = node->u.n->flag;
assert(!(flag & jit_flag_patch)); assert(!(flag & jit_flag_patch));
if (_jitc->patches.offset >= _jitc->patches.length) { if (_jitc->patches.offset >= _jitc->patches.length) {
_jitc->patches.ptr = realloc(_jitc->patches.ptr, jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
(_jitc->patches.length + 1024) * _jitc->patches.length * sizeof(jit_patch_t),
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; _jitc->patches.length += 1024;
} }
_jitc->patches.ptr[_jitc->patches.offset].inst = instr; _jitc->patches.ptr[_jitc->patches.offset].inst = instr;

View file

@ -59,8 +59,6 @@ static void _bmp_clear(jit_state_t*);
memset(_jitc->blockmask.ptr, 0, \ memset(_jitc->blockmask.ptr, 0, \
_jitc->blockmask.length * sizeof(jit_word_t)) _jitc->blockmask.length * sizeof(jit_word_t))
static void _bmp_zero(jit_state_t*);
#define bmp_set(bit) _bmp_set(_jit, bit) #define bmp_set(bit) _bmp_set(_jit, bit)
static void _bmp_set(jit_state_t*, jit_word_t); 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; size = (_jit->data.length + length + 4096) & - 4095;
assert(size >= _jit->data.length); assert(size >= _jit->data.length);
if (_jit->data.ptr == NULL) if (_jit->data.ptr == NULL)
_jit->data.ptr = calloc(1, size); jit_alloc((jit_pointer_t *)&_jit->data.ptr, size);
else { else
_jit->data.ptr = realloc(_jit->data.ptr, size); jit_realloc((jit_pointer_t *)&_jit->data.ptr,
memset(_jit->data.ptr + _jit->data.length, 0, _jit->data.length, size);
size - _jit->data.length);
}
_jit->data.length = size; _jit->data.length = size;
} }
if (_jitc->data.table == NULL) 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); key = hash_data(data, length) & (_jitc->data.size - 1);
node = _jitc->data.table[key]; 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 *next;
jit_node_t *temp; 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++) { for (i = 0; i < _jitc->data.size; i++) {
temp = _jitc->data.table[i]; temp = _jitc->data.table[i];
for (; temp; temp = next) { for (; temp; temp = next) {
@ -426,7 +424,7 @@ _jit_data(jit_state_t *_jit, jit_pointer_t data,
hash[key] = temp; hash[key] = temp;
} }
} }
free(_jitc->data.table); jit_free((jit_pointer_t *)&_jitc->data.table);
_jitc->data.table = hash; _jitc->data.table = hash;
_jitc->data.size <<= 1; _jitc->data.size <<= 1;
} }
@ -442,16 +440,16 @@ _new_pool(jit_state_t *_jit)
jit_int32_t offset; jit_int32_t offset;
if (_jitc->pool.offset >= _jitc->pool.length) { if (_jitc->pool.offset >= _jitc->pool.length) {
jit_node_t **ptr; jit_int32_t length;
jit_int32_t length;
length = _jitc->pool.length + 16; length = _jitc->pool.length + 16;
ptr = realloc(_jitc->pool.ptr, length * sizeof(jit_node_t)); jit_realloc((jit_pointer_t *)&_jitc->pool.ptr,
memset(ptr + _jitc->pool.length, 0, 16 * sizeof(jit_node_t)); _jitc->pool.length * sizeof(jit_node_t *),
_jitc->pool.ptr = ptr; length * sizeof(jit_node_t *));
_jitc->pool.length = length; _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]; list = _jitc->pool.ptr[_jitc->pool.offset];
for (offset = 1; offset < 1024; offset++, list++) for (offset = 1; offset < 1024; offset++, list++)
list->next = list + 1; list->next = list + 1;
@ -523,15 +521,15 @@ static void
_bmp_init(jit_state_t *_jit) _bmp_init(jit_state_t *_jit)
{ {
_jitc->blockmask.length = 16; _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 static void
_bmp_clear(jit_state_t *_jit) _bmp_clear(jit_state_t *_jit)
{ {
_jitc->blockmask.length = 0; _jitc->blockmask.length = 0;
free(_jitc->blockmask.ptr); jit_free((jit_pointer_t *)&_jitc->blockmask.ptr);
_jitc->blockmask.ptr = NULL;
} }
static void static void
@ -543,10 +541,9 @@ _bmp_set(jit_state_t *_jit, jit_word_t bit)
boff = 1LL << (bit & (__WORDSIZE - 1)); boff = 1LL << (bit & (__WORDSIZE - 1));
if (woff >= _jitc->blockmask.length) { if (woff >= _jitc->blockmask.length) {
jit_word_t length = (woff + 16) & -16; jit_word_t length = (woff + 16) & -16;
_jitc->blockmask.ptr = realloc(_jitc->blockmask.ptr, jit_realloc((jit_pointer_t *)&_jitc->blockmask.ptr,
length * sizeof(jit_word_t)); _jitc->blockmask.length * sizeof(jit_word_t),
memset(_jitc->blockmask.ptr + _jitc->blockmask.length, length * sizeof(jit_word_t));
0, (length - _jitc->blockmask.length) * sizeof(jit_word_t));
_jitc->blockmask.length = length; _jitc->blockmask.length = length;
} }
_jitc->blockmask.ptr[woff] |= boff; _jitc->blockmask.ptr[woff] |= boff;
@ -580,8 +577,8 @@ jit_new_state(void)
{ {
jit_state_t *_jit; jit_state_t *_jit;
_jit = calloc(1, sizeof(jit_state_t)); jit_alloc((jit_pointer_t *)&_jit, sizeof(jit_state_t));
_jitc = calloc(1, sizeof(jit_compiler_t)); jit_alloc((jit_pointer_t *)&_jitc, sizeof(jit_compiler_t));
jit_regset_new(_jitc->regarg); jit_regset_new(_jitc->regarg);
jit_regset_new(_jitc->regsav); jit_regset_new(_jitc->regsav);
jit_regset_new(_jitc->reglive); jit_regset_new(_jitc->reglive);
@ -590,21 +587,24 @@ jit_new_state(void)
jit_init(); jit_init();
_jitc->spill = calloc(_jitc->reglen, sizeof(jit_node_t*)); jit_alloc((jit_pointer_t *)&_jitc->spill,
_jitc->gen = calloc(_jitc->reglen, sizeof(jit_int32_t)); _jitc->reglen * sizeof(jit_node_t*));
_jitc->values = calloc(_jitc->reglen, sizeof(jit_value_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));
_jitc->patches.ptr = calloc(_jitc->patches.length = 1024, jit_alloc((jit_pointer_t *)&_jitc->patches.ptr,
sizeof(jit_patch_t)); (_jitc->patches.length = 1024) * sizeof(jit_patch_t));
_jitc->functions.ptr = calloc(_jitc->functions.length = 16, jit_alloc((jit_pointer_t *)&_jitc->functions.ptr,
sizeof(jit_function_t)); (_jitc->functions.length = 16) * sizeof(jit_function_t));
_jitc->pool.ptr = calloc(_jitc->pool.length = 16, jit_alloc((jit_pointer_t *)&_jitc->pool.ptr,
sizeof(jit_node_t*)); (_jitc->pool.length = 16) * sizeof(jit_node_t*));
_jitc->blocks.ptr = calloc(_jitc->blocks.length = 16, jit_alloc((jit_pointer_t *)&_jitc->blocks.ptr,
sizeof(jit_block_t)); (_jitc->blocks.length = 16) * sizeof(jit_block_t));
#if __arm__ && DISASSEMBLER #if __arm__ && DISASSEMBLER
_jitc->data_info.ptr = calloc(_jitc->data_info.length = 1024, jit_alloc((jit_pointer_t *)&_jitc->data_info.ptr,
sizeof(jit_data_info_t)); (_jitc->data_info.length = 1024) * sizeof(jit_data_info_t));
#endif #endif
/* allocate at most one extra note in case jit_name() is /* allocate at most one extra note in case jit_name() is
@ -627,37 +627,29 @@ _jit_clear_state(jit_state_t *_jit)
bmp_clear(); bmp_clear();
free(_jitc->data.table); jit_free((jit_pointer_t *)&_jitc->data.table);
_jitc->data.table = NULL;
_jitc->data.size = _jitc->data.count = 0; _jitc->data.size = _jitc->data.count = 0;
free(_jitc->spill); jit_free((jit_pointer_t *)&_jitc->spill);
_jitc->spill = NULL; jit_free((jit_pointer_t *)&_jitc->gen);
free(_jitc->gen); jit_free((jit_pointer_t *)&_jitc->values);
_jitc->gen = NULL;
free(_jitc->values);
_jitc->values = NULL;
free(_jitc->blocks.ptr); jit_free((jit_pointer_t *)&_jitc->blocks.ptr);
_jitc->blocks.ptr = NULL;
free(_jitc->patches.ptr); jit_free((jit_pointer_t *)&_jitc->patches.ptr);
_jitc->patches.ptr = NULL;
_jitc->patches.offset = _jitc->patches.length = 0; _jitc->patches.offset = _jitc->patches.length = 0;
for (offset = 0; offset < _jitc->functions.offset; offset++) { for (offset = 0; offset < _jitc->functions.offset; offset++) {
function = _jitc->functions.ptr + offset; function = _jitc->functions.ptr + offset;
free(function->regoff); jit_free((jit_pointer_t *)&function->regoff);
function->regoff = NULL;
} }
free(_jitc->functions.ptr); jit_free((jit_pointer_t *)&_jitc->functions.ptr);
_jitc->functions.offset = _jitc->functions.length = 0; _jitc->functions.offset = _jitc->functions.length = 0;
_jitc->function = NULL; _jitc->function = NULL;
for (offset = 0; offset < _jitc->pool.length; offset++) for (offset = 0; offset < _jitc->pool.offset; offset++)
free(_jitc->pool.ptr[offset]); jit_free((jit_pointer_t *)(_jitc->pool.ptr + offset));
free(_jitc->pool.ptr); jit_free((jit_pointer_t *)&_jitc->pool.ptr);
_jitc->pool.ptr = NULL;
_jitc->pool.offset = _jitc->pool.length = 0; _jitc->pool.offset = _jitc->pool.length = 0;
_jitc->list = NULL; _jitc->list = NULL;
@ -666,16 +658,14 @@ _jit_clear_state(jit_state_t *_jit)
_jitc->note.base = NULL; _jitc->note.base = NULL;
#if __arm__ && DISASSEMBLER #if __arm__ && DISASSEMBLER
free(_jitc->data_info.ptr); jit_free((jit_pointer_t *)&_jitc->data_info.ptr);
_jitc->data_info.ptr = NULL;
#endif #endif
#if __powerpc64__ #if __powerpc64__
free(_jitc->prolog.ptr); jit_free((jit_pointer_t *)&_jitc->prolog.ptr);
_jitc->prolog.ptr = NULL;
#endif #endif
free(_jitc); jit_free((jit_pointer_t *)&_jitc);
} }
void void
@ -683,7 +673,7 @@ _jit_destroy_state(jit_state_t *_jit)
{ {
munmap(_jit->code.ptr, _jit->code.length); munmap(_jit->code.ptr, _jit->code.length);
munmap(_jit->data.ptr, _jit->data.length); munmap(_jit->data.ptr, _jit->data.length);
free(_jit); jit_free((jit_pointer_t *)&_jit);
} }
jit_node_t * jit_node_t *
@ -893,9 +883,9 @@ _jit_link(jit_state_t *_jit, jit_node_t *node)
jit_word_t length; jit_word_t length;
length = _jitc->blocks.length + 16; length = _jitc->blocks.length + 16;
block = realloc(_jitc->blocks.ptr, length * sizeof(jit_block_t)); jit_realloc((jit_pointer_t *)&_jitc->blocks.ptr,
memset(block + _jitc->blocks.length, 0, 16 * sizeof(jit_block_t)); _jitc->blocks.length * sizeof(jit_block_t),
_jitc->blocks.ptr = block; length * sizeof(jit_block_t));
_jitc->blocks.length = length; _jitc->blocks.length = length;
} }
block = _jitc->blocks.ptr + _jitc->blocks.offset; block = _jitc->blocks.ptr + _jitc->blocks.offset;
@ -1316,7 +1306,7 @@ _jit_optimize(jit_state_t *_jit)
MAP_PRIVATE | MAP_ANON, -1, 0); MAP_PRIVATE | MAP_ANON, -1, 0);
assert(ptr != MAP_FAILED); assert(ptr != MAP_FAILED);
memcpy(ptr, _jit->data.ptr, _jitc->data.offset); memcpy(ptr, _jit->data.ptr, _jitc->data.offset);
free(_jit->data.ptr); jit_free((jit_pointer_t *)&_jit->data.ptr);
_jit->data.ptr = ptr; _jit->data.ptr = ptr;
/* to be filled with note contents once offsets are known */ /* to be filled with note contents once offsets are known */