NeXusJavaBindings  1
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
handle.c
Go to the documentation of this file.
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 static void **pointerArray = NULL;
16 
17 /*----------------------------------------------------------------------*/
18 static void checkArray()
19 {
20  if(pointerArray == NULL)
21  {
22  pointerArray = (void **)malloc(MAXHANDLE*sizeof(void *));
23  assert(pointerArray != NULL);
24  memset(pointerArray,0,MAXHANDLE*sizeof(void *));
25  }
26 }
27 /*--------------------------------------------------------------------*/
28 int HHMakeHandle(void *pData)
29 {
30  int i;
31 
32  checkArray();
33  /*
34  find first free slot in the pointerArray, store the pointer and
35  return the index.
36  */
37  for(i = 0; i < MAXHANDLE; i++)
38  {
39  if(pointerArray[i] == NULL)
40  {
41  pointerArray[i] = pData;
42  return i;
43  }
44  }
45  return -1;
46 }
47 /*---------------------------------------------------------------------*/
48 void *HHGetPointer(int handle)
49 {
50  assert(handle < MAXHANDLE && handle >= 0);
51  checkArray();
52  return pointerArray[handle];
53 }
54 /*---------------------------------------------------------------------*/
55 void HHRemoveHandle(int handle)
56 {
57  assert(handle < MAXHANDLE && handle >= 0);
58  checkArray();
59  pointerArray[handle] = NULL;
60 }
61