summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--LICENSE.txt7
-rw-r--r--Makefile24
-rw-r--r--README.txt49
-rw-r--r--heap.c276
-rw-r--r--heap.h145
6 files changed, 505 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..46e8e5e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.so
+*.a
+*.o
+testprg
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..462947e
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,7 @@
+Copyright © 2021 Jon Santmyer <jon@jonsantmyer.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b8de4df
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,24 @@
+CFILES := $(wildcard *.c)
+OFILES := $(patsubst %.c,%.o,$(CFILES))
+
+SHARED ?= 1
+STATIC ?= 0
+
+CC ?= gcc
+AR ?= ar
+
+WARNS ?= -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wrestrict -Wnull-dereference -Wdouble-promotion -Wshadow -Wcast-align
+CFLAGS ?= $(WARNS) -L$(shell pwd)
+OUT := libmoditheap
+
+.PHONY: all
+all: $(OFILES)
+ifeq ($(SHARED),1)
+ $(CC) -shared $(CFLAGS) -o $(OUT).so -fPIC $(CFILES)
+endif
+ifeq ($(STATIC),1)
+ $(AR) rcs $(OUT).a $^
+endif
+
+%.o:%.c
+ $(CC) -c -o $@ $<
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..f613882
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,49 @@
+diheap
+Drop In Heap
+
+A simple thread-unsafe linked list heap without weird metadata between
+allocations.
+
+Licensed under MIT
+
+Requirements:
+stdint.h
+ uintmax_t, uintptr_t
+stddef.h
+ NULL, size_t
+string.h
+ memset
+
+Example:
+########################
+#include "heap.h"
+
+#define HEAP_SIZE 0x10000
+
+struct heap stdlib_heap;
+struct mutex stdlib_heap_mutex;
+
+void*
+malloc(size_t size)
+{
+ void *r = 0;
+ mutex_lock(&stdlib_heap_mutex);
+ r = heap_alloc(&stdlib_heap, size);
+ mutex_unlock(&stdlib_heap_mutex);
+ return r;
+}
+
+void
+free(void *ptr)
+{
+ mutex_lock(&stdlib_heap_mutex);
+ heap_free(&stdlib_heap, size);
+ mutex_unlock(&stdlib_heap_mutex);
+}
+
+void
+__stdlib_init(void)
+{
+ heap_init(&stdlib_heap, (uintptr_t)sbrk(0), HEAP_SIZE);
+ //Heap is done baybee!
+}
diff --git a/heap.c b/heap.c
new file mode 100644
index 0000000..dc739fd
--- /dev/null
+++ b/heap.c
@@ -0,0 +1,276 @@
+/*
+ * heap.c
+ * Copyright (c) 2021 Jon Santmyer <jon@jonsantmyer.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+/**
+ * File : heap.c
+ * Author : Jon Santmyer <jon@jonsantmyer.com>
+ * Date : 10.12.2021
+ */
+
+#include "heap.h"
+#include <string.h>
+
+int
+heap_create(struct heap *heap, uintptr_t at, size_t len)
+{
+ struct crate *firstcrate = NULL;
+ struct bucket *cratebucket = NULL;
+ struct bucket *freebucket = NULL;
+
+ memset(heap, 0, sizeof(struct heap));
+
+ heap->start = at;
+ heap->size = len;
+
+ /*check if heap can fit crate*/
+ if(len < HEAP_CRATE_SIZE) return -1;
+
+ /*Populate first crate*/
+ heap->crates_tail = heap->crates_head = firstcrate = (struct crate*)at;
+ cratebucket = &firstcrate->buckets[0];
+
+ heap->used = HEAP_CRATE_SIZE;
+ memset(firstcrate, 0, HEAP_CRATE_SIZE);
+
+ /*Allocate first bucket for crate*/
+ heap->buckets_tail = heap->buckets_head = cratebucket;
+ cratebucket->address = (uintptr_t)firstcrate;
+ cratebucket->sizetaken = HEAP_CRATE_SIZE | 1;
+
+ /*Set buckets after 1 to link to free list*/
+ for(size_t i = 2; i < HEAP_CRATE_BUCKETS; i++) {
+ struct bucket *prev = &firstcrate->buckets[i - 1];
+ struct bucket *bucket = &firstcrate->buckets[i];
+
+ prev->next = bucket;
+ bucket->prev = prev;
+ }
+ heap->buckets_unused_tail = &firstcrate->buckets[1];
+
+ return 0;
+}
+
+struct bucket*
+__heap_bucket_findbest(struct heap *heap, size_t size)
+{
+ struct bucket *best = NULL;
+ for(struct bucket *bucket = heap->buckets_tail;
+ bucket != NULL;
+ bucket = bucket->next)
+ {
+ if(HEAP_BUCKET_TAKEN(bucket)) continue;
+ if(bucket->sizetaken < size) continue;
+ if(best == NULL) best = bucket;
+ if(best->sizetaken > bucket->sizetaken) best = bucket;
+ }
+
+ return best;
+}
+
+struct bucket*
+__heap_crate_create(struct heap *heap)
+{
+ size_t remaining = heap->size - heap->used;
+ struct bucket *head = heap->buckets_head;
+ struct bucket *crate_bucket = NULL;
+ struct crate *crate = NULL;
+
+ /*Check for room in heap*/
+ if(remaining < HEAP_CRATE_SIZE) {
+ return NULL;
+ }
+
+ /*Take crate bucket from crate head*/
+ crate_bucket = &heap->crates_head->reserved;
+
+ /*Populate crate bucket*/
+ crate_bucket->address = heap->start + heap->used;
+ crate_bucket->sizetaken = HEAP_CRATE_SIZE | 1;
+ crate_bucket->prev = heap->buckets_head;
+ heap->buckets_head = crate_bucket;
+
+ /*Populate crate, linking buckets together*/
+ crate = (struct crate*)crate_bucket->address;
+ memset(crate, 0, HEAP_CRATE_SIZE);
+
+ for(size_t i = 0; i < HEAP_CRATE_BUCKETS; i++) {
+ struct bucket *bucket = &crate->buckets[i];
+ struct bucket *prev = &crate->buckets[i-1];
+
+ bucket->prev = prev;
+ prev->next = bucket;
+ }
+
+ /*Set unused tail to first bucket*/
+ heap->buckets_unused_tail = &crate->buckets[0];
+
+ /*Add crate to crate list*/
+ heap->crates_head->next = crate;
+ heap->crates_head = crate;
+
+ /*Add used memory to heap info*/
+ heap->used += HEAP_CRATE_SIZE;
+
+ return heap->buckets_unused_tail;
+}
+
+struct bucket*
+__heap_bucket_append(struct heap *heap, struct bucket *bucket, size_t size)
+{
+ /*Get free bucket from crate*/
+ struct bucket *free = heap->buckets_unused_tail;
+ if(free == NULL) {
+ free = __heap_crate_create(heap);
+ if(free == NULL) return NULL;
+ }
+ heap->buckets_unused_tail = heap->buckets_unused_tail->next;
+
+ /*Append free to bucket chain*/
+ free->prev = bucket;
+ free->next = bucket->next;
+ if(free->next != NULL) free->next->prev = free;
+ bucket->next = free;
+
+ /*Fix heap buckets_head*/
+ if(free->next == NULL) heap->buckets_head = free;
+
+ /*Populate free bucket*/
+ free->address = bucket->address + bucket->sizetaken;
+ free->sizetaken = size;
+
+ return free;
+}
+
+void
+__heap_bucket_release(struct heap *heap, struct bucket *bucket)
+{
+ /*Disconnect bucket from main chain*/
+ if(bucket->prev != NULL) {
+ bucket->prev->next = bucket->next;
+ }else{
+ heap->buckets_tail = bucket->next;
+ }
+
+ if(bucket->next != NULL) {
+ bucket->next->prev = bucket->prev;
+ }else{
+ heap->buckets_head = bucket->prev;
+ }
+
+ /*Attach bucket to unused list*/
+ bucket->next = heap->buckets_unused_tail;
+ bucket->prev = NULL;
+
+ heap->buckets_unused_tail = bucket;
+}
+
+void
+__heap_bucket_split(struct heap *heap, struct bucket *bucket, size_t size)
+{
+ /*Check if split is required*/
+ intmax_t diff = bucket->sizetaken - size;
+ if(diff <= 0) return;
+
+ /*Shrink bucket to required size*/
+ bucket->sizetaken = size;
+
+ /*Create new bucket with remainder*/
+ __heap_bucket_append(heap, bucket, diff);
+}
+
+void
+__heap_bucket_merge(struct heap *heap, struct bucket *bucket)
+{
+ struct bucket *prev = bucket->prev;
+ struct bucket *next = bucket->next;
+
+ /*Merge bucket with prev*/
+ if(prev != NULL) {
+ if(!HEAP_BUCKET_TAKEN(prev)) {
+ prev->sizetaken += bucket->sizetaken;
+ __heap_bucket_release(heap, bucket);
+ __heap_bucket_merge(heap, prev);
+ return;
+ }
+ }
+ /*Merge bucket with next*/
+ if(next != NULL) {
+ if(!HEAP_BUCKET_TAKEN(next)) {
+ bucket->sizetaken += next->sizetaken;
+ __heap_bucket_release(heap, next);
+ __heap_bucket_merge(heap, bucket);
+ }
+ }
+}
+
+void*
+heap_alloc(struct heap *heap, size_t size)
+{
+ struct bucket *bestfit = NULL;
+ size_t remaining = heap->size - heap->used;
+ if(size == 0) return NULL;
+
+ /*Check for room in heap*/
+ if(remaining < size) return NULL;
+
+ /*Resize to fit with granularity*/
+ size = ((size + (HEAP_ALLOC_GRAN - 1)) / HEAP_ALLOC_GRAN) * HEAP_ALLOC_GRAN;
+
+ bestfit = __heap_bucket_findbest(heap, size);
+ if(bestfit == NULL) {
+ if(!HEAP_BUCKET_TAKEN(heap->buckets_head)) {
+ /*Expand empty head to fit*/
+ heap->buckets_head->sizetaken = size;
+ bestfit = heap->buckets_head;
+ }else{
+ /*Append new bucket to list*/
+ bestfit = __heap_bucket_append(heap, heap->buckets_head, size);
+ }
+ if(bestfit == NULL) return NULL; /*Heap ran out of space*/
+ }
+
+ /*Split bucket into perfectly sized piece*/
+ __heap_bucket_split(heap, bestfit, size);
+
+ /*Take memory in heap, mark bucket as taken*/
+ heap->used += bestfit->sizetaken;
+ bestfit->sizetaken |= 1;
+ return (void*)bestfit->address;
+}
+
+int
+heap_free(struct heap *heap, void *ptr)
+{
+ uintptr_t addr = (uintptr_t)ptr;
+ for(struct bucket *bucket = heap->buckets_tail;
+ bucket != NULL;
+ bucket = bucket->next)
+ {
+ if(bucket->address == addr) {
+ bucket->sizetaken &= ~1;
+ __heap_bucket_merge(heap, bucket);
+ return 0;
+ }
+ }
+ return -1;
+}
diff --git a/heap.h b/heap.h
new file mode 100644
index 0000000..56f7cc4
--- /dev/null
+++ b/heap.h
@@ -0,0 +1,145 @@
+/**
+ * heap.h
+ * Copyright (c) 2021 Jon Santmyer <jon@jonsantmyer.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+/**
+ * File : heap.h
+ * Author : Jon Santmyer <jon@jonsantmyer.com>
+ * Date : 10.12.2021
+ *
+ * A simple thread-unsafe double linked-list bucket heap allocator.
+ * Written for moderately fast variable size object allocations without
+ * external memory regions
+ * Heap object is expected to be allocated by the system
+ * Heap members (lists, buckets, crates) are stored in the heap memory region.
+ * Users are expected to write wrapper functions that handles errors and
+ * provides a locking mechanism for threaded applications.
+ *
+ * Required functions and headers:
+ * stdint.h
+ * uintptr_t, uintmax_t
+ * stddef.h
+ * size_t
+ * string.h
+ * memset
+ *
+ * Lists are defined by the elements they contain: they are named thusly.
+ * The first element of the list is suffixed by _tail
+ * The last element of the list is suffixed by _head
+ *
+ * Crates are page-sized bucket containers allocated inside of heap memory
+ * designed to allow the heap to dynamically create and destroy buckets.
+ * The amount of buckets each crate can hold depends on the preset size defined
+ * in HEAP_CRATE_SIZE, calculated in HEAP_CRATE_BUCKETS.
+ * Crates are stored in the previous crate's reserved bucket, except for the
+ * initial crate, which stores itself.
+ * Crates are singly linked, with the head and tail stored in the heap
+ * data structure.
+ *
+ * Buckets hold information about which memory region it represents, it's
+ * neighbors, and if it's been taken by an allocation.
+ * Buckets determine if they are taken by setting the first bit in the
+ * sizetaken field. 0 represents free, 1 represents taken.
+ * Buckets currently in-use by the heap are stored in the general
+ * buckets list, with buckets reserved for future allocations stored in
+ * the buckets_unused list.
+ *
+ * The heap datatype stores information about where allocations can take
+ * place, the buckets allocated, a list of crates, and a list of
+ * unused buckets.
+ * Unused buckets are taken when a bucket is split into two, or a bucket needs
+ * to be appended to the heap.
+ *
+ * When buckets assigned to an allocation are bigger than the needed size, the
+ * bucket is "split". the difference in sizes are stored, the bucket is
+ * resized to the requested size, and a new bucket is appended to the old
+ * bucket with the difference as it's size.
+ * When a bucket is freed, the bucket checks it's neighbors to see if it can be
+ * combined. If two adjacent buckets are not taken, they are "merged".
+ * both buckets' sizes are added into the left-most bucket, and the right-most
+ * bucket is released into the unused list.
+ * These actions are made to ensure that memory allocations are as compact
+ * as possible.
+ *
+ * When the unused list is empty and a bucket needs to be appended, the heap
+ * calls for a new crate to be created. The new crate occupies the reserved
+ * bucket of the head crate, and the bucket is pointed to the last address
+ * in the bucket list. The crates' buckets are initialized to link with
+ * eachother and are set as the head of the unused list.
+ */
+#ifndef JSH_HEAP_H
+#define JSH_HEAP_H 1
+
+#include <stdint.h>
+#include <stddef.h>
+
+//I want the crate to be loaded into the l1 cache
+#define HEAP_CRATE_SIZE 4096
+#define HEAP_BUCKET_SIZE sizeof(struct bucket)
+#define HEAP_CRATE_BUCKETS (((HEAP_CRATE_SIZE - sizeof(uintptr_t)) / HEAP_BUCKET_SIZE) - 1)
+
+#define HEAP_BUCKET_TAKEN(b) (b->sizetaken & 1)
+#define HEAP_ALLOC_GRAN sizeof(uintptr_t)
+
+struct bucket
+{
+ uintptr_t address;
+ uintmax_t sizetaken;
+ struct bucket *prev;
+ struct bucket *next;
+};
+
+struct crate
+{
+ struct bucket buckets[HEAP_CRATE_BUCKETS];
+ struct bucket reserved;
+ struct crate *next;
+};
+
+struct heap
+{
+ uintptr_t start;
+ size_t size;
+ size_t used;
+
+ struct crate *crates_tail;
+ struct crate *crates_head;
+ struct bucket *buckets_tail;
+ struct bucket *buckets_head;
+ struct bucket *buckets_unused_tail;
+};
+
+/* Fills a heap datastructure with passed arguments and populates the first crate
+ * If len is less than MODIT_HEAP_CRATE_SIZE, function fails
+ * */
+int heap_create(struct heap *heap, uintptr_t at, size_t len);
+
+/* Allocates a block of memory atleast [size] bytes long in heap
+ * */
+void *heap_alloc(struct heap *heap, size_t size);
+
+/* Frees the memory pointed at by [ptr] for later allocations.
+ * Returns 0 if memory was found and freed
+ * */
+int heap_free(struct heap *heap, void *ptr);
+
+#endif