validate_mem_util.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdio.h>
  2. #include "mem_util.h"
  3. struct number
  4. {
  5. struct number *next;
  6. int n;
  7. };
  8. int main()
  9. {
  10. struct counter_helper *ch;
  11. struct counter_header *ci;
  12. struct stack_helper *sh;
  13. struct mem_helper *mh,*mhp;
  14. struct temp_mem *th;
  15. struct number **nm;
  16. struct number *nd,*np;
  17. int i;
  18. ch = create_counter( sizeof(struct number) , 8 );
  19. sh = create_stack( sizeof(struct number) , 6 );
  20. mh = create_mem( sizeof(struct number) , 7 );
  21. th = create_temp( 5 );
  22. nm = (struct number**) temp_malloc( 10*sizeof(struct number*) , th );
  23. nd = (struct number*) temp_malloc( sizeof(struct number) , th );
  24. for (i=0;i<10;i++)
  25. {
  26. nm[i] = (struct number*) mem_get(mh);
  27. nm[i]->n = i;
  28. nm[i]->next = NULL;
  29. }
  30. printf("Memory lives here:\n");
  31. for (i=0;i<10;i++)
  32. {
  33. printf(" %x",nm[i]);
  34. if ((i%5)==4) printf("\n");
  35. }
  36. printf("\n");
  37. stack_push(sh , nm[8]);
  38. stack_push(sh , nm[5]);
  39. stack_push(sh , nm[8]);
  40. stack_push(sh , nm[4]);
  41. stack_push(sh , nm[5]);
  42. stack_push(sh , nm[3]);
  43. stack_push(sh , nm[4]);
  44. stack_push(sh , nm[1]);
  45. stack_push(sh , nm[0]);
  46. stack_push(sh , nm[0]);
  47. stack_push(sh , nm[1]);
  48. stack_push(sh , nm[2]);
  49. stack_push(sh , nm[1]);
  50. stack_push(sh , nm[5]);
  51. printf("Pushed 858 453 4100 1215.\n");
  52. np = stack_access(sh,1); printf("Top item: %d; ",np->n);
  53. np = stack_access(sh,5); printf("5th item: %d; ",np->n);
  54. np = stack_access(sh,9); printf("9th item: %d\n",np->n);
  55. printf("Popping: ");
  56. while (stack_nonempty(sh))
  57. {
  58. stack_pop(sh,nd);
  59. printf("%d ",nd->n);
  60. }
  61. printf("EMPTY \n\n");
  62. counter_add(ch , nm[8]);
  63. counter_add(ch , nm[5]);
  64. counter_add(ch , nm[8]);
  65. counter_add(ch , nm[4]);
  66. counter_add(ch , nm[5]);
  67. counter_add(ch , nm[3]);
  68. counter_add(ch , nm[4]);
  69. counter_add(ch , nm[1]);
  70. counter_add(ch , nm[0]);
  71. counter_add(ch , nm[0]);
  72. counter_add(ch , nm[1]);
  73. counter_add(ch , nm[2]);
  74. counter_add(ch , nm[1]);
  75. counter_add(ch , nm[5]);
  76. printf("Counted 858 453 4100 1215.\n");
  77. ci = counter_iterator(ch);
  78. if (ch->head == NULL) printf("Ooops, somehow counted nothing!\n");
  79. printf("Counting: ");
  80. while (ci != NULL)
  81. {
  82. counter_read(ch,ci,nd);
  83. printf("%d of %d ; ", counter_howmany(ci), nd);
  84. ci = counter_next_entry(ci);
  85. }
  86. printf("DONE \n\n");
  87. for (i=0;i<10;i++)
  88. {
  89. int j;
  90. for (j=i;j<10;j++)
  91. {
  92. mem_put(mh,nm[j]);
  93. }
  94. for (j=i;j<10;j++)
  95. {
  96. nm[j] = (struct number*) mem_get(mh);
  97. }
  98. }
  99. printf("Memory lives here:\n");
  100. for (i=0;i<10;i++)
  101. {
  102. printf(" %x",nm[i]);
  103. if ((i%5)==4) printf("\n");
  104. }
  105. printf("\n");
  106. delete_mem(mh);
  107. free_temp(th);
  108. }