bottleros/Userland/SampleCodeModule/shell/commands/test_mm.c

74 lines
1.6 KiB
C

// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "test_mm.h"
void *memset(void *destination, int32_t c, uint64_t length);
typedef struct MM_rq{
void *address;
uint32_t size;
} mm_rq;
static uint32_t m_z = 362436069;
static uint32_t m_w = 521288629;
uint32_t GetUint(){
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w;
}
uint32_t GetUniform(uint32_t max){
uint32_t u = GetUint();
return (u + 1.0) * 2.328306435454494e-10 * max;
}
uint8_t memcheck(void *start, uint8_t value, uint32_t size){
uint8_t *p = (uint8_t *) start;
uint32_t i;
for (i = 0; i < size; i++, p++)
if (*p != value)
return 0;
return 1;
}
void test_mm(int argc, char ** argv){
mm_rq mm_rqs[MAX_BLOCKS];
uint8_t rq;
uint32_t total;
while (1){
rq = 0;
total = 0;
while(rq < MAX_BLOCKS && total < MAX_MEMORY){
mm_rqs[rq].size = GetUniform(MAX_MEMORY - total - 1) + 1;
mm_rqs[rq].address = sys_malloc(mm_rqs[rq].size);
//TODO: check if NULL
total += mm_rqs[rq].size;
rq++;
}
// Set
uint32_t i;
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
memset(mm_rqs[i].address, i, mm_rqs[i].size);
// Check
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
if(!memcheck(mm_rqs[i].address, i, mm_rqs[i].size))
printStringLen("ERROR!\n", 8);
// Free
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
sys_free(mm_rqs[i].address);
}
sys_exit();
}