blob: d4004308fb3d27558cc7ff404ca5342ae7d65288 [file] [log] [blame]
Alexandre Julliarde2991ea1995-07-29 13:09:43 +00001/***************************************************************************
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>
Alexandre Julliarde2991ea1995-07-29 13:09:43 +000012#define DEBUG_DEFINE_VARIABLES /* just avoid dumb errors */
Alexandre Julliard9fe7a251999-05-14 08:17:14 +000013#include "debugtools.h" /* for "stddeb" */
Alexandre Julliarde2991ea1995-07-29 13:09:43 +000014#include <stdlib.h>
15#include <string.h>
16#include "shm_block.h"
17#include "shm_fragment.h"
Alexandre Julliard902da691995-11-05 14:39:02 +000018#include "xmalloc.h"
Alexandre Julliarde2991ea1995-07-29 13:09:43 +000019
20#define DO_FREE(id) (-id)
21#define LIST_LENGTH 20
22
23int 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
Alexandre Julliard902da691995-11-05 14:39:02 +000063 block=xmalloc(SHM_MINBLOCK);
Alexandre Julliarde2991ea1995-07-29 13:09:43 +000064
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);
Alexandre Julliard7e56f681996-01-31 19:02:28 +000083 memset( ret,0, size ); /* test boundaries */
Alexandre Julliarde2991ea1995-07-29 13:09:43 +000084 }
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}