Skip to content

Commit

Permalink
[memory] Testdrive a delayed flushing in FlushableMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
thoni56 committed Jan 31, 2025
1 parent 067f7bd commit 9072a6d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ void initFlushableMemory(FlushableMemory *memory) {
memory->size = 0;
memory->top = 0;
memory->blocks = NULL;
memory->pendingFlushIndex = -1;
}

void *allocateFlushableMemory(FlushableMemory *memory, size_t size) {
Expand Down Expand Up @@ -283,3 +284,30 @@ bool flushableMemoryIsFreed(FlushableMemory *memory, void *pointer) {
return true;
return false;
}

void markAsFlushable(FlushableMemory *memory, void *pointer) {
for (int i=memory->top; i >= 0; i--) {
if (memory->blocks[i] == pointer) {
memory->pendingFlushIndex = i;
return;
}
}
assert(0);
}

bool memoryWouldBeFlushed(FlushableMemory *memory, void *pointer) {
if (memory->pendingFlushIndex == -1)
return false; /* No flush pending */

for (int i=0; i < memory->pendingFlushIndex && i <= memory->top; i++)
if (memory->blocks[i] == pointer)
return false;
return true;
}

void flushPendingMemory(FlushableMemory *memory) {
for (int i=memory->pendingFlushIndex; i <= memory->top; i++) {
free(memory->blocks[i]);
memory->blocks[i] = NULL;
}
}
6 changes: 5 additions & 1 deletion src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,23 @@ extern bool cxMemoryIsFreed(void *pointer);
extern bool cxMemoryOverflowHandler(int n);

/***********************************************************************/
/* New, so far, unused new Memory handling */
/* New, so far unused, new Memory handling */

#define DONT_USE_NEW_CXMEMORY

typedef struct {
int size;
int top;
void **blocks;
int pendingFlushIndex;
} FlushableMemory;

extern void initFlushableMemory(FlushableMemory *memory);
extern void *allocateFlushableMemory(FlushableMemory *memory, size_t size);
extern void freeFlushableMemoryUntil(FlushableMemory *memory, void *pointer);
extern bool flushableMemoryIsFreed(FlushableMemory *memory, void *pointer);
extern bool memoryWouldBeFlushed(FlushableMemory *memory, void *pointer);
extern void markAsFlushable(FlushableMemory *memory, void *pointer);
extern void flushPendingMemory(FlushableMemory *memory);

#endif
29 changes: 29 additions & 0 deletions src/memory_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,32 @@ Ensure(Memory, can_see_if_flushable_memory_is_freed) {
assert_that(flushableMemoryIsFreed(&memory, c));
assert_that(flushableMemoryIsFreed(&memory, d));
}

Ensure(Memory, can_mark_flushable_memory_for_flushing) {
FlushableMemory memory;

initFlushableMemory(&memory);

void *a = allocateFlushableMemory(&memory, 4);
void *b = allocateFlushableMemory(&memory, 4);
void *c = allocateFlushableMemory(&memory, 4);
void *d = allocateFlushableMemory(&memory, 4);

assert_that(!memoryWouldBeFlushed(&memory, a));
assert_that(!memoryWouldBeFlushed(&memory, b));
assert_that(!memoryWouldBeFlushed(&memory, c));
assert_that(!memoryWouldBeFlushed(&memory, d));

markAsFlushable(&memory, b);
assert_that(!memoryWouldBeFlushed(&memory, a));
assert_that(memoryWouldBeFlushed(&memory, b));
assert_that(memoryWouldBeFlushed(&memory, c));
assert_that(memoryWouldBeFlushed(&memory, d));

flushPendingMemory(&memory);
assert_that(memory.blocks[0], is_not_null);
assert_that(memory.blocks[1], is_null);
assert_that(memory.blocks[2], is_null);
assert_that(memory.blocks[3], is_null);

}

0 comments on commit 9072a6d

Please sign in to comment.