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

krakjoe / ort / 16290972494

15 Jul 2025 10:36AM UTC coverage: 92.607% (-0.07%) from 92.68%
16290972494

push

github

krakjoe
fix windoze

5386 of 5816 relevant lines covered (92.61%)

71563.78 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
            &__ort_alloc_default_for_onnx);
82
    if (status) {
3,168✔
83
        fprintf(stderr,
×
84
            "ort: failed to register allocator: %s\n",
85
            api->GetErrorMessage(status));
×
86

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

169
    return fallback;
2,376✔
170
}
171

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

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

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

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

191
    return SUCCESS;
3,168✔
192
}
193

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

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