Release 970305
[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 #include "xmalloc.h"
20
21 #define DO_FREE(id) (-id)
22 #define LIST_LENGTH 20
23
24 int main()
25 {
26   struct shm_block *block;
27   char *ret;
28   int size;
29   int i;
30
31   /* important: The test will work only for the current implementation of */
32   /* allocation, if the implementation will change, the list should also */
33   /* cahnge. */
34   static int sizes[LIST_LENGTH]={
35     SHM_MINBLOCK,                  /* 0: should fail */
36     0x3fe0-4,                      /* 1: */
37     0x4000-4,                      /* 2: */
38     0x4000-4,                      /* 3: */
39     0x4000-4+1,                    /* 4: should fail */
40     0x4000-4,                      /* 5: */
41     /* allocated(5,3,2,1) free() */
42     -5,                            /* 6: */
43     0x1c00-4,                      /* 7: */
44     0x1400-4,                      /* 8: */
45     0x1000-4,                      /* 9: */
46     /* allocated(9,8,7,3,2,1) free() */
47     -9,                            /* 10: */
48     -3,                            /* 11: */
49     -1,                            /* 12: */
50     /* allocated(8,7,2) free(9,3,1) */
51     0x1000-4,                      /* 13: */
52     -13,                           /* 14: */
53     0x1000+1-4,                    /* 15: */
54     /* allocated(8,7,15,2) free(9,[3-15],1) */
55     -2,                            /* 16: */
56     /* allocated(8,7,15) free(9,[3-15],1+2) */
57     -8,                            /* 17: */
58     -7,                            /* 18: */
59     -15                            /* 19: */
60     };
61   
62   static char *ptr[LIST_LENGTH];
63   
64   block=xmalloc(SHM_MINBLOCK);
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            memset( ret,0, 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 }