| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: lmem.h,v 1.5 2004/04/08 01:36:15 mdw Exp $ |
| 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 | #ifndef CATACOMB_LMEM_H |
| 31 | #define CATACOMB_LMEM_H |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | #include <stddef.h> |
| 40 | |
| 41 | #include <mLib/arena.h> |
| 42 | #include <mLib/dstr.h> |
| 43 | |
| 44 | /*----- Data structures ---------------------------------------------------*/ |
| 45 | |
| 46 | /* --- Block list --- * |
| 47 | * |
| 48 | * The block list is kept in normal memory, to avoid wasting precious locked |
| 49 | * memory. Entries are sorted into ascending address order to make |
| 50 | * coalescing free blocks easier. All blocks, free or not, are included in |
| 51 | * the list. |
| 52 | */ |
| 53 | |
| 54 | typedef struct l_node { |
| 55 | struct l_node *next; /* Next free block in chain */ |
| 56 | char *p; /* Pointer to the block */ |
| 57 | size_t sz; /* Size of the block */ |
| 58 | unsigned f; /* Various flags */ |
| 59 | } l_node; |
| 60 | |
| 61 | #define LF_ALLOC 1u |
| 62 | |
| 63 | /* --- Locked memory buffer state --- */ |
| 64 | |
| 65 | typedef struct lmem { |
| 66 | arena a; /* Arena header block */ |
| 67 | unsigned f; /* Various flags */ |
| 68 | char *p; /* Pointer to locked buffer */ |
| 69 | l_node *l; /* Pointer to block list */ |
| 70 | size_t sz; /* Size of locked buffer */ |
| 71 | size_t free; /* Size of free area */ |
| 72 | int err; char *emsg; /* Error indicators */ |
| 73 | } lmem; |
| 74 | |
| 75 | #define LF_LOCKED 1u |
| 76 | |
| 77 | /*----- Functions provided ------------------------------------------------*/ |
| 78 | |
| 79 | /* --- @l_init@ --- * |
| 80 | * |
| 81 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
| 82 | * @size_t sz@ = size of locked memory area requested |
| 83 | * |
| 84 | * Returns: Zero if everything is fine, @+1@ if some insecure memory was |
| 85 | * allocated, and @-1@ if everything went horribly wrong. |
| 86 | * |
| 87 | * Use: Initializes the locked memory manager. This function is safe |
| 88 | * to call in a privileged program; privileges should usually be |
| 89 | * dropped after allocating the locked memory block. |
| 90 | * |
| 91 | * You must call @sub_init@ before allocating locked memory |
| 92 | * buffers. |
| 93 | */ |
| 94 | |
| 95 | extern int l_init(lmem */*lm*/, size_t /*sz*/); |
| 96 | |
| 97 | /* --- @l_alloc@ --- * |
| 98 | * |
| 99 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
| 100 | * @size_t sz@ = size requested |
| 101 | * |
| 102 | * Returns: Pointer to allocated memory. |
| 103 | * |
| 104 | * Use: Allocates @sz@ bytes of locked memory. |
| 105 | */ |
| 106 | |
| 107 | extern void *l_alloc(lmem */*lm*/, size_t /*sz*/); |
| 108 | |
| 109 | /* --- @l_free@ --- * |
| 110 | * |
| 111 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
| 112 | * @void *p@ = pointer to block |
| 113 | * |
| 114 | * Returns: --- |
| 115 | * |
| 116 | * Use: Releases a block of locked memory. |
| 117 | */ |
| 118 | |
| 119 | extern void l_free(lmem */*lm*/, void */*p*/); |
| 120 | |
| 121 | /* --- @l_purge@ --- * |
| 122 | * |
| 123 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
| 124 | * |
| 125 | * Returns: --- |
| 126 | * |
| 127 | * Use: Purges all the free blocks in the buffer, and clears all of |
| 128 | * the locked memory. Memory is not freed back to the system. |
| 129 | */ |
| 130 | |
| 131 | extern void l_purge(lmem */*lm*/); |
| 132 | |
| 133 | /* --- @l_destroy@ --- * |
| 134 | * |
| 135 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
| 136 | * |
| 137 | * Returns: --- |
| 138 | * |
| 139 | * Use: Disposes of a locked memory arena permanently. |
| 140 | */ |
| 141 | |
| 142 | extern void l_destroy(lmem */*lm*/); |
| 143 | |
| 144 | /* --- @l_report@ --- * |
| 145 | * |
| 146 | * Arguments: @lmem *lm@ = pointer to locked memory descriptor |
| 147 | * @dstr *d@ = string to write the error message on |
| 148 | * |
| 149 | * Returns: Zero if the buffer is fine, @+1@ if there was a problem |
| 150 | * getting locked memory but insecure stuff could be allocated, |
| 151 | * and @-1@ if not even insecure memory could be found. |
| 152 | * |
| 153 | * Use: Returns a user-digestable explanation for the state of a |
| 154 | * locked memory buffer. If the return code is zero, no message |
| 155 | * is emitted to the string @d@. |
| 156 | */ |
| 157 | |
| 158 | extern int l_report(lmem */*lm*/, dstr */*d*/); |
| 159 | |
| 160 | /*----- That's all, folks -------------------------------------------------*/ |
| 161 | |
| 162 | #ifdef __cplusplus |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | #endif |