1
0
Fork 0
forked from len0rd/rockbox

malloc is not used in Rockbox

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4210 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Daniel Stenberg 2004-01-08 15:43:37 +00:00
parent 21fba08fc3
commit e11a1e29df
4 changed files with 0 additions and 475 deletions

View file

@ -1,36 +0,0 @@
OBJS1 = mytest.o
TARGET1 = mytest
OBJS2 = Malloc.o
TARGET2 = mtest
OBJS3 = dmytest.o
TARGET3 = dmytest
# define this to talk a lot in runtime
# -DDEBUG_VERBOSE
CFLAGS = -g -Wall -DDEBUG -I../../malloc
CC = gcc
AR = ar
LDFLAGS = -L../../malloc -ldmalloc
all: $(TARGET1) $(TARGET2) $(TARGET3)
clean:
rm -f core *~ $(TARGET1) $(TARGET2) $(TARGET3) \
$(OBJS1) $(OBJS2) $(OBJS3)
$(TARGET1): $(OBJS1)
$(CC) -g -o $(TARGET1) $(OBJS1) $(LDFLAGS)
$(TARGET2): $(OBJS2)
$(CC) -g -o $(TARGET2) $(OBJS2) $(LDFLAGS)
$(TARGET3): $(OBJS3)
$(CC) -g -o $(TARGET3) $(OBJS3) $(LDFLAGS)
dmytest.o: dmytest.c
Malloc.o: Malloc.c
mytest.o: mytest.c

View file

@ -1,196 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Storleken på allokeringen bestäms genom att först slumpas en position i
"size_table" ut, sedan slumpas en storlek mellan den postionen och nästa värde
i tabellen. Genom att ha tabellen koncentrerad med låga värden, skapas
flest såna. Rutinen håller tills minnet en allokeringen nekas. Den kommer
aldrig att ha mer än MAXIMAL_MEMORY_TO_ALLOCATE allokerat samtidigt. Maximalt
har den MAX_ALLOCATIONS allokeringar samtidigt.
Statistiskt sätt kommer efter ett tag MAX_ALLOCATIONS/2 allokeringar finnas
samtidigt, med varje allokering i median med värdet av halva "size_table".
När minnet är slut (malloc()=NULL), frågas användaren om han ska fortsätta.
Med jämna mellanrum skrivs statisktik ut skärmen. (DISPLAY_WHEN)
För att stressa systemet med fler små allokeringar, kan man öka
MAX_ALLOCATIONS. AMOUNT_OF_MEMORY bör den att slå i taket fortare om man
minskar det.
Ingen initiering görs av slumptalen, allt är upprepbart (men plocka bort
kommentaren srand() och det löser sig.
*/
#define BMALLOC /* go go go */
#ifdef BMALLOC
#include "dmalloc.h"
#include "bmalloc.h"
#endif
#define MAX_ALLOCATIONS 100000
#define AMOUNT_OF_MEMORY 100000 /* bytes */
#define MAXIMAL_MEMORY_TO_ALLOCATE 49000 /* Sätt den här högre än
AMOUNT_OF_MEMORY, och malloc() bör
returnera NULL förr eller senare */
#define DISPLAY_WHEN (10000) /* When to display statistic */
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define BOOL char
#define TRUE 1
#define FALSE 0
typedef struct {
char *memory;
long size;
char filled_with;
long table_position;
} MallocStruct;
/*
Skapar en lista med MAX_ALLOCATIONS storlek där det slumpvis allokeras
eller reallokeras i.
*/
MallocStruct my_mallocs[MAX_ALLOCATIONS];
long size_table[]={5,8,10,11,12,14,16,18,20,26,33,50,70,90,120,150,200,400,800,1000,2000,4000,8000};
#define TABLESIZE ((sizeof(size_table)-1)/sizeof(long))
long size_allocs[TABLESIZE];
int main(void)
{
long count=-1;
long count_free=0, count_malloc=0, count_realloc=0;
long total_memory=0;
long out_of_memory=FALSE;
dmalloc_initialize();
#ifdef BMALLOC
void *thisisourheap;
thisisourheap = (malloc)(AMOUNT_OF_MEMORY);
if(!thisisourheap)
return -1; /* can't get memory */
bmalloc_add_pool(thisisourheap, AMOUNT_OF_MEMORY);
#endif
srand( 0 ); /* Initialize to a fixed random */
while (!out_of_memory) {
long number=rand()%MAX_ALLOCATIONS;
long size;
long table_position=rand()%TABLESIZE;
char fill_with=rand()&255;
count++;
size=rand()%(size_table[table_position+1]-
size_table[table_position])+
size_table[table_position];
/* fprintf(stderr, "number %d size %d\n", number, size); */
if (my_mallocs[number].size) { /* Om allokering redan finns på den här
positionen, reallokerar vi eller
friar. */
long old_size=my_mallocs[number].size;
if (my_mallocs[number].size && fill_with<40) {
free(my_mallocs[number].memory);
total_memory -= my_mallocs[number].size;
count_free++;
size_allocs[my_mallocs[number].table_position]--;
size=0;
} else {
/*
* realloc() part
*
*/
char *temp;
#if 0
if(my_mallocs[number].size > size) {
printf("*** %d is realloc()ed to %d\n",
my_mallocs[number].size, size);
}
#endif
if (total_memory-old_size+size>MAXIMAL_MEMORY_TO_ALLOCATE)
goto output; /* for-loop */
temp = (char *)realloc(my_mallocs[number].memory, size);
if (!temp)
out_of_memory=size;
else {
my_mallocs[number].memory = temp;
my_mallocs[number].size=size;
size_allocs[my_mallocs[number].table_position]--;
size_allocs[table_position]++;
total_memory -= old_size;
total_memory += size;
old_size=min(old_size, size);
while (--old_size>0) {
if (my_mallocs[number].memory[old_size]!=my_mallocs[number].filled_with)
fprintf(stderr, "Wrong filling!\n");
}
count_realloc++;
}
}
} else {
if (total_memory+size>MAXIMAL_MEMORY_TO_ALLOCATE) {
goto output; /* for-loop */
}
my_mallocs[number].memory=(char *)malloc(size); /* Allokera! */
if (!my_mallocs[number].memory)
out_of_memory=size;
else {
size_allocs[table_position]++;
count_malloc++;
total_memory += size;
}
}
if(!out_of_memory) {
my_mallocs[number].table_position=table_position;
my_mallocs[number].size=size;
my_mallocs[number].filled_with=fill_with;
memset(my_mallocs[number].memory, fill_with, size);
}
output:
if (out_of_memory || !(count%DISPLAY_WHEN)) {
printf("(%ld) malloc %ld, realloc %ld, free %ld, total size %ld\n",
count, count_malloc, count_realloc, count_free, total_memory);
{
int count;
printf("[size bytes]=[number of allocations]\n");
for (count=0; count<TABLESIZE; count++) {
printf(" %ld=%ld\n", size_table[count], size_allocs[count]);
}
printf("\n\n");
}
}
if (out_of_memory) {
if(out_of_memory)
printf("Couldn't get %ld bytes\n", out_of_memory);
dmalloc_status();
bmalloc_status();
fprintf(stderr, "Memory is out! Continue (y/n)");
switch (getchar()) {
case 'y':
case 'Y':
out_of_memory=FALSE;
break;
}
fprintf(stderr, "\n");
}
}
printf("\n");
return 0;
}

View file

@ -1,173 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "dmalloc.h"
#include "bmalloc.h"
#define MAX 500
#define MAX2 1000
#define MAXC 2
#define TESTA
#define TESTB
#define TESTC
#define TESTD
int test1(void)
{
#define MAXK 100
int i;
void *wow[MAXK];
for(i=0; i<MAXK; i++)
if(!(wow[i]=malloc(412))) {
printf("*** Couldn't allocate memory, exiting\n");
return -2;
}
for(i=MAXK-1; i>=0; i-=2)
free(wow[i]);
return 0;
}
int test2(void)
{
#define MAXS 10
#define MAXS1 0
int i;
void *ptr[MAXS];
for(i=MAXS1; i< MAXS; i++) {
printf("%d malloc(%d)\n", i, i*55);
ptr[i] = malloc (i*55);
}
for(i=MAXS1; i< MAXS; i++) {
void *tmp;
printf("%d realloc(%d)\n", i, i*155);
tmp=realloc(ptr[i], i*155);
if(tmp)
ptr[i] = tmp;
}
for(i=MAXS1; i< MAXS; i++) {
if(ptr[i]) {
printf("%d free(%d)\n", i, i*155);
free(ptr[i]);
}
}
return 0;
}
int test3(void)
{
int i;
void *ptr[MAXC];
printf("This is test C:\n");
for(i=0; i< MAXC; i++) {
printf("%d malloc(100)\n", i+1);
ptr[i] = malloc(100);
printf(" ...returned %p\n", ptr[i]);
}
for(i=0; i< MAXC; i++) {
printf("%d free()\n", i+1);
if(ptr[i])
free(ptr[i]);
}
printf("End of test C:\n");
return 0;
}
int test4(void)
{
int i;
int memory = 0;
void *pointers[MAX];
printf("This is test I:\n");
for(i=0; i<MAX; i++) {
printf("%d attempts malloc(%d)\n", i, i*6);
pointers[i]=malloc(i*6);
if(!pointers[i]) {
printf("cant get more memory!");
return(0);
}
memory += (i*6);
}
printf("\namount: %d\n", memory);
memory = 0;
for(i=0; i<MAX; i++) {
printf("%d attempts realloc(%d)\n", i, i*7);
pointers[i]=realloc(pointers[i], i*7);
memory += i*7;
}
printf("\namount: %d\n", memory);
for(i=0; i<MAX; i++) {
printf("%d attempts free(%d)\n", i, i*7);
free(pointers[i]);
}
printf("\nend of test 1\n");
return 0;
}
int test5(void)
{
int memory = 0;
int i;
void *pointers2[MAX2];
memory = 0;
printf("\nTest II\n");
for(i=0; i< MAX2; i++) {
printf("%d attempts malloc(%d)\n", i, 7);
pointers2[i] = malloc(7);
memory += 7;
}
printf("\namount: %d\n", memory);
for(i=0; i< MAX2; i++) {
if(pointers2[i])
free(pointers2[i]);
}
printf("\nend of test II\n");
return 0;
}
#define HEAPSIZE 10000
void smallblocks(void)
{
void *ptr;
int i=0;
do {
ptr = malloc(16);
i++;
} while(ptr);
printf("I: %d\n", i);
}
int main(int argc, char **argv)
{
void *heap = (malloc)(HEAPSIZE);
if(!heap)
return -1;
dmalloc_initialize();
bmalloc_add_pool(heap, HEAPSIZE);
smallblocks();
bmalloc_status();
dmalloc_status();
return 0;
test1();
test2();
test3();
test4();
test5();
return 0;
}

View file

@ -1,70 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "bmalloc.h"
int main(int argc, char **argv)
{
void *pointers[5];
int i;
void *area;
for(i=0; i<5; i++)
pointers[i] = malloc(8000);
if(argc>1) {
switch(argv[1][0]) {
case '1':
for(i=0; i<5; i++) {
bmalloc_add_pool(pointers[i], 4000);
bmalloc_add_pool((char *)pointers[i]+4000, 4000);
}
break;
case '2':
area = malloc(20000);
bmalloc_add_pool(area, 3000);
bmalloc_add_pool((char *)area+6000, 3000);
bmalloc_add_pool((char *)area+3000, 3000);
bmalloc_add_pool((char *)area+12000, 3000);
bmalloc_add_pool((char *)area+9000, 3000);
break;
case '3':
{
void *ptr[10];
area = malloc(20000);
bmalloc_add_pool(area, 20000);
printf(" ** TEST USAGE\n");
for(i=0; i<9; i++)
ptr[i]=bmalloc(200);
bmalloc_status();
for(i=0; i<9; i++)
bfree(ptr[i]);
printf(" ** END OF TEST USAGE\n");
}
break;
case '4':
{
void *ptr[10];
area = malloc(20000);
bmalloc_add_pool(area, 20000);
ptr[0]=bmalloc(4080);
bmalloc_status();
bfree(ptr[0]);
printf(" ** END OF TEST USAGE\n");
}
break;
}
}
else
for(i=4; i>=0; i--)
bmalloc_add_pool(pointers[i], 8000-i*100);
bmalloc_status();
return 0;
}