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

krakjoe / ort / 20951260995

13 Jan 2026 08:52AM UTC coverage: 96.115% (-0.4%) from 96.535%
20951260995

push

github

krakjoe
fix float16 neon f16v impl

22 of 22 new or added lines in 2 files covered. (100.0%)

16 existing lines in 2 files now uncovered.

12419 of 12921 relevant lines covered (96.11%)

471019.51 hits per line

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

78.5
/src/alloc.c
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
#include "ort.h"
20
#include "env.h"
21
#include "alloc.h"
22

23
#ifdef HAVE_ONNXRUNTIME
24
static OrtMemoryInfo* minfo;
25

26
const OrtMemoryInfo* __ort_alloc_default_info_for_onnx(
8,104✔
27
    const OrtAllocator* allocator) {
28
    if (!minfo) {
8,104✔
29
        OrtStatus* status =
4,952✔
30
            api->CreateCpuMemoryInfo(
3,817✔
31
                OrtDeviceAllocator, OrtMemTypeDefault, &minfo);
32
        if (status) {
3,817✔
UNCOV
33
            fprintf(stderr,
×
34
                "ort: failed to create default memory info: %s\n",
UNCOV
35
                api->GetErrorMessage(status));
×
36

UNCOV
37
            api->ReleaseStatus(status);
×
UNCOV
38
            exit(EXIT_FAILURE);
×
39
        }
40
    }
1,135✔
41
    return minfo;
8,104✔
42
}
43

UNCOV
44
static void* __ort_alloc_default_malloc_for_onnx(
×
45
    OrtAllocator* allocator, size_t size) {
UNCOV
46
    return ort_alloc(size, 1);
×
47
}
48

UNCOV
49
static void __ort_alloc_default_free_for_onnx(
×
50
    OrtAllocator* allocator, void* ptr) {
UNCOV
51
    ort_free(ptr);
×
UNCOV
52
}
×
53

UNCOV
54
static void* __ort_alloc_default_reserve_for_onnx(
×
55
    OrtAllocator* allocator, size_t size) {
UNCOV
56
    return ort_alloc(size, 1);
×
57
}
58

59
static const OrtAllocator __ort_alloc_default_for_onnx = {
60
    .Alloc   = __ort_alloc_default_malloc_for_onnx,
61
    .Reserve = __ort_alloc_default_reserve_for_onnx,
62
    .Free    = __ort_alloc_default_free_for_onnx,
63
    .Info    = __ort_alloc_default_info_for_onnx,
64
    .version = ORT_API_VERSION,
65
};
66

67
static volatile zend_bool __ort_alloc_default_initialized = 0;
68

69
static void __ort_alloc_default_startup(ort_alloc_t* allocator) {
7,791✔
70
    if (__ort_alloc_default_initialized) {
7,791✔
71
        return;
1,179✔
72
    }
73

74
    /* The allocator will be started in every thread:
75
        short circuit this registration in other threads */
76
    __ort_alloc_default_initialized = 1;
3,817✔
77

78
    OrtStatus* status =
4,952✔
79
        api->RegisterAllocator(
4,952✔
80
            php_ort_environment(),
1,135✔
81
            (OrtAllocator*)
82
                &__ort_alloc_default_for_onnx);
83
    if (status) {
3,817✔
UNCOV
84
        fprintf(stderr,
×
85
            "ort: failed to register allocator: %s\n",
UNCOV
86
            api->GetErrorMessage(status));
×
87

UNCOV
88
        api->ReleaseStatus(status);
×
UNCOV
89
        exit(EXIT_FAILURE);
×
90
    }
91
}
2,314✔
92
#else
93
static void __ort_alloc_default_startup(ort_alloc_t* allocator) {
94
    (void)allocator;
95
}
96
#endif
97

98
static void* __ort_alloc_default_alloc(size_t size, size_t count, size_t alignment) {
76,919✔
99
    assert(
76,919✔
100
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
101
        alignment >= sizeof(void*));          /* minimum alignment */
102

103
    void *raw = pemalloc(
76,919✔
104
        (size * count) + alignment - 1 + sizeof(void*), 1);
105

106
    uintptr_t address = (uintptr_t)raw + sizeof(void*);
76,919✔
107

108
    // Ensure we have space for the raw pointer before alignment
109
    uintptr_t aligned = (address + alignment - 1) & ~(uintptr_t)(alignment - 1);
76,919✔
110

111
    // If there's not enough space before aligned address, move to next boundary
112
    if (aligned - (uintptr_t)raw < sizeof(void*)) {
76,919✔
113
        aligned += alignment;
×
114
    }
115

116
    void **start = (void**)aligned;
76,919✔
117
    start[-1] = raw;
76,919✔
118

119
    // zero'd memory is a requirement of the allocator
120
    memset((void*)start, 0, size * count);
76,919✔
121

122
    return (void*)start;
102,804✔
123
}
25,885✔
124

125
static void* __ort_alloc_default_memcpy(void *dest, const void *src, size_t n) {
11,110✔
126
    if (n == 0) {
11,110✔
127
        return dest;
4✔
128
    }
129
    return memcpy(dest, src, n);
11,103✔
130
}
3,705✔
131

132
static void __ort_alloc_default_free(void* ptr) {
76,345✔
133
    if (ptr) {
76,345✔
134
        void *raw =
102,020✔
135
            ((void**)ptr)[-1];
25,675✔
136
        pefree(raw, 1);
76,345✔
137
    }
25,675✔
138
}
76,345✔
139

140
static void __ort_alloc_default_shutdown(ort_alloc_t* allocator) {}
7,785✔
141

142
ORT_TLS ort_alloc_t __ort_allocator = {
143
    .alloc    = __ort_alloc_default_alloc,
144
    .memcpy_fn = __ort_alloc_default_memcpy,
145
    .free     = __ort_alloc_default_free,
146

147
    .startup  = __ort_alloc_default_startup,
148
    .activate = NULL,
149
    .deactivate = NULL,
150
    .shutdown = __ort_alloc_default_shutdown,
151

152
    .alignment = sizeof(void*) * 2,
153
};
154

155
void ort_alloc_align(size_t alignment) {
5,719✔
156
    assert(
5,719✔
157
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
158
        alignment >= sizeof(void*));          /* minimum alignment */
159

160
    __ort_allocator.alignment = alignment;
5,719✔
161
}
5,719✔
162

163
size_t ort_alloc_alignment(void) {
×
164
    return __ort_allocator.alignment;
×
165
}
166

167
zend_bool ort_alloc_aligned(void* ptr) {
×
168
    return (zend_bool)
×
169
        ((uintptr_t)ptr % 
×
170
            __ort_allocator.alignment) == 0;
×
171
}
172

173
ort_memcpy_func_t ort_alloc_memcpy(ort_memcpy_func_t memcpy) {
5,717✔
174
    ort_memcpy_func_t fallback = __ort_allocator.memcpy_fn;
5,717✔
175

176
    if (memcpy) {
5,717✔
177
        __ort_allocator.memcpy_fn = memcpy;
5,717✔
178
    }
1,851✔
179

180
    return fallback;
7,568✔
181
}
1,851✔
182

183
void* ort_alloc(size_t size, size_t count) {
78,279✔
184
    return __ort_allocator.alloc(size, count,
104,164✔
185
        __ort_allocator.alignment);
25,885✔
186
}
187

188
void* ort_memcpy(void *dest, const void *src, size_t n) {
11,115✔
189
    return __ort_allocator.memcpy_fn(dest, src, n);
11,115✔
190
}
191

192
void ort_free(void* ptr) {
77,693✔
193
    __ort_allocator.free(ptr);
77,693✔
194
}
77,693✔
195

196
void ort_alloc_setup(
2,314✔
197
    ort_alloc_t                *backup,
198
    ort_alloc_activate_func_t   activate,
199
    ort_alloc_func_t            _alloc,
200
    ort_memcpy_func_t           _memcpy,
201
    ort_free_func_t             _free,
202
    ort_alloc_deactivate_func_t deactivate) {
203

204
    memcpy(backup, &__ort_allocator, sizeof(ort_alloc_t));
2,314✔
205

206
    __ort_allocator.activate   = activate;
2,314✔
207
    __ort_allocator.alloc      = _alloc;
2,314✔
208
    __ort_allocator.memcpy_fn  = _memcpy;
2,314✔
209
    __ort_allocator.free       = _free;
2,314✔
210
    __ort_allocator.deactivate = deactivate;
2,314✔
211
}
2,314✔
212

213
void ort_alloc_startup(void)
7,788✔
214
{
215
    if (__ort_allocator.startup) {
7,788✔
216
        __ort_allocator.startup(&__ort_allocator);
7,790✔
217
    }
2,314✔
218
}
7,793✔
219

220
void ort_alloc_activate(void)
7,788✔
221
{
222
    if (__ort_allocator.activate) {
7,788✔
223
        __ort_allocator.activate();
×
224
    }
225
}
7,788✔
226

227
void ort_alloc_deactivate(void)
7,793✔
228
{
229
    if (__ort_allocator.deactivate) {
7,793✔
230
        __ort_allocator.deactivate();
2,316✔
231
    }
232
}
7,793✔
233

234
void ort_alloc_shutdown(void)
7,788✔
235
{
236
    if (__ort_allocator.shutdown) {
7,788✔
237
        __ort_allocator.shutdown(&__ort_allocator);
7,788✔
238
    }
2,315✔
239
}
7,788✔
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