Skip to content

Commit

Permalink
libks: sync
Browse files Browse the repository at this point in the history
  • Loading branch information
mptre committed Jan 8, 2024
1 parent 7e20be0 commit 67d69f3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
48 changes: 44 additions & 4 deletions libks/arena.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
# define POISON_SIZE 0
#endif

#define MAX_SOURCE_LOCATIONS 8

struct source_location {
const char *fun;
int lno;
};

struct arena_frame {
char *ptr;
size_t size;
Expand All @@ -61,6 +68,7 @@ struct arena {
} flags;
int refs;
struct arena_stats stats;
struct source_location scope_locations[MAX_SOURCE_LOCATIONS];
};

union address {
Expand Down Expand Up @@ -203,6 +211,10 @@ void
arena_scope_leave(struct arena_scope *s)
{
struct arena *a = s->arena;
int idx = a->refs;

if (idx < MAX_SOURCE_LOCATIONS)
a->scope_locations[idx] = (struct source_location){0};

/* Do nothing if arena_free() already has been called. */
if (a->flags.dying) {
Expand Down Expand Up @@ -233,8 +245,17 @@ arena_scope_leave(struct arena_scope *s)
}

struct arena_scope
arena_scope_enter(struct arena *a)
arena_scope_enter_impl(struct arena *a, const char *fun, int lno)
{
int idx = a->refs;

if (idx < MAX_SOURCE_LOCATIONS) {
a->scope_locations[idx] = (struct source_location){
.fun = fun,
.lno = lno,
};
}

arena_ref(a);
return (struct arena_scope){
.arena = a,
Expand All @@ -247,12 +268,31 @@ arena_scope_enter(struct arena *a)
static void
arena_scope_validate(struct arena *a, struct arena_scope *s, size_t size)
{
const struct source_location fallback = {
.fun = "?",
.lno = 0,
};
const struct source_location *scope_location;
int i;

if (likely(s->id == a->refs))
return;

warnx("arena: Allocating %zu bytes from scope S%d which will be freed "
"by nested scope S%d\n",
size, s->id, a->refs);
scope_location = s->id - 1 < MAX_SOURCE_LOCATIONS ?
&a->scope_locations[s->id - 1] :
&fallback;

fprintf(stderr, "arena: allocating %zu bytes from scope #%d at %s:%d "
"which will be freed by nested scope(s):\n",
size, s->id, scope_location->fun, scope_location->lno);
for (i = a->refs; i >= 0 && i > s->id; i--) {
scope_location = i - 1 < MAX_SOURCE_LOCATIONS ?
&a->scope_locations[i - 1] :
&fallback;
fprintf(stderr, "#%d %s:%d\n",
i, scope_location->fun, scope_location->lno);
}

__builtin_trap();
}

Expand Down
6 changes: 5 additions & 1 deletion libks/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ struct arena_stats {
struct arena *arena_alloc(void);
void arena_free(struct arena *);

struct arena_scope arena_scope_enter(struct arena *);
#define arena_scope_enter(a) \
arena_scope_enter_impl((a), __func__, __LINE__)
struct arena_scope arena_scope_enter_impl(struct arena *, const char *,
int);

void arena_scope_leave(struct arena_scope *);

void *arena_malloc(struct arena_scope *, size_t)
Expand Down

0 comments on commit 67d69f3

Please sign in to comment.