Skip to content

Heap linked list implementation #3

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 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
beae0a6
added separate header files and updated c file name to make design im…
memoz1981 Apr 1, 2024
a23dfe8
Added new linked list header file and empty heap implemntation using …
memoz1981 Apr 1, 2024
b06dacd
Added general functions and function to add a new node to the linked …
memoz1981 Apr 1, 2024
9b0559b
Drafted new design using linked lists - some functions not implemente…
memoz1981 Apr 2, 2024
4028031
all functions drafted, main will need to be re-done
memoz1981 Apr 2, 2024
43cc773
Got it partially working - need to sort out how to scan characters to…
memoz1981 Apr 3, 2024
dd18c31
Got allocation working, some clean up is required
memoz1981 Apr 5, 2024
d1eb104
Alloc & de-alloc works
memoz1981 Apr 5, 2024
af69e34
De-coupled memory array size
memoz1981 Apr 5, 2024
7a1e68a
Addional properties to linked list to track all node counts
memoz1981 Apr 5, 2024
de24b49
cleanup up function names to camelCase (except for main functions - w…
memoz1981 Apr 5, 2024
e7f5b23
Drafted merging logic - need to fix bugs
memoz1981 Apr 5, 2024
d7b6fd7
Allocation and de-allocation working
memoz1981 Apr 5, 2024
dfd6b9e
Added logger struct and functions - not fully implemented yet
memoz1981 Apr 6, 2024
fb64a0b
Implemented logger in a very ugly way
memoz1981 Apr 6, 2024
3bdfa4a
Restored changes to a file
memoz1981 Apr 6, 2024
312e7c4
Tried to fully de-couple 2 implementations
memoz1981 Apr 6, 2024
f14f8a6
Updated makefile to dynamically run one or other implementation
memoz1981 Apr 6, 2024
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
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
CFLAGS=-Wall -Wextra -Werror -std=c11 -pedantic -ggdb

heap: main.c heap.c heap.h
$(CC) $(CFLAGS) -o heap main.c heap.c
# Default target
all: clean heap

# Target for building heap with heap1.c
chunk: main.c heap_using_chunk_lists.c heap.h chunk_lists.h
$(CC) $(CFLAGS) -o heap main.c heap_using_chunk_lists.c logger.c

# Target for building heap with heap2.c
linkedlist: main.c heap_using_linked_lists.c heap.h logger.c logger.h linked_list.h
$(CC) $(CFLAGS) -o heap main.c heap_using_linked_lists.c logger.c

# Target for cleaning up
clean:
rm -f heap

.PHONY: all heap1 heap2 clean
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

## Quick Start

### To run original implementation
```console
$ make
$ make chunk
$ ./heap
```

### Alternative Implementation
```console
$ make linkedlist
$ ./heap
```

Expand Down
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_
32 changes: 2 additions & 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 @@ -24,27 +17,6 @@ extern const uintptr_t *stack_base;
void *heap_alloc(size_t size_bytes);
void heap_free(void *ptr);
void heap_collect();
void run_tests(void);

#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_
85 changes: 84 additions & 1 deletion heap.c → heap_using_chunk_lists.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "./heap.h"
#include "heap.h"
#include "chunk_lists.h"

uintptr_t heap[HEAP_CAP_WORDS] = {0};
const uintptr_t *stack_base = 0;
Expand Down Expand Up @@ -162,3 +164,84 @@ void heap_collect()
heap_free(to_free[i]);
}
}

/*-------------------------MAIN FUNCTION IMPLEMENTATION-------------------------------*/

#define JIM_IMPLEMENTATION
#include "jim.h"

typedef struct Node Node;

struct Node {
char x;
Node *left;
Node *right;
};

Node *generate_tree(size_t level_cur, size_t level_max)
{
if (level_cur < level_max) {
Node *root = heap_alloc(sizeof(*root));
assert((char) level_cur - 'a' <= 'z');
root->x = level_cur + 'a';
root->left = generate_tree(level_cur + 1, level_max);
root->right = generate_tree(level_cur + 1, level_max);
return root;
} else {
return NULL;
}
}

void print_tree(Node *root, Jim *jim)
{
if (root != NULL) {
jim_object_begin(jim);

jim_member_key(jim, "value");
jim_string_sized(jim, &root->x, 1);

jim_member_key(jim, "left");
print_tree(root->left, jim);

jim_member_key(jim, "right");
print_tree(root->right, jim);

jim_object_end(jim);
} else {
jim_null(jim);
}
}

#define N 10

void *ptrs[N] = {0};

void run_tests(void)
{
stack_base = (const uintptr_t*)__builtin_frame_address(0);

for (size_t i = 0; i < 10; ++i) {
heap_alloc(i);
}

Node *root = generate_tree(0, 3);

printf("root: %p\n", (void*)root);

Jim jim = {
.sink = stdout,
.write = (Jim_Write) fwrite,
};

print_tree(root, &jim);

printf("\n------------------------------\n");
heap_collect();
chunk_list_dump(&alloced_chunks, "Alloced");
chunk_list_dump(&freed_chunks, "Freed");
printf("------------------------------\n");
root = NULL;
heap_collect();
chunk_list_dump(&alloced_chunks, "Alloced");
chunk_list_dump(&freed_chunks, "Freed");
}
Loading