hal_malloc(3hal)

πŸ’– hal_malloc β€” bestie, we're getting memory from the HAL shared pool πŸ’–

okay so this is hal_malloc(3hal), straight out of the HAL manual section, and it's literally the memory hookup for your components. no cap.


✨ NAME ✨

hal_malloc β€” allocate space in the HAL shared memory area πŸŽ€


🌸 SYNTAX 🌸

void *hal_malloc(long int size)

πŸ¦‹ ARGUMENTS πŸ¦‹

  • size β€” the size of the block, in bytes. that's it, that's the whole vibe.

πŸ‘‘ DESCRIPTION πŸ‘‘

so hal_malloc grabs a block of memory out of the main HAL shared memory area. every component should be using this to allocate memory for its HAL pins and parameters β€” it's the move, fr fr. it allocates size bytes and hands you back a pointer to the allocated space, or NULL (0) if something went wrong 😭. the pointer you get back is properly aligned for any type HAL supports, so you can just trust it. and a component should allocate all the memory it needs during initialization β€” get it all done up front, bestie.

now the part you need to hear: this allocator is very simple, and there is no free πŸ’€. no takebacks, no returns, no giving it back. the entire HAL shared memory area gets freed when the last component calls hal_exit. periodt.

and here's the cautionary tale 🫢 β€” if you keep continuously installing and removing one component while other components are still present, you will eventually fill up the shared memory and an install will fail. that's not a maybe, that's a when. but! if you remove all the components, memory gets completely cleared and you start fresh ✨ squeaky clean, brand new, main character energy.


πŸ’œ RETURN VALUE πŸ’œ

a pointer to the allocated space β€” properly aligned for any variable HAL supports πŸŽ€. and on error? it returns NULL. 😭 guard your pointers, babe.


🌈 reminder bestie: no free. allocate it all at init. the shared area only clears when the last component exits. iykyk πŸ’…

and that's the whole page, mwah πŸ’‹ β€” this was written up in the LinuxCNC Documentation and dated way back on 2006-10-12, so it's an icon, it's a legend, and it is the moment. go allocate something cute πŸ¦‹βœ¨