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

krakjoe / ort / 16291795092

15 Jul 2025 11:17AM UTC coverage: 92.607%. Remained the same
16291795092

push

github

krakjoe
matmul still needs work before aligned load/store can be used

8 of 36 new or added lines in 3 files covered. (22.22%)

5 existing lines in 1 file now uncovered.

5386 of 5816 relevant lines covered (92.61%)

71568.13 hits per line

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

73.33
/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
struct _ort_alloc_t {
24
    ort_alloc_func_t alloc;
25
    ort_memcpy_func_t memcpy;
26
    ort_free_func_t free;
27

28
    ort_alloc_startup_func_t startup;
29
    ort_alloc_shutdown_func_t shutdown;
30

31
    size_t alignment;
32
};
33

34
static OrtMemoryInfo* minfo;
35

36
const OrtMemoryInfo* __ort_alloc_default_info_for_onnx(
6,336✔
37
    const OrtAllocator* allocator) {
38
    if (!minfo) {
6,336✔
39
        OrtStatus* status =
3,168✔
40
            api->CreateCpuMemoryInfo(
3,168✔
41
                OrtDeviceAllocator, OrtMemTypeDefault, &minfo);
42
        if (status) {
3,168✔
43
            fprintf(stderr,
×
44
                "ort: failed to create default memory info: %s\n",
45
                api->GetErrorMessage(status));
×
46

47
            api->ReleaseStatus(status);
×
48
            exit(EXIT_FAILURE);
×
49
        }
50
    }
51
    return minfo;
6,336✔
52
}
53

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

59
static void __ort_alloc_default_free_for_onnx(
×
60
    OrtAllocator* allocator, void* ptr) {
61
    ort_free(ptr);
×
62
}
×
63

64
static void* __ort_alloc_default_reserve_for_onnx(
×
65
    OrtAllocator* allocator, size_t size) {
66
    return ort_alloc(size, 1);
×
67
}
68

69
static const OrtAllocator __ort_alloc_default_for_onnx = {
70
    .Alloc   = __ort_alloc_default_malloc_for_onnx,
71
    .Reserve = __ort_alloc_default_reserve_for_onnx,
72
    .Free    = __ort_alloc_default_free_for_onnx,
73
    .Info    = __ort_alloc_default_info_for_onnx,
74
    .version = ORT_API_VERSION,
75
};
76

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

88
        api->ReleaseStatus(status);
×
UNCOV
89
        exit(EXIT_FAILURE);
×
90
    }
91
}
3,168✔
92

93
static void* __ort_alloc_default_alloc(size_t size, size_t count, size_t alignment) {
71,328✔
94
    assert(
71,328✔
95
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
96
        alignment >= sizeof(void*));          /* minimum alignment */
97

98
    void *raw = pemalloc(
71,328✔
99
        (size * count) + alignment - 1 + sizeof(void*), 1);
100

101
    uintptr_t address = (uintptr_t)raw + sizeof(void*);
71,328✔
102
    
103
    // Ensure we have space for the raw pointer before alignment
104
    uintptr_t aligned = (address + alignment - 1) & ~(uintptr_t)(alignment - 1);
71,328✔
105
    
106
    // If there's not enough space before aligned address, move to next boundary
107
    if (aligned - (uintptr_t)raw < sizeof(void*)) {
71,328✔
UNCOV
108
        aligned += alignment;
×
109
    }
110

111
    void **start = (void**)aligned;
71,328✔
112
    start[-1] = raw;
71,328✔
113

114
    return (void*)start;
71,328✔
115
}
116

117
static void* __ort_alloc_default_memcpy(void *dest, const void *src, size_t n) {
11,856✔
118
    if (n == 0) {
11,856✔
119
        return dest;
120
    }
121
    return memcpy(dest, src, n);
11,844✔
122
}
123

124
static void __ort_alloc_default_free(void* ptr) {
70,656✔
125
    if (ptr) {
70,656✔
126
        void *raw =
70,656✔
127
            ((void**)ptr)[-1];
128
        pefree(raw, 1);
70,656✔
129
    }
130
}
70,656✔
131

132
static void __ort_alloc_default_shutdown(ort_alloc_t* allocator) {}
3,168✔
133

134
static ort_alloc_t __ort_allocator = {
135
    .alloc    = __ort_alloc_default_alloc,
136
    .memcpy   = __ort_alloc_default_memcpy,
137
    .free     = __ort_alloc_default_free,
138

139
    .startup  = __ort_alloc_default_startup,
140
    .shutdown = __ort_alloc_default_shutdown,
141

142
    .alignment = sizeof(void*) * 2,
143
};
144

145
void ort_alloc_align(size_t alignment) {
2,376✔
146
    assert(
2,376✔
147
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
148
        alignment >= sizeof(void*));          /* minimum alignment */
149

150
    __ort_allocator.alignment = alignment;
2,376✔
151
}
2,376✔
152

153
size_t ort_alloc_alignment(void) {
23,424✔
154
    return __ort_allocator.alignment;
23,424✔
155
}
156

157
zend_bool ort_alloc_aligned(void* ptr) {
×
158
    return (zend_bool)
×
159
        ((uintptr_t)ptr % 
×
UNCOV
160
            __ort_allocator.alignment) == 0;
×
161
}
162

163
ort_memcpy_func_t ort_alloc_memcpy(ort_memcpy_func_t memcpy) {
2,376✔
164
    ort_memcpy_func_t fallback = __ort_allocator.memcpy;
2,376✔
165

166
    if (memcpy) {
2,376✔
167
        __ort_allocator.memcpy = memcpy;
2,376✔
168
    }
169

170
    return fallback;
2,376✔
171
}
172

173
void* ort_alloc(size_t size, size_t count) {
71,328✔
174
    return __ort_allocator.alloc(size, count,
71,328✔
175
        __ort_allocator.alignment);
176
}
177

178
void* ort_memcpy(void *dest, const void *src, size_t n) {
11,856✔
179
    return __ort_allocator.memcpy(dest, src, n);
11,856✔
180
}
181

182
void ort_free(void* ptr) {
70,656✔
183
    __ort_allocator.free(ptr);
70,656✔
184
}
70,656✔
185

186
PHP_MINIT_FUNCTION(ORT_ALLOC) 
3,168✔
187
{
188
    if (__ort_allocator.startup) {
3,168✔
189
        __ort_allocator.startup(&__ort_allocator);
3,168✔
190
    }
191

192
    return SUCCESS;
3,168✔
193
}
194

195
PHP_MSHUTDOWN_FUNCTION(ORT_ALLOC)
3,168✔
196
{
197
    if (__ort_allocator.shutdown) {
3,168✔
198
        __ort_allocator.shutdown(&__ort_allocator);
3,168✔
199
    }
200

201
    return SUCCESS;
3,168✔
202
}
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