3 * $Id: lmem.h,v 1.4 2000/12/06 20:33:27 mdw Exp $
5 * Locked memory allocation
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2000/12/06 20:33:27 mdw
34 * Make flags be macros rather than enumerations, to ensure that they're
37 * Revision 1.3 2000/07/29 21:58:15 mdw
38 * (l_destroy): New function for destroying locked memory blocks.
40 * Revision 1.2 2000/06/17 11:29:38 mdw
43 * Revision 1.1 1999/12/22 16:02:52 mdw
44 * Interface to allocating `locked' memory (which isn't paged out).
48 #ifndef CATACOMB_LMEM_H
49 #define CATACOMB_LMEM_H
55 /*----- Header files ------------------------------------------------------*/
59 #include <mLib/arena.h>
60 #include <mLib/dstr.h>
62 /*----- Data structures ---------------------------------------------------*/
64 /* --- Block list --- *
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
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 */
81 /* --- Locked memory buffer state --- */
84 arena a
; /* Arena header block */
85 unsigned f
; /* Various flags */
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 */
95 /*----- Functions provided ------------------------------------------------*/
99 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
100 * @size_t sz@ = size of locked memory area requested
102 * Returns: Zero if everything is fine, @+1@ if some insecure memory was
103 * allocated, and @-1@ if everything went horribly wrong.
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.
109 * You must call @sub_init@ before allocating locked memory
113 extern int l_init(lmem */
*lm*/
, size_t /*sz*/);
115 /* --- @l_alloc@ --- *
117 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
118 * @size_t sz@ = size requested
120 * Returns: Pointer to allocated memory.
122 * Use: Allocates @sz@ bytes of locked memory.
125 extern void *l_alloc(lmem */
*lm*/
, size_t /*sz*/);
127 /* --- @l_free@ --- *
129 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
130 * @void *p@ = pointer to block
134 * Use: Releases a block of locked memory.
137 extern void l_free(lmem */
*lm*/
, void */
*p*/
);
139 /* --- @l_purge@ --- *
141 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
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.
149 extern void l_purge(lmem */
*lm*/
);
151 /* --- @l_destroy@ --- *
153 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
157 * Use: Disposes of a locked memory arena permanently.
160 extern void l_destroy(lmem */
*lm*/
);
162 /* --- @l_report@ --- *
164 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
165 * @dstr *d@ = string to write the error message on
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.
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@.
176 extern int l_report(lmem */
*lm*/
, dstr */
*d*/
);
178 /*----- That's all, folks -------------------------------------------------*/