2 * Copyright (c) 2016-2019 The Khronos Group Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * OpenCL is a trademark of Apple Inc. used under license by Khronos.
24 #include <sys/types.h>
28 static pthread_once_t initialized = PTHREAD_ONCE_INIT;
32 * Vendor enumeration functions
36 // go through the list of vendors in the two configuration files
37 void khrIcdOsVendorsEnumerate(void)
40 struct dirent *dirEntry = NULL;
42 char *vendorPath = "/system/vendor/Khronos/OpenCL/vendors/";
44 char *vendorPath = "/etc/OpenCL/vendors/";
48 dir = opendir(vendorPath);
51 KHR_ICD_TRACE("Failed to open path %s\n", vendorPath);
55 // attempt to load all files in the directory
56 for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir) )
58 switch(dirEntry->d_type)
64 const char* extension = ".icd";
66 char* fileName = NULL;
70 // make sure the file name ends in .icd
71 if (strlen(extension) > strlen(dirEntry->d_name) )
75 if (strcmp(dirEntry->d_name + strlen(dirEntry->d_name) - strlen(extension), extension) )
80 // allocate space for the full path of the vendor library name
81 fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 1);
84 KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
87 sprintf(fileName, "%s%s", vendorPath, dirEntry->d_name);
89 // open the file and read its contents
90 fin = fopen(fileName, "r");
96 fseek(fin, 0, SEEK_END);
97 bufferSize = ftell(fin);
99 buffer = malloc(bufferSize+1);
106 memset(buffer, 0, bufferSize+1);
107 fseek(fin, 0, SEEK_SET);
108 if (bufferSize != (long)fread(buffer, 1, bufferSize, fin) )
115 // ignore a newline at the end of the file
116 if (buffer[bufferSize-1] == '\n') buffer[bufferSize-1] = '\0';
118 // load the string read from the file
119 khrIcdVendorAdd(buffer);
133 // free resources and exit
140 // go through the list of vendors only once
141 void khrIcdOsVendorsEnumerateOnce(void)
143 pthread_once(&initialized, khrIcdOsVendorsEnumerate);
148 * Dynamic library loading functions
152 // dynamically load a library. returns NULL on failure
153 void *khrIcdOsLibraryLoad(const char *libraryName)
155 return dlopen (libraryName, RTLD_NOW);
158 // get a function pointer from a loaded library. returns NULL on failure.
159 void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
161 return dlsym(library, functionName);
165 void khrIcdOsLibraryUnload(void *library)
170 // implement device type platform behavior
171 void khrIcdDeviceTypeGetPlatform(cl_device_type device_type, cl_platform_id *outPlatform)