Release 950901
[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 #include <stddebug.h>
13 #define DEBUG_DEFINE_VARIABLES     /* just avoid dumb errors */
14 #include <debug.h>                 /* for "stddeb" */
15 #include <stdlib.h>
16 #include <string.h>
17 #include "shm_block.h"
18 #include "shm_fragment.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=malloc(SHM_MINBLOCK);
64   assert(block);
65
66   /* setup first item in the free list */
67   shm_FragmentInit(block, sizeof(*block), SHM_MINBLOCK);
68   
69   fprintf(stddeb,"After shm_FragmentInit\n");
70   shm_print_free_list(block);
71
72   for(i=0 ; i < LIST_LENGTH; i++) {
73      size=sizes[i];
74      if (size>0) {                 /* allocate */
75         ret=shm_FragPtrAlloc(block, size);
76         ptr[i]=ret;
77         fprintf(stddeb,
78                 "%d: After shm_FragmentAlloc(block, 0x%06x) == ",
79                 i, size);
80         if (ret==NULL)
81            fprintf(stddeb, "NULL\n");
82         else {
83            fprintf(stddeb, "0x%06x\n", (int)ret-(int)block);
84            bzero (ret,size);       /* test boundaries */
85         }
86      } else {                      /* free */
87         /* free shm fragment */
88         ret=ptr[-sizes[i]];
89         fprintf(stddeb, "%d: Doing shm_FragmentFree(block, ", i);
90         if (ret==NULL)
91            fprintf(stddeb, "NULL)\n");
92         else 
93            fprintf(stddeb, "0x%06x)\n", (int)ret-(int)block);
94         fflush(stddeb);
95         shm_FragPtrFree(block, ret);
96      }
97      shm_print_free_list(block);
98   }
99   return 0;
100 }