| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/TADS2/msdos/oswinmem.c,v 1.1 1999/05/29 15:51:03 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | oswinmem.c - windows memory management functions |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 05/25/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <memory.h> |
| 25 | #include <assert.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdarg.h> |
| 28 | |
| 29 | #include "os.h" |
| 30 | |
| 31 | /* ------------------------------------------------------------------------ */ |
| 32 | /* |
| 33 | * Windows memory management functions. Our memory manager keeps track |
| 34 | * of all allocations made with osmalloc(); this allows us to free all |
| 35 | * such allocations, so that we can start over with all memory freed. |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * memory block prefix - each block we allocate has this prefix attached |
| 40 | * just before the pointer that we return to the program |
| 41 | */ |
| 42 | typedef struct mem_prefix_t |
| 43 | { |
| 44 | long id; |
| 45 | size_t siz; |
| 46 | struct mem_prefix_t *nxt; |
| 47 | struct mem_prefix_t *prv; |
| 48 | } mem_prefix_t; |
| 49 | |
| 50 | /* head and tail of memory allocation linked list */ |
| 51 | static mem_prefix_t *mem_head = 0; |
| 52 | static mem_prefix_t *mem_tail = 0; |
| 53 | |
| 54 | /* head and tail of static memory allocation list */ |
| 55 | static mem_prefix_t *static_mem_head = 0, *static_mem_tail = 0; |
| 56 | |
| 57 | /* |
| 58 | * Allocate a block, storing it in a doubly-linked list of blocks and |
| 59 | * giving the block a unique ID. |
| 60 | */ |
| 61 | void *oss_win_malloc(size_t siz) |
| 62 | { |
| 63 | static long id; |
| 64 | |
| 65 | mem_prefix_t *mem = (mem_prefix_t *)malloc(siz + sizeof(mem_prefix_t)); |
| 66 | if (mem == 0) |
| 67 | return 0; |
| 68 | |
| 69 | mem->id = id++; |
| 70 | mem->siz = siz; |
| 71 | mem->prv = mem_tail; |
| 72 | mem->nxt = 0; |
| 73 | if (mem_tail) |
| 74 | mem_tail->nxt = mem; |
| 75 | else |
| 76 | mem_head = mem; |
| 77 | mem_tail = mem; |
| 78 | |
| 79 | return (void *)(mem + 1); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * reallocate a block - to simplify, we'll allocate a new block, copy |
| 84 | * the old block up to the smaller of the two block sizes, and delete |
| 85 | * the old block |
| 86 | */ |
| 87 | void *oss_win_realloc(void *oldptr, size_t newsiz) |
| 88 | { |
| 89 | void *newptr; |
| 90 | size_t oldsiz; |
| 91 | |
| 92 | /* allocate a new block */ |
| 93 | newptr = oss_win_malloc(newsiz); |
| 94 | |
| 95 | /* copy the old block into the new block */ |
| 96 | oldsiz = (((mem_prefix_t *)oldptr) - 1)->siz; |
| 97 | memcpy(newptr, oldptr, (oldsiz <= newsiz ? oldsiz : newsiz)); |
| 98 | |
| 99 | /* free the old block */ |
| 100 | oss_win_free(oldptr); |
| 101 | |
| 102 | /* return the new block */ |
| 103 | return newptr; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* free a block, removing it from the allocation block list */ |
| 108 | void oss_win_free(void *ptr) |
| 109 | { |
| 110 | static int check = 0; |
| 111 | mem_prefix_t *mem = ((mem_prefix_t *)ptr) - 1; |
| 112 | |
| 113 | if (check) |
| 114 | { |
| 115 | mem_prefix_t *p; |
| 116 | for (p = mem_head ; p ; p = p->nxt) |
| 117 | { |
| 118 | if (p == mem) |
| 119 | break; |
| 120 | } |
| 121 | assert(p != 0); |
| 122 | } |
| 123 | |
| 124 | if (mem->prv) |
| 125 | mem->prv->nxt = mem->nxt; |
| 126 | else |
| 127 | mem_head = mem->nxt; |
| 128 | |
| 129 | if (mem->nxt) |
| 130 | mem->nxt->prv = mem->prv; |
| 131 | else |
| 132 | mem_tail = mem->prv; |
| 133 | |
| 134 | free((void *)mem); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Notify the oss_win layer that we're past static initializations. The |
| 139 | * main entrypoint (main or WinMain, according to the build type) should |
| 140 | * call this immediately (or nearly immediately) after entry. This takes |
| 141 | * any memory blocks allocated before this point, and detaches them from |
| 142 | * the master deletion list, so that they won't be deleted on a future |
| 143 | * oss_win_free_all() call. Anything allocated before the main program |
| 144 | * entrypoint must have been allocated by a C++ static object constructor, |
| 145 | * so it must not be counted among dynamically allocated program objects, |
| 146 | * even though it was malloc'd or new'd. |
| 147 | * |
| 148 | * This is important for HTML TADS, Workbench, and any other configuration |
| 149 | * that cycles in and out of multiple invocations of the interpreter. HTML |
| 150 | * TADS and Workbench use oss_win_free_all() to clear out all memory |
| 151 | * allocated by the interpreter after the interpreter exits. This ensures |
| 152 | * that we completely clean up after each interpreter invocation, freeing |
| 153 | * all memory allocated within the interpreter, even if the interpreter |
| 154 | * leaves blocks unfreed. The complication is that TADS 3, being a C++ |
| 155 | * program, allocates memory with malloc() and 'new' statically, from |
| 156 | * constructors for static objects. These static allocations must not be |
| 157 | * freed between interpreter invocations, because static objects have |
| 158 | * process lifetime - we have no way to reinitialize them when we re-invoke |
| 159 | * the interpreter. This routine solves the problem by explicitly |
| 160 | * segregating any statically allocated objects from the recycle list. |
| 161 | * Anything that was allocated before 'main' has to have come form a static |
| 162 | * object constructor, so we can take those objects and move them to a |
| 163 | * separate list. |
| 164 | */ |
| 165 | void oss_win_static_init_done() |
| 166 | { |
| 167 | /* if there's anything in the memory list, move it to the static list */ |
| 168 | if (mem_tail != 0) |
| 169 | { |
| 170 | /* append the memory list to the static list */ |
| 171 | mem_head->prv = static_mem_tail; |
| 172 | if (static_mem_tail != 0) |
| 173 | static_mem_tail->nxt = mem_head; |
| 174 | else |
| 175 | static_mem_head = mem_head; |
| 176 | static_mem_tail = mem_tail; |
| 177 | |
| 178 | /* the dynamic memory list is now empty */ |
| 179 | mem_head = mem_tail = 0; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Free all memory blocks |
| 185 | */ |
| 186 | void oss_win_free_all() |
| 187 | { |
| 188 | /* free all blocks in our list */ |
| 189 | while (mem_head != 0) |
| 190 | { |
| 191 | mem_prefix_t *nxt; |
| 192 | |
| 193 | /* remember the next block for after when we free this block */ |
| 194 | nxt = mem_head->nxt; |
| 195 | |
| 196 | /* free this block */ |
| 197 | free((void *)mem_head); |
| 198 | |
| 199 | /* move on to the next block */ |
| 200 | mem_head = nxt; |
| 201 | } |
| 202 | |
| 203 | /* there's no tail any more */ |
| 204 | mem_tail = 0; |
| 205 | } |
| 206 | |
| 207 | /* ------------------------------------------------------------------------ */ |
| 208 | /* |
| 209 | * sprintf equivalents with buffer allocation |
| 210 | */ |
| 211 | int os_asprintf(char **bufptr, const char *fmt, ...) |
| 212 | { |
| 213 | va_list argp; |
| 214 | int ret; |
| 215 | |
| 216 | va_start(argp, fmt); |
| 217 | ret = os_vasprintf(bufptr, fmt, argp); |
| 218 | va_end(argp); |
| 219 | |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | int os_vasprintf(char **bufptr, const char *fmt, va_list argp) |
| 224 | { |
| 225 | /* count the length of the result */ |
| 226 | int len = _vscprintf(fmt, argp); |
| 227 | |
| 228 | /* allocate a buffer */ |
| 229 | if ((*bufptr = (char *)osmalloc(len + 1)) == 0) |
| 230 | return -1; |
| 231 | |
| 232 | /* format the result */ |
| 233 | return _vsnprintf(*bufptr, len + 1, fmt, argp); |
| 234 | } |