datapath-windows: Don't assert for unknown actions
[cascardo/ovs.git] / datapath-windows / ovsext / Util.c
1 /*
2  * Copyright (c) 2014 VMware, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "precomp.h"
18 #ifdef OVS_DBG_MOD
19 #undef OVS_DBG_MOD
20 #endif
21 #define OVS_DBG_MOD OVS_DBG_OTHERS
22
23 #include "Debug.h"
24
25 extern NDIS_HANDLE gOvsExtDriverHandle;
26
27 VOID*
28 OvsAllocateMemoryWithTag(size_t size, ULONG tag)
29 {
30     OVS_VERIFY_IRQL_LE(DISPATCH_LEVEL);
31     return NdisAllocateMemoryWithTagPriority(gOvsExtDriverHandle,
32         (UINT32)size, tag, NormalPoolPriority);
33 }
34
35 VOID
36 OvsFreeMemoryWithTag(VOID *ptr, ULONG tag)
37 {
38     ASSERT(ptr);
39     NdisFreeMemoryWithTagPriority(gOvsExtDriverHandle, ptr, tag);
40 }
41
42 VOID *
43 OvsAllocateMemory(size_t size)
44 {
45     OVS_VERIFY_IRQL_LE(DISPATCH_LEVEL);
46     return NdisAllocateMemoryWithTagPriority(gOvsExtDriverHandle,
47         (UINT32)size, OVS_MEMORY_TAG, NormalPoolPriority);
48 }
49
50 VOID *
51 OvsAllocateAlignedMemory(size_t size, UINT16 align)
52 {
53     OVS_VERIFY_IRQL_LE(DISPATCH_LEVEL);
54
55     ASSERT((align == 8) || (align == 16));
56
57     if ((align == 8) || (align == 16)) {
58         /*
59          * XXX: NdisAllocateMemory*() functions don't talk anything about
60          * alignment. Hence using ExAllocatePool*();
61          */
62         return (VOID *)ExAllocatePoolWithTagPriority(NonPagedPool, size,
63                                                      OVS_MEMORY_TAG,
64                                                      NormalPoolPriority);
65     }
66
67     /* Invalid user input. */
68     return NULL;
69 }
70
71 VOID
72 OvsFreeMemory(VOID *ptr)
73 {
74     ASSERT(ptr);
75     NdisFreeMemoryWithTagPriority(gOvsExtDriverHandle, ptr, OVS_MEMORY_TAG);
76 }
77
78 VOID
79 OvsFreeAlignedMemory(VOID *ptr)
80 {
81     ASSERT(ptr);
82     ExFreePoolWithTag(ptr, OVS_MEMORY_TAG);
83 }
84
85 VOID
86 OvsAppendList(PLIST_ENTRY dst, PLIST_ENTRY src)
87 {
88     PLIST_ENTRY srcFirst, srcLast, dstLast;
89     if (IsListEmpty(src)) {
90         return;
91     }
92     srcFirst = src->Flink;
93     srcLast = src->Blink;
94     dstLast = dst->Blink;
95
96     dstLast->Flink = srcFirst;
97     srcFirst->Blink = dstLast;
98
99     srcLast->Flink = dst;
100     dst->Blink = srcLast;
101
102     src->Flink = src;
103     src->Blink = src;
104 }
105
106 BOOLEAN
107 OvsCompareString(PVOID string1, PVOID string2)
108 {
109     /*
110      * Not a super-efficient string compare since we walk over the strings
111      * twice: to initialize, and then to do the comparison.
112      */
113     STRING str1, str2;
114
115     RtlInitString(&str1, string1);
116     RtlInitString(&str2, string2);
117     return RtlEqualString(&str1, &str2, FALSE);
118 }