Skip to content

Commit

Permalink
lone/memory/heap: hardcode heap size into #define
Browse files Browse the repository at this point in the history
The number of lone heap values in a lone heap is not really varying.
Might as well hardcode it and save some bytes at runtime.
Simpler, less wasteful and more cache friendly code.
  • Loading branch information
matheusmoreira committed Dec 13, 2023
1 parent 20a3e13 commit b6c3ed6
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 34 deletions.
4 changes: 4 additions & 0 deletions include/lone/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#define LONE_MEMORY_SIZE (1024 * 1024)
#endif

#ifndef LONE_MEMORY_HEAP_VALUE_COUNT
#define LONE_MEMORY_HEAP_VALUE_COUNT 512
#endif

#ifndef LONE_ALIGNMENT
#define LONE_ALIGNMENT 16
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/lone/lisp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
│ │
╰────────────────────────────────────────────────────────────────────────╯ */

void lone_lisp_initialize(struct lone_lisp *lone, struct lone_bytes memory, size_t heap_size, void *stack, struct lone_bytes random);
void lone_lisp_initialize(struct lone_lisp *lone, struct lone_bytes memory, void *stack, struct lone_bytes random);

#endif /* LONE_LISP_HEADER */
1 change: 0 additions & 1 deletion include/lone/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
void lone_memory_initialize(
struct lone_lisp *lone,
struct lone_bytes memory,
size_t heap_size,
void *stack
);

Expand Down
7 changes: 4 additions & 3 deletions include/lone/memory/heap.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* SPDX-License-Identifier: AGPL-3.0-or-later */

#include <lone/types.h>

#ifndef LONE_MEMORY_HEAP_HEADER
#define LONE_MEMORY_HEAP_HEADER

void lone_heap_initialize(struct lone_lisp *lone, size_t heap_size);
#include <lone/definitions.h>
#include <lone/types.h>

void lone_heap_initialize(struct lone_lisp *lone);
struct lone_heap_value *lone_heap_allocate_value(struct lone_lisp *lone);
void lone_deallocate_dead_heaps(struct lone_lisp *lone);

Expand Down
5 changes: 3 additions & 2 deletions include/lone/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <linux/types.h>
#include <linux/elf.h>

#include <lone/definitions.h>

typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;
typedef __kernel_off_t off_t;
Expand Down Expand Up @@ -410,8 +412,7 @@ struct lone_memory {

struct lone_heap {
struct lone_heap *next;
size_t count;
struct lone_heap_value values[];
struct lone_heap_value values[LONE_MEMORY_HEAP_VALUE_COUNT];
};

#endif /* LONE_TYPES_HEADER */
2 changes: 1 addition & 1 deletion source/lone.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ long lone(int argc, char **argv, char **envp, struct lone_auxiliary_vector *auxv
struct lone_bytes memory = { sizeof(bytes), bytes }, random = lone_auxiliary_vector_random(auxv);
struct lone_lisp lone;

lone_lisp_initialize(&lone, memory, 1024, stack, random);
lone_lisp_initialize(&lone, memory, stack, random);
lone_modules_intrinsic_initialize(&lone, argc, argv, envp, auxv);
lone_module_path_push_all(&lone, 4,

Expand Down
4 changes: 2 additions & 2 deletions source/lone/lisp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
#include <lone/value/table.h>
#include <lone/value/symbol.h>

void lone_lisp_initialize(struct lone_lisp *lone, struct lone_bytes memory, size_t heap_size, void *stack, struct lone_bytes random)
void lone_lisp_initialize(struct lone_lisp *lone, struct lone_bytes memory, void *stack, struct lone_bytes random)
{
struct lone_function_flags flags = { .evaluate_arguments = 0, .evaluate_result = 0 };
struct lone_value import, export;

lone_memory_initialize(lone, memory, heap_size, stack);
lone_memory_initialize(lone, memory, stack);

lone_hash_initialize(lone, random);

Expand Down
4 changes: 2 additions & 2 deletions source/lone/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <lone/memory.h>
#include <lone/memory/heap.h>

void lone_memory_initialize(struct lone_lisp *lone, struct lone_bytes memory, size_t heap_size, void *stack)
void lone_memory_initialize(struct lone_lisp *lone, struct lone_bytes memory, void *stack)
{
lone->memory.stack = stack;

Expand All @@ -12,5 +12,5 @@ void lone_memory_initialize(struct lone_lisp *lone, struct lone_bytes memory, si
lone->memory.general->free = 1;
lone->memory.general->size = memory.count - sizeof(struct lone_memory);

lone_heap_initialize(lone, heap_size);
lone_heap_initialize(lone);
}
4 changes: 2 additions & 2 deletions source/lone/memory/garbage_collector.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static bool lone_points_to_heap(struct lone_lisp *lone, void *pointer)
if (!lone_points_to_general_memory(lone, pointer)) { return false; }

for (heap = lone->memory.heaps; heap; heap = heap->next) {
if (lone_points_within_range(pointer, heap->values, heap->values + heap->count)) { return true; }
if (lone_points_within_range(pointer, heap->values, heap->values + LONE_MEMORY_HEAP_VALUE_COUNT)) { return true; }
}

return false;
Expand Down Expand Up @@ -143,7 +143,7 @@ static void lone_kill_all_unmarked_values(struct lone_lisp *lone)
size_t i;

for (heap = lone->memory.heaps; heap; heap = heap->next) {
for (i = 0; i < heap->count; ++i) {
for (i = 0; i < LONE_MEMORY_HEAP_VALUE_COUNT; ++i) {
value = &heap->values[i];

if (!value->live) { continue; }
Expand Down
27 changes: 7 additions & 20 deletions source/lone/memory/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@
#include <lone/memory/heap.h>
#include <lone/memory/allocator.h>

static struct lone_heap *lone_allocate_heap(struct lone_lisp *lone, size_t count)
{
size_t i, size = sizeof(struct lone_heap) + (sizeof(struct lone_heap_value) * count);
struct lone_heap *heap = lone_allocate(lone, size);
heap->next = 0;
heap->count = count;
return heap;
}

static struct lone_heap_value *lone_allocate_from_heap(struct lone_lisp *lone)
struct lone_heap_value *lone_heap_allocate_value(struct lone_lisp *lone)
{
struct lone_heap_value *element;
struct lone_heap *heap, *prev;
size_t i;

for (prev = lone->memory.heaps, heap = prev; heap; prev = heap, heap = heap->next) {
for (i = 0; i < heap->count; ++i) {
for (i = 0; i < LONE_MEMORY_HEAP_VALUE_COUNT; ++i) {
element = &heap->values[i];

if (!element->live) {
Expand All @@ -28,8 +19,9 @@ static struct lone_heap_value *lone_allocate_from_heap(struct lone_lisp *lone)
}
}

heap = lone_allocate_heap(lone, lone->memory.heaps[0].count);
heap = lone_allocate(lone, sizeof(struct lone_heap));
prev->next = heap;
heap->next = 0;
element = &heap->values[0];

resurrect:
Expand All @@ -43,7 +35,7 @@ void lone_deallocate_dead_heaps(struct lone_lisp *lone)
size_t i;

while (heap) {
for (i = 0; i < heap->count; ++i) {
for (i = 0; i < LONE_MEMORY_HEAP_VALUE_COUNT; ++i) {
if (heap->values[i].live) { /* at least one live object */ goto next_heap; }
}

Expand All @@ -58,12 +50,7 @@ void lone_deallocate_dead_heaps(struct lone_lisp *lone)
}
}

struct lone_heap_value *lone_heap_allocate_value(struct lone_lisp *lone)
{
return lone_allocate_from_heap(lone);
}

void lone_heap_initialize(struct lone_lisp *lone, size_t heap_size)
void lone_heap_initialize(struct lone_lisp *lone)
{
lone->memory.heaps = lone_allocate_heap(lone, heap_size);
lone->memory.heaps = lone_allocate(lone, sizeof(struct lone_heap));
}

0 comments on commit b6c3ed6

Please sign in to comment.