| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Licensed Materials - Property of IBM | 3 * Licensed Materials - Property of IBM |
| 4 * | 4 * |
| 5 * trousers - An open source TCG Software Stack | 5 * trousers - An open source TCG Software Stack |
| 6 * | 6 * |
| 7 * (C) Copyright International Business Machines Corp. 2004 | 7 * (C) Copyright International Business Machines Corp. 2004 |
| 8 * | 8 * |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 | 11 |
| 12 #include <stdlib.h> | 12 #include <stdlib.h> |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 | 15 |
| 16 #include "trousers/tss.h" | 16 #include "trousers/tss.h" |
| 17 #include "trousers_types.h" | 17 #include "trousers_types.h" |
| 18 #include "spi_utils.h" | 18 #include "spi_utils.h" |
| 19 #include "capabilities.h" | 19 #include "capabilities.h" |
| 20 #include "memmgr.h" | 20 #include "memmgr.h" |
| 21 #include "tsplog.h" | 21 #include "tsplog.h" |
| 22 #include "obj.h" | 22 #include "obj.h" |
| 23 | 23 |
| 24 static struct memTable * |
| 25 __tspi_createTable() |
| 26 { |
| 27 struct memTable *table = NULL; |
| 28 /* |
| 29 * No table has yet been created to hold the memory allocations of |
| 30 * this context, so we need to create one |
| 31 */ |
| 32 table = calloc(1, sizeof(struct memTable)); |
| 33 if (table == NULL) { |
| 34 LogError("malloc of %zd bytes failed.", sizeof(struct memTable))
; |
| 35 return NULL; |
| 36 } |
| 37 return (table); |
| 38 } |
| 39 |
| 24 /* caller needs to lock memtable lock */ | 40 /* caller needs to lock memtable lock */ |
| 25 struct memTable * | 41 struct memTable * |
| 26 getTable(TSS_HCONTEXT tspContext) | 42 getTable(TSS_HCONTEXT tspContext) |
| 27 { | 43 { |
| 28 struct memTable *tmp; | 44 struct memTable *tmp; |
| 29 | 45 |
| 30 for (tmp = SpiMemoryTable; tmp; tmp = tmp->nextTable) | 46 for (tmp = SpiMemoryTable; tmp; tmp = tmp->nextTable) |
| 31 if (tmp->tspContext == tspContext) | 47 if (tmp->tspContext == tspContext) |
| 32 return tmp; | 48 return tmp; |
| 33 | 49 |
| 34 return NULL; | 50 return NULL; |
| 35 } | 51 } |
| 36 | 52 |
| 53 /* caller needs to lock memtable lock */ |
| 54 static void |
| 55 __tspi_addTable(struct memTable *new) |
| 56 { |
| 57 struct memTable *tmp = SpiMemoryTable; |
| 58 |
| 59 /* base case, this is the first table */ |
| 60 if (SpiMemoryTable == NULL) { |
| 61 SpiMemoryTable = new; |
| 62 return; |
| 63 } |
| 64 |
| 65 /* else add @new onto the end */ |
| 66 for (; tmp; tmp = tmp->nextTable) |
| 67 if (tmp->nextTable == NULL) { |
| 68 tmp->nextTable = new; |
| 69 break; |
| 70 } |
| 71 } |
| 72 |
| 37 /* caller needs to lock memtable lock and be sure the context mem slot for | 73 /* caller needs to lock memtable lock and be sure the context mem slot for |
| 38 * @tspContext exists before calling. | 74 * @tspContext exists before calling. |
| 39 */ | 75 */ |
| 40 void | 76 void |
| 41 __tspi_addEntry(TSS_HCONTEXT tspContext, struct memEntry *new) | 77 __tspi_addEntry(TSS_HCONTEXT tspContext, struct memEntry *new) |
| 42 { | 78 { |
| 43 struct memTable *tmp = getTable(tspContext); | 79 struct memTable *tmp = getTable(tspContext); |
| 44 » struct memEntry *tmp_entry = tmp->entries; | 80 » struct memEntry *tmp_entry; |
| 81 |
| 82 » if (tmp == NULL) { |
| 83 » » if ((tmp = __tspi_createTable()) == NULL) |
| 84 » » » return; |
| 85 » » tmp->tspContext = tspContext; |
| 86 » » __tspi_addTable(tmp); |
| 87 » } |
| 88 |
| 89 » tmp_entry = tmp->entries; |
| 45 | 90 |
| 46 if (tmp->entries == NULL) { | 91 if (tmp->entries == NULL) { |
| 47 tmp->entries = new; | 92 tmp->entries = new; |
| 48 return; | 93 return; |
| 49 } | 94 } |
| 50 | 95 |
| 51 /* else tack @new onto the end */ | 96 /* else tack @new onto the end */ |
| 52 for (; tmp_entry; tmp_entry = tmp_entry->nextEntry) { | 97 for (; tmp_entry; tmp_entry = tmp_entry->nextEntry) { |
| 53 if (tmp_entry->nextEntry == NULL) { | 98 if (tmp_entry->nextEntry == NULL) { |
| 54 tmp_entry->nextEntry = new; | 99 tmp_entry->nextEntry = new; |
| 55 break; | 100 break; |
| 56 } | 101 } |
| 57 } | 102 } |
| 58 } | 103 } |
| 59 | 104 |
| 60 /* caller needs to lock memtable lock */ | 105 /* caller needs to lock memtable lock */ |
| 61 void | |
| 62 __tspi_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 | 106 TSS_RESULT |
| 82 __tspi_freeTable(TSS_HCONTEXT tspContext) | 107 __tspi_freeTable(TSS_HCONTEXT tspContext) |
| 83 { | 108 { |
| 84 struct memTable *prev = NULL, *index = NULL, *next = NULL; | 109 struct memTable *prev = NULL, *index = NULL, *next = NULL; |
| 85 struct memEntry *entry = NULL, *entry_next = NULL; | 110 struct memEntry *entry = NULL, *entry_next = NULL; |
| 86 | 111 |
| 87 for(index = SpiMemoryTable; index; index = index->nextTable) { | 112 for(index = SpiMemoryTable; index; index = index->nextTable) { |
| 88 next = index->nextTable; | 113 next = index->nextTable; |
| 89 if (index->tspContext == tspContext) { | 114 if (index->tspContext == tspContext) { |
| 90 for (entry = index->entries; entry; entry = entry_next)
{ | 115 for (entry = index->entries; entry; entry = entry_next)
{ |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 void * | 184 void * |
| 160 calloc_tspi(TSS_HCONTEXT tspContext, UINT32 howMuch) | 185 calloc_tspi(TSS_HCONTEXT tspContext, UINT32 howMuch) |
| 161 { | 186 { |
| 162 struct memTable *table = NULL; | 187 struct memTable *table = NULL; |
| 163 struct memEntry *newEntry = NULL; | 188 struct memEntry *newEntry = NULL; |
| 164 | 189 |
| 165 MUTEX_LOCK(memtable_lock); | 190 MUTEX_LOCK(memtable_lock); |
| 166 | 191 |
| 167 table = getTable(tspContext); | 192 table = getTable(tspContext); |
| 168 if (table == NULL) { | 193 if (table == NULL) { |
| 169 » » /* no table has yet been created to hold the memory allocations
of | 194 » » if ((table = __tspi_createTable()) == NULL) { |
| 170 » » * this context, so we need to create one | |
| 171 » » */ | |
| 172 » » table = calloc(1, sizeof(struct memTable)); | |
| 173 » » if (table == NULL) { | |
| 174 » » » LogError("malloc of %zd bytes failed.", sizeof(struct me
mTable)); | |
| 175 MUTEX_UNLOCK(memtable_lock); | 195 MUTEX_UNLOCK(memtable_lock); |
| 176 return NULL; | 196 return NULL; |
| 177 } | 197 } |
| 178 table->tspContext = tspContext; | 198 table->tspContext = tspContext; |
| 179 __tspi_addTable(table); | 199 __tspi_addTable(table); |
| 180 } | 200 } |
| 181 | 201 |
| 182 newEntry = calloc(1, sizeof(struct memEntry)); | 202 newEntry = calloc(1, sizeof(struct memEntry)); |
| 183 if (newEntry == NULL) { | 203 if (newEntry == NULL) { |
| 184 LogError("malloc of %zd bytes failed.", sizeof(struct memEntry))
; | 204 LogError("malloc of %zd bytes failed.", sizeof(struct memEntry))
; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 return TSPERR(TSS_E_INVALID_RESOURCE); | 249 return TSPERR(TSS_E_INVALID_RESOURCE); |
| 230 } | 250 } |
| 231 | 251 |
| 232 /* just free one entry */ | 252 /* just free one entry */ |
| 233 result = __tspi_freeEntry(index, memPointer); | 253 result = __tspi_freeEntry(index, memPointer); |
| 234 | 254 |
| 235 MUTEX_UNLOCK(memtable_lock); | 255 MUTEX_UNLOCK(memtable_lock); |
| 236 | 256 |
| 237 return result; | 257 return result; |
| 238 } | 258 } |
| OLD | NEW |