language: C (gcc-4.7.2)
date: 233 days 17 hours ago
link:
visibility: public
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define PAGESIZE 4096
#define NO_OF_POINTERS 2
#define NO_OF_ITERATIONS 20
 
// change the following lines to test the real malloc and free
#define MALLOC mymalloc
#define FREE myfree
typedef struct { unsigned long l,h; } ti;
typedef struct { unsigned long sz,ml,tp,ap,tpg,apg,tv,av; } ms;
#ifdef __cplusplus
extern "C" {
#endif
unsigned long * _stdcall VirtualAlloc(void *,unsigned long,unsigned long,unsigned long);
int _stdcall VirtualFree(void *,unsigned long,unsigned long);
void _stdcall GlobalMemoryStatus(ms *);
void * _stdcall GetCurrentProcess(void);
unsigned long _stdcall GetVersion(void);
int _stdcall GetProcessTimes(void *, ti *,ti *, ti *, ti *);
void _stdcall Sleep(unsigned long);
#ifdef __cplusplus
}
#endif
 
int cputime(void) { // return cpu time used by current process
   ti ct,et,kt,ut;
   if(GetVersion()<0x80000000) {  // are we running on NT/2000/XP
      GetProcessTimes(GetCurrentProcess(),&ct,&et,&kt,&ut);
      return (ut.l+kt.l)/10000; // include time in kernel
   }
   else return clock(); // for Windows 95/98/Me
}
int memory(void) { // return memory available to current process
   ms m;
   GlobalMemoryStatus(&m);
   return m.av;
}
 
// you are not allowed to change the following function
void *allocpages(int n) { // allocate n pages and return start address
   return VirtualAlloc(0,n * PAGESIZE,4096+8192,4);
}
 
// you are not allowed to change the following function
int freepages(void *p) { // free previously allocated pages.
   return VirtualFree(p,0,32768);
}
 
 
void *mymalloc(int n) { // very simple memory allocation
   void *p;
   p=allocpages((n/PAGESIZE)+1);
   if(!p) puts("Failed");
   return p;
}
 
int myfree(void *p) { // very simple free
   int n;
   n=freepages(p);
   if(!n) puts("Failed");
   return n;
}
 
unsigned seed=7652;
 
int myrand() { // pick a random number
   seed=(seed*2416+374441)%1771875;
   return seed;
}
 
int randomsize() { // choose the size of memory to allocate
   int j,k;
   k=myrand();
   j=(k&3)+(k>>2 &3)+(k>>4 &3)+(k>>6 &3)+(k>>8 &3)+(k>>10 &3);
   j=1<<j;
   return (myrand() % j) +1;
}
 
 
int main() {
   int i,k;
   int size;
 
   // used to store pointers to allocated memory
   unsigned char *n[NO_OF_POINTERS]; 
 
   int s[5000]; // used to store sizes when testing
 
   int start_time;
   int start_mem;
 
   for(i=0;i<NO_OF_POINTERS;i++) {
      n[i]=0;     // initially nothing is allocated
   }
 
   start_time=cputime();
   start_mem=memory();
 
   for(i=0;i<NO_OF_ITERATIONS;i++) {
      k=myrand()%NO_OF_POINTERS; // pick a pointer
      if(n[k]) { // if it was allocated then free it
         // check that the stuff we wrote has not changed
         if(n[k][0]!=(unsigned char)(n[k]+s[k]+k))
            printf("Error when checking first byte!\n");
         if(s[k]>1 && n[k][s[k]-1]!=(unsigned char)(n[k]-s[k]-k))
            printf("Error when checking last byte!\n");
         FREE(n[k]);
      }
      size=randomsize(); // pick a random size
                size=1;
      n[k]=(unsigned char *)MALLOC(size); // do the allocation
      s[k]=size; // remember the size
      n[k][0]=(unsigned char)(n[k]+s[k]+k);  // put some data in the first and
      if(size>1) n[k][size-1]=(unsigned char)(n[k]-s[k]-k); // last byte
   }
 
   // print some statistics
   printf("That took %.3f seconds and used %d bytes\n",
         ((float)(cputime()-start_time))/1000,
         start_mem-memory());
 
   return 1;
}
prog.c:16: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘VirtualAlloc’
prog.c:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘VirtualFree’
prog.c:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘GlobalMemoryStatus’
prog.c:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘GetCurrentProcess’
prog.c:20: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘GetVersion’
prog.c:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘GetProcessTimes’
prog.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘Sleep’
prog.c: In function ‘cputime’:
prog.c:29: warning: implicit declaration of function ‘GetVersion’
prog.c:30: warning: implicit declaration of function ‘GetProcessTimes’
prog.c:30: warning: implicit declaration of function ‘GetCurrentProcess’
prog.c: In function ‘memory’:
prog.c:37: warning: implicit declaration of function ‘GlobalMemoryStatus’
prog.c: In function ‘allocpages’:
prog.c:43: warning: implicit declaration of function ‘VirtualAlloc’
prog.c:43: warning: return makes pointer from integer without a cast
prog.c: In function ‘freepages’:
prog.c:48: warning: implicit declaration of function ‘VirtualFree’
prog.c: In function ‘main’:
prog.c:105: warning: cast from pointer to integer of different size
prog.c:107: warning: cast from pointer to integer of different size
prog.c:115: warning: cast from pointer to integer of different size
prog.c:116: warning: cast from pointer to integer of different size