Compare commits

..

3 Commits

Author SHA1 Message Date
9594b76202 Added ralloc function. 2024-02-08 17:51:03 -08:00
103277bf63 Added ralloc function. 2024-02-08 17:47:44 -08:00
c0da3fdefe Added ralloc function. 2024-02-08 17:42:03 -08:00
2 changed files with 60 additions and 38 deletions

5
.idea/vcs.xml generated
View File

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GitSharedSettings">
<option name="FORCE_PUSH_PROHIBITED_PATTERNS">
<list />
</option>
</component>
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" /> <mapping directory="" vcs="Git" />
</component> </component>

93
main.c
View File

@ -23,27 +23,26 @@ struct block* last = NULL;
/// @returns The new memory block. /// @returns The new memory block.
struct block* extend_heap(size_t s) struct block* extend_heap(size_t s)
{ {
struct block* b = sbrk(0); // Ensure the allocated size is at least the minimum block size
if (s < MINIMUM_BLOCK_SIZE)
s = MINIMUM_BLOCK_SIZE;
// Ensure the allocated size is at least the minimum block size struct block* b = sbrk(0);
if (s < MINIMUM_BLOCK_SIZE) {
s = MINIMUM_BLOCK_SIZE;
}
if (sbrk(BLOCK_SIZE + s) == (void*)-1) if (sbrk(BLOCK_SIZE + s) == (void*)-1)
return NULL; return NULL;
b->size = s; b->size = s;
b->prev = last; b->prev = last;
b->next = NULL; b->next = NULL;
b->free = 0; b->free = 0;
if (last) if (last)
last->next = b; last->next = b;
last = b; last = b;
return b; return b;
} }
/// Finds the first block that will fit the given size. /// Finds the first block that will fit the given size.
@ -58,6 +57,10 @@ struct block* find_first(size_t s)
return current; return current;
} }
/// Fragments an existing free memory block into the given size.
/// @param [in] in The memory block to fragment.
/// @param [in] s The size of the new memory block.
/// @returns The new memory block.
struct block* fragment_block(struct block* in, size_t s) struct block* fragment_block(struct block* in, size_t s)
{ {
size_t totalSize = BLOCK_SIZE + s; size_t totalSize = BLOCK_SIZE + s;
@ -79,35 +82,49 @@ struct block* fragment_block(struct block* in, size_t s)
/// @todo Fragmenting functionality. /// @todo Fragmenting functionality.
void* malloc(size_t size) void* malloc(size_t size)
{ {
struct block* b; struct block* b;
size_t alignedSize = ALIGN4(size); size = ALIGN4(size);
// Enforce the minimum block size if (first)
if (alignedSize < MINIMUM_BLOCK_SIZE) { {
alignedSize = MINIMUM_BLOCK_SIZE; b = find_first(size);
} if (!b)
b = extend_heap(size);
else if (b->size > BLOCK_SIZE + size)
b = fragment_block(b, size);
}
else
{
b = extend_heap(size);
if (!b)
return NULL;
if (first) first = b;
{ }
b = find_first(alignedSize);
if (!b)
b = extend_heap(alignedSize);
else if (b->size > BLOCK_SIZE + alignedSize)
b = fragment_block(b, alignedSize);
}
else
{
b = extend_heap(alignedSize);
if (!b)
return NULL;
first = b; return b + 1;
}
return b + 1;
} }
void* realloc(void* ptr, size_t new_size)
{
if (!ptr)
return malloc(new_size);
if (!new_size)
return NULL;
struct block* b = (struct block*)ptr - 1;
if (b->free)
return NULL;
if (b->size == new_size)
return ptr;
else if (b->size > BLOCK_SIZE + new_size)
return fragment_block(b, new_size) + 1;
return NULL;
}
/// Will flag the provided memory as free and will defragment other blocks adjacent to it. /// Will flag the provided memory as free and will defragment other blocks adjacent to it.
/// @param [in] ptr The memory to flag as free. /// @param [in] ptr The memory to flag as free.