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

krakjoe / ort / 18868881391

28 Oct 2025 08:38AM UTC coverage: 91.519% (+0.2%) from 91.307%
18868881391

push

github

krakjoe
[ci skip] show diff

3054 of 3337 relevant lines covered (91.52%)

26115.35 hits per line

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

59.74
/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_fn; /* renamed to avoid conflict with system memcpy macro */
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
#ifdef HAVE_ONNXRUNTIME
35
static OrtMemoryInfo* minfo;
36

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

48
            api->ReleaseStatus(status);
×
49
            exit(EXIT_FAILURE);
×
50
        }
51
    }
52
    return minfo;
1,260✔
53
}
54

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

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

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

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

78
static volatile zend_bool __ort_alloc_default_initialized = 0;
79

80
static void __ort_alloc_default_startup(ort_alloc_t* allocator) {
1,275✔
81
    if (__ort_alloc_default_initialized) {
1,275✔
82
        return;
83
    }
84

85
    /* The allocator will be started in every thread:
86
        short circuit this registration in other threads */
87
    __ort_alloc_default_initialized = 1;
630✔
88

89
    OrtStatus* status =
630✔
90
        api->RegisterAllocator(
630✔
91
            php_ort_environment(),
92
            (OrtAllocator*)
93
                &__ort_alloc_default_for_onnx);
94
    if (status) {
630✔
95
        fprintf(stderr,
×
96
            "ort: failed to register allocator: %s\n",
97
            api->GetErrorMessage(status));
×
98

99
        api->ReleaseStatus(status);
×
100
        exit(EXIT_FAILURE);
×
101
    }
102
}
103
#else
104
static void __ort_alloc_default_startup(ort_alloc_t* allocator) {
105
    (void)allocator;
106
}
107
#endif
108

109
static void* __ort_alloc_default_alloc(size_t size, size_t count, size_t alignment) {
13,854✔
110
    assert(
13,854✔
111
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
112
        alignment >= sizeof(void*));          /* minimum alignment */
113

114
    void *raw = pemalloc(
13,854✔
115
        (size * count) + alignment - 1 + sizeof(void*), 1);
116

117
    uintptr_t address = (uintptr_t)raw + sizeof(void*);
13,854✔
118

119
    // Ensure we have space for the raw pointer before alignment
120
    uintptr_t aligned = (address + alignment - 1) & ~(uintptr_t)(alignment - 1);
13,854✔
121

122
    // If there's not enough space before aligned address, move to next boundary
123
    if (aligned - (uintptr_t)raw < sizeof(void*)) {
13,854✔
124
        aligned += alignment;
×
125
    }
126

127
    void **start = (void**)aligned;
13,854✔
128
    start[-1] = raw;
13,854✔
129

130
    // zero'd memory is a requirement of the allocator
131
    memset((void*)start, 0, size * count);
13,854✔
132

133
    return (void*)start;
13,854✔
134
}
135

136
static void* __ort_alloc_default_memcpy(void *dest, const void *src, size_t n) {
2,223✔
137
    if (n == 0) {
2,223✔
138
        return dest;
139
    }
140
    return memcpy(dest, src, n);
2,223✔
141
}
142

143
static void __ort_alloc_default_free(void* ptr) {
13,728✔
144
    if (ptr) {
13,728✔
145
        void *raw =
13,728✔
146
            ((void**)ptr)[-1];
147
        pefree(raw, 1);
13,728✔
148
    }
149
}
13,728✔
150

151
static void __ort_alloc_default_shutdown(ort_alloc_t* allocator) {}
1,275✔
152

153
ORT_TLS ort_alloc_t __ort_allocator = {
154
    .alloc    = __ort_alloc_default_alloc,
155
    .memcpy_fn = __ort_alloc_default_memcpy,
156
    .free     = __ort_alloc_default_free,
157

158
    .startup  = __ort_alloc_default_startup,
159
    .shutdown = __ort_alloc_default_shutdown,
160

161
    .alignment = sizeof(void*) * 2,
162
};
163

164
void ort_alloc_align(size_t alignment) {
×
165
    assert(
×
166
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
167
        alignment >= sizeof(void*));          /* minimum alignment */
168

169
    __ort_allocator.alignment = alignment;
×
170
}
×
171

172
size_t ort_alloc_alignment(void) {
×
173
    return __ort_allocator.alignment;
×
174
}
175

176
zend_bool ort_alloc_aligned(void* ptr) {
×
177
    return (zend_bool)
×
178
        ((uintptr_t)ptr % 
×
179
            __ort_allocator.alignment) == 0;
×
180
}
181

182
ort_memcpy_func_t ort_alloc_memcpy(ort_memcpy_func_t memcpy) {
×
183
    ort_memcpy_func_t fallback = __ort_allocator.memcpy_fn;
×
184

185
    if (memcpy) {
×
186
        __ort_allocator.memcpy_fn = memcpy;
×
187
    }
188

189
    return fallback;
×
190
}
191

192
void* ort_alloc(size_t size, size_t count) {
13,854✔
193
    return __ort_allocator.alloc(size, count,
13,854✔
194
        __ort_allocator.alignment);
195
}
196

197
void* ort_memcpy(void *dest, const void *src, size_t n) {
2,223✔
198
    return __ort_allocator.memcpy_fn(dest, src, n);
2,223✔
199
}
200

201
void ort_free(void* ptr) {
13,728✔
202
    __ort_allocator.free(ptr);
13,728✔
203
}
13,728✔
204

205
void ort_alloc_startup(void)
1,275✔
206
{
207
    if (__ort_allocator.startup) {
1,275✔
208
        __ort_allocator.startup(&__ort_allocator);
1,275✔
209
    }
210
}
1,275✔
211

212
void ort_alloc_shutdown(void)
1,270✔
213
{
214
    if (__ort_allocator.shutdown) {
1,270✔
215
        __ort_allocator.shutdown(&__ort_allocator);
1,270✔
216
    }
217
}
1,274✔
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