simple_allocator

A simple, poorly-performing memory allocator
git clone https://git.bracken.jp/simple_allocator.git
Log | Files | Refs | Submodules | README | LICENSE

commit 82b49fd168f0c3c91518f68a37c9e3fd8216f049
parent bdbe9797060b943d71bb021219aba3ec5933f293
Author: Chris Bracken <chris@bracken.jp>
Date:   Fri, 21 Oct 2022 23:53:38 -0700

Remove C++-isms

Use plain C rather than C++ casts and I/O.

Diffstat:
Msrc/main.cc | 59+++++++++++++++++++++++++++++------------------------------
1 file changed, 29 insertions(+), 30 deletions(-)

diff --git a/src/main.cc b/src/main.cc @@ -1,4 +1,5 @@ -#include <iostream> +#include <pthread.h> +#include <stdio.h> #include <unistd.h> struct header_t { @@ -14,10 +15,8 @@ void print_header(struct header_t *header); void print_block(void *p); void print_alloc_list(); -namespace { -struct header_t *head, *tail; -pthread_mutex_t global_malloc_lock; -} +static struct header_t *head, *tail; +static pthread_mutex_t global_malloc_lock; //////////////////////////////////////////////////////////////////////// // Implementation. @@ -53,18 +52,18 @@ void *malloc(size_t size) { if (header) { header->is_free = 0; pthread_mutex_unlock(&global_malloc_lock); - return static_cast<void*>(header + 1); + return (void*)(header + 1); } // If none, allocate a new block. total_size = sizeof(struct header_t) + size; - block = sbrk(static_cast<int>(total_size)); - if (block == reinterpret_cast<void*>(-1)) { + block = sbrk((int)total_size); + if (block == (void*)(-1)) { pthread_mutex_unlock(&global_malloc_lock); return nullptr; } - header = static_cast<struct header_t*>(block); + header = (struct header_t*)block; header->size = size; header->is_free = 0; header->next = nullptr; @@ -74,7 +73,7 @@ void *malloc(size_t size) { tail->next = header; tail = header; pthread_mutex_unlock(&global_malloc_lock); - return static_cast<void*>(header + 1); + return (void*)(header + 1); } /** @@ -87,10 +86,10 @@ void free(void *block) { pthread_mutex_lock(&global_malloc_lock); // Get block header. - struct header_t *header = static_cast<struct header_t*>(block) - 1; + struct header_t *header = (struct header_t*)block - 1; void *program_break = sbrk(0); - if (static_cast<char*>(block) + header->size == program_break) { + if ((char*)block + header->size == program_break) { // If we're the last allocated block before brk, decrement it. if (head == tail) { head = tail = nullptr; @@ -103,7 +102,7 @@ void free(void *block) { } } size_t total_size = sizeof(struct header_t) + header->size; - sbrk(-static_cast<int>(total_size)); + sbrk(-(int)total_size); pthread_mutex_unlock(&global_malloc_lock); return; } else { @@ -119,27 +118,28 @@ void free(void *block) { */ void print_brk() { void *p = sbrk(0); - if (p == reinterpret_cast<void*>(-1)) - std::cerr << "sbrk() failed" << std::endl; - std::cout << "brk: " << p << std::endl; + if (p == (void*)(-1)) + fprintf(stderr, "sbrk() failed\n"); + printf("brk: %p\n", p); } void print_header(struct header_t *header) { - if (!header) + printf("hdr: %p\n", header); + if (!header) { return; - std::cout << "hdr: " << header << std::endl; - std::cout << "size: " << header->size << std::endl; - std::cout << "free: " << static_cast<bool>(header->is_free) << std::endl; + } + printf("size: %#zx\n", header->size); + printf("free: %s\n", header->is_free ? "true" : "false"); } /** * Print the header for a block. */ void print_block(void *block) { - std::cout << "addr: " << block << std::endl; + printf("addr: %p\n", block); if (!block) return; - struct header_t *h = static_cast<struct header_t*>(block) - 1; + struct header_t *h = (struct header_t*)(block) - 1; print_header(h); } @@ -147,28 +147,27 @@ void print_block(void *block) { * Print the alloc list. */ void print_alloc_list() { - std::cout << "== head" << std::endl; + printf("== head\n"); print_header(head); - std::cout << "== tail" << std::endl; + printf("== tail\n"); print_header(tail); } int main(int argc, char** argv) { - for (auto i = 0; i < argc; ++i) - std::cout << "argv[" << i << "]: " << argv[i] << std::endl; - print_brk(); + printf("Header size is: %#zx\n", sizeof(struct header_t)); print_alloc_list(); - std::cout << std::endl; + printf("\n"); void *p = malloc(20); - std::cout << "== malloc'ed block" << std::endl; + printf("*** malloced a block\n"); print_block(p); print_brk(); print_alloc_list(); + printf("\n"); free(p); - std::cout << "== free'ed block" << std::endl; + printf("*** freed a block\n"); print_brk(); print_alloc_list(); }