3 * Locked memory allocation
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_LMEM_H
29 #define CATACOMB_LMEM_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/arena.h>
40 #include <mLib/dstr.h>
42 /*----- Data structures ---------------------------------------------------*/
44 /* --- Block list --- *
46 * The block list is kept in normal memory, to avoid wasting precious locked
47 * memory. Entries are sorted into ascending address order to make
48 * coalescing free blocks easier. All blocks, free or not, are included in
52 typedef struct l_node {
53 struct l_node *next; /* Next free block in chain */
54 char *p; /* Pointer to the block */
55 size_t sz; /* Size of the block */
56 unsigned f; /* Various flags */
61 /* --- Locked memory buffer state --- */
64 arena a; /* Arena header block */
65 unsigned f; /* Various flags */
66 char *p; /* Pointer to locked buffer */
67 l_node *l; /* Pointer to block list */
68 size_t sz; /* Size of locked buffer */
69 size_t free; /* Size of free area */
70 int err; char *emsg; /* Error indicators */
75 /*----- Functions provided ------------------------------------------------*/
79 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
80 * @size_t sz@ = size of locked memory area requested
82 * Returns: Zero if everything is fine, @+1@ if some insecure memory was
83 * allocated, and @-1@ if everything went horribly wrong.
85 * Use: Initializes the locked memory manager. This function is safe
86 * to call in a privileged program; privileges should usually be
87 * dropped after allocating the locked memory block.
89 * You must call @sub_init@ before allocating locked memory
93 extern int l_init(lmem */*lm*/, size_t /*sz*/);
95 /* --- @l_alloc@ --- *
97 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
98 * @size_t sz@ = size requested
100 * Returns: Pointer to allocated memory.
102 * Use: Allocates @sz@ bytes of locked memory.
105 extern void *l_alloc(lmem */*lm*/, size_t /*sz*/);
107 /* --- @l_free@ --- *
109 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
110 * @void *p@ = pointer to block
114 * Use: Releases a block of locked memory.
117 extern void l_free(lmem */*lm*/, void */*p*/);
119 /* --- @l_purge@ --- *
121 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
125 * Use: Purges all the free blocks in the buffer, and clears all of
126 * the locked memory. Memory is not freed back to the system.
129 extern void l_purge(lmem */*lm*/);
131 /* --- @l_destroy@ --- *
133 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
137 * Use: Disposes of a locked memory arena permanently.
140 extern void l_destroy(lmem */*lm*/);
142 /* --- @l_report@ --- *
144 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
145 * @dstr *d@ = string to write the error message on
147 * Returns: Zero if the buffer is fine, @+1@ if there was a problem
148 * getting locked memory but insecure stuff could be allocated,
149 * and @-1@ if not even insecure memory could be found.
151 * Use: Returns a user-digestable explanation for the state of a
152 * locked memory buffer. If the return code is zero, no message
153 * is emitted to the string @d@.
156 extern int l_report(lmem */*lm*/, dstr */*d*/);
158 /*----- That's all, folks -------------------------------------------------*/