More #pragma fixes (idea Ove Kaaven, mistakes are mine).
[wine] / ipc / shm_fragment_test.c
1 /***************************************************************************
2  * Copyright 1995, Technion, Israel Institute of Technology
3  * Electrical Eng, Software Lab.
4  * Author:    Michael Veksler.
5  ***************************************************************************
6  * File:      shm_fragment_test.c
7  * Purpose:   Test data fragments and free list items. Allocate and free blocks.
8  ***************************************************************************
9  */
10 #include <assert.h>
11 #include <stdio.h>                 
12 #define DEBUG_DEFINE_VARIABLES     /* just avoid dumb errors */
13 #include <debug.h>                 /* for "stddeb" */
14 #include <stdlib.h>
15 #include <string.h>
16 #include "shm_block.h"
17 #include "shm_fragment.h"
18 #include "xmalloc.h"
19
20 #define DO_FREE(id) (-id)
21 #define LIST_LENGTH 20
22
23 int main()
24 {
25   struct shm_block *block;
26   char *ret;
27   int size;
28   int i;
29
30   /* important: The test will work only for the current implementation of */
31   /* allocation, if the implementation will change, the list should also */
32   /* cahnge. */
33   static int sizes[LIST_LENGTH]={
34     SHM_MINBLOCK,                  /* 0: should fail */
35     0x3fe0-4,                      /* 1: */
36     0x4000-4,                      /* 2: */
37     0x4000-4,                      /* 3: */
38     0x4000-4+1,                    /* 4: should fail */
39     0x4000-4,                      /* 5: */
40     /* allocated(5,3,2,1) free() */
41     -5,                            /* 6: */
42     0x1c00-4,                      /* 7: */
43     0x1400-4,                      /* 8: */
44     0x1000-4,                      /* 9: */
45     /* allocated(9,8,7,3,2,1) free() */
46     -9,                            /* 10: */
47     -3,                            /* 11: */
48     -1,                            /* 12: */
49     /* allocated(8,7,2) free(9,3,1) */
50     0x1000-4,                      /* 13: */
51     -13,                           /* 14: */
52     0x1000+1-4,                    /* 15: */
53     /* allocated(8,7,15,2) free(9,[3-15],1) */
54     -2,                            /* 16: */
55     /* allocated(8,7,15) free(9,[3-15],1+2) */
56     -8,                            /* 17: */
57     -7,                            /* 18: */
58     -15                            /* 19: */
59     };
60   
61   static char *ptr[LIST_LENGTH];
62   
63   block=xmalloc(SHM_MINBLOCK);
64
65   /* setup first item in the free list */
66   shm_FragmentInit(block, sizeof(*block), SHM_MINBLOCK);
67   
68   fprintf(stddeb,"After shm_FragmentInit\n");
69   shm_print_free_list(block);
70
71   for(i=0 ; i < LIST_LENGTH; i++) {
72      size=sizes[i];
73      if (size>0) {                 /* allocate */
74         ret=shm_FragPtrAlloc(block, size);
75         ptr[i]=ret;
76         fprintf(stddeb,
77                 "%d: After shm_FragmentAlloc(block, 0x%06x) == ",
78                 i, size);
79         if (ret==NULL)
80            fprintf(stddeb, "NULL\n");
81         else {
82            fprintf(stddeb, "0x%06x\n", (int)ret-(int)block);
83            memset( ret,0, size );  /* test boundaries */
84         }
85      } else {                      /* free */
86         /* free shm fragment */
87         ret=ptr[-sizes[i]];
88         fprintf(stddeb, "%d: Doing shm_FragmentFree(block, ", i);
89         if (ret==NULL)
90            fprintf(stddeb, "NULL)\n");
91         else 
92            fprintf(stddeb, "0x%06x)\n", (int)ret-(int)block);
93         fflush(stddeb);
94         shm_FragPtrFree(block, ret);
95      }
96      shm_print_free_list(block);
97   }
98   return 0;
99 }