crypt32: Use list struct directly instead of ContextList wrapper.
[wine.git] / dlls / crypt32 / context.c
blobd6d67d129fe37c67a8de86466f3f99ebf2c9fd53
1 /*
2 * Copyright 2006 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 #include <assert.h>
19 #include <stdarg.h>
20 #include "windef.h"
21 #include "winbase.h"
22 #include "wincrypt.h"
23 #include "wine/debug.h"
24 #include "crypt32_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(context);
28 void *Context_CreateDataContext(size_t contextSize, const context_vtbl_t *vtbl)
30 context_t *context;
32 context = CryptMemAlloc(sizeof(context_t) + contextSize);
33 if (!context)
34 return NULL;
36 context->vtbl = vtbl;
37 context->ref = 1;
38 context->linked = NULL;
39 context->properties = ContextPropertyList_Create();
40 if (!context->properties)
42 CryptMemFree(context);
43 return NULL;
46 TRACE("returning %p\n", context);
47 return context_ptr(context);
50 context_t *Context_CreateLinkContext(unsigned int contextSize, context_t *linked)
52 context_t *context;
54 TRACE("(%d, %p)\n", contextSize, linked);
56 context = CryptMemAlloc(sizeof(context_t) + contextSize);
57 if (!context)
58 return NULL;
60 memcpy(context_ptr(context), context_ptr(linked), contextSize);
61 context->vtbl = linked->vtbl;
62 context->ref = 1;
63 context->linked = linked;
64 context->properties = linked->properties;
65 Context_AddRef(linked);
67 TRACE("returning %p\n", context);
68 return context;
71 void Context_AddRef(context_t *context)
73 InterlockedIncrement(&context->ref);
74 TRACE("(%p) ref=%d\n", context, context->ref);
77 BOOL Context_Release(context_t *context)
79 BOOL ret = TRUE;
81 if (context->ref <= 0)
83 ERR("%p's ref count is %d\n", context, context->ref);
84 return FALSE;
86 if (InterlockedDecrement(&context->ref) == 0)
88 TRACE("freeing %p\n", context);
89 if (!context->linked)
91 ContextPropertyList_Free(context->properties);
92 context->vtbl->free(context);
93 } else {
94 Context_Release(context->linked);
96 CryptMemFree(context);
98 else
99 TRACE("%p's ref count is %d\n", context, context->ref);
100 return ret;
103 void Context_CopyProperties(const void *to, const void *from)
105 CONTEXT_PROPERTY_LIST *toProperties, *fromProperties;
107 toProperties = context_from_ptr(to)->properties;
108 fromProperties = context_from_ptr(from)->properties;
109 assert(toProperties && fromProperties);
110 ContextPropertyList_Copy(toProperties, fromProperties);
113 context_t *ContextList_Add(ContextList *list, CRITICAL_SECTION *cs, context_t *toLink,
114 context_t *existing, struct WINE_CRYPTCERTSTORE *store, BOOL use_link)
116 context_t *context;
118 TRACE("(%p, %p, %p)\n", list, toLink, existing);
120 context = toLink->vtbl->clone(toLink, store, use_link);
121 if (context)
123 TRACE("adding %p\n", context);
124 EnterCriticalSection(cs);
125 if (existing)
127 context->u.entry.prev = existing->u.entry.prev;
128 context->u.entry.next = existing->u.entry.next;
129 context->u.entry.prev->next = &context->u.entry;
130 context->u.entry.next->prev = &context->u.entry;
131 list_init(&existing->u.entry);
132 Context_Release(existing);
134 else
135 list_add_head(list, &context->u.entry);
136 LeaveCriticalSection(cs);
138 return context;
141 context_t *ContextList_Enum(ContextList *list, CRITICAL_SECTION *cs, context_t *prev)
143 struct list *listNext;
144 context_t *ret;
146 EnterCriticalSection(cs);
147 if (prev)
149 listNext = list_next(list, &prev->u.entry);
150 Context_Release(prev);
152 else
153 listNext = list_next(list, list);
154 LeaveCriticalSection(cs);
156 if (listNext)
158 ret = LIST_ENTRY(listNext, context_t, u.entry);
159 Context_AddRef(ret);
161 else
162 ret = NULL;
163 return ret;
166 BOOL ContextList_Remove(ContextList *list, CRITICAL_SECTION *cs, context_t *context)
168 BOOL inList = FALSE;
170 EnterCriticalSection(cs);
171 if (!list_empty(&context->u.entry))
173 list_remove(&context->u.entry);
174 list_init(&context->u.entry);
175 inList = TRUE;
177 LeaveCriticalSection(cs);
179 return inList;
182 void ContextList_Free(ContextList *list)
184 context_t *context, *next;
186 LIST_FOR_EACH_ENTRY_SAFE(context, next, list, context_t, u.entry)
188 TRACE("removing %p\n", context);
189 list_remove(&context->u.entry);
190 Context_Release(context);