File: get_info.h

package info (click to toggle)
intel-compute-runtime 25.35.35096.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,324 kB
  • sloc: cpp: 926,243; lisp: 3,433; sh: 715; makefile: 162; python: 21
file content (113 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2018-2023 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once
#include "get_info_status.h"

// Need for linux compatibility with memcpy_s
#include "shared/source/helpers/string.h"

#include <limits>

namespace GetInfo {

inline constexpr size_t invalidSourceSize = std::numeric_limits<size_t>::max();

inline GetInfoStatus getInfo(void *destParamValue, size_t destParamValueSize,
                             const void *srcParamValue, size_t srcParamValueSize) {

    if (srcParamValueSize == 0) {
        // Report ok if there is nothing to copy.
        return GetInfoStatus::success;
    }

    if ((srcParamValue == nullptr) || (srcParamValueSize == invalidSourceSize)) {
        return GetInfoStatus::invalidValue;
    }

    if (destParamValue == nullptr) {
        // Report ok if only size is queried.
        return GetInfoStatus::success;
    }

    if (destParamValueSize < srcParamValueSize) {
        return GetInfoStatus::invalidValue;
    }

    // Report ok if we can copy safely.
    memcpy_s(destParamValue, destParamValueSize, srcParamValue, srcParamValueSize);
    return GetInfoStatus::success;
}

inline void setParamValueReturnSize(size_t *paramValueSizeRet, size_t newValue, GetInfoStatus getInfoStatus) {
    if ((paramValueSizeRet != nullptr) && (getInfoStatus == GetInfoStatus::success)) {
        *paramValueSizeRet = newValue;
    }
}

} // namespace GetInfo

struct GetInfoHelper {
    GetInfoHelper(void *dst, size_t dstSize, size_t *retSize, GetInfoStatus *retVal = nullptr)
        : dst(dst), dstSize(dstSize), retSize(retSize), retVal(retVal) {
    }

    template <typename DataType>
    GetInfoStatus set(const DataType &val) {
        auto errCode = GetInfoStatus::success;
        if (retSize != nullptr) {
            *retSize = sizeof(val);
        }
        if (dst != nullptr) {
            if (dstSize >= sizeof(val)) {
                *reinterpret_cast<DataType *>(dst) = val;
            } else {
                errCode = GetInfoStatus::invalidValue;
            }
        }
        if (retVal)
            *retVal = errCode;
        return errCode;
    }

    template <typename DataType>
    static void set(DataType *dst, DataType val) {
        if (dst) {
            *dst = val;
        }
    }

    void *dst;
    size_t dstSize;
    size_t *retSize;
    GetInfoStatus *retVal;
};

struct ErrorCodeHelper {
    ErrorCodeHelper(int *errcodeRet, int defaultCode)
        : errcodeRet(errcodeRet) {
        set(defaultCode);
    }

    void set(int code) {
        if (errcodeRet != nullptr) {
            *errcodeRet = code;
        }
        localErrcode = code;
    }

    int *errcodeRet;
    int localErrcode;
};

template <typename T>
T getValidParam(T param, T defaultVal = 1, T invalidVal = 0) {
    if (param == invalidVal) {
        return defaultVal;
    }
    return param;
}