summaryrefslogtreecommitdiff
path: root/firmware/test/malloc/dmytest.c
blob: a63bb30d749012799e018f6bc58cec12164caf38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#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;
}