On 6/2/07, chromatic <[EMAIL PROTECTED]> wrote:
On Friday 01 June 2007 16:29:08 Mehmet Yavuz Selim Soyturk wrote:
> After a suggestion of Bernhard at #parrot, I added a function named
> C<mem_sys_realloc_zeroed> to parrot:
>
> void * mem__sys_realloc_zeroed(void *from, size_t size, size_t
> old_size)
>
> It zeroes the contents of C<from> from C<old_size>.
> I had to cast C<from> to a C<char*> to suppress a warning from gcc
> about pointer arithmetics, but I don't know if it's a good solution:
>
> memset((char*)ptr + old_size, 0, size - old_size);
>
> > I thought that might happen. I prefer this memset() patch, but if it
> > doesn't work everywhere, your patch should work.
>
> I attached a patch that makes use of C<mem_sys_allocate_zeroed> and
> C<mem_sys_realloc_zeroed>.
Thanks, applied as r18757.
-- c
I grepped for other files that can make use of mem_sys_*_zeroed
variants. I attached a patch that affects objects.c, vtables.c,
gc/register.c and stm/waitlist.c.
--
Mehmet
Index: src/objects.c
===================================================================
--- src/objects.c (revision 18722)
+++ src/objects.c (working copy)
@@ -1376,7 +1376,7 @@
PMC *
Parrot_find_method_with_cache(Interp *interp, PMC *_class, STRING *method_name /* NN */)
{
- UINTVAL type, bits, i;
+ UINTVAL type, bits;
Caches *mc;
Meth_cache_entry *e, *old;
@@ -1396,26 +1396,20 @@
if (type >= mc->mc_size) {
if (mc->idx) {
- mc->idx = (Meth_cache_entry ***)mem_sys_realloc(mc->idx,
- sizeof (Meth_cache_entry ***) * (type + 1));
+ mc->idx = (Meth_cache_entry ***)mem_sys_realloc_zeroed(mc->idx,
+ sizeof (Meth_cache_entry ***) * (type + 1),
+ sizeof (Meth_cache_entry ***) * mc->mc_size);
}
else {
- mc->idx = (Meth_cache_entry ***)mem_sys_allocate(
+ mc->idx = (Meth_cache_entry ***)mem_sys_allocate_zeroed(
sizeof (Meth_cache_entry ***) * (type + 1));
}
-
- for (i = mc->mc_size; i <= type; ++i)
- mc->idx[i] = NULL;
-
mc->mc_size = type + 1;
}
if (!mc->idx[type]) {
- mc->idx[type] = (Meth_cache_entry **)mem_sys_allocate(
+ mc->idx[type] = (Meth_cache_entry **)mem_sys_allocate_zeroed(
sizeof (Meth_cache_entry *) * TBL_SIZE);
-
- for (i = 0; i < TBL_SIZE; ++i)
- mc->idx[type][i] = NULL;
}
e = mc->idx[type][bits];
Index: src/gc/register.c
===================================================================
--- src/gc/register.c (revision 18722)
+++ src/gc/register.c (working copy)
@@ -141,7 +141,6 @@
void
create_initial_context(Interp *interp)
{
- int i;
static INTVAL num_regs[] ={32,32,32,32};
/* Create some initial free_list slots. */
@@ -149,10 +148,8 @@
#define INITIAL_FREE_SLOTS 8
interp->ctx_mem.n_free_slots = INITIAL_FREE_SLOTS;
interp->ctx_mem.free_list =
- (void **)mem_sys_allocate(INITIAL_FREE_SLOTS * sizeof (void *));
+ (void **)mem_sys_allocate_zeroed(INITIAL_FREE_SLOTS * sizeof (void *));
- for (i = 0; i < INITIAL_FREE_SLOTS; ++i)
- interp->ctx_mem.free_list[i] = NULL;
/*
* For now create context with 32 regs each. Some src tests (and maybe other
* extenders) are assuming the presence of these registers
Index: src/vtables.c
===================================================================
--- src/vtables.c (revision 18722)
+++ src/vtables.c (working copy)
@@ -90,13 +90,9 @@
table and we could get bigger without blowing much memory
*/
const INTVAL new_max = interp->n_vtable_alloced + 16;
- const INTVAL new_size = new_max * sizeof (VTABLE *);
- INTVAL i;
- interp->vtables = (VTABLE **)mem_sys_realloc(interp->vtables, new_size);
- /* Should set all the empty slots to the null PMC's
- vtable pointer */
- for (i = interp->n_vtable_max; i < new_max; ++i)
- interp->vtables[i] = NULL;
+ const INTVAL new_size = new_max * sizeof (VTABLE *);
+ const INTVAL old_size = interp->n_vtable_max * sizeof (VTABLE *);
+ interp->vtables = (VTABLE **)mem_sys_realloc_zeroed(interp->vtables, new_size, old_size);
interp->n_vtable_alloced = new_max;
}
Index: src/stm/waitlist.c
===================================================================
--- src/stm/waitlist.c (revision 18722)
+++ src/stm/waitlist.c (working copy)
@@ -41,12 +41,9 @@
}
if (thr->used_entries >= thr->entry_count) {
- size_t i;
- thr->entries = mem_sys_realloc(thr->entries,
- sizeof (*thr->entries) * thr->entry_count * 2);
- for (i = thr->entry_count; i < thr->entry_count * 2; ++i) {
- thr->entries[i] = NULL;
- }
+ thr->entries = mem_sys_realloc_zeroed(thr->entries,
+ sizeof (*thr->entries) * thr->entry_count * 2,
+ sizeof (*thr->entries) * thr->entry_count);
thr->entry_count *= 2;
}