~ubuntu-branches/ubuntu/karmic/trousers/karmic

« back to all changes in this revision

Viewing changes to src/tspi/memmgr.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-01-23 22:03:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080123220300-fhtqja3c0oq0gp6z
Tags: 0.3.1-4
* Added patch from Aaron M. Ucko <ucko@debian.org> to allow trousers to
  build successfully on amd64, and presumably also other 64-bit
  architectures (Closes: #457400).
* Including udev rule for /dev/tpm from William Lima
  <wlima.amadeus@gmail.com> as suggested by David Smith <dds@google.com>
  (Closes: #459682).
* Added lintian overrides.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/*
3
 
 * Licensed Materials - Property of IBM
4
 
 *
5
 
 * trousers - An open source TCG Software Stack
6
 
 *
7
 
 * (C) Copyright International Business Machines Corp. 2004
8
 
 *
9
 
 */
10
 
 
11
 
 
12
 
#include <stdlib.h>
13
 
#include <stdio.h>
14
 
#include <string.h>
15
 
 
16
 
#include "trousers/tss.h"
17
 
#include "spi_internal_types.h"
18
 
#include "spi_utils.h"
19
 
#include "capabilities.h"
20
 
#include "memmgr.h"
21
 
#include "tsplog.h"
22
 
#include "obj.h"
23
 
 
24
 
/* caller needs to lock memtable lock */
25
 
struct memTable *
26
 
getTable(TSS_HCONTEXT tspContext)
27
 
{
28
 
        struct memTable *tmp;
29
 
 
30
 
        for (tmp = SpiMemoryTable; tmp; tmp = tmp->nextTable)
31
 
                if (tmp->tspContext == tspContext)
32
 
                        return tmp;
33
 
 
34
 
        return NULL;
35
 
}
36
 
 
37
 
/* caller needs to lock memtable lock and be sure the context mem slot for
38
 
 * @tspContext exists before calling.
39
 
 */
40
 
void
41
 
addEntry(TSS_HCONTEXT tspContext, struct memEntry *new)
42
 
{
43
 
        struct memTable *tmp = getTable(tspContext);
44
 
        struct memEntry *tmp_entry = tmp->entries;
45
 
 
46
 
        if (tmp->entries == NULL) {
47
 
                tmp->entries = new;
48
 
                return;
49
 
        }
50
 
 
51
 
        /* else tack @new onto the end */
52
 
        for (; tmp_entry; tmp_entry = tmp_entry->nextEntry) {
53
 
                if (tmp_entry->nextEntry == NULL) {
54
 
                        tmp_entry->nextEntry = new;
55
 
                        break;
56
 
                }
57
 
        }
58
 
}
59
 
 
60
 
/* caller needs to lock memtable lock */
61
 
void
62
 
addTable(struct memTable *new)
63
 
{
64
 
        struct memTable *tmp = SpiMemoryTable;
65
 
 
66
 
        /* base case, this is the first table */
67
 
        if (SpiMemoryTable == NULL) {
68
 
                SpiMemoryTable = new;
69
 
                return;
70
 
        }
71
 
 
72
 
        /* else add @new onto the end */
73
 
        for (; tmp; tmp = tmp->nextTable)
74
 
                if (tmp->nextTable == NULL) {
75
 
                        tmp->nextTable = new;
76
 
                        break;
77
 
                }
78
 
}
79
 
 
80
 
/* caller needs to lock memtable lock */
81
 
TSS_RESULT
82
 
freeTable(TSS_HCONTEXT tspContext)
83
 
{
84
 
        struct memTable *prev = NULL, *index = NULL, *next = NULL;
85
 
        struct memEntry *entry = NULL, *entry_next = NULL;
86
 
 
87
 
        for(index = SpiMemoryTable; index; index = index->nextTable) {
88
 
                next = index->nextTable;
89
 
                if (index->tspContext == tspContext) {
90
 
                        for (entry = index->entries; entry; entry = entry_next) {
91
 
                                /* this needs to be set before we do free(entry) */
92
 
                                entry_next = entry->nextEntry;
93
 
                                free(entry->memPointer);
94
 
                                free(entry);
95
 
                        }
96
 
 
97
 
                        if (prev != NULL)
98
 
                                prev->nextTable = next;
99
 
                        else
100
 
                                SpiMemoryTable = NULL;
101
 
 
102
 
                        free(index);
103
 
                        break;
104
 
                }
105
 
                prev = index;
106
 
        }
107
 
 
108
 
        return TSS_SUCCESS;
109
 
}
110
 
 
111
 
TSS_RESULT
112
 
freeEntry(struct memTable *table, void *pointer)
113
 
{
114
 
        struct memEntry *index = NULL;
115
 
        struct memEntry *prev = NULL;
116
 
        struct memEntry *toKill = NULL;
117
 
 
118
 
        for (index = table->entries; index; prev = index, index = index->nextEntry) {
119
 
                if (index->memPointer == pointer) {
120
 
                        toKill = index;
121
 
                        if (prev == NULL)
122
 
                                table->entries = toKill->nextEntry;
123
 
                        else
124
 
                                prev->nextEntry = toKill->nextEntry;
125
 
 
126
 
                        free(pointer);
127
 
                        free(toKill);
128
 
                        return TSS_SUCCESS;
129
 
 
130
 
                }
131
 
 
132
 
        }
133
 
        LogError("Internal error: pointer to allocated memory not found.");
134
 
        return TSPERR(TSS_E_INTERNAL_ERROR);
135
 
}
136
 
 
137
 
TSS_RESULT
138
 
add_mem_entry(TSS_HCONTEXT tspContext, void *allocd_mem)
139
 
{
140
 
        struct memEntry *newEntry = calloc(1, sizeof(struct memEntry));
141
 
        if (newEntry == NULL) {
142
 
                LogError("malloc of %zd bytes failed.", sizeof(struct memEntry));
143
 
                return TSPERR(TSS_E_OUTOFMEMORY);
144
 
        }
145
 
 
146
 
        newEntry->memPointer = allocd_mem;
147
 
 
148
 
        pthread_mutex_lock(&memtable_lock);
149
 
 
150
 
        addEntry(tspContext, newEntry);
151
 
 
152
 
        pthread_mutex_unlock(&memtable_lock);
153
 
 
154
 
        return TSS_SUCCESS;
155
 
}
156
 
 
157
 
/*
158
 
 * calloc_tspi will be called by functions outside of this file. All locking
159
 
 * is done here.
160
 
 */
161
 
void *
162
 
calloc_tspi(TSS_HCONTEXT tspContext, UINT32 howMuch)
163
 
{
164
 
        struct memTable *table = NULL;
165
 
        struct memEntry *newEntry = NULL;
166
 
 
167
 
        pthread_mutex_lock(&memtable_lock);
168
 
 
169
 
        table = getTable(tspContext);
170
 
        if (table == NULL) {
171
 
                /* no table has yet been created to hold the memory allocations of
172
 
                 * this context, so we need to create one
173
 
                 */
174
 
                table = calloc(1, sizeof(struct memTable));
175
 
                if (table == NULL) {
176
 
                        LogError("malloc of %zd bytes failed.", sizeof(struct memTable));
177
 
                        pthread_mutex_unlock(&memtable_lock);
178
 
                        return NULL;
179
 
                }
180
 
                table->tspContext = tspContext;
181
 
                addTable(table);
182
 
        }
183
 
 
184
 
        newEntry = calloc(1, sizeof(struct memEntry));
185
 
        if (newEntry == NULL) {
186
 
                LogError("malloc of %zd bytes failed.", sizeof(struct memEntry));
187
 
                pthread_mutex_unlock(&memtable_lock);
188
 
                return NULL;
189
 
        }
190
 
 
191
 
        newEntry->memPointer = calloc(1, howMuch);
192
 
        if (newEntry->memPointer == NULL) {
193
 
                LogError("malloc of %d bytes failed.", howMuch);
194
 
                free(newEntry);
195
 
                pthread_mutex_unlock(&memtable_lock);
196
 
                return NULL;
197
 
        }
198
 
 
199
 
        /* this call must happen inside the lock or else another thread could
200
 
         * remove the context mem slot, causing a segfault
201
 
         */
202
 
        addEntry(tspContext, newEntry);
203
 
 
204
 
        pthread_mutex_unlock(&memtable_lock);
205
 
 
206
 
        return newEntry->memPointer;
207
 
}
208
 
 
209
 
/*
210
 
 * free_tspi will be called by functions outside of this file. All locking
211
 
 * is done here.
212
 
 */
213
 
TSS_RESULT
214
 
free_tspi(TSS_HCONTEXT tspContext, void *memPointer)
215
 
{
216
 
        struct memTable *index;
217
 
        TSS_RESULT result;
218
 
 
219
 
        pthread_mutex_lock(&memtable_lock);
220
 
 
221
 
        if (memPointer == NULL) {
222
 
                result = freeTable(tspContext);
223
 
                pthread_mutex_unlock(&memtable_lock);
224
 
                return result;
225
 
        }
226
 
 
227
 
        if ((index = getTable(tspContext)) == NULL) {
228
 
                pthread_mutex_unlock(&memtable_lock);
229
 
                return TSPERR(TSS_E_INVALID_HANDLE);
230
 
        }
231
 
 
232
 
        /* just free one entry */
233
 
        result = freeEntry(index, memPointer);
234
 
 
235
 
        pthread_mutex_unlock(&memtable_lock);
236
 
 
237
 
        return result;
238
 
}