359df778 |
1 | /* -*-c-*- |
2 | * |
37038e63 |
3 | * $Id: lmem.h,v 1.3 2000/07/29 21:58:15 mdw Exp $ |
359df778 |
4 | * |
5 | * Locked memory allocation |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: lmem.h,v $ |
37038e63 |
33 | * Revision 1.3 2000/07/29 21:58:15 mdw |
34 | * (l_destroy): New function for destroying locked memory blocks. |
35 | * |
47d5f7c2 |
36 | * Revision 1.2 2000/06/17 11:29:38 mdw |
37 | * Add arena support. |
38 | * |
359df778 |
39 | * Revision 1.1 1999/12/22 16:02:52 mdw |
40 | * Interface to allocating `locked' memory (which isn't paged out). |
41 | * |
42 | */ |
43 | |
44 | #ifndef CATACOMB_LMEM_H |
45 | #define CATACOMB_LMEM_H |
46 | |
47 | #ifdef __cplusplus |
48 | extern "C" { |
49 | #endif |
50 | |
51 | /*----- Header files ------------------------------------------------------*/ |
52 | |
53 | #include <stddef.h> |
54 | |
47d5f7c2 |
55 | #include <mLib/arena.h> |
359df778 |
56 | #include <mLib/dstr.h> |
57 | |
58 | /*----- Data structures ---------------------------------------------------*/ |
59 | |
60 | /* --- Block list --- * |
61 | * |
62 | * The block list is kept in normal memory, to avoid wasting precious locked |
63 | * memory. Entries are sorted into ascending address order to make |
64 | * coalescing free blocks easier. All blocks, free or not, are included in |
65 | * the list. |
66 | */ |
67 | |
68 | typedef struct l_node { |
69 | struct l_node *next; /* Next free block in chain */ |
70 | char *p; /* Pointer to the block */ |
71 | size_t sz; /* Size of the block */ |
72 | unsigned f; /* Various flags */ |
73 | } l_node; |
74 | |
75 | enum { |
76 | LF_ALLOC = 1 |
77 | }; |
78 | |
79 | /* --- Locked memory buffer state --- */ |
80 | |
81 | typedef struct lmem { |
47d5f7c2 |
82 | arena a; /* Arena header block */ |
37038e63 |
83 | unsigned f; /* Various flags */ |
359df778 |
84 | char *p; /* Pointer to locked buffer */ |
85 | l_node *l; /* Pointer to block list */ |
86 | size_t sz; /* Size of locked buffer */ |
87 | size_t free; /* Size of free area */ |
88 | int err; char *emsg; /* Error indicators */ |
89 | } lmem; |
90 | |
37038e63 |
91 | enum { |
92 | LF_LOCKED = 1 |
93 | }; |
47d5f7c2 |
94 | |
359df778 |
95 | /*----- Functions provided ------------------------------------------------*/ |
96 | |
97 | /* --- @l_init@ --- * |
98 | * |
99 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
100 | * @size_t sz@ = size of locked memory area requested |
101 | * |
102 | * Returns: Zero if everything is fine, @+1@ if some insecure memory was |
103 | * allocated, and @-1@ if everything went horribly wrong. |
104 | * |
105 | * Use: Initializes the locked memory manager. This function is safe |
106 | * to call in a privileged program; privileges should usually be |
107 | * dropped after allocating the locked memory block. |
108 | * |
109 | * You must call @sub_init@ before allocating locked memory |
110 | * buffers. |
111 | */ |
112 | |
113 | extern int l_init(lmem */*lm*/, size_t /*sz*/); |
114 | |
115 | /* --- @l_alloc@ --- * |
116 | * |
117 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
118 | * @size_t sz@ = size requested |
119 | * |
120 | * Returns: Pointer to allocated memory. |
121 | * |
122 | * Use: Allocates @sz@ bytes of locked memory. |
123 | */ |
124 | |
125 | extern void *l_alloc(lmem */*lm*/, size_t /*sz*/); |
126 | |
127 | /* --- @l_free@ --- * |
128 | * |
129 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
130 | * @void *p@ = pointer to block |
131 | * |
132 | * Returns: --- |
133 | * |
134 | * Use: Releases a block of locked memory. |
135 | */ |
136 | |
137 | extern void l_free(lmem */*lm*/, void */*p*/); |
138 | |
139 | /* --- @l_purge@ --- * |
140 | * |
141 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
142 | * |
143 | * Returns: --- |
144 | * |
145 | * Use: Purges all the free blocks in the buffer, and clears all of |
146 | * the locked memory. Memory is not freed back to the system. |
147 | */ |
148 | |
149 | extern void l_purge(lmem */*lm*/); |
150 | |
37038e63 |
151 | /* --- @l_destroy@ --- * |
152 | * |
153 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
154 | * |
155 | * Returns: --- |
156 | * |
157 | * Use: Disposes of a locked memory arena permanently. |
158 | */ |
159 | |
160 | extern void l_destroy(lmem */*lm*/); |
161 | |
359df778 |
162 | /* --- @l_report@ --- * |
163 | * |
164 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
165 | * @dstr *d@ = string to write the error message on |
166 | * |
167 | * Returns: Zero if the buffer is fine, @+1@ if there was a problem |
168 | * getting locked memory but insecure stuff could be allocated, |
169 | * and @-1@ if not even insecure memory could be found. |
170 | * |
171 | * Use: Returns a user-digestable explanation for the state of a |
172 | * locked memory buffer. If the return code is zero, no message |
173 | * is emitted to the string @d@. |
174 | */ |
175 | |
176 | extern int l_report(lmem */*lm*/, dstr */*d*/); |
177 | |
178 | /*----- That's all, folks -------------------------------------------------*/ |
179 | |
180 | #ifdef __cplusplus |
181 | } |
182 | #endif |
183 | |
184 | #endif |