| 1 | /* |
|---|
| 2 | This implements a handle management module. Sometimes it is useful to |
|---|
| 3 | protect the user of some software module from messing with complicated |
|---|
| 4 | datastructures. In such cases it is useful to use an integer handle |
|---|
| 5 | which can be translated into a pointer when needed by the code implementing |
|---|
| 6 | the module. Such a scheme is implemented in this module. |
|---|
| 7 | |
|---|
| 8 | Mark Koennecke, October 2000 |
|---|
| 9 | */ |
|---|
| 10 | #include <stdlib.h> |
|---|
| 11 | #include <string.h> |
|---|
| 12 | #include <assert.h> |
|---|
| 13 | #include "handle.h" |
|---|
| 14 | |
|---|
| 15 | #define MAX_NUMBEROFLINKS 200 |
|---|
| 16 | |
|---|
| 17 | static void **pointerArray = NULL; |
|---|
| 18 | |
|---|
| 19 | /* This is a 2 dimensional dynamic array assosiates the link handles with their |
|---|
| 20 | * file id handles so they can be removed with in nxclose. |
|---|
| 21 | */ |
|---|
| 22 | static int (*listOfNXlinks)[MAXHANDLE] = NULL; |
|---|
| 23 | |
|---|
| 24 | static int iscreated = 0; |
|---|
| 25 | |
|---|
| 26 | /*----------------------------------------------------------------------*/ |
|---|
| 27 | static void checkArray() |
|---|
| 28 | { |
|---|
| 29 | if(pointerArray == NULL) |
|---|
| 30 | { |
|---|
| 31 | pointerArray = (void **)malloc(MAXHANDLE*sizeof(void *)); |
|---|
| 32 | assert(pointerArray != NULL); |
|---|
| 33 | memset(pointerArray,0,MAXHANDLE*sizeof(void *)); |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | /*--------------------------------------------------------------------*/ |
|---|
| 37 | int HHMakeHandle(void *pData) |
|---|
| 38 | { |
|---|
| 39 | int i; |
|---|
| 40 | |
|---|
| 41 | checkArray(); |
|---|
| 42 | /* |
|---|
| 43 | find first free slot in the pointerArray, store the pointer and |
|---|
| 44 | return the index. |
|---|
| 45 | */ |
|---|
| 46 | for(i = 0; i < MAXHANDLE; i++) |
|---|
| 47 | { |
|---|
| 48 | if(pointerArray[i] == NULL) |
|---|
| 49 | { |
|---|
| 50 | pointerArray[i] = pData; |
|---|
| 51 | iscreated = 1; |
|---|
| 52 | listOfNXlinks = calloc (MAX_NUMBEROFLINKS, sizeof (*listOfNXlinks)); |
|---|
| 53 | |
|---|
| 54 | return i; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | return -1; |
|---|
| 58 | } |
|---|
| 59 | /*--------------------------------------------------------------------*/ |
|---|
| 60 | void HHChangeHandle(void *pData, int currentHandle) |
|---|
| 61 | { |
|---|
| 62 | int i; |
|---|
| 63 | |
|---|
| 64 | pointerArray[currentHandle] = pData; |
|---|
| 65 | iscreated = 1; |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | /*--------------------------------------------------------------------*/ |
|---|
| 69 | int HHMakeLinkHandle(void *pData, int currentHandle) |
|---|
| 70 | { |
|---|
| 71 | int i; |
|---|
| 72 | int k; |
|---|
| 73 | |
|---|
| 74 | checkArray(); |
|---|
| 75 | /* |
|---|
| 76 | find first free slot in the pointerArray, store the pointer and |
|---|
| 77 | return the index. |
|---|
| 78 | */ |
|---|
| 79 | for(i = 0; i < MAXHANDLE; i++) |
|---|
| 80 | { |
|---|
| 81 | if(pointerArray[i] == NULL) |
|---|
| 82 | { |
|---|
| 83 | pointerArray[i] = pData; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | for(k = 0; k < MAX_NUMBEROFLINKS; k++) |
|---|
| 87 | { |
|---|
| 88 | if(listOfNXlinks[currentHandle][k] == NULL) |
|---|
| 89 | { |
|---|
| 90 | listOfNXlinks[currentHandle][k] = i; |
|---|
| 91 | return i; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | return -1; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /*---------------------------------------------------------------------*/ |
|---|
| 103 | void *HHGetPointer(int handle) |
|---|
| 104 | { |
|---|
| 105 | checkArray(); |
|---|
| 106 | return pointerArray[handle]; |
|---|
| 107 | } |
|---|
| 108 | /*---------------------------------------------------------------------*/ |
|---|
| 109 | int HHRemoveHandle(int handle) |
|---|
| 110 | { |
|---|
| 111 | int k; |
|---|
| 112 | |
|---|
| 113 | assert(handle < MAXHANDLE && handle >= 0); |
|---|
| 114 | checkArray(); |
|---|
| 115 | pointerArray[handle] = NULL; |
|---|
| 116 | |
|---|
| 117 | for(k = 0; k < MAX_NUMBEROFLINKS; k++) |
|---|
| 118 | { |
|---|
| 119 | if(listOfNXlinks[handle][k] == NULL) |
|---|
| 120 | { |
|---|
| 121 | return(0); |
|---|
| 122 | } |
|---|
| 123 | free(pointerArray[(listOfNXlinks[handle][k])]); |
|---|
| 124 | pointerArray[(listOfNXlinks[handle][k])] = NULL; |
|---|
| 125 | } |
|---|
| 126 | return(-1); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /*---------------------------------------------------------------------*/ |
|---|
| 130 | int HHCheckIfHandleExists(int check) |
|---|
| 131 | { |
|---|
| 132 | if(iscreated == 1) { |
|---|
| 133 | if(pointerArray[check] == NULL) |
|---|
| 134 | check = -1; |
|---|
| 135 | else check = 0; |
|---|
| 136 | } |
|---|
| 137 | else check = -1; |
|---|
| 138 | return check; |
|---|
| 139 | |
|---|
| 140 | } |
|---|