0

To recode the malloc function, I do a sbrk(stack) where :

void *malloc(size_t size)
{
  stack = 0;
  while (stack < size)
    stack += 4096;
}

I malloc always more than I need then I want to take some of this allocated area of size size and return it, and if I want to do another malloc after I already have allocated memory so I dont have to do multiple call of sbrk. How can i do that, i tried to go back with brk(start_of_the_allocated_space) , sbrk(size) to have the start and end of the space i need but it segfault.

EDIT:

struct s_block {
size_t size;
struct s_block *next;
struct s_block *prev;
void *start;
void *end;
}

Here is my structure. Then I have a func who create a block

struct s_block   *create_block(size_t size, unsigned int stack)
{
struct s_block *block;
block = sbrk(sizeof(s_block));
block->start = sbrk(stack);
block->size = stack;
block->end = sbrk(0);
block->next = set_free_space(size, block);
block->size -= size;
block->next->prev = block;
block->prev = NULL;
return (block->next);
}

struct s_block *set_free_space(size_t size, struct s_block *block)
{ 
struct s_block new_block;
new_block = sbrk(sizeof(s_block));
new_block->start = block->start;
new_block->next = NULL;
new_block->size = size;
new_block->end = ???; // this is where I want to split the first sbrk
// I tried new_block->end = new_block->start + size; but it doesn't work either
block->start = new_block->end + 1; // and i set the new start of the big block at the end of the one i use
return (new_block);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

If I understand your question, It seems you want to do one big sbrk() and then split a new piece everytime you malloc.

The idea is good due to the load sbrk takes on your program, but it seems you misunderstand something conscerning malloc.

Malloc in its simplest implementation allocates a certain amount of space looking like this:

struct metadata
{
   size_t size;
   int free;
   struct metadata *next;
   void *data; 
}

data being a pointer to the second part of the allocated zone, containing the actual space. next is the pointer to the end of the allocated zone.

When you malloc a certain space, you create this struct and return the data pointer. Then, to free you just have to return set the "free" value to 1.

This generates a linked list containing all your data, and all in your sbrk'd zone.

for more info on different malloc implementations, see this answer which uses mmap but can just as well use sbrk

Community
  • 1
  • 1
Adalcar
  • 1,458
  • 11
  • 26
  • "data being a pointer to the second part of the allocated zone". That's what I don't understand how you can find the pointer of the second zone. If I want to to a `malloc(10)` I'll do a `sbrk(4096)` from 0x0000 to 0x4096 for exemple, and I want to take some of this allocated space for my malloc for exemple 0x0000 to 0x0010 so my free space will be 0x0011 to 0x4096 after the malloc – Jack Nonnen Feb 02 '17 at 09:37
  • If the allocated space is 10, then the next zone starts at base + 10, no? – Adalcar Feb 02 '17 at 09:55
  • In this case, if I have a `size_t size = 10` and a `void *data = 0x0000` (start of my `sbrk(4096)` ) doing `void *new_end = data + size` doesn't work and I can't find out why – Jack Nonnen Feb 02 '17 at 09:59
  • That is probably due to the pointer type: a void* cannot be used in arithmetics since its size is unknown – Adalcar Feb 02 '17 at 10:07
  • How cant I set the my new_end at start + size I want to malloc then? – Jack Nonnen Feb 02 '17 at 10:14