arena
This commit is contained in:
@@ -3,6 +3,20 @@
|
||||
#import "modules/std.sx";
|
||||
#import "modules/socket.sx";
|
||||
|
||||
// --- Logger ---
|
||||
|
||||
Logger :: struct {
|
||||
prefix: string;
|
||||
count: s64;
|
||||
}
|
||||
|
||||
log :: (logger: *Logger, msg: string) {
|
||||
logger.count += 1;
|
||||
print("[{}] {}\n", logger.prefix, msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
main :: () -> s32 {
|
||||
PORT :: 8080;
|
||||
|
||||
@@ -29,29 +43,39 @@ main :: () -> s32 {
|
||||
|
||||
print("listening on http://localhost:{}\n", PORT);
|
||||
|
||||
arena := arena_create(65536);
|
||||
logger := Logger.{ prefix = "http", count = 0 };
|
||||
|
||||
while true {
|
||||
client := accept(fd, null, null);
|
||||
if client < 0 { continue; }
|
||||
|
||||
// Read request
|
||||
buf : [4096]u8 = ---;
|
||||
read(client, buf, buf.len);
|
||||
|
||||
// Send response
|
||||
body := "<html><body><h1>Hello from sx!</h1></body></html>";
|
||||
response := format("HTTP/1.1 200 OK\r
|
||||
Content-Type: text/html\r
|
||||
Connection: close\r
|
||||
Content-Length: {}\r
|
||||
\r
|
||||
{}", body.len, body);
|
||||
|
||||
write(client, response, response.len);
|
||||
push Context.{ arena = @arena, data = xx @logger } {
|
||||
handle(client);
|
||||
}
|
||||
|
||||
arena_reset(@arena);
|
||||
close(client);
|
||||
print(" served request\n");
|
||||
}
|
||||
|
||||
arena_destroy(@arena);
|
||||
close(fd);
|
||||
0;
|
||||
}
|
||||
|
||||
handle :: (client: s32) {
|
||||
// Read request
|
||||
buf : [4096]u8 = ---;
|
||||
read(client, buf, buf.len);
|
||||
|
||||
body := "<html><body><h1>Hello from sx!</h1></body></html>";
|
||||
response := format("HTTP/1.1 200 OK\r
|
||||
Content-Type: text/html\r
|
||||
Connection: close\r
|
||||
Content-Length: {}\r
|
||||
\r\n{}", body.len, body);
|
||||
|
||||
write(client, response, response.len);
|
||||
logger : *Logger = xx context.data;
|
||||
log(logger, format("served request #{}", logger.count + 1));
|
||||
}
|
||||
@@ -1184,5 +1184,33 @@ END;
|
||||
print("compound: {}\n", arr[2]);
|
||||
}
|
||||
|
||||
// === 26. #using struct composition ===
|
||||
print("=== 26. #using ===\n");
|
||||
{
|
||||
UBase :: struct { x: s32; y: s32; }
|
||||
UExt :: struct { #using UBase; z: s32; }
|
||||
e := UExt.{ x = 1, y = 2, z = 3 };
|
||||
print("using-x: {}\n", e.x);
|
||||
print("using-y: {}\n", e.y);
|
||||
print("using-z: {}\n", e.z);
|
||||
|
||||
// #using in middle position
|
||||
UHeader :: struct { version: s32; }
|
||||
UPacket :: struct { id: s32; #using UHeader; payload: s32; }
|
||||
p := UPacket.{ id = 10, version = 42, payload = 99 };
|
||||
print("pkt-id: {}\n", p.id);
|
||||
print("pkt-ver: {}\n", p.version);
|
||||
print("pkt-pay: {}\n", p.payload);
|
||||
|
||||
// Multiple #using
|
||||
UPos :: struct { px: s32; py: s32; }
|
||||
UCol :: struct { r: s32; g: s32; }
|
||||
USprite :: struct { #using UPos; #using UCol; scale: s32; }
|
||||
s := USprite.{ px = 10, py = 20, r = 255, g = 128, scale = 1 };
|
||||
print("sprite-px: {}\n", s.px);
|
||||
print("sprite-r: {}\n", s.r);
|
||||
print("sprite-scale: {}\n", s.scale);
|
||||
}
|
||||
|
||||
print("=== DONE ===\n");
|
||||
}
|
||||
|
||||
@@ -18,13 +18,52 @@ field_value_int :: ($T: Type, idx: s64) -> s64 #builtin;
|
||||
field_index :: ($T: Type, val: T) -> s64 #builtin;
|
||||
string :: []u8 #builtin;
|
||||
|
||||
// --- Arena allocator & Context ---
|
||||
|
||||
Arena :: struct {
|
||||
buf: string;
|
||||
pos: s64;
|
||||
}
|
||||
|
||||
Context :: struct {
|
||||
arena: *Arena;
|
||||
data: *void;
|
||||
}
|
||||
|
||||
context : Context = ---;
|
||||
|
||||
arena_create :: (size: s64) -> Arena {
|
||||
Arena.{ buf = cstring(size), pos = 0 };
|
||||
}
|
||||
|
||||
arena_alloc :: (arena: *Arena, size: s64) -> *void {
|
||||
aligned := (size + 7) & (0 - 8);
|
||||
if arena.pos + aligned > arena.buf.len {
|
||||
return malloc(aligned);
|
||||
}
|
||||
ptr : *void = xx @arena.buf[arena.pos];
|
||||
arena.pos = arena.pos + aligned;
|
||||
ptr;
|
||||
}
|
||||
|
||||
arena_reset :: (arena: *Arena) {
|
||||
arena.pos = 0;
|
||||
}
|
||||
|
||||
arena_destroy :: (arena: *Arena) {
|
||||
free(arena.buf.ptr);
|
||||
}
|
||||
|
||||
// --- String allocation ---
|
||||
|
||||
CString :: union {
|
||||
s: string;
|
||||
struct { ptr: *void; len: s64; };
|
||||
}
|
||||
|
||||
cstring :: (size: s64) -> string {
|
||||
raw := malloc(size + 1);
|
||||
p : s64 = xx context.arena;
|
||||
raw := if p != 0 then arena_alloc(context.arena, size + 1) else malloc(size + 1);
|
||||
memset(raw, 0, size + 1);
|
||||
rs : CString = ---;
|
||||
rs.ptr = raw;
|
||||
|
||||
Reference in New Issue
Block a user