Skip to content

Make design implementation independent #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS=-Wall -Wextra -Werror -std=c11 -pedantic -ggdb

heap: main.c heap.c heap.h
$(CC) $(CFLAGS) -o heap main.c heap.c
heap: main.c heap_using_chunk_lists.c heap.h
$(CC) $(CFLAGS) -o heap main.c heap_using_chunk_lists.c
37 changes: 37 additions & 0 deletions chunk_lists.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef CHUNK_LISTS_H_
#define CHUNK_LISTS_H_

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>

#define UNIMPLEMENTED \
do { \
fprintf(stderr, "%s:%d: %s is not implemented yet\n", \
__FILE__, __LINE__, __func__); \
abort(); \
} while(0)

#define CHUNK_LIST_CAP 1024

typedef struct {
uintptr_t *start;
size_t size;
} Chunk;

typedef struct {
size_t count;
Chunk chunks[CHUNK_LIST_CAP];
} Chunk_List;

extern Chunk_List alloced_chunks;
extern Chunk_List freed_chunks;
extern Chunk_List tmp_chunks;

void chunk_list_insert(Chunk_List *list, void *start, size_t size);
void chunk_list_merge(Chunk_List *dst, const Chunk_List *src);
void chunk_list_dump(const Chunk_List *list, const char *name);
int chunk_list_find(const Chunk_List *list, uintptr_t *ptr);
void chunk_list_remove(Chunk_List *list, size_t index);

#endif //CHUNK_LISTS_H_
31 changes: 1 addition & 30 deletions heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
#include <stdint.h>
#include <stdlib.h>

#define UNIMPLEMENTED \
do { \
fprintf(stderr, "%s:%d: %s is not implemented yet\n", \
__FILE__, __LINE__, __func__); \
abort(); \
} while(0)

#define HEAP_CAP_BYTES 640000
static_assert(HEAP_CAP_BYTES % sizeof(uintptr_t) == 0,
"The heap capacity is not divisible by "
Expand All @@ -25,26 +18,4 @@ void *heap_alloc(size_t size_bytes);
void heap_free(void *ptr);
void heap_collect();

#define CHUNK_LIST_CAP 1024

typedef struct {
uintptr_t *start;
size_t size;
} Chunk;

typedef struct {
size_t count;
Chunk chunks[CHUNK_LIST_CAP];
} Chunk_List;

extern Chunk_List alloced_chunks;
extern Chunk_List freed_chunks;
extern Chunk_List tmp_chunks;

void chunk_list_insert(Chunk_List *list, void *start, size_t size);
void chunk_list_merge(Chunk_List *dst, const Chunk_List *src);
void chunk_list_dump(const Chunk_List *list, const char *name);
int chunk_list_find(const Chunk_List *list, uintptr_t *ptr);
void chunk_list_remove(Chunk_List *list, size_t index);

#endif // HEAP_H_
#endif // HEAP_H_
1 change: 1 addition & 0 deletions heap.c → heap_using_chunk_lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdbool.h>
#include <string.h>
#include "./heap.h"
#include "./chunk_lists.h"

uintptr_t heap[HEAP_CAP_WORDS] = {0};
const uintptr_t *stack_base = 0;
Expand Down
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>

#include "./heap.h"
#include "./chunk_lists.h"

#define JIM_IMPLEMENTATION
#include "jim.h"
Expand Down