• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

krakjoe / ort / 16390146533

19 Jul 2025 03:31PM UTC coverage: 93.135% (-0.02%) from 93.152%
16390146533

push

github

krakjoe
skipif

5644 of 6060 relevant lines covered (93.14%)

119633.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/src/ort.h
1
/*
2
  +----------------------------------------------------------------------+
3
  | ort                                                                  |
4
  +----------------------------------------------------------------------+
5
  | Copyright (c) Joe Watkins 2025                                       |
6
  +----------------------------------------------------------------------+
7
  | This source file is subject to version 3.01 of the PHP license,      |
8
  | that is bundled with this package in the file LICENSE, and is        |
9
  | available through the world-wide-web at the following url:           |
10
  | http://www.php.net/license/3_01.txt                                  |
11
  | If you did not receive a copy of the PHP license and are unable to   |
12
  | obtain it through the world-wide-web, please send a note to          |
13
  | license@php.net so we can mail you a copy immediately.               |
14
  +----------------------------------------------------------------------+
15
  | Author: krakjoe                                                      |
16
  +----------------------------------------------------------------------+
17
 */
18

19
#ifndef HAVE_ORT_CORE
20
#define HAVE_ORT_CORE
21

22
#include <php.h>
23

24
#ifdef HAVE_CONFIG_H
25
# include "config.h"
26
#endif
27

28
#ifdef _WIN32
29
# define ORT_TLS __declspec(thread)
30
#else
31
# define ORT_TLS __thread
32
#endif
33

34
#ifdef HAVE_ONNXRUNTIME
35
# include <onnxruntime_c_api.h>
36
#else
37
typedef enum {
38
    ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED = 0,
39
    ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT = 1,
40
    ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE,
41
    ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8,
42
    ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16,
43
    ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32,
44
    ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64,
45
    ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8,
46
    ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16,
47
    ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32,
48
    ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64,
49
    ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL
50
} ONNXTensorElementDataType;
51
#endif
52

53
typedef enum {
54
    PHP_ORT_OWN_ZEND = 0,
55
    PHP_ORT_OWN_HEAP = 1
56
} php_ort_owner_t;
57

58
static zend_always_inline const char* php_ort_type_name(ONNXTensorElementDataType type) {
16,944✔
59
    switch (type) {
16,944✔
60
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:   return "FLOAT";
61
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE:  return "DOUBLE";
5,472✔
62
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:    return "INT8";
1,040✔
63
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:   return "INT16";
912✔
64
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:   return "INT32";
928✔
65
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:   return "INT64";
912✔
66
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:   return "UINT8";
944✔
67
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16:  return "UINT16";
912✔
68
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32:  return "UINT32";
912✔
69
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:    return "BOOL";
192✔
70
        default: return "UNKNOWN";
×
71
    }
72
}
73

74
static zend_always_inline size_t php_ort_type_sizeof(ONNXTensorElementDataType type) {
2,394,400✔
75
    switch (type) {
2,394,400✔
76
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:  return sizeof(float);
77
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE: return sizeof(double);
78
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:   return sizeof(int8_t);
79
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:  return sizeof(int16_t);
80
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:  return sizeof(int32_t);
81
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:  return sizeof(int64_t);
82
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:  return sizeof(uint8_t);
83
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16: return sizeof(uint16_t);
84
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32: return sizeof(uint32_t);
85
        case ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:   return sizeof(uint8_t);
86
        default: return 0;
87
    }
88
}
89

90
static zend_always_inline void php_ort_atomic_addref(uint32_t *refcount) {
784✔
91
#ifdef _WIN32
92
    InterlockedAdd(refcount, 1);
93
#elif defined(HAVE_BUILTIN_ATOMIC_CPP11)
94
    __atomic_add_fetch(refcount, 1, __ATOMIC_SEQ_CST);
720✔
95
#else
96
    __sync_add_and_fetch(refcount, 1);
97
#endif
98
}
64✔
99

100
static zend_always_inline uint32_t php_ort_atomic_delref(uint32_t *refcount) {
59,616✔
101
#ifdef _WIN32
102
    return InterlockedAdd(refcount, -1);
103
#elif defined(HAVE_BUILTIN_ATOMIC_CPP11)
104
    return __atomic_sub_fetch(refcount, 1, __ATOMIC_SEQ_CST);
59,616✔
105
#else
106
    return __sync_sub_and_fetch(refcount, 1);
107
#endif
108
}
109

110
#ifdef _WIN32
111
/* {{{ Windows-specific functions }}}*/
112
/* {{{
113
    Note: we need to convert UTF-8 strings to UTF-16 (wchar_t*) for ONNX Runtime API calls.
114
}}}*/
115
#include <windows.h>
116
#include <stdlib.h>
117

118
/** {{{ Temporary strings must only be returned by php_ort_string_pass,
119
        the caller must free them with php_ort_string_free() }}} */
120
typedef wchar_t* php_ort_string_temp_t;
121

122
// Converts a UTF-8 C string to a newly allocated wchar_t* (UTF-16).
123
static zend_always_inline php_ort_string_temp_t php_ort_string_pass(const char* utf8)
124
{
125
    if (!utf8)
126
        return NULL;
127
    int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
128

129
    if (wlen == 0) {
130
        return NULL; // Conversion failed
131
    }
132

133
    wchar_t* wide = (wchar_t*)ecalloc(wlen, sizeof(wchar_t));
134

135
    if (MultiByteToWideChar(
136
            CP_UTF8, 0, utf8, -1, wide, wlen) == 0) {
137
        efree(wide);
138
        return NULL;
139
    }
140

141
    return wide;
142
}
143
// Frees a wchar_t* allocated by php_ort_string_pass
144
static zend_always_inline void php_ort_string_free(php_ort_string_temp_t wide) {
145
    if (wide) {
146
        efree(wide);
147
    }
148
}
149
#else
150
typedef char* php_ort_string_temp_t;
151

152
static zend_always_inline php_ort_string_temp_t php_ort_string_pass(const char* utf8) {
153
    return (php_ort_string_temp_t) utf8; // No conversion needed on non-Windows platforms
154
}
155
static zend_always_inline void php_ort_string_free(php_ort_string_temp_t utf8) {
156
    // Don't need to free because we don't allocate memory for utf8
157
}
158
#endif
159

160
static zend_always_inline zend_string* php_ort_string_copy(zend_string *source) {
1,808✔
161
    zend_string *dest = zend_string_alloc(ZSTR_LEN(source), 1);
1,808✔
162

163
    memcpy(ZSTR_VAL(dest),
2,272✔
164
           ZSTR_VAL(source),
1,808✔
165
           ZSTR_LEN(source));
166

167
    ZSTR_VAL(dest)[ZSTR_LEN(dest)] = 0;
2,272✔
168

169
    ZSTR_LEN(dest) = ZSTR_LEN(source);
2,272✔
170
    ZSTR_H(dest)   = ZSTR_H(source);
2,272✔
171

172
    GC_TYPE_INFO(dest) =
2,272✔
173
        IS_STRING |
174
        ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT);
175

176
    return dest;
1,808✔
177
}
178

179
#ifdef HAVE_ONNXRUNTIME
180
extern const OrtApi* api;
181
#endif
182

183
PHP_MINIT_FUNCTION(ORT_CORE);
184
PHP_MSHUTDOWN_FUNCTION(ORT_CORE);
185

186
PHP_RINIT_FUNCTION(ORT_CORE);
187
PHP_RSHUTDOWN_FUNCTION(ORT_CORE);
188

189
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc