spliting die malloc Funktion neu zu kodieren, kann ich ein sbrk(stack)
wo:eine sbrk in 2
void *malloc(size_t size)
{
stack = 0;
while (stack < size)
stack += 4096;
}
ich malloc immer mehr als ich brauche dann möchte ich einen Teil dieser zugewiesenen Bereich der Größe nehmen size
und es zurückgeben , und wenn ich noch ein malloc machen will, nachdem ich bereits Speicher reserviert habe, muss ich nicht mehrere Aufrufe von sbrk machen. Wie kann ich das tun, ich habe versucht, mit brk(start_of_the_allocated_space)
, sbrk(size)
zurückzugehen, um den Anfang und das Ende des Raumes zu haben, den ich brauche, aber es segfault.
EDIT:
struct s_block {
size_t size;
struct s_block *next;
struct s_block *prev;
void *start;
void *end;
}
Hier meine Struktur ist. Dann habe ich eine func, die ein 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);
}
verwenden genauso gut können, wenn Sie '# include
Olaf
'stdlib.h' ist natürlich nicht enthalten –
Woher weißt du das? – Olaf