I have a full userdata in Lua module written in C. The userdata has __gc() metamethod, which is called by garbage collector. Does lua interpreter free userdata memory after __gc() call, or do I have to free() it inside __gc()?
You should not free the memory, as you didn't malloc() it yourself; Lua does both for you. In fact, the memory isn't even collected in the same garbage-collection cycle, as section 2.10.1 in the Lua 5.1 reference manual explains.
Related
I'm trying to find a way to do heap memory allocation in armv8-a assembly, and after looking through syscall tables and trying to look at the Linux Programmer's Manual I can't find any way to allocate and de-allocate memory at runtime without using malloc and free from the c standard library.
I've looked at brk() but that doesn't appear to have any way to de-allocate memory.
mmap with MAP_ANONYMOUS is preferred to sbrk/brk for most purposes in modern programs. Use munmap to free.
By the way, brk can deallocate memory; simply pass an address lower than the current break point. But this does limit you to freeing in a last-in-first-out fashion.
I am really confused about Rust's system of memory allocation.
In Java you use new to allocate memory on the heap. In C you use malloc(), everything else goes on the stack.
I thought, in Rust, Box<T> allocates memory on the heap but after reading "Defining Our Own Smart Pointer" section in chapter 15.2 in The Rust Programming Language it seems like MyBox<T> doesn't have any special annotation to make the value of T live on the heap.
What exactly goes on the stack and what goes on the heap?
Is the implementation of MyBox<T> essentially the same as Box<T>?
If the implementations are identical, what makes T stored on the heap rather than the stack?
If the implementations aren't identical what makes a Box<T> allocate memory on the heap?
This is hard to say. Usually Rust avoid allocating anything on the heap. Never will the compiler do an implicit allocation on the heap, but may library functions can do it for you. At least anything that is dynamically sized (eg. Vec<T>) will need something on the heap under the hood, for the rest, the documentation should hint it.
Note that even in C, many functions can do heap allocation without an explicit call to malloc. Eg. I've had to debug a memory leak recently where a developer called getaddrinfo without a corresponding freeaddrinfo, ignoring that this function allocates memory on the heap. This class of bugs should be really rare in Rust however, thanks to RAII.
Not at all! The book is simplifying things here to avoid details not important for this section.
—
Box is a compiler built-in. Under the hood what allocates the memory is an allocator defined as in liballoc. You can think of this allocator as providing malloc-like functionality. In practice, the default allocator uses jemalloc on most targets, it is also possible to use a custom allocator, for example the alloc_system crate uses the system's malloc/realloc/free functions to build its allocator.
Just for setting the record straight, malloc is heap, new is heap. Stack allocation doesn't require any malloc, and the malloc can just be liberated through a free, the same for the new can only be liberated by a deleted; otherwise you get a process leak. In those languages, the developer manages what is stack allocation and heap allocation.
For the question, the Box::new goes in the heap. The memory is claimed back with Box::Drop (you don't see this), unless you transfer ownership.
To complement --> types that have a known size at compile time are stored entirely on the stack. This tells you that Rust is managing what goes on the stack and what goes on the heap. Rust ownership and borrowing should clarify this.
This question is about the functions alloca and malloc from Foreign.Marshal.Alloc and newForeignPtr and mallocForeignPtr from Foreign.ForeignPtr. Where does the allocated memory live and how does the garbage collector treat it?
The memory pointed to by Ptr a allocated by malloc lives on the heap, as in the C programming language. It is ignored by the garbage collector - you have to manually deallocate it yourself using free, and be careful to never use it again after that.
alloca f does a similar allocation, call f with the pointer, and free the memory after that. The pointer must not be used after f returns.
These routines are not meant to be used in everyday code, but only to interface with other languages, using a C-like interface (FFI). You get precisely the same memory safety guarantees C offers -- which means practically none. As such it is quite dangerous, and should be used with great care.
By comparison, the ForeignPtr-pointed memory still lives on the heap, but will be garbage collected after no more pointers (i.e. Haskell's ForeignPtr a) refer to the memory. Note that, even if garbage collection is used here, this kind of pointers are not risk-free. Indeed, when Haskell has no more live pointers to the memory the runtime will free it, even if that pointer is still live in the foreign language. The programmer must ensure that this will never happen.
malloc calls the C malloc(), so it allocates memory on the C heap which you have to free manually. (You can also do that from C with free() if you like.)
alloca and mallocForeignPtr allocate pinned byte arrays, which live on the Haskell pinned heap. The GC will free these automatically when they're no longer needed, but will never move them (since they're pinned) so you can pass their addresses to a C function.
newForeignPtr doesn't seem relevant to your question.
All allocate dynamic memory and it is on the heap.
The odd one is malloc; the memory allocated by malloc needs to be EXPLICITELY deallocated.
alloca allocates temporary dynamic memory and passes it to a computation. the memory is automatically deallocated when computation has ended.
mallocForeignPtr allocates memory and returns a foreign pointer. The memory is deallocated automatically when the pointer is discarded.
newForeignPtr adds new reference to an existing dynamically allocated memory. The memory would be deallocated ONLY when the LAST reference to the object is being dropped.
If you know C++: malloc is the naked new, malloca is the unique_ptr/auto_ptr, mallocForeignPtr and newForeignPtr are shared_ptr.
I use Code::Blocks to write my code in C. As far as I know, it combines a text editor, compiler and debugger.
My concern is whether using the malloc command without using the free function will lead to memory leaks or whether Code::Blocks will clean up by itself after each time I run my program from Code::Blocks?
Well, CodeBlocks is just an IDE, which means you can edit, compile, debug and run your codes by using it. However, the software itself(I mean CodeBlocks) cannot disturb or do any impact on the program you write in RUNTIME.
After you "build and run" your code, the operation system will give your program resource (memory and CPU, etc), but OS cannot "rewrite" your program, either.
To avoid memory leak, you should remember to free memory after you call the allocator(calloc or malloc) and use the memory.
To learn more about the tips of memory usage in C, you can read the chapter 9, virtual memory of CSAPP.
You are right, Codeblocks is an integrated development environment, but it is not a C++ runtime. It only integrates with the compiler, and has no control over the execution of your code.
Whenever you call malloc you must call free. The platform executing your code will reclaim the leaked memory after your program terminates, but this is not the responsibility of either Codeblocks, or the operating system.
Never call malloc without calling free.
I want to develop my own operating system from the beginning. But I have some doubts about dynamic memory allocation. For example: There will be a linked list that implements a ready process queue. When I allocate memory in my programs, I use malloc. But, could I use malloc directly in the kernel implementation? Or I must develop my own memory allocator? I am not sure, but I think that malloc uses system calls to check the page table, so I could not use that in my own kernel. If I cant use malloc, how could I allocate memory for the queue?
Thanks.