~ubuntu-branches/ubuntu/wily/gargoyle-free/wily-proposed

« back to all changes in this revision

Viewing changes to tads/tads2/msdos/oswinmem.c

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Beucler
  • Date: 2009-09-11 20:09:43 UTC
  • Revision ID: james.westby@ubuntu.com-20090911200943-idgzoyupq6650zpn
Tags: upstream-2009-08-25
ImportĀ upstreamĀ versionĀ 2009-08-25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
27
#include "os.h"
 
28
 
 
29
/* ------------------------------------------------------------------------ */
 
30
/*
 
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.  
 
34
 */
 
35
 
 
36
/*
 
37
 *   memory block prefix - each block we allocate has this prefix attached
 
38
 *   just before the pointer that we return to the program 
 
39
 */
 
40
typedef struct mem_prefix_t
 
41
{
 
42
    long id;
 
43
    size_t siz;
 
44
    struct mem_prefix_t *nxt;
 
45
    struct mem_prefix_t *prv;
 
46
} mem_prefix_t;
 
47
 
 
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;
 
51
 
 
52
/*
 
53
 *   Allocate a block, storing it in a doubly-linked list of blocks and
 
54
 *   giving the block a unique ID.  
 
55
 */
 
56
void *oss_win_malloc(size_t siz)
 
57
{
 
58
    static long id;
 
59
 
 
60
    mem_prefix_t *mem = (mem_prefix_t *)malloc(siz + sizeof(mem_prefix_t));
 
61
    if (mem == 0)
 
62
        return 0;
 
63
    
 
64
    mem->id = id++;
 
65
    mem->siz = siz;
 
66
    mem->prv = mem_tail;
 
67
    mem->nxt = 0;
 
68
    if (mem_tail)
 
69
        mem_tail->nxt = mem;
 
70
    else
 
71
        mem_head = mem;
 
72
    mem_tail = mem;
 
73
 
 
74
    return (void *)(mem + 1);
 
75
}
 
76
 
 
77
/*
 
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
 
80
 *   the old block 
 
81
 */
 
82
void *oss_win_realloc(void *oldptr, size_t newsiz)
 
83
{
 
84
    void *newptr;
 
85
    size_t oldsiz;
 
86
 
 
87
    /* allocate a new block */
 
88
    newptr = oss_win_malloc(newsiz);
 
89
 
 
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));
 
93
 
 
94
    /* free the old block */
 
95
    oss_win_free(oldptr);
 
96
 
 
97
    /* return the new block */
 
98
    return newptr;
 
99
}
 
100
 
 
101
 
 
102
/* free a block, removing it from the allocation block list */
 
103
void oss_win_free(void *ptr)
 
104
{
 
105
    static int check = 0;
 
106
    mem_prefix_t *mem = ((mem_prefix_t *)ptr) - 1;
 
107
 
 
108
    if (check)
 
109
    {
 
110
        mem_prefix_t *p;
 
111
        for (p = mem_head ; p ; p = p->nxt)
 
112
        {
 
113
            if (p == mem)
 
114
                break;
 
115
        }
 
116
        assert(p != 0);
 
117
    }
 
118
 
 
119
    if (mem->prv)
 
120
        mem->prv->nxt = mem->nxt;
 
121
    else
 
122
        mem_head = mem->nxt;
 
123
 
 
124
    if (mem->nxt)
 
125
        mem->nxt->prv = mem->prv;
 
126
    else
 
127
        mem_tail = mem->prv;
 
128
 
 
129
    free((void *)mem);
 
130
}
 
131
 
 
132
/*
 
133
 *   Free all memory blocks 
 
134
 */
 
135
void oss_win_free_all()
 
136
{
 
137
    /* free all blocks in our list */
 
138
    while (mem_head != 0)
 
139
    {
 
140
        mem_prefix_t *nxt;
 
141
 
 
142
        /* remember the next block for after when we free this block */
 
143
        nxt = mem_head->nxt;
 
144
 
 
145
        /* free this block */
 
146
        free((void *)mem_head);
 
147
 
 
148
        /* move on to the next block */
 
149
        mem_head = nxt;
 
150
    }
 
151
 
 
152
    /* there's no tail any more */
 
153
    mem_tail = 0;
 
154
}
 
155