Memory Allocator (ITI8510): erinevus redaktsioonide vahel

Allikas: Lambda
Mine navigeerimisribale Mine otsikasti
Resümee puudub
1. rida: 1. rida:
{{itv8510}}
{{iti8510}}
This task will be part of the final grading.
This task will be part of the final grading.



Redaktsioon: 8. aprill 2010, kell 14:57

Real-Time Operating Systems and Systems Programming
[ @ ]

This task will be part of the final grading.

Task

Write your own memory allocator which could replace malloc()/free() pair. There are a few requirements:

  • Allocator should re-use freed memory.
  • It should not crash.

Write two functions:

void * Malloc(size_t size)
void Free(void * ptr)

Questions

  • How should I allocate the memory for allocator?

Since the program should handle its memory all by itself, you should use sbrk() function to move the heap pointer (the same thing what malloc does internally). Do not free the memory using sbrk(), just mark it for re-use.

  • What algorithm should I use?

Use either first-fit or best fit algorithm. Joining of nearby blocks is highly recommended. Memory segregation is not needed (you do not have to create classes for larger and smaller allocations).