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

Subversion Repositories or1k_old

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
 
5
 
6
struct list {
7
        struct list *next;
8
};
9
 
10
int main(void)
11
{
12
        int z=999;
13
        int *y=&z;
14
        int *x=NULL;
15
        struct list *save;
16
        struct list *lp;
17
        int i;
18
 
19
 
20
        printf("pointer to x is %p\n", x);
21
        printf("pointer to y is %p\n", y);
22
        x=malloc(sizeof(int)*2000);
23
        printf("pointer to x is %p\n", x);
24
        y=malloc(sizeof(int)*100);
25
        printf("pointer to y is %p\n", y);
26
        free(x);
27
        free(y);
28
        printf("about to free(0)\n");
29
        free(0);
30
 
31
        x=malloc(13);
32
        printf("x = %p\n", x);
33
        memcpy(x, "Small string", 13);
34
        printf("0x%p test string1: %s\n", x, (char *)x);
35
        y = realloc(x, 36);
36
        printf("0x%p test string1: %s\n", y, (char *)y);
37
        memcpy(y, "********** Larger string **********", 36);
38
        printf("0x%p test string2: %s\n", y, (char *)y);
39
        free(y);
40
 
41
 
42
        printf("Allocate 100 nodes 500 bytes each\n");
43
        save = 0;
44
        for (i=0; i<100; i++) {
45
                lp = malloc(500);
46
                if (lp == 0) {
47
                        printf("loop 1: malloc returned 0\n");
48
                        goto Failed;
49
                }
50
                lp->next = save;
51
                save = lp;
52
        }
53
 
54
        printf("freeing 100 nodes\n");
55
        while (save) {
56
                lp = save;
57
                save = save->next;
58
                free(lp);
59
        }
60
 
61
        printf("try realloc 100 times \n");
62
        lp = 0;
63
        for (i=1; i<=100; i++) {
64
                lp = realloc(lp, i*200);
65
                if (lp == 0) {
66
                        printf("loop 3: realloc returned 0\n");
67
                        goto Failed;
68
                }
69
        }
70
        realloc(lp, 0);
71
 
72
        printf("Allocate another 100 nodes 600 bytes each\n");
73
        save = 0;
74
        for (i=0; i<100; i++) {
75
                lp = malloc(600);
76
                if (lp == 0) {
77
                        printf("loop 2: malloc returned 0\n");
78
                        goto Failed;
79
                }
80
                lp->next = save;
81
                save = lp;
82
        }
83
 
84
        printf("freeing 100 nodes\n");
85
        while (save) {
86
                lp = save;
87
                save = save->next;
88
                free(lp);
89
        }
90
 
91
 
92
        printf("alloc test PASSED\n");
93
        exit(0);
94
 
95
Failed:
96
        printf("!!!!!!!!!!!! alloc test FAILED. !!!!!!!!!!!!!!!\n");
97
        exit(1);
98
}

powered by: WebSVN 2.1.0

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