359df778 |
1 | /* -*-c-*- |
2 | * |
16efd15b |
3 | * $Id: lmem.h,v 1.4 2000/12/06 20:33:27 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 $ |
16efd15b |
33 | * Revision 1.4 2000/12/06 20:33:27 mdw |
34 | * Make flags be macros rather than enumerations, to ensure that they're |
35 | * unsigned. |
36 | * |
37038e63 |
37 | * Revision 1.3 2000/07/29 21:58:15 mdw |
38 | * (l_destroy): New function for destroying locked memory blocks. |
39 | * |
47d5f7c2 |
40 | * Revision 1.2 2000/06/17 11:29:38 mdw |
41 | * Add arena support. |
42 | * |
359df778 |
43 | * Revision 1.1 1999/12/22 16:02:52 mdw |
44 | * Interface to allocating `locked' memory (which isn't paged out). |
45 | * |
46 | */ |
47 | |
48 | #ifndef CATACOMB_LMEM_H |
49 | #define CATACOMB_LMEM_H |
50 | |
51 | #ifdef __cplusplus |
52 | extern "C" { |
53 | #endif |
54 | |
55 | /*----- Header files ------------------------------------------------------*/ |
56 | |
57 | #include <stddef.h> |
58 | |
47d5f7c2 |
59 | #include <mLib/arena.h> |
359df778 |
60 | #include <mLib/dstr.h> |
61 | |
62 | /*----- Data structures ---------------------------------------------------*/ |
63 | |
64 | /* --- Block list --- * |
65 | * |
66 | * The block list is kept in normal memory, to avoid wasting precious locked |
67 | * memory. Entries are sorted into ascending address order to make |
68 | * coalescing free blocks easier. All blocks, free or not, are included in |
69 | * the list. |
70 | */ |
71 | |
72 | typedef struct l_node { |
73 | struct l_node *next; /* Next free block in chain */ |
74 | char *p; /* Pointer to the block */ |
75 | size_t sz; /* Size of the block */ |
76 | unsigned f; /* Various flags */ |
77 | } l_node; |
78 | |
16efd15b |
79 | #define LF_ALLOC 1u |
359df778 |
80 | |
81 | /* --- Locked memory buffer state --- */ |
82 | |
83 | typedef struct lmem { |
47d5f7c2 |
84 | arena a; /* Arena header block */ |
37038e63 |
85 | unsigned f; /* Various flags */ |
359df778 |
86 | char *p; /* Pointer to locked buffer */ |
87 | l_node *l; /* Pointer to block list */ |
88 | size_t sz; /* Size of locked buffer */ |
89 | size_t free; /* Size of free area */ |
90 | int err; char *emsg; /* Error indicators */ |
91 | } lmem; |
92 | |
16efd15b |
93 | #define LF_LOCKED 1u |
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 |