2 * Copyright (c) 2016 The Khronos Group Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software source and associated documentation files (the "Materials"),
6 * to deal in the Materials without restriction, including without limitation
7 * the rights to use, copy, modify, compile, merge, publish, distribute,
8 * sublicense, and/or sell copies of the Materials, and to permit persons to
9 * whom the Materials are furnished to do so, subject the following terms and
12 * All modifications to the Materials used to create a binary that is
13 * distributed to third parties shall be provided to Khronos with an
14 * unrestricted license to use for the purposes of implementing bug fixes and
15 * enhancements to the Materials;
17 * If the binary is used as part of an OpenCL(TM) implementation, whether binary
18 * is distributed together with or separately to that implementation, then
19 * recipient must become an OpenCL Adopter and follow the published OpenCL
20 * conformance process for that implementation, details at:
21 * http://www.khronos.org/conformance/;
23 * The above copyright notice, the OpenCL trademark license, and this permission
24 * notice shall be included in all copies or substantial portions of the
27 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 * OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN
35 * OpenCL is a trademark of Apple Inc. used under license by Khronos.
43 #include <sys/types.h>
47 static pthread_once_t initialized = PTHREAD_ONCE_INIT;
51 * Vendor enumeration functions
55 // go through the list of vendors in the two configuration files
56 void khrIcdOsVendorsEnumerate(void)
59 struct dirent *dirEntry = NULL;
61 char *vendorPath = "/system/vendor/Khronos/OpenCL/vendors/";
63 char *vendorPath = "/etc/OpenCL/vendors/";
67 dir = opendir(vendorPath);
70 KHR_ICD_TRACE("Failed to open path %s\n", vendorPath);
74 // attempt to load all files in the directory
75 for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir) )
77 switch(dirEntry->d_type)
83 const char* extension = ".icd";
85 char* fileName = NULL;
89 // make sure the file name ends in .icd
90 if (strlen(extension) > strlen(dirEntry->d_name) )
94 if (strcmp(dirEntry->d_name + strlen(dirEntry->d_name) - strlen(extension), extension) )
99 // allocate space for the full path of the vendor library name
100 fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 1);
103 KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
106 sprintf(fileName, "%s%s", vendorPath, dirEntry->d_name);
108 // open the file and read its contents
109 fin = fopen(fileName, "r");
115 fseek(fin, 0, SEEK_END);
116 bufferSize = ftell(fin);
118 buffer = malloc(bufferSize+1);
125 memset(buffer, 0, bufferSize+1);
126 fseek(fin, 0, SEEK_SET);
127 if (bufferSize != (long)fread(buffer, 1, bufferSize, fin) )
134 // ignore a newline at the end of the file
135 if (buffer[bufferSize-1] == '\n') buffer[bufferSize-1] = '\0';
137 // load the string read from the file
138 khrIcdVendorAdd(buffer);
152 // free resources and exit
159 // go through the list of vendors only once
160 void khrIcdOsVendorsEnumerateOnce(void)
162 pthread_once(&initialized, khrIcdOsVendorsEnumerate);
167 * Dynamic library loading functions
171 // dynamically load a library. returns NULL on failure
172 void *khrIcdOsLibraryLoad(const char *libraryName)
174 return dlopen (libraryName, RTLD_NOW);
177 // get a function pointer from a loaded library. returns NULL on failure.
178 void *khrIcdOsLibraryGetFunctionAddress(void *library, const char *functionName)
180 return dlsym(library, functionName);
184 void khrIcdOsLibraryUnload(void *library)