/*
 * oclh_h_base_dev_clapi_wrappers.c
 *      Author: havock
 */

#include <oclh_h_base_dev_clapi_wrappers.h>
#include <oclh_h_base_defs.h>

cl_bool _ghf_getDevInf_bool(const cl_device_id   clDev,
                            const cl_device_info InfoVal,
                                  cl_int*  const pclErr) {
    cl_bool clbRes=0u; size_t szRes=0ul;
    *pclErr=clGetDeviceInfo(clDev, InfoVal, sizeof(cl_bool),&clbRes,&szRes);
    if(*pclErr) clbRes=(cl_bool)_GHM_UNDEFUINTVAL;
    return(clbRes);
}

char* _ghf_getDevInf_charptr(const cl_device_id   clDev,
                             const cl_device_info InfoVal,
                                   cl_int*  const pclErr) {
    size_t szRes=0ul; char* pcInf=NULL;
    *pclErr=clGetDeviceInfo(clDev, InfoVal, 0ul, NULL, &szRes);
    if(!*pclErr) {
        if(szRes) {
            pcInf=malloc(szRes);
            if(pcInf) {
                *pclErr=clGetDeviceInfo(clDev, InfoVal, szRes, pcInf, NULL);
                if(*pclErr) { free(pcInf); pcInf=NULL; }
            }
        } else { pcInf=malloc(1ul); if(pcInf) pcInf[0]='\0'; }
    }
    return(pcInf);
}

cl_uint _ghf_getDevInf_cluint(const cl_device_id   clDev,
                              const cl_device_info InfoVal,
                                    cl_int*  const pclErr) {
    cl_uint cluRes=0u; size_t szRes=0ul;
    *pclErr=clGetDeviceInfo(clDev, InfoVal, sizeof(cl_uint), &cluRes, &szRes);
    if(*pclErr) cluRes=(cl_uint)_GHM_UNDEFUINTVAL;
    return(cluRes);
}

cl_ulong _ghf_getDevInf_clulong(const cl_device_id   clDev,
                                const cl_device_info InfoVal,
                                      cl_int*  const pclErr) {
    cl_ulong Res=0ul; size_t szRes=0ul;
    *pclErr=clGetDeviceInfo(clDev, InfoVal, sizeof(cl_ulong), &Res, &szRes);
    if(*pclErr) Res=_GHM_UNDEFUINTVAL;
    return(Res);
}

size_t _ghf_getDevInf_size(const cl_device_id   clDev,
                           const cl_device_info InfoVal,
                                 cl_int*  const pclErr) {
    size_t Res=0ul; size_t szRes=0ul;
    *pclErr=clGetDeviceInfo(clDev, InfoVal, sizeof(size_t), &Res, &szRes);
    if(*pclErr) Res=_GHM_UNDEFUINTVAL;
    return(Res);
}

uintptr_t _ghf_getDevInf_uintptr(const cl_device_id   clDev,
                                 const cl_device_info InfoVal,
                                       cl_int*  const pclErr) {
    uintptr_t ptrRes=(uintptr_t)NULL; size_t szRes=0ul;
    *pclErr=clGetDeviceInfo(clDev, InfoVal, sizeof(uintptr_t), &ptrRes, &szRes);
    if(*pclErr) ptrRes=(uintptr_t)_GHM_UNDEFPTR;
    return(ptrRes);
}
