OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [linux/] [uClibc/] [test/] [malloc/] [malloc.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
 
2
#include <unistd.h>
3
#include <stdlib.h>
4
#include <stdio.h>
5
 
6
#define N_PTRS 1000
7
#define N_ALLOCS 10000
8
#define MAX_SIZE 0x10000
9
 
10
#define random_size()   (random()%MAX_SIZE)
11
#define random_ptr()    (random()%N_PTRS)
12
 
13
void test1(void);
14
void test2(void);
15
 
16
int main(int argc,char *argv[])
17
{
18
        test1();
19
        test2();
20
        return 0;
21
}
22
 
23
void test1(void)
24
{
25
        void **ptrs;
26
        int i,j;
27
        int size;
28
 
29
        srandom(0x19730929);
30
 
31
        ptrs = malloc(N_PTRS*sizeof(void *));
32
 
33
        for(i=0;i<N_PTRS;i++){
34
                ptrs[i]=malloc(random_size());
35
        }
36
        for(i=0;i<N_ALLOCS;i++){
37
                j=random_ptr();
38
                free(ptrs[j]);
39
 
40
                size=random_size();
41
                ptrs[j]=malloc(size);
42
                if(!ptrs[j]){
43
                        printf("malloc failed! %d\n",i);
44
                }
45
                memset(ptrs[j],0,size);
46
        }
47
        for(i=0;i<N_PTRS;i++){
48
                free(ptrs[i]);
49
        }
50
}
51
 
52
void test2(void)
53
{
54
        void *ptr = NULL;
55
 
56
        ptr = realloc(ptr,100);
57
        if(!ptr){
58
                printf("couldn't realloc() a NULL pointer\n");
59
        }else{
60
                free(ptr);
61
        }
62
 
63
        ptr = malloc(100);
64
        ptr = realloc(ptr, 0);
65
        if(ptr){
66
                printf("realloc(,0) failed\n");
67
                free(ptr);
68
        }
69
}
70
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.