diff --git a/Makefile b/Makefile index e1296cf..4ba2f9f 100644 --- a/Makefile +++ b/Makefile @@ -21,10 +21,5 @@ tags: compile_commands.json: bear -- make -format: - clang-format -i $(shell git ls-files '*.c' '*.h') - clean: rm -rf $(OBJ) $(ELF) *.json .cache - - .PHONY: format diff --git a/buddy_allocator.c b/buddy_allocator.c index ec454fd..4b7743d 100644 --- a/buddy_allocator.c +++ b/buddy_allocator.c @@ -14,97 +14,98 @@ // Round size up to nearest power-of-two order static int size_to_order(size_t size) { - int order = MIN_ORDER; - while ((1U << order) < size) order++; - return order; + int order = MIN_ORDER; + while ((1U << order) < size) + order++; + return order; } typedef struct block { - struct block *next; - // int order; - // int is_free; + struct block *next; + // int order; + // int is_free; } block_t; static char *heap; static block_t *free_lists[MAX_ORDER - MIN_ORDER + 1]; void buddy_init() { - block_t *initial = (block_t *)heap; - initial->next = NULL; - // initial->order = MAX_ORDER; - // initial->is_free = 1; - free_lists[MAX_ORDER - MIN_ORDER] = initial; + block_t *initial = (block_t *)heap; + initial->next = NULL; + // initial->order = MAX_ORDER; + // initial->is_free = 1; + free_lists[MAX_ORDER - MIN_ORDER] = initial; } void *buddy_alloc(size_t size) { - int order = size_to_order(size); - int index = order - MIN_ORDER; + int order = size_to_order(size); + int index = order - MIN_ORDER; - // Find the first available block of order >= needed - int i = index; // i is the lowest possible order here - while (i <= MAX_ORDER - MIN_ORDER && !free_lists[i]) { - i++; - } + // Find the first available block of order >= needed + int i = index; // i is the lowest possible order here + while (i <= MAX_ORDER - MIN_ORDER && !free_lists[i]) { + i++; + } - // Check if were still within range, if not there are no free blocks - if (i > MAX_ORDER - MIN_ORDER) - return NULL; + // Check if were still within range, if not there are no free blocks + if (i > MAX_ORDER - MIN_ORDER) + return NULL; - // Split blocks down to the requested size - while (i > index) { // Does not run if i == index - block_t *bigger = free_lists[i]; - free_lists[i] = bigger->next; // This may be null, doesnt matter + // Split blocks down to the requested size + while (i > index) { // Does not run if i == index + block_t *bigger = free_lists[i]; + free_lists[i] = bigger->next; // This may be null, doesnt matter - i--; // i is now equivalent to one order below + i--; // i is now equivalent to one order below - size_t split_size = ORDER_TO_SZ(i); - // FIXME: Char??? - char *middle = ((char *)bigger + split_size); - block_t *buddy = (block_t *)middle; - // block_t *buddy = (block_t *)((char *)bigger + split_size); - buddy->next = NULL; - bigger->next = buddy; // Biggers buddy is of equal size - free_lists[i] = bigger; // Bigger is no longer bigger here - } + size_t split_size = ORDER_TO_SZ(i); + // FIXME: Char??? + char *middle = ((char *)bigger + split_size); + block_t *buddy = (block_t *)middle; + // block_t *buddy = (block_t *)((char *)bigger + split_size); + buddy->next = NULL; + bigger->next = buddy; // Biggers buddy is of equal size + free_lists[i] = bigger; // Bigger is no longer bigger here + } - // Allocate from free list - block_t *block = free_lists[index]; - free_lists[index] = block->next; - return (void *)block; + // Allocate from free list + block_t *block = free_lists[index]; + free_lists[index] = block->next; + return (void *)block; } // Free a block (no coalescing) void buddy_free(void *ptr, size_t size) { - memset(ptr, 0xFFFFFFFF, size); + memset(ptr, 0xFFFFFFFF, size); - int order = size_to_order(size); - int index = order - MIN_ORDER; + int order = size_to_order(size); + int index = order - MIN_ORDER; - // Push this in front of the orders index - block_t *block = (block_t *)ptr; - block->next = free_lists[index]; - free_lists[index] = block; + // Push this in front of the orders index + block_t *block = (block_t *)ptr; + block->next = free_lists[index]; + free_lists[index] = block; } int main(void) { - heap = malloc(1 << 20); + heap = malloc(1 << 20); - printf("Order: %d\n", size_to_order(100)); - printf("Order: %d\n", size_to_order(1000)); + printf("Order: %d\n", size_to_order(100)); + printf("Order: %d\n", size_to_order(1000)); - buddy_init(); + buddy_init(); - void *a = buddy_alloc(100); - void *b = buddy_alloc(1000); + void *a = buddy_alloc(100); + void *b = buddy_alloc(1000); - *(int *)a = 10; - printf("a = %d\n", *(int *)a); + *(int *)a = 10; + printf("a = %d\n", *(int *)a); - buddy_free(a, 100); - buddy_free(b, 1000); + buddy_free(a, 100); + buddy_free(b, 1000); - printf("a = %d\n", *(int *)a); + printf("a = %d\n", *(int *)a); - free(heap); - return 0; + free(heap); + return 0; } diff --git a/sock.c b/sock.c index e1ad2f1..fbf2e14 100644 --- a/sock.c +++ b/sock.c @@ -1,10 +1,10 @@ -#include -#include #include #include #include -#include #include +#include +#include +#include int main(void) { // Get TCP protocol entry @@ -25,12 +25,11 @@ int main(void) { struct sockaddr_in server_addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_ANY), // Bind to any available address - .sin_port = htons(8080) // Port 8080 + .sin_port = htons(8080) // Port 8080 }; // Bind socket - if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < - 0) { + if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("bind failed"); close(server_fd); return EXIT_FAILURE; @@ -48,8 +47,7 @@ int main(void) { // Accept one client connection struct sockaddr_in client_addr; socklen_t client_len = sizeof(client_addr); - int client_fd = - accept(server_fd, (struct sockaddr *)&client_addr, &client_len); + int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len); if (client_fd < 0) { perror("accept failed"); close(server_fd);