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>
* 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_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 */

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)
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
*/

View file

@ -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

View file

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

View file

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

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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));
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));
_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->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 */