Skip to content

Commit 590ab04

Browse files
memory allocator updated
1 parent e2aeddc commit 590ab04

File tree

6 files changed

+138
-134
lines changed

6 files changed

+138
-134
lines changed

automate.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
git add .
2+
git commit - m $?
3+
git push origin main
4+
5+
6+

cmd/assembly/exit-code.asm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
section .data
2+
message db 'Hello, World!', 0xA ; The message to print, followed by a newline
3+
message_length equ $ - message ; Calculate the length of the message
4+
5+
section .text
6+
global _start ; Entry point for the program
7+
8+
_start:
9+
; Prepare for the write system call
10+
mov eax, 4 ; System call number for sys_write (4)
11+
mov ebx, 1 ; File descriptor 1 (stdout)
12+
mov ecx, message ; Pointer to the message
13+
mov edx, message_length ; Length of the message
14+
15+
; Make the system call
16+
int 0x80 ; Call the kernel
17+
18+
; Exit the program
19+
mov eax, 1 ; System call number for sys_exit (1)
20+
xor ebx, ebx ; Exit code 0
21+
int 0x80 ; Call the kernel

cmd/assembly/initexor.asm

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
section .data
2+
mem_size equ 1024 * 1024 ; Total memory size (1 MB)
3+
mem_start resb mem_size ; Reserve 1 MB of memory for allocation
4+
free_list db 0 ; Pointer to the head of the free list
5+
6+
section .bss
7+
; Structure for a free block
8+
struct FreeBlock:
9+
size resd 1 ; Size of the block
10+
next resd 1 ; Pointer to the next free block
11+
12+
section .text
13+
global _start
14+
15+
_start:
16+
; Initialize the free list
17+
mov dword [mem_start], mem_size ; Set the size of the first free block
18+
mov dword [mem_start + 4], 0 ; Next pointer is NULL
19+
mov dword [free_list], mem_start ; Set the free list to point to the start
20+
21+
; Example allocations
22+
mov eax, 128 ; Request 128 bytes
23+
call allocate_memory
24+
; Store the pointer to the allocated memory (for demonstration)
25+
mov ebx, eax ; Store pointer in ebx
26+
27+
mov eax, 256 ; Request 256 bytes
28+
call allocate_memory
29+
; Store the pointer to the allocated memory (for demonstration)
30+
mov ecx, eax ; Store pointer in ecx
31+
32+
; Free the first allocated block
33+
call free_memory
34+
mov eax, ebx ; Pointer to the first block
35+
call free_memory
36+
37+
; Exit the program
38+
mov eax, 1 ; sys_exit
39+
xor ebx, ebx ; Exit code 0
40+
int 0x80
41+
42+
; Function to allocate memory
43+
; Input: EAX = size of memory to allocate
44+
; Output: EAX = pointer to allocated memory or 0 if failed
45+
allocate_memory:
46+
push ebp
47+
mov ebp, esp
48+
push eax ; Save requested size
49+
50+
; Align size to 4 bytes
51+
add eax, 3
52+
shr eax, 2
53+
shl eax, 2
54+
55+
; Traverse the free list to find a suitable block
56+
mov esi, [free_list] ; Start from the head of the free list
57+
mov edi, 0 ; Previous block pointer
58+
59+
find_block:
60+
cmp esi, 0 ; Check if we reached the end of the list
61+
je no_block_found ; No suitable block found
62+
mov ebx, [esi] ; Get the size of the current block
63+
cmp ebx, eax ; Is the block big enough?
64+
jl next_block ; No, check the next block
65+
66+
; Found a suitable block
67+
; If the block is larger than needed, split it
68+
sub ebx, eax ; Remaining size after allocation
69+
cmp ebx, 8 ; Minimum size for a free block
70+
jl allocate_full_block ; Not enough space to split
71+
72+
; Split the block
73+
mov [esi], ebx ; Update the size of the current block
74+
add esi, ebx ; Move to the new block
75+
mov [esi], eax ; Set the size of the allocated block
76+
mov dword [esi + 4], 0 ; Next pointer is NULL
77+
jmp done
78+
79+
allocate_full_block:
80+
; Allocate the entire block
81+
mov dword [esi + 4], 0 ; Set next pointer to NULL
82+
jmp done
83+
84+
next_block:
85+
mov edi, esi ; Update previous block pointer
86+
mov esi, [esi + 4] ; Move to the next block
87+
jmp find_block
88+
89+
no_block_found:
90+
; No suitable block found
91+
xor eax, eax ; Return 0 (NULL)
92+
93+
done:
94+
pop eax ; Restore requested size
95+
pop ebp
96+
ret
97+
98+
; Function to free memory
99+
; Input: EAX = pointer to memory to free
100+
free_memory:
101+
push ebp
102+
mov ebp, esp
103+
push eax ; Save pointer to free
104+
105+
; Get the size of the block to free
106+
mov ebx, [eax] ; Get the size of the block
107+
mov dword [eax + 4], [free_list] ;

cmd/dbcommands/main.asm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ sub %ebi
1111
min $eax
1212

1313

14-
1514
1615
mov *sp

cmd/external.go

Lines changed: 0 additions & 129 deletions
This file was deleted.

cmd/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func handleConnection(conn net.Conn, cache *Cache) {
344344
}
345345
}
346346
cache.Set(key, value, ttl)
347-
conn.Write([]byte("+ OK\r\n"))
347+
conn.Write([]byte("OK\r\n"))
348348

349349
case "GET":
350350
if len(parts) != 2 {
@@ -354,7 +354,7 @@ func handleConnection(conn net.Conn, cache *Cache) {
354354
key := parts[1]
355355
value, exists := cache.Get(key)
356356
if exists {
357-
conn.Write([]byte("*" + value + "\r\n"))
357+
conn.Write([]byte("" + value + "\r\n"))
358358
} else {
359359
conn.Write([]byte("key not availible\r\n"))
360360
}
@@ -496,7 +496,7 @@ const dashboardTemplate = `
496496
<!DOCTYPE html>
497497
<html>
498498
<head>
499-
<title>High-Performance Cache Dashboard</title>
499+
<title>Dustdb</title>
500500
<style>
501501
body {
502502
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
@@ -623,7 +623,7 @@ const dashboardTemplate = `
623623
</head>
624624
<body>
625625
<div class="container">
626-
<h1>High-Performance Cache Dashboard</h1>
626+
<h1>Dustdb</h1>
627627
628628
<h2>Cache Statistics</h2>
629629
<div class="stats-grid">

0 commit comments

Comments
 (0)