Compare commits
2 commits
c2b9a13235
...
655b310328
Author | SHA1 | Date | |
---|---|---|---|
![]() |
655b310328 | ||
![]() |
5392c8e418 |
3 changed files with 72 additions and 66 deletions
5
Makefile
5
Makefile
|
@ -21,5 +21,10 @@ tags:
|
||||||
compile_commands.json:
|
compile_commands.json:
|
||||||
bear -- make
|
bear -- make
|
||||||
|
|
||||||
|
format:
|
||||||
|
clang-format -i $(shell git ls-files '*.c' '*.h')
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJ) $(ELF) *.json .cache
|
rm -rf $(OBJ) $(ELF) *.json .cache
|
||||||
|
|
||||||
|
.PHONY: format
|
||||||
|
|
|
@ -14,98 +14,97 @@
|
||||||
|
|
||||||
// Round size up to nearest power-of-two order
|
// Round size up to nearest power-of-two order
|
||||||
static int size_to_order(size_t size) {
|
static int size_to_order(size_t size) {
|
||||||
int order = MIN_ORDER;
|
int order = MIN_ORDER;
|
||||||
while ((1U << order) < size)
|
while ((1U << order) < size) order++;
|
||||||
order++;
|
return order;
|
||||||
return order;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct block {
|
typedef struct block {
|
||||||
struct block *next;
|
struct block *next;
|
||||||
// int order;
|
// int order;
|
||||||
// int is_free;
|
// int is_free;
|
||||||
} block_t;
|
} block_t;
|
||||||
|
|
||||||
static char *heap;
|
static char *heap;
|
||||||
static block_t *free_lists[MAX_ORDER - MIN_ORDER + 1];
|
static block_t *free_lists[MAX_ORDER - MIN_ORDER + 1];
|
||||||
|
|
||||||
void buddy_init() {
|
void buddy_init() {
|
||||||
block_t *initial = (block_t *)heap;
|
block_t *initial = (block_t *)heap;
|
||||||
initial->next = NULL;
|
initial->next = NULL;
|
||||||
// initial->order = MAX_ORDER;
|
// initial->order = MAX_ORDER;
|
||||||
// initial->is_free = 1;
|
// initial->is_free = 1;
|
||||||
free_lists[MAX_ORDER - MIN_ORDER] = initial;
|
free_lists[MAX_ORDER - MIN_ORDER] = initial;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *buddy_alloc(size_t size) {
|
void *buddy_alloc(size_t size) {
|
||||||
int order = size_to_order(size);
|
int order = size_to_order(size);
|
||||||
int index = order - MIN_ORDER;
|
int index = order - MIN_ORDER;
|
||||||
|
|
||||||
// Find the first available block of order >= needed
|
// Find the first available block of order >= needed
|
||||||
int i = index; // i is the lowest possible order here
|
int i = index; // i is the lowest possible order here
|
||||||
while (i <= MAX_ORDER - MIN_ORDER && !free_lists[i]) {
|
while (i <= MAX_ORDER - MIN_ORDER && !free_lists[i]) {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if were still within range, if not there are no free blocks
|
// Check if were still within range, if not there are no free blocks
|
||||||
if (i > MAX_ORDER - MIN_ORDER)
|
if (i > MAX_ORDER - MIN_ORDER)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// Split blocks down to the requested size
|
// Split blocks down to the requested size
|
||||||
while (i > index) { // Does not run if i == index
|
while (i > index) { // Does not run if i == index
|
||||||
block_t *bigger = free_lists[i];
|
block_t *bigger = free_lists[i];
|
||||||
free_lists[i] = bigger->next; // This may be null, doesnt matter
|
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);
|
size_t split_size = ORDER_TO_SZ(i);
|
||||||
// FIXME: Char???
|
// FIXME: Char???
|
||||||
char *middle = ((char *)bigger + split_size);
|
char *middle = ((char *)bigger + split_size);
|
||||||
block_t *buddy = (block_t *)middle;
|
block_t *buddy = (block_t *)middle;
|
||||||
// block_t *buddy = (block_t *)((char *)bigger + split_size);
|
// block_t *buddy = (block_t *)((char *)bigger + split_size);
|
||||||
buddy->next = NULL;
|
buddy->next = NULL;
|
||||||
bigger->next = buddy; // Biggers buddy is of equal size
|
bigger->next = buddy; // Biggers buddy is of equal size
|
||||||
free_lists[i] = bigger; // Bigger is no longer bigger here
|
free_lists[i] = bigger; // Bigger is no longer bigger here
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate from free list
|
// Allocate from free list
|
||||||
block_t *block = free_lists[index];
|
block_t *block = free_lists[index];
|
||||||
free_lists[index] = block->next;
|
free_lists[index] = block->next;
|
||||||
return (void *)block;
|
return (void *)block;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free a block (no coalescing)
|
// Free a block (no coalescing)
|
||||||
void buddy_free(void *ptr, size_t size) {
|
void buddy_free(void *ptr, size_t size) {
|
||||||
memset(ptr, 0xFFFFFFFF, size);
|
memset(ptr, 0xFFFFFFFF, size);
|
||||||
|
|
||||||
int order = size_to_order(size);
|
int order = size_to_order(size);
|
||||||
int index = order - MIN_ORDER;
|
int index = order - MIN_ORDER;
|
||||||
|
|
||||||
// Push this in front of the orders index
|
// Push this in front of the orders index
|
||||||
block_t *block = (block_t *)ptr;
|
block_t *block = (block_t *)ptr;
|
||||||
block->next = free_lists[index];
|
block->next = free_lists[index];
|
||||||
free_lists[index] = block;
|
free_lists[index] = block;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
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(100));
|
||||||
printf("Order: %d\n", size_to_order(1000));
|
printf("Order: %d\n", size_to_order(1000));
|
||||||
|
|
||||||
buddy_init();
|
buddy_init();
|
||||||
|
|
||||||
void *a = buddy_alloc(100);
|
void *a = buddy_alloc(100);
|
||||||
void *b = buddy_alloc(1000);
|
void *b = buddy_alloc(1000);
|
||||||
|
|
||||||
*(int *)a = 10;
|
*(int *)a = 10;
|
||||||
printf("a = %d\n", *(int *)a);
|
printf("a = %d\n", *(int *)a);
|
||||||
|
|
||||||
buddy_free(a, 100);
|
buddy_free(a, 100);
|
||||||
buddy_free(b, 1000);
|
buddy_free(b, 1000);
|
||||||
|
|
||||||
printf("a = %d\n", *(int *)a);
|
printf("a = %d\n", *(int *)a);
|
||||||
|
|
||||||
free(heap);
|
free(heap);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
14
sock.c
14
sock.c
|
@ -1,10 +1,10 @@
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
// Get TCP protocol entry
|
// Get TCP protocol entry
|
||||||
|
@ -25,11 +25,12 @@ int main(void) {
|
||||||
struct sockaddr_in server_addr = {
|
struct sockaddr_in server_addr = {
|
||||||
.sin_family = AF_INET,
|
.sin_family = AF_INET,
|
||||||
.sin_addr.s_addr = htonl(INADDR_ANY), // Bind to any available address
|
.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
|
// 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");
|
perror("bind failed");
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
@ -47,7 +48,8 @@ int main(void) {
|
||||||
// Accept one client connection
|
// Accept one client connection
|
||||||
struct sockaddr_in client_addr;
|
struct sockaddr_in client_addr;
|
||||||
socklen_t client_len = sizeof(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) {
|
if (client_fd < 0) {
|
||||||
perror("accept failed");
|
perror("accept failed");
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
|
|
Loading…
Add table
Reference in a new issue