ce7e585e788e45f11d5b137d1876f9f412c2e268
[alexxy/gromacs.git] / include / memtab.h
1 /*
2  * $Id$
3  * 
4  *       This source code is part of
5  * 
6  *        G   R   O   M   A   C   S
7  * 
8  * GROningen MAchine for Chemical Simulations
9  * 
10  *               VERSION 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Good ROcking Metal Altar for Chronical Sinners
28  */
29
30 #ifndef _memtab_h
31 #define _memtab_h
32
33 static char *SRCID_memtab_h = "$Id$";
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifdef HAVE_IDENT
40 #ident  "@(#) memtab.h 1.12 12/16/92"
41 #endif /* HAVE_IDENT */
42
43 /*
44  * This module is intended for alloc(at)ing memory in one contiguous
45  * block, using only local references. All references within this block
46  * are made relative to the start of the block. This means that before
47  * using a pointer in this block, they must be reallocated, simply by
48  * adding the base address to the pointer value.
49  */
50
51 #define NOENTRY         -1      /* Denotes a NULL pointer reference     */
52
53 typedef struct
54 {
55   void *ref;            /* The "physical" address of an entry           */
56   void *handle;         /* The associated handle                        */
57 } t_mref;
58
59 typedef struct
60 {
61   int nref;             /* The number of inserted references            */
62   t_mref *refs;         /* The inserted references and their handles    */
63   int msize;            /* The total size of the memory allocated sofar */
64   char *mtab;           /* The allocated memory (one contiguous block)  */
65 } t_memtab;
66
67 extern void init_memtab(t_memtab *mtab);
68      /*
69       * Initialises the struct, should be called before any other action
70       * The function init_memtab() will initialise a struct which can be
71       * extended by successive invokations of put_memtab().
72       */
73
74 extern void dispose_memtab(t_memtab *mtab);
75      /*
76       * Disposes all the memory allocated for the struct. After this
77       * any reference within the block may become invalid (depends on
78       * the definition of the free() call).
79       */
80
81 extern void *put_memtab(t_memtab *mtab,int size,void *ref);
82      /*
83       * The function put_memtab() returns a handle to the memory block
84       * specified by pointer ref and size bytes. If the address was inserted
85       * before in the struct, this (previous) handle will be returned else
86       * the struct will be extended, a new handle will be created and the
87       * data will be copied into the struct. Note that the returned handle
88       * is actually the offset of the copied block within the struct.
89       * NULL pointers and null sizes will return a handle that will convert 
90       * to NULL after invokation of get_memtab(). Note that after every
91       * invokation absolute addresses, got before from get_memtab() might
92       * have changed due to a move within the reallocation.
93       */
94
95 extern void *get_memtab(t_memtab *mtab,void *handle);
96      /*
97       * Returns the (physical) address corresponding to the specified handle.
98       * Handle should be a value returned by put_memtab(). When invoked
99       * with NULL for handle, get_memtab() returns the base address of
100       * the allocated block.
101       */
102
103 long wr_memtab(FILE *fp,t_memtab *mtab);
104      /*
105       * Writes the referenced contents of the struct to the file specified
106       * by fp. The function wr_memtab() returnes the number of bytes written.
107       */
108
109 void *rd_memtab(FILE *fp,int size,t_memtab *mtab);
110      /*
111       * Extends the struct by reading size bytes from the file specified
112       * by fp. The function rd_memtab() will return a handle to the read
113       * block.
114       */
115
116 #endif  /* _memtab_h */