Temp Allocators are defeating me...

Hey all,

I posted a while ago about my http project (which i've completed at a library level) and decided to go back to a different project (a custom flac player for me to use).

i'm struggling with allocations and defers.... for instance strings are always allocated but i'm struggling to figure out how to deallocate without NEEDING function specific arenas, something aboiut it doesn't feel correct but i'm definitely not experienced enough to understand if what i'm doing is 1) correct and 2) idiomatic....

in my main function that will init the raylib window and run the main initialization functions like initializing the miniaudio engine and initializing my music directories paths into memory, etc. essnetially the "bring it all together" feels like it's going to get messy using temp allocators and context allocators... (maybe this is why allocators exist and i'm learning why it was a pain for a long time). for example in my main i'm creating a dynamic main_alloc to use with all of the top level functions instead of using temp allocators or manually freeing becuase to my understanding freeing temp allocators in child functions clears every temp allocation program wide... htrough research i do see this TEMP_ALLOCATOR_GUARD thingy mentioned but going through the files it looks like a private type and not really usable in my project I'd like to have functions clear there memory on scope end and only leave the global allocation for freeing in main.

am I crazy for thinking about this like this. if this is confusing please let me know and i'll gladly show snippets.

TLDR i'm just not sure how to use allocators in a way where there is a ton of boilerplate or complexity to free non-essential memory on scope end of children functions to main, are creating allocators per function the play or is there a way to guard temp allocators per function so the only clear the function's allocations and leave the upper level allocations.

thanks sorry for wall just not really sure where to learn about this type of scenario as most docs and tutorials are quite basic.

reddit.com
u/Alternative-Ad-8606 — 3 days ago
▲ 7 r/linux

Want to try Stacking WM, but gnome and kde are too fancy

Hey all I've been using Linux for about a year and a half now and have really been loving it (I'm on void btw).

I have always used Tiling WMs since the beginning, I used Hyprland for a bit, ended up hating it for both the Dev's shadiness but also the absolute incredulous amount of churn. So I switched to Sway, from the very beginning I've only ever used Wayland.

I'm not at a place where I'm content but I am curious about swapping to a stacking WM. I was going to go KDE but, and I'm probably in the minority, I don't really like Kwin, and I actually find KDE to look kinda ugly.

I've been drawn to XFCE which I have installed but not tested yet. It's aesthetic is much more my vibe and classic with all the theming available.

Does Wayland have any compositors in this ball park of stable and consistent but not the usual?

reddit.com
u/Alternative-Ad-8606 — 9 days ago

I think I discovered a bug in the latest release...

I'm only posting this here because I'm a noob and I could be completely wrong about this as it's my first real memory managed language.

I also want to say (cause i'll probably get told i'm a poser, even though i'm learning "casually")

i'm having AI explain concepts for HTTP 1.1 (something i've never learned about before cause i'm self taught), I'm writing the code 100% by hand, using insights for flow and new concepts (via unrelated code snippets) from the AI and real old fashioned google searching and documentation reading.

I've dabbled in Rust (i think maybe on the intermediate side of beginner) and Go and other languages as well, but Odin has really caught my attention for it's simple syntax with better readability and fun factor.

on to the bug, for a "simple" first big project (and because it doesn't exist) my first real undertaking with Odin is to make an HTTP 1.1 spec library for me to use (and maybe share to be judged).

now i've been going through the maths and everything to get everythign set up but to construct my strings i do something like this

chunk :: proc(w: ^Writer, data: string) {
w.chunked = true

hex := fmt.tprintf("{:x}", len(data))
w.body = fmt.tprintf("{}{}\r\n{}\r\n", w.body, hex, data)
}

in fact his is an exact snippet from the code that bugged. for some reason tprintf doesn't deallocate in my code properly so sending a chunked request gives the client the proper response but the server segfaults and crashes.

using some gdb wizardry i came to the conclusion that something related to delete() of the response string chopping off the first byte of the header.... now I know i'm dumb and this is still relatively new to me but i debugged this for a bout 2 hours by hand trying to come up with any solution I could search for.

i removed the deletes and let the server run with the leak and confirmed that i wasn't crazy for assuming tprintf allocates on the heap cause the memory usage went up....

ultimately my solution was to convert the whole connection handling function (which would've been done at a later point if not for this segfault only happening on chunked requests ONLY) to dynamic arena allocators and that solved it. ultimately i'm curious to know if this is actually something weird with tprintf, if i'm bad at programming and misunderstanding a fundamental to memory management or there's another reason.

i'm not afraid of people looking at me learn (and find out how much time i spend at my desk job coding things) so my repo is linked here:

ohttp

PS: any other input and feedback would be great and help me improve it. it's still a work in progress as I haven't even implemented the thing i actually need from it which would be a request builder. but I htink i'm just about finished with the HTTP 1.1 Spec part of it.

PSS: If you look at the commit history i commited the bugged version just to remember it and maybe figure out why it was doign that. (it could be my defer delete order or something so i saved it to look back if i did soemthing like this)

u/Alternative-Ad-8606 — 13 days ago

Got an offer in Hangzhou

Hey all my wife and I currently live in GZ and we've been looking at positions in Hangzhou for the upcoming year when our contracts expire, I received an offer for 24k in Hangzhou and was wondering how livable is it for a more western style life (a decent apartment and reasonable food cost (we cook 6 days a week) for 2 people for at least a month or so. The recruiters always sell you on how great a deal is so I want to verify it'll be an okay salary. In Guangzhou it's a decent salary (on the lower end) these days. So am I crazy for thinking it's decent?

reddit.com
u/Alternative-Ad-8606 — 2 months ago

I created my first C program!

I have been learning (teaching myself) how to code for the past 2 years but always genuinely get burnt out by creating projects that have such a large scope, it overwhelms me and I stop. take too long of a break from coding then comeback and repeat.

This time I told myself any project I make must be use-able and have low scope until i get more familiar with larger projects.

EDIT: And I needed to forego any AI usage that was giving code or direct uses, instead i poll it for learning design paradigms and asking function questions (never giving usable code for the projects)

This was the perfect opportunity for me to rewrite a tool that while fairly niche is something I've used daily since moving to China for other work.

The project is a small CLI tool that does really basic functions for my vpn backend that I use hear (an issue i've had since switching to linux permanently about 5 months ago)

anyway i'd love feed back or any potential bugs or best practices for writing C. Again guys I know it's not the end all be all of projects but it is (sadly, and amazingly) the first thing i've finished and kept scope low.

Some thoughts on C after reading a bunch the last few weeks and then spending the last few days working with it... I'm hooked, was learning rust but found myself getting consistently frustrated by the verbosity of everything, C was a breath of fresh air and was actually fun to write, I look forward to my next project (considering a tui front end for this tool for prettier usage, or maybe exploring some interpreter/compiler books with C when i finish Crafting Interpreters with Rust).

codeberg.org
u/Alternative-Ad-8606 — 3 months ago