HelenOS sources
This source file includes following definitions.
- init_mem
- done_mem
- overlap_match
- test_overlap
- check_consistency
- checked_malloc
- alloc_block
- free_block
- block_expected_value
- fill_block
- check_block
- get_random_block
- map_area
- unmap_area
- area_expected_value
- fill_area
#include <stdlib.h>
#include <as.h>
#include <adt/list.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>
#include "../tester.h"
#include "common.h"
bool error_flag = false;
size_t mem_allocated;
size_t mem_blocks_count;
static LIST_INITIALIZE(mem_blocks);
static LIST_INITIALIZE(mem_areas);
void init_mem(void)
{
mem_allocated = 0;
mem_blocks_count = 0;
}
void done_mem(void)
{
link_t *link;
while ((link = list_first(&mem_blocks)) != NULL) {
mem_block_t *block = list_get_instance(link, mem_block_t, link);
free_block(block);
}
while ((link = list_first(&mem_areas)) != NULL) {
mem_area_t *area = list_get_instance(link, mem_area_t, link);
unmap_area(area);
}
}
static bool overlap_match(mem_block_t *block, void *addr, size_t size)
{
uint8_t *mbeg = (uint8_t *) block;
uint8_t *mend = (uint8_t *) block + sizeof(mem_block_t);
uint8_t *bbeg = (uint8_t *) block->addr;
uint8_t *bend = (uint8_t *) block->addr + block->size;
uint8_t *dbeg = (uint8_t *) addr;
uint8_t *dend = (uint8_t *) addr + size;
if (((mbeg >= dbeg) && (mbeg < dend)) ||
((mend > dbeg) && (mend <= dend)) ||
((bbeg >= dbeg) && (bbeg < dend)) ||
((bend > dbeg) && (bend <= dend)))
return true;
return false;
}
static int test_overlap(void *addr, size_t size)
{
bool fnd = false;
list_foreach(mem_blocks, link, mem_block_t, block) {
if (overlap_match(block, addr, size)) {
fnd = true;
break;
}
}
return fnd;
}
static void check_consistency(const char *loc)
{
void *prob = heap_check();
if (prob != NULL) {
TPRINTF("\nError: Heap inconsistency at %p in %s.\n",
prob, loc);
TSTACKTRACE();
error_flag = true;
}
}
static void *checked_malloc(size_t size)
{
void *data;
data = malloc(size);
check_consistency("checked_malloc");
if (data == NULL)
return NULL;
if (test_overlap(data, size)) {
TPRINTF("\nError: Allocated block overlaps with another "
"previously allocated block.\n");
TSTACKTRACE();
error_flag = true;
}
return data;
}
mem_block_t *alloc_block(size_t size)
{
if (mem_allocated >= MAX_ALLOC)
return NULL;
mem_block_t *block =
(mem_block_t *) checked_malloc(sizeof(mem_block_t));
if (block == NULL)
return NULL;
link_initialize(&block->link);
block->addr = checked_malloc(size);
if (block->addr == NULL) {
free(block);
check_consistency("alloc_block");
return NULL;
}
block->size = size;
list_append(&block->link, &mem_blocks);
mem_allocated += size + sizeof(mem_block_t);
mem_blocks_count++;
return block;
}
void free_block(mem_block_t *block)
{
list_remove(&block->link);
mem_allocated -= block->size + sizeof(mem_block_t);
mem_blocks_count--;
free(block->addr);
check_consistency("free_block (a)");
free(block);
check_consistency("free_block (b)");
}
static inline uint8_t block_expected_value(mem_block_t *block, uint8_t *pos)
{
return ((uintptr_t) block ^ (uintptr_t) pos) & 0xff;
}
void fill_block(mem_block_t *block)
{
for (uint8_t *pos = block->addr, *end = pos + block->size;
pos < end; pos++)
*pos = block_expected_value(block, pos);
check_consistency("fill_block");
}
void check_block(mem_block_t *block)
{
for (uint8_t *pos = block->addr, *end = pos + block->size;
pos < end; pos++) {
if (*pos != block_expected_value(block, pos)) {
TPRINTF("\nError: Corrupted content of a data block.\n");
TSTACKTRACE();
error_flag = true;
return;
}
}
}
mem_block_t *get_random_block(void)
{
if (mem_blocks_count == 0)
return NULL;
unsigned long idx = rand() % mem_blocks_count;
link_t *entry = list_nth(&mem_blocks, idx);
if (entry == NULL) {
TPRINTF("\nError: Corrupted list of allocated memory blocks.\n");
TSTACKTRACE();
error_flag = true;
}
return list_get_instance(entry, mem_block_t, link);
}
mem_area_t *map_area(size_t size)
{
mem_area_t *area =
(mem_area_t *) checked_malloc(sizeof(mem_area_t));
if (area == NULL)
return NULL;
link_initialize(&area->link);
area->addr = as_area_create(AS_AREA_ANY, size,
AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE,
AS_AREA_UNPAGED);
if (area->addr == AS_MAP_FAILED) {
free(area);
check_consistency("map_area (a)");
return NULL;
}
area->size = size;
list_append(&area->link, &mem_areas);
return area;
}
void unmap_area(mem_area_t *area)
{
list_remove(&area->link);
errno_t ret = as_area_destroy(area->addr);
if (ret != EOK)
error_flag = true;
free(area);
check_consistency("unmap_area");
}
static inline uint8_t area_expected_value(mem_area_t *area, uint8_t *pos)
{
return ((uintptr_t) area ^ (uintptr_t) pos) & 0xaa;
}
void fill_area(mem_area_t *area)
{
for (uint8_t *pos = area->addr, *end = pos + area->size;
pos < end; pos++)
*pos = area_expected_value(area, pos);
check_consistency("fill_area");
}
HelenOS homepage, sources at GitHub