PulkoMandy | 17fc759 | 2022-07-28 18:27:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (c) Thomas Pornin 1998, 1999, 2000 |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * 4. The name of the authors may not be used to endorse or promote |
| 13 | * products derived from this software without specific prior written |
| 14 | * permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 22 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #ifndef UCPP__MEM__ |
| 31 | #define UCPP__MEM__ |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | void die(void); |
| 36 | void *incmem(void *, size_t, size_t); |
| 37 | char *sdup(char *); |
| 38 | |
| 39 | #if defined AUDIT || defined MEM_CHECK |
| 40 | void *getmem(size_t); |
| 41 | #else |
| 42 | #define getmem malloc |
| 43 | #endif |
| 44 | |
| 45 | #ifdef AUDIT |
| 46 | void freemem(void *); |
| 47 | #else |
| 48 | #define freemem free |
| 49 | #endif |
| 50 | |
| 51 | #ifdef AUDIT |
| 52 | void *mmv(void *, void *, size_t); |
| 53 | void *mmvwo(void *, void *, size_t); |
| 54 | #else |
| 55 | #define mmv memcpy |
| 56 | #define mmvwo memmove |
| 57 | #endif |
| 58 | |
| 59 | /* |
| 60 | * this macro adds the object obj at the end of the array list, handling |
| 61 | * memory allocation when needed; ptr contains the number of elements in |
| 62 | * the array, and memg is the granularity of memory allocations (a power |
| 63 | * of 2 is recommanded, for optimization reasons) |
| 64 | * |
| 65 | * list and ptr may be updated, and thus need to be lvalues |
| 66 | */ |
| 67 | #define aol(list,ptr,obj,memg) do { \ |
| 68 | if (((ptr) % (memg)) == 0) { \ |
| 69 | if ((ptr) != 0) { \ |
| 70 | (list) = incmem((list), (ptr) * sizeof(obj), \ |
| 71 | ((ptr) + (memg)) * sizeof(obj)); \ |
| 72 | } else { \ |
| 73 | (list) = getmem((memg) * sizeof(obj)); \ |
| 74 | } \ |
| 75 | } \ |
| 76 | (list)[(ptr) ++] = (obj); \ |
| 77 | } while (0) |
| 78 | |
| 79 | /* |
| 80 | * bol() does the same as aol(), but adds the new item at the beginning |
| 81 | * of the list; beware, the computational cost is greater. |
| 82 | */ |
| 83 | #define bol(list,ptr,obj,memg) do { \ |
| 84 | if (((ptr) % (memg)) == 0) { \ |
| 85 | if ((ptr) != 0) { \ |
| 86 | (list) = incmem((list), (ptr) * sizeof(obj), \ |
| 87 | ((ptr) + (memg)) * sizeof(obj)); \ |
| 88 | } else { \ |
| 89 | (list) = getmem((memg) * sizeof(obj)); \ |
| 90 | } \ |
| 91 | } \ |
| 92 | if ((ptr) != 0) \ |
| 93 | mmvwo((list) + 1, (list), (ptr) * sizeof(obj)); \ |
| 94 | (ptr) ++; \ |
| 95 | (list)[0] = (obj); \ |
| 96 | } while (0) |
| 97 | |
| 98 | /* |
| 99 | * mbol() does the same as bol(), but adds the new item at the given |
| 100 | * emplacement; bol() is equivalent to mbol with 0 as last argument. |
| 101 | */ |
| 102 | #define mbol(list,ptr,obj,memg,n) do { \ |
| 103 | if (((ptr) % (memg)) == 0) { \ |
| 104 | if ((ptr) != 0) { \ |
| 105 | (list) = incmem((list), (ptr) * sizeof(obj), \ |
| 106 | ((ptr) + (memg)) * sizeof(obj)); \ |
| 107 | } else { \ |
| 108 | (list) = getmem((memg) * sizeof(obj)); \ |
| 109 | } \ |
| 110 | } \ |
| 111 | if ((ptr) > n) \ |
| 112 | mmvwo((list) + n + 1, (list) + n, \ |
| 113 | ((ptr) - n) * sizeof(obj)); \ |
| 114 | (ptr) ++; \ |
| 115 | (list)[n] = (obj); \ |
| 116 | } while (0) |
| 117 | |
| 118 | /* |
| 119 | * this macro adds the object obj at the end of the array list, doubling |
| 120 | * the size of list when needed; as for aol(), ptr and list must be |
| 121 | * lvalues, and so must be llng |
| 122 | */ |
| 123 | |
| 124 | #define wan(list,ptr,obj,llng) do { \ |
| 125 | if ((ptr) == (llng)) { \ |
| 126 | (llng) += (llng); \ |
| 127 | (list) = incmem((list), (ptr) * sizeof(obj), \ |
| 128 | (llng) * sizeof(obj)); \ |
| 129 | } \ |
| 130 | (list)[(ptr) ++] = (obj); \ |
| 131 | } while (0) |
| 132 | |
| 133 | #endif |