17-simple-memory-management/mem_man.c
2025-03-08 16:49:22 +01:00

95 lines
No EOL
3.2 KiB
C

/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Title: Simple Memory Manager.
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implementation of a simple memory manager.
* ----------------------------------------------------------
*/
#include "mem_man.h"
#include <stdio.h>
#include <string.h>
/**
* The memory manager operates on a global, statically allocated, linear memory partition (array).
* This memory is organized in memory blocks of 32 bits (4 byte), the number of totally
* available blocks is defined by the constant 'MEMORY_BLOCK_CNT'.
* The smallest allocatable memory unit is one block, larger memory units are allocates always
* a multiple of one block. This means, if a memory up to 32 bit is requested, one block is allocated,
* if a memory of size from 33 bits to 64 bits is request, two blocks are allocated, and so one.
*
* Each memory block shall be initialized with the pattern '0xDEADBEEF", freed memory shall be set to
* the pattern '0xAFFEBAFF'. Such regions are easily recognizable in a memory dump.
*
* The housekeeping data (start address and size) of allocated memory units
* shall be maintained in a linked list.
*/
#define MEMORY_BLOCK_CNT 1024;
/**
* Allocates a memory block of the given size in bytes
* in a similar way as `malloc(size)`.
*
* @param size The number of bytes to allocate.
* @return The pointer to the allocated memory block or 0,
* if no memory could be allocated.
*/
void* my_alloc(size_t size) {
return 0;
}
/**
* Releases the addressed memory block that was allocated via function `my_alloc(...)`.
* If the addressed memory is not allocated, a warning is printed.
*
* @param p The pointer to the memory to free.
*/
void my_free(void* p) {
return;
}
/**
* Prints a brief statistic report about memory that reveals the following key values:
* # total available memory in bytes.
* # free (not allocated) memory in bytes.
* # used (allocated) memory in bytes
* # free memory in percentage (0 % to 100%)
* # number of allocated units (continuously allocated portions of the memory, not memory blocks)
* # largest free continuous memory unit in bytes.
*/
MemStat mem_get_statistics() {
return (MemStat){0, 0, 0, 0, 0, 0};
}
/**
* Prints the allocated units (continuously allocated portions of the memory, not memory blocks)
* including their start address within the managed memory as well as their size
* ordered by their start address.
*/
void mem_print_units() {
return;
}
/**
* Provides the index of the allocated unit (continuously allocated portions of the memory, not memory blocks)
* the addressed memory belongs to. If the addressed memory is not allocated or the address is outside
* the range of the managed memory, a value less than zero provided.
*
* @param p_mem The pointer to the memory to query.
* @return The unit index the address belongs to or a value less than 0 if the memory is not allocated.
*/
int get_unit_index(void* p_mem) {
return 0;
}
/**
* Dumps the content of the memory.
*/
void mem_dump() {
return;
}