sbrk

What is the aligment requirements for sys_brk

一曲冷凌霜 提交于 2020-01-11 10:42:29
问题 I'm using sys_brk syscall to dynamically allocate memory in the heap. I noticed that when acquiring the current break location I usually get value similar to this: mov rax, 0x0C mov rdi, 0x00 syscall results in rax 0x401000 The value usually 512 bytes aligned. So I would like to ask is there some alignment requirements on the break value? Or we can misalign it the way we want? 回答1: The kernel does track the break with byte granularity. But don't use it directly for small allocations if you

How do I free memory obtained by sbrk()?

只愿长相守 提交于 2019-12-30 05:45:08
问题 I have a custom allocator function which uses sbrk() to obtain memory. How do I release this memory when it's no longer needed? Is there a function equivalent to free() for malloc() ? or do I have to use brk() to set the end of the data segment ? 回答1: You need to use brk or sbrk again to shrink. In the end the only way you have to modify the amount of memory(apart from mmap like syscalls), is to increase or decrease the heap, so you move it up with sbrk or brk and you move it down with brk or

MIPS Dynamic Memory Allocation using sbrk

大兔子大兔子 提交于 2019-12-23 20:32:24
问题 I was trying to use sbrk for dynamic memory allocation. But, being a new comer to SPIM and MIPS, I was unable to do so. I sketched out a rough code for the same. . data var: .word 25 .text main: li $v0, 9 la $v0, var lw $a0, var syscall # DYNAMICALLY ALLOCATING MEMORY OF SIZE 4 BYTES AT ADDRESS OF VAR sw $v0, var li $v0, 10 syscall 回答1: .data var: .word 25 .text main: li $v0, 9 lw $a0, var syscall # DYNAMICALLY ALLOCATING MEMORY OF SIZE 4 BYTES AT ADDRESS OF VAR sw $v0, var li $v0, 10 syscall

Casting a pointer to an int

巧了我就是萌 提交于 2019-12-19 04:14:36
问题 I am writing my own functions for malloc and free in C for an assignment. I need to take advantage of the C sbrk() wrapper function. From what I understand sbrk() increments the program's data space by the number of bytes passed as an argument and points to the location of the program break. If I have the following code snippet: #define BLOCK_SIZE 20 int x; x = (int)sbrk(BLOCK_SIZE + 4); I get the compiler error warning: cast from pointer to integer of different size . Why is this and is

mmap vs sbrk, performance comparison

我的梦境 提交于 2019-12-18 13:39:20
问题 Which of these calls is faster on average? I've heard that mmap is faster for smaller allocations but I haven't heard a comparison of either. Any information on performance for these would be nice. 回答1: You should tag this with a particular implementation (like linux ) since the answer surely varies by implementation. For now I'll assume Linux since it's the most popular. With that said, brk is in theory more optimizable, and in practice it runs about 10% faster on my machine. Allocating one

About sbrk() and malloc()

ぃ、小莉子 提交于 2019-12-13 12:33:09
问题 I've read the linux manual about sbrk() thoroughly: sbrk() changes the location of the program break, which defines the end of the process's data segment (i.e., the program break is the first location after the end of the uninitialized data segment). And I do know that user space memory's organization is like the following: The problem is: When I call sbrk(1), why does it say I am increasing the size of heap? As the manual says, I am changing the end position of "data segment & bss". So, what

What's unsafe/legacy about brk/sbrk?

删除回忆录丶 提交于 2019-12-12 10:44:11
问题 I've heard in a lot of places (musl mailing list, macOS forums, etc.) that brk() and sbrk() are unsafe. Many of these places either don't give explanations at all, or give very vague explanations. For example, this link states that "these functions are fundamentally broken", and goes on to say that the malloc and sbrk subsystems are utterly broken, that they ruin the heap, et al. My question is: Why is this so? If malloc is used in such a way that it allocates a block of memory with sbrk

What is the aligment requirements for sys_brk

坚强是说给别人听的谎言 提交于 2019-12-11 15:44:08
问题 I'm using sys_brk syscall to dynamically allocate memory in the heap. I noticed that when acquiring the current break location I usually get value similar to this: mov rax, 0x0C mov rdi, 0x00 syscall results in rax 0x401000 The value usually 512 bytes aligned. So I would like to ask is there some alignment requirements on the break value? Or we can misalign it the way we want? 回答1: The kernel does track the break with byte granularity. But don't use it directly for small allocations if you

Undefined reference to _sbrk

余生长醉 提交于 2019-12-03 07:22:35
问题 I am having a problem with _sbrk. In a link phase of compilation i use below comand to link my objects and i get undefined reference to _sbrk. arm-none-eabi-ld -static -T linkerscript.ld -o exe timer_example.o /home/ziga/projects/cs_lite/arm-none-eabi/lib/libc.a /home/ziga/projects/cs_lite/lib/gcc/arm-none-eabi/4.5.1/libgcc.a I am compiling for arm926ej-s and in ARM mode so i think i have chosen the right multilib (libc.a and libgcc.a) which is located in folder home/ziga/projects/cs_lite/arm