Compare commits
3 Commits
85b32bbc2e
...
9594b76202
Author | SHA1 | Date | |
---|---|---|---|
9594b76202 | |||
103277bf63 | |||
c0da3fdefe |
5
.idea/vcs.xml
generated
5
.idea/vcs.xml
generated
@ -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>
|
||||||
|
41
main.c
41
main.c
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#define ALIGN4(s) (((((s)-1)>>2)<<2)+4)
|
#define ALIGN4(s) (((((s)-1)>>2)<<2)+4)
|
||||||
#define BLOCK_SIZE sizeof(struct block)
|
#define BLOCK_SIZE sizeof(struct block)
|
||||||
|
#define MINIMUM_BLOCK_SIZE 16
|
||||||
|
|
||||||
/// The memory block's header.
|
/// The memory block's header.
|
||||||
struct block
|
struct block
|
||||||
@ -22,6 +23,10 @@ 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)
|
||||||
{
|
{
|
||||||
|
// Ensure the allocated size is at least the minimum block size
|
||||||
|
if (s < MINIMUM_BLOCK_SIZE)
|
||||||
|
s = MINIMUM_BLOCK_SIZE;
|
||||||
|
|
||||||
struct block* b = sbrk(0);
|
struct block* b = sbrk(0);
|
||||||
|
|
||||||
if (sbrk(BLOCK_SIZE + s) == (void*)-1)
|
if (sbrk(BLOCK_SIZE + s) == (void*)-1)
|
||||||
@ -52,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;
|
||||||
@ -75,19 +84,19 @@ void* malloc(size_t size)
|
|||||||
{
|
{
|
||||||
struct block* b;
|
struct block* b;
|
||||||
|
|
||||||
size_t alignedSize = ALIGN4(size);
|
size = ALIGN4(size);
|
||||||
|
|
||||||
if (first)
|
if (first)
|
||||||
{
|
{
|
||||||
b = find_first(alignedSize);
|
b = find_first(size);
|
||||||
if (!b)
|
if (!b)
|
||||||
b = extend_heap(alignedSize);
|
b = extend_heap(size);
|
||||||
else if (b->size > BLOCK_SIZE + alignedSize)
|
else if (b->size > BLOCK_SIZE + size)
|
||||||
b = fragment_block(b, alignedSize);
|
b = fragment_block(b, size);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
b = extend_heap(alignedSize);
|
b = extend_heap(size);
|
||||||
if (!b)
|
if (!b)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -97,6 +106,26 @@ void* malloc(size_t size)
|
|||||||
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.
|
||||||
/// @note If all data after the provided memory is free, it will reduce the heap size.
|
/// @note If all data after the provided memory is free, it will reduce the heap size.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user