1 /* ************************************************************************
2  * Copyright 2013 Advanced Micro Devices, 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 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include <kerngen.h>
22 #include <list.h>
23 #include <dis_warning.h>
24 
25 typedef struct FuncNode {
26     void *pattern;
27     char funcName[FUNC_NAME_MAXLEN];
28     ListNode node;
29 } FuncNode;
30 
31 typedef struct FuncNodeKey {
32     const void *pattern;
33     size_t patSize;
34 } FuncNodeKey;
35 
36 struct KgenGuard {
37     struct KgenContext *ctx;
38     int (*genCallback)(struct KgenContext*, const void*);
39     size_t patSize;
40     ListHead funcs;
41 };
42 
43 static int
funcNodeCmp(const ListNode * n,const void * key)44 funcNodeCmp(const ListNode *n, const void *key)
45 {
46     const FuncNode *fnode = container_of(n, node, FuncNode);
47     const FuncNodeKey *fkey = (FuncNodeKey*)key;
48 
49     return memcmp(fnode->pattern, fkey->pattern, fkey->patSize);
50 }
51 
52 static void
destroyFuncNode(ListNode * node)53 destroyFuncNode(ListNode *node)
54 {
55     FuncNode *fnode = container_of(node, node, FuncNode);
56 
57     free(fnode->pattern);
58     free(fnode);
59 }
60 
61 struct KgenGuard
createKgenGuard(struct KgenContext * ctx,int (* genCallback)(struct KgenContext * ctx,const void * pattern),size_t patSize)62 *createKgenGuard(
63     struct KgenContext *ctx,
64     int (*genCallback)(struct KgenContext *ctx, const void *pattern),
65     size_t patSize)
66 {
67     struct KgenGuard *guard;
68 
69     guard = malloc(sizeof(struct KgenGuard));
70     if (guard != NULL) {
71         guard->ctx = ctx;
72         guard->genCallback = genCallback;
73         guard->patSize = patSize;
74         listInitHead(&guard->funcs);
75     }
76 
77     return guard;
78 }
79 
80 void
reinitKgenGuard(struct KgenGuard * guard,struct KgenContext * ctx,int (* genCallback)(struct KgenContext * ctx,const void * pattern),size_t patSize)81 reinitKgenGuard(
82     struct KgenGuard *guard,
83     struct KgenContext *ctx,
84     int (*genCallback)(struct KgenContext *ctx, const void *pattern),
85     size_t patSize)
86 {
87     listDoForEachSafe(&guard->funcs, destroyFuncNode);
88     listInitHead(&guard->funcs);
89     guard->ctx = ctx;
90     guard->genCallback = genCallback;
91     guard->patSize = patSize;
92 }
93 
94 /*
95  * Invokes generator to generate a function
96  * matching to the 'pattern' pattern or just
97  * returns its name if the function is already
98  * generated
99  */
100 int
findGenerateFunction(struct KgenGuard * guard,const void * pattern,char * name,size_t nameLen)101 findGenerateFunction(
102     struct KgenGuard *guard,
103     const void *pattern,
104     char *name,
105     size_t nameLen)
106 {
107     ListNode *n;
108     FuncNode *fnode = NULL;
109 
110     FuncNodeKey fkey = {pattern, guard->patSize};
111     int ret = 0;
112 
113     n = listNodeSearch(&guard->funcs, &fkey, funcNodeCmp);
114     if (n == NULL) {
115         ret = guard->genCallback(guard->ctx, pattern);
116         if (!ret) {
117             fnode = malloc(sizeof(FuncNode));
118             if (fnode == NULL) {
119                 ret = -ENOMEM;
120             }
121             else {
122                 fnode->pattern = malloc(guard->patSize);
123                 if (fnode->pattern == NULL) {
124                     free(fnode);
125                     ret = -ENOMEM;
126                 }
127                 else {
128                     memcpy(fnode->pattern, pattern, guard->patSize);
129                     kgenGetLastFuncName(fnode->funcName,
130                                         sizeof(fnode->funcName),
131                                         guard->ctx);
132                     fnode->funcName[FUNC_NAME_MAXLEN - 1] = '\0';
133                     listAddToTail(&guard->funcs, &fnode->node);
134                 }
135             }
136         }
137         else {
138             ret = -EOVERFLOW;
139         }
140     }
141     else {
142         fnode = container_of(n, node, FuncNode);
143     }
144 
145     if (!ret) {
146         strncpy(name, fnode->funcName, nameLen);
147         name[nameLen - 1] = '\0';
148     }
149 
150     return ret;
151 }
152 
153 void
destroyKgenGuard(struct KgenGuard * guard)154 destroyKgenGuard(struct KgenGuard *guard)
155 {
156     listDoForEachSafe(&guard->funcs, destroyFuncNode);
157     free(guard);
158 }
159 
160