Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/whiteknight/pmc_is_ptr'
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Sep 6, 2011
2 parents 64522d5 + dd3f6b8 commit e9d0322
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/gc/fixed_allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ Parrot_gc_pool_new(SHIM_INTERP, size_t object_size)
newpool->hi_arena_ptr = NULL;
newpool->newfree = NULL;
newpool->newlast = NULL;
newpool->num_arenas = 0;
newpool->arena_bounds = (void **)mem_sys_allocate(NEXT_ARENA_BOUNDS_SIZE(0));

return newpool;
}
Expand All @@ -330,6 +332,7 @@ Parrot_gc_pool_destroy(SHIM_INTERP, ARGMOD(Pool_Allocator *pool))
mem_internal_free(arena);
arena = next;
}
mem_sys_free(pool->arena_bounds);

mem_internal_free(pool);
}
Expand Down Expand Up @@ -506,25 +509,20 @@ static int
pool_is_owned(ARGMOD(Pool_Allocator *pool), ARGIN(const void *ptr))
{
ASSERT_ARGS(pool_is_owned)
int p;

if (ptr >= pool->lo_arena_ptr && ptr <= pool->hi_arena_ptr) {
const Pool_Allocator_Arena *arena = pool->top_arena;

/* We can cache these values. All arenas are same size */
const ptrdiff_t a_size = arena_size(pool);

while (arena) {
const Pool_Allocator_Arena * const arena_item = arena + 1;
const ptrdiff_t ptr_diff = (const char *) ptr - (const char *) arena_item;

if (ptr_diff >= 0 && ptr_diff < a_size
&& ptr_diff % pool->object_size == 0)
for (p = 0; p < pool->num_arenas; p++) {
const size_t idx = 2 * p;
void * const low = pool->arena_bounds[idx];
void * const high = pool->arena_bounds[idx + 1];
if (ptr >= low && ptr <= high) {
const ptrdiff_t ptrdiff = (const char *)ptr - (const char *)low;
if (ptrdiff % pool->object_size == 0)
return 1;

arena = arena->next;
}
}
}

return 0;
}

Expand Down Expand Up @@ -575,6 +573,15 @@ allocate_new_pool_arena(PARROT_INTERP, ARGMOD(Pool_Allocator *pool))

if ((char *)pool->hi_arena_ptr < (char *)new_arena + total_size)
pool->hi_arena_ptr = (char *)new_arena + total_size;

if (pool->num_arenas % ARENA_BOUNDS_PADDING == 0)
pool->arena_bounds = (void **)mem_sys_realloc(pool->arena_bounds, NEXT_ARENA_BOUNDS_SIZE(pool->num_arenas));
pool->num_arenas++;
{
size_t ptr_idx = (pool->num_arenas - 1) * 2;
pool->arena_bounds[ptr_idx] = new_arena + 1;
pool->arena_bounds[ptr_idx + 1] = new_arena + total_size;
}
}

/*
Expand Down Expand Up @@ -612,6 +619,7 @@ arena_size(ARGIN(const Pool_Allocator *self))
=cut
*/

/*
Expand Down
7 changes: 7 additions & 0 deletions src/gc/fixed_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ src/gc/fixed_allocator.h - implementation of allocator for small-size objects.
increase *_HEADERS_PER_ALLOC and GC_FIXED_SIZE_POOL_SIZE to be large
enough to satisfy most startup costs. */

#define ARENA_BOUNDS_PADDING 128
#define NEXT_ARENA_BOUNDS_SIZE(n) (2 * ((n) + ARENA_BOUNDS_PADDING) * sizeof (void*))

typedef struct Pool_Allocator_Free_List {
struct Pool_Allocator_Free_List * next;
} Pool_Allocator_Free_List;
Expand All @@ -45,6 +48,10 @@ typedef struct Pool_Allocator {
/* Pointers of arena bounds. Used in .is_owned check */
void *lo_arena_ptr;
void *hi_arena_ptr;

int num_arenas; /* number of arenas, for keeping track of the
size of arena_bounds */
void **arena_bounds; /* Array of low/high pairs for each arena. */
} Pool_Allocator;

typedef struct Fixed_Allocator
Expand Down

0 comments on commit e9d0322

Please sign in to comment.