Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 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> |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 12 | #define DEBUG_DEFINE_VARIABLES /* just avoid dumb errors */ |
Alexandre Julliard | 9fe7a25 | 1999-05-14 08:17:14 +0000 | [diff] [blame^] | 13 | #include "debugtools.h" /* for "stddeb" */ |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include "shm_block.h" |
| 17 | #include "shm_fragment.h" |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 18 | #include "xmalloc.h" |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | 902da69 | 1995-11-05 14:39:02 +0000 | [diff] [blame] | 63 | block=xmalloc(SHM_MINBLOCK); |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 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); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 83 | memset( ret,0, size ); /* test boundaries */ |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 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 | } |