7 /* ----------------------------------------------------------------------
8 * String handling routines.
14 char *p = smalloc(len + 1);
19 /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
20 char *dupcat(char *s1, ...)
29 sn = va_arg(ap, char *);
42 sn = va_arg(ap, char *);
53 /* ----------------------------------------------------------------------
54 * Generic routines to deal with send buffers: a linked list of
55 * smallish blocks, with the operations
57 * - add an arbitrary amount of data to the end of the list
58 * - remove the first N bytes from the list
59 * - return a (pointer,length) pair giving some initial data in
60 * the list, suitable for passing to a send or write system
62 * - return the current size of the buffer chain in bytes
65 #define BUFFER_GRANULE 512
67 struct bufchain_granule {
68 struct bufchain_granule *next;
70 char buf[BUFFER_GRANULE];
73 void bufchain_init(bufchain *ch)
75 ch->head = ch->tail = NULL;
79 void bufchain_clear(bufchain *ch)
81 struct bufchain_granule *b;
84 ch->head = ch->head->next;
91 int bufchain_size(bufchain *ch)
93 return ch->buffersize;
96 void bufchain_add(bufchain *ch, void *data, int len)
98 char *buf = (char *)data;
100 ch->buffersize += len;
102 if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) {
103 int copylen = min(len, BUFFER_GRANULE - ch->tail->buflen);
104 memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen);
107 ch->tail->buflen += copylen;
110 int grainlen = min(len, BUFFER_GRANULE);
111 struct bufchain_granule *newbuf;
112 newbuf = smalloc(sizeof(struct bufchain_granule));
114 newbuf->buflen = grainlen;
115 memcpy(newbuf->buf, buf, grainlen);
119 ch->tail->next = newbuf;
121 ch->head = ch->tail = newbuf;
127 void bufchain_consume(bufchain *ch, int len)
129 assert(ch->buffersize >= len);
130 assert(ch->head != NULL && ch->head->bufpos + len <= ch->head->buflen);
131 ch->head->bufpos += len;
132 ch->buffersize -= len;
133 if (ch->head->bufpos >= ch->head->buflen) {
134 struct bufchain_granule *tmp = ch->head;
135 ch->head = tmp->next;
142 void bufchain_prefix(bufchain *ch, void **data, int *len)
144 *len = ch->head->buflen - ch->head->bufpos;
145 *data = ch->head->buf + ch->head->bufpos;
148 /* ----------------------------------------------------------------------
149 * My own versions of malloc, realloc and free. Because I want
150 * malloc and realloc to bomb out and exit the program if they run
151 * out of memory, realloc to reliably call malloc if passed a NULL
152 * pointer, and free to reliably do nothing if passed a NULL
153 * pointer. We can also put trace printouts in, if we need to; and
154 * we can also replace the allocator with an ElectricFence-like
160 * Minefield - a Windows equivalent for Electric Fence
163 #define PAGESIZE 4096
168 * We start by reserving as much virtual address space as Windows
169 * will sensibly (or not sensibly) let us have. We flag it all as
172 * Any allocation attempt is satisfied by committing one or more
173 * pages, with an uncommitted page on either side. The returned
174 * memory region is jammed up against the _end_ of the pages.
176 * Freeing anything causes instantaneous decommitment of the pages
177 * involved, so stale pointers are caught as soon as possible.
180 static int minefield_initialised = 0;
181 static void *minefield_region = NULL;
182 static long minefield_size = 0;
183 static long minefield_npages = 0;
184 static long minefield_curpos = 0;
185 static unsigned short *minefield_admin = NULL;
186 static void *minefield_pages = NULL;
188 static void minefield_admin_hide(int hide)
190 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
191 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
194 static void minefield_init(void)
200 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
201 minefield_region = VirtualAlloc(NULL, size,
202 MEM_RESERVE, PAGE_NOACCESS);
203 if (minefield_region)
206 minefield_size = size;
209 * Firstly, allocate a section of that to be the admin block.
210 * We'll need a two-byte field for each page.
212 minefield_admin = minefield_region;
213 minefield_npages = minefield_size / PAGESIZE;
214 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
215 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
216 minefield_pages = (char *) minefield_region + admin_size;
219 * Commit the admin region.
221 VirtualAlloc(minefield_admin, minefield_npages * 2,
222 MEM_COMMIT, PAGE_READWRITE);
225 * Mark all pages as unused (0xFFFF).
227 for (i = 0; i < minefield_npages; i++)
228 minefield_admin[i] = 0xFFFF;
231 * Hide the admin region.
233 minefield_admin_hide(1);
235 minefield_initialised = 1;
238 static void minefield_bomb(void)
240 div(1, *(int *) minefield_pages);
243 static void *minefield_alloc(int size)
246 int pos, lim, region_end, region_start;
250 npages = (size + PAGESIZE - 1) / PAGESIZE;
252 minefield_admin_hide(0);
255 * Search from current position until we find a contiguous
256 * bunch of npages+2 unused pages.
258 pos = minefield_curpos;
259 lim = minefield_npages;
261 /* Skip over used pages. */
262 while (pos < lim && minefield_admin[pos] != 0xFFFF)
264 /* Count unused pages. */
266 while (pos < lim && pos - start < npages + 2 &&
267 minefield_admin[pos] == 0xFFFF)
269 if (pos - start == npages + 2)
271 /* If we've reached the limit, reset the limit or stop. */
273 if (lim == minefield_npages) {
274 /* go round and start again at zero */
275 lim = minefield_curpos;
278 minefield_admin_hide(1);
284 minefield_curpos = pos - 1;
287 * We have npages+2 unused pages starting at start. We leave
288 * the first and last of these alone and use the rest.
290 region_end = (start + npages + 1) * PAGESIZE;
291 region_start = region_end - size;
292 /* FIXME: could align here if we wanted */
295 * Update the admin region.
297 for (i = start + 2; i < start + npages - 1; i++)
298 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
299 minefield_admin[start + 1] = region_start % PAGESIZE;
301 minefield_admin_hide(1);
303 VirtualAlloc((char *) minefield_pages + region_start, size,
304 MEM_COMMIT, PAGE_READWRITE);
305 return (char *) minefield_pages + region_start;
308 static void minefield_free(void *ptr)
310 int region_start, i, j;
312 minefield_admin_hide(0);
314 region_start = (char *) ptr - (char *) minefield_pages;
315 i = region_start / PAGESIZE;
316 if (i < 0 || i >= minefield_npages ||
317 minefield_admin[i] != region_start % PAGESIZE)
319 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
320 minefield_admin[j] = 0xFFFF;
323 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
325 minefield_admin_hide(1);
328 static int minefield_get_size(void *ptr)
330 int region_start, i, j;
332 minefield_admin_hide(0);
334 region_start = (char *) ptr - (char *) minefield_pages;
335 i = region_start / PAGESIZE;
336 if (i < 0 || i >= minefield_npages ||
337 minefield_admin[i] != region_start % PAGESIZE)
339 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
341 minefield_admin_hide(1);
343 return j * PAGESIZE - region_start;
346 static void *minefield_c_malloc(size_t size)
348 if (!minefield_initialised)
350 return minefield_alloc(size);
353 static void minefield_c_free(void *p)
355 if (!minefield_initialised)
361 * realloc _always_ moves the chunk, for rapid detection of code
362 * that assumes it won't.
364 static void *minefield_c_realloc(void *p, size_t size)
368 if (!minefield_initialised)
370 q = minefield_alloc(size);
371 oldsize = minefield_get_size(p);
372 memcpy(q, p, (oldsize < size ? oldsize : size));
377 #endif /* MINEFIELD */
380 static FILE *fp = NULL;
382 static char *mlog_file = NULL;
383 static int mlog_line = 0;
385 void mlog(char *file, int line)
390 fp = fopen("putty_mem.log", "w");
391 setvbuf(fp, NULL, _IONBF, BUFSIZ);
394 fprintf(fp, "%s:%d: ", file, line);
398 void *safemalloc(size_t size)
402 p = minefield_c_malloc(size);
409 sprintf(str, "Out of memory! (%s:%d, size=%d)",
410 mlog_file, mlog_line, size);
411 fprintf(fp, "*** %s\n", str);
414 strcpy(str, "Out of memory!");
416 MessageBox(NULL, str, "PuTTY Fatal Error",
417 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
422 fprintf(fp, "malloc(%d) returns %p\n", size, p);
427 void *saferealloc(void *ptr, size_t size)
432 p = minefield_c_malloc(size);
438 p = minefield_c_realloc(ptr, size);
440 p = realloc(ptr, size);
446 sprintf(str, "Out of memory! (%s:%d, size=%d)",
447 mlog_file, mlog_line, size);
448 fprintf(fp, "*** %s\n", str);
451 strcpy(str, "Out of memory!");
453 MessageBox(NULL, str, "PuTTY Fatal Error",
454 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
459 fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
464 void safefree(void *ptr)
469 fprintf(fp, "free(%p)\n", ptr);
472 minefield_c_free(ptr);
479 fprintf(fp, "freeing null pointer - no action taken\n");
483 /* ----------------------------------------------------------------------
484 * Debugging routines.
488 static FILE *debug_fp = NULL;
489 static int debug_got_console = 0;
491 static void dputs(char *buf)
495 if (!debug_got_console) {
497 debug_got_console = 1;
500 debug_fp = fopen("debug.log", "w");
503 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw,
505 fputs(buf, debug_fp);
510 void dprintf(char *fmt, ...)
516 vsprintf(buf, fmt, ap);
522 void debug_memdump(void *buf, int len, int L)
525 unsigned char *p = buf;
529 dprintf("\t%d (0x%x) bytes:\n", len, len);
530 delta = 15 & (int) p;
534 for (; 0 < len; p += 16, len -= 16) {
538 strcpy(foo, "................"); /* sixteen dots */
539 for (i = 0; i < 16 && i < len; ++i) {
540 if (&p[i] < (unsigned char *) buf) {
541 dputs(" "); /* 3 spaces */
545 &p[i] != (unsigned char *) buf
546 && i % 4 ? '.' : ' ', p[i]
548 if (p[i] >= ' ' && p[i] <= '~')
549 foo[i] = (char) p[i];
553 dprintf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
557 #endif /* def DEBUG */