3
"$Header: d:/cvsroot/tads/TADS2/msdos/oswinmem.c,v 1.1 1999/05/29 15:51:03 MJRoberts Exp $";
7
* Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
9
* Please see the accompanying license file, LICENSE.TXT, for information
10
* on using and copying this software.
14
oswinmem.c - windows memory management functions
20
05/25/99 MJRoberts - Creation
29
/* ------------------------------------------------------------------------ */
31
* Windows memory management functions. Our memory manager keeps track
32
* of all allocations made with osmalloc(); this allows us to free all
33
* such allocations, so that we can start over with all memory freed.
37
* memory block prefix - each block we allocate has this prefix attached
38
* just before the pointer that we return to the program
40
typedef struct mem_prefix_t
44
struct mem_prefix_t *nxt;
45
struct mem_prefix_t *prv;
48
/* head and tail of memory allocation linked list */
49
static mem_prefix_t *mem_head = 0;
50
static mem_prefix_t *mem_tail = 0;
53
* Allocate a block, storing it in a doubly-linked list of blocks and
54
* giving the block a unique ID.
56
void *oss_win_malloc(size_t siz)
60
mem_prefix_t *mem = (mem_prefix_t *)malloc(siz + sizeof(mem_prefix_t));
74
return (void *)(mem + 1);
78
* reallocate a block - to simplify, we'll allocate a new block, copy
79
* the old block up to the smaller of the two block sizes, and delete
82
void *oss_win_realloc(void *oldptr, size_t newsiz)
87
/* allocate a new block */
88
newptr = oss_win_malloc(newsiz);
90
/* copy the old block into the new block */
91
oldsiz = (((mem_prefix_t *)oldptr) - 1)->siz;
92
memcpy(newptr, oldptr, (oldsiz <= newsiz ? oldsiz : newsiz));
94
/* free the old block */
97
/* return the new block */
102
/* free a block, removing it from the allocation block list */
103
void oss_win_free(void *ptr)
105
static int check = 0;
106
mem_prefix_t *mem = ((mem_prefix_t *)ptr) - 1;
111
for (p = mem_head ; p ; p = p->nxt)
120
mem->prv->nxt = mem->nxt;
125
mem->nxt->prv = mem->prv;
133
* Free all memory blocks
135
void oss_win_free_all()
137
/* free all blocks in our list */
138
while (mem_head != 0)
142
/* remember the next block for after when we free this block */
145
/* free this block */
146
free((void *)mem_head);
148
/* move on to the next block */
152
/* there's no tail any more */