• 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

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
#ifdef HAVE_ONNXRUNTIME
35
static OrtMemoryInfo* minfo;
36

37
const OrtMemoryInfo* __ort_alloc_default_info_for_onnx(
6,560✔
38
    const OrtAllocator* allocator) {
39
    if (!minfo) {
6,560✔
40
        OrtStatus* status =
3,280✔
41
            api->CreateCpuMemoryInfo(
3,280✔
42
                OrtDeviceAllocator, OrtMemTypeDefault, &minfo);
43
        if (status) {
3,280✔
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;
6,560✔
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 void __ort_alloc_default_startup(ort_alloc_t* allocator) {
3,280✔
79
    OrtStatus* status =
3,280✔
80
        api->RegisterAllocator(
3,280✔
81
            php_ort_environment(),
82
            (OrtAllocator*)
83
                &__ort_alloc_default_for_onnx);
84
    if (status) {
3,280✔
85
        fprintf(stderr,
×
86
            "ort: failed to register allocator: %s\n",
87
            api->GetErrorMessage(status));
×
88

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

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

104
    void *raw = pemalloc(
71,760✔
105
        (size * count) + alignment - 1 + sizeof(void*), 1);
106

107
    uintptr_t address = (uintptr_t)raw + sizeof(void*);
71,760✔
108
    
109
    // Ensure we have space for the raw pointer before alignment
110
    uintptr_t aligned = (address + alignment - 1) & ~(uintptr_t)(alignment - 1);
71,760✔
111
    
112
    // If there's not enough space before aligned address, move to next boundary
113
    if (aligned - (uintptr_t)raw < sizeof(void*)) {
71,760✔
114
        aligned += alignment;
×
115
    }
116

117
    void **start = (void**)aligned;
71,760✔
118
    start[-1] = raw;
71,760✔
119

120
    return (void*)start;
71,760✔
121
}
122

123
static void* __ort_alloc_default_memcpy(void *dest, const void *src, size_t n) {
11,856✔
124
    if (n == 0) {
11,856✔
125
        return dest;
126
    }
127
    return memcpy(dest, src, n);
11,844✔
128
}
129

130
static void __ort_alloc_default_free(void* ptr) {
71,088✔
131
    if (ptr) {
71,088✔
132
        void *raw =
71,088✔
133
            ((void**)ptr)[-1];
134
        pefree(raw, 1);
71,088✔
135
    }
136
}
71,088✔
137

138
static void __ort_alloc_default_shutdown(ort_alloc_t* allocator) {}
3,280✔
139

140
ORT_TLS ort_alloc_t __ort_allocator = {
141
    .alloc    = __ort_alloc_default_alloc,
142
    .memcpy   = __ort_alloc_default_memcpy,
143
    .free     = __ort_alloc_default_free,
144

145
    .startup  = __ort_alloc_default_startup,
146
    .shutdown = __ort_alloc_default_shutdown,
147

148
    .alignment = sizeof(void*) * 2,
149
};
150

151
void ort_alloc_align(size_t alignment) {
5,064✔
152
    assert(
5,064✔
153
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
154
        alignment >= sizeof(void*));          /* minimum alignment */
155

156
    __ort_allocator.alignment = alignment;
5,064✔
157
}
5,064✔
158

159
size_t ort_alloc_alignment(void) {
23,616✔
160
    return __ort_allocator.alignment;
23,616✔
161
}
162

163
zend_bool ort_alloc_aligned(void* ptr) {
×
164
    return (zend_bool)
×
165
        ((uintptr_t)ptr % 
×
166
            __ort_allocator.alignment) == 0;
×
167
}
168

169
ort_memcpy_func_t ort_alloc_memcpy(ort_memcpy_func_t memcpy) {
5,064✔
170
    ort_memcpy_func_t fallback = __ort_allocator.memcpy;
5,064✔
171

172
    if (memcpy) {
5,064✔
173
        __ort_allocator.memcpy = memcpy;
5,064✔
174
    }
175

176
    return fallback;
5,064✔
177
}
178

179
void* ort_alloc(size_t size, size_t count) {
71,760✔
180
    return __ort_allocator.alloc(size, count,
71,760✔
181
        __ort_allocator.alignment);
182
}
183

184
void* ort_memcpy(void *dest, const void *src, size_t n) {
11,856✔
185
    return __ort_allocator.memcpy(dest, src, n);
11,856✔
186
}
187

188
void ort_free(void* ptr) {
71,088✔
189
    __ort_allocator.free(ptr);
71,088✔
190
}
71,088✔
191

192
PHP_MINIT_FUNCTION(ORT_ALLOC) 
3,280✔
193
{
194
    if (__ort_allocator.startup) {
3,280✔
195
        __ort_allocator.startup(&__ort_allocator);
3,280✔
196
    }
197

198
    return SUCCESS;
3,280✔
199
}
200

201
PHP_MSHUTDOWN_FUNCTION(ORT_ALLOC)
3,280✔
202
{
203
    if (__ort_allocator.shutdown) {
3,280✔
204
        __ort_allocator.shutdown(&__ort_allocator);
3,280✔
205
    }
206

207
    return SUCCESS;
3,280✔
208
}
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