fork(2) download
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #define PAGESIZE 4096
  5. #define NO_OF_POINTERS 2
  6. #define NO_OF_ITERATIONS 20
  7.  
  8. // change the following lines to test the real malloc and free
  9. #define MALLOC mymalloc
  10. #define FREE myfree
  11. typedef struct { unsigned long l,h; } ti;
  12. typedef struct { unsigned long sz,ml,tp,ap,tpg,apg,tv,av; } ms;
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. unsigned long * _stdcall VirtualAlloc(void *,unsigned long,unsigned long,unsigned long);
  17. int _stdcall VirtualFree(void *,unsigned long,unsigned long);
  18. void _stdcall GlobalMemoryStatus(ms *);
  19. void * _stdcall GetCurrentProcess(void);
  20. unsigned long _stdcall GetVersion(void);
  21. int _stdcall GetProcessTimes(void *, ti *,ti *, ti *, ti *);
  22. void _stdcall Sleep(unsigned long);
  23. #ifdef __cplusplus
  24. }
  25. #endif
  26.  
  27. int cputime(void) { // return cpu time used by current process
  28. ti ct,et,kt,ut;
  29. if(GetVersion()<0x80000000) { // are we running on NT/2000/XP
  30. GetProcessTimes(GetCurrentProcess(),&ct,&et,&kt,&ut);
  31. return (ut.l+kt.l)/10000; // include time in kernel
  32. }
  33. else return clock(); // for Windows 95/98/Me
  34. }
  35. int memory(void) { // return memory available to current process
  36. ms m;
  37. GlobalMemoryStatus(&m);
  38. return m.av;
  39. }
  40.  
  41. // you are not allowed to change the following function
  42. void *allocpages(int n) { // allocate n pages and return start address
  43. return VirtualAlloc(0,n * PAGESIZE,4096+8192,4);
  44. }
  45.  
  46. // you are not allowed to change the following function
  47. int freepages(void *p) { // free previously allocated pages.
  48. return VirtualFree(p,0,32768);
  49. }
  50.  
  51.  
  52. void *mymalloc(int n) { // very simple memory allocation
  53. void *p;
  54. p=allocpages((n/PAGESIZE)+1);
  55. if(!p) puts("Failed");
  56. return p;
  57. }
  58.  
  59. int myfree(void *p) { // very simple free
  60. int n;
  61. n=freepages(p);
  62. if(!n) puts("Failed");
  63. return n;
  64. }
  65.  
  66. unsigned seed=7652;
  67.  
  68. int myrand() { // pick a random number
  69. seed=(seed*2416+374441)%1771875;
  70. return seed;
  71. }
  72.  
  73. int randomsize() { // choose the size of memory to allocate
  74. int j,k;
  75. k=myrand();
  76. j=(k&3)+(k>>2 &3)+(k>>4 &3)+(k>>6 &3)+(k>>8 &3)+(k>>10 &3);
  77. j=1<<j;
  78. return (myrand() % j) +1;
  79. }
  80.  
  81.  
  82. int main() {
  83. int i,k;
  84. int size;
  85.  
  86. // used to store pointers to allocated memory
  87. unsigned char *n[NO_OF_POINTERS];
  88.  
  89. int s[5000]; // used to store sizes when testing
  90.  
  91. int start_time;
  92. int start_mem;
  93.  
  94. for(i=0;i<NO_OF_POINTERS;i++) {
  95. n[i]=0; // initially nothing is allocated
  96. }
  97.  
  98. start_time=cputime();
  99. start_mem=memory();
  100.  
  101. for(i=0;i<NO_OF_ITERATIONS;i++) {
  102. k=myrand()%NO_OF_POINTERS; // pick a pointer
  103. if(n[k]) { // if it was allocated then free it
  104. // check that the stuff we wrote has not changed
  105. if(n[k][0]!=(unsigned char)(n[k]+s[k]+k))
  106. printf("Error when checking first byte!\n");
  107. if(s[k]>1 && n[k][s[k]-1]!=(unsigned char)(n[k]-s[k]-k))
  108. printf("Error when checking last byte!\n");
  109. FREE(n[k]);
  110. }
  111. size=randomsize(); // pick a random size
  112. size=1;
  113. n[k]=(unsigned char *)MALLOC(size); // do the allocation
  114. s[k]=size; // remember the size
  115. n[k][0]=(unsigned char)(n[k]+s[k]+k); // put some data in the first and
  116. if(size>1) n[k][size-1]=(unsigned char)(n[k]-s[k]-k); // last byte
  117. }
  118.  
  119. // print some statistics
  120. printf("That took %.3f seconds and used %d bytes\n",
  121. ((float)(cputime()-start_time))/1000,
  122. start_mem-memory());
  123.  
  124. return 1;
  125. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
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
stdout

Standard output is empty