Index: src/tspi/tsp_context_mem.c |
diff --git a/src/tspi/tsp_context_mem.c b/src/tspi/tsp_context_mem.c |
index b1993bc6f911ffd5450f4ee148fdc28985bbb79a..13f1243dd3b6ce31d068db3ffa8ec33385e21f70 100644 |
--- a/src/tspi/tsp_context_mem.c |
+++ b/src/tspi/tsp_context_mem.c |
@@ -21,6 +21,22 @@ |
#include "tsplog.h" |
#include "obj.h" |
+static struct memTable * |
+__tspi_createTable() |
+{ |
+ struct memTable *table = NULL; |
+ /* |
+ * No table has yet been created to hold the memory allocations of |
+ * this context, so we need to create one |
+ */ |
+ table = calloc(1, sizeof(struct memTable)); |
+ if (table == NULL) { |
+ LogError("malloc of %zd bytes failed.", sizeof(struct memTable)); |
+ return NULL; |
+ } |
+ return (table); |
+} |
+ |
/* caller needs to lock memtable lock */ |
struct memTable * |
getTable(TSS_HCONTEXT tspContext) |
@@ -34,6 +50,26 @@ getTable(TSS_HCONTEXT tspContext) |
return NULL; |
} |
+/* caller needs to lock memtable lock */ |
+static void |
+__tspi_addTable(struct memTable *new) |
+{ |
+ struct memTable *tmp = SpiMemoryTable; |
+ |
+ /* base case, this is the first table */ |
+ if (SpiMemoryTable == NULL) { |
+ SpiMemoryTable = new; |
+ return; |
+ } |
+ |
+ /* else add @new onto the end */ |
+ for (; tmp; tmp = tmp->nextTable) |
+ if (tmp->nextTable == NULL) { |
+ tmp->nextTable = new; |
+ break; |
+ } |
+} |
+ |
/* caller needs to lock memtable lock and be sure the context mem slot for |
* @tspContext exists before calling. |
*/ |
@@ -41,7 +77,16 @@ void |
__tspi_addEntry(TSS_HCONTEXT tspContext, struct memEntry *new) |
{ |
struct memTable *tmp = getTable(tspContext); |
- struct memEntry *tmp_entry = tmp->entries; |
+ struct memEntry *tmp_entry; |
+ |
+ if (tmp == NULL) { |
+ if ((tmp = __tspi_createTable()) == NULL) |
+ return; |
+ tmp->tspContext = tspContext; |
+ __tspi_addTable(tmp); |
+ } |
+ |
+ tmp_entry = tmp->entries; |
if (tmp->entries == NULL) { |
tmp->entries = new; |
@@ -58,26 +103,6 @@ __tspi_addEntry(TSS_HCONTEXT tspContext, struct memEntry *new) |
} |
/* caller needs to lock memtable lock */ |
-void |
-__tspi_addTable(struct memTable *new) |
-{ |
- struct memTable *tmp = SpiMemoryTable; |
- |
- /* base case, this is the first table */ |
- if (SpiMemoryTable == NULL) { |
- SpiMemoryTable = new; |
- return; |
- } |
- |
- /* else add @new onto the end */ |
- for (; tmp; tmp = tmp->nextTable) |
- if (tmp->nextTable == NULL) { |
- tmp->nextTable = new; |
- break; |
- } |
-} |
- |
-/* caller needs to lock memtable lock */ |
TSS_RESULT |
__tspi_freeTable(TSS_HCONTEXT tspContext) |
{ |
@@ -166,12 +191,7 @@ calloc_tspi(TSS_HCONTEXT tspContext, UINT32 howMuch) |
table = getTable(tspContext); |
if (table == NULL) { |
- /* no table has yet been created to hold the memory allocations of |
- * this context, so we need to create one |
- */ |
- table = calloc(1, sizeof(struct memTable)); |
- if (table == NULL) { |
- LogError("malloc of %zd bytes failed.", sizeof(struct memTable)); |
+ if ((table = __tspi_createTable()) == NULL) { |
MUTEX_UNLOCK(memtable_lock); |
return NULL; |
} |