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

krakjoe / ort / 16440320110

22 Jul 2025 09:27AM UTC coverage: 92.876% (-0.3%) from 93.144%
16440320110

push

github

krakjoe
develop better scaling internally and externally ... for many reasons

235 of 248 new or added lines in 6 files covered. (94.76%)

14 existing lines in 2 files now uncovered.

5828 of 6275 relevant lines covered (92.88%)

81892.33 hits per line

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

71.05
/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,624✔
38
    const OrtAllocator* allocator) {
39
    if (!minfo) {
6,624✔
40
        OrtStatus* status =
3,312✔
41
            api->CreateCpuMemoryInfo(
3,312✔
42
                OrtDeviceAllocator, OrtMemTypeDefault, &minfo);
43
        if (status) {
3,312✔
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,624✔
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) {
6,704✔
81
    if (__ort_alloc_default_initialized) {
6,704✔
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;
3,312✔
88

89
    OrtStatus* status =
3,312✔
90
        api->RegisterAllocator(
3,312✔
91
            php_ort_environment(),
92
            (OrtAllocator*)
93
                &__ort_alloc_default_for_onnx);
94
    if (status) {
3,312✔
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) {
72,192✔
110
    assert(
72,192✔
111
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
112
        alignment >= sizeof(void*));          /* minimum alignment */
113

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

117
    uintptr_t address = (uintptr_t)raw + sizeof(void*);
72,192✔
118
    
119
    // Ensure we have space for the raw pointer before alignment
120
    uintptr_t aligned = (address + alignment - 1) & ~(uintptr_t)(alignment - 1);
72,192✔
121
    
122
    // If there's not enough space before aligned address, move to next boundary
123
    if (aligned - (uintptr_t)raw < sizeof(void*)) {
72,192✔
124
        aligned += alignment;
×
125
    }
126

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

130
    return (void*)start;
72,192✔
131
}
132

133
static void* __ort_alloc_default_memcpy(void *dest, const void *src, size_t n) {
11,856✔
134
    if (n == 0) {
11,856✔
135
        return dest;
136
    }
137
    return memcpy(dest, src, n);
11,844✔
138
}
139

140
static void __ort_alloc_default_free(void* ptr) {
71,520✔
141
    if (ptr) {
71,520✔
142
        void *raw =
71,520✔
143
            ((void**)ptr)[-1];
144
        pefree(raw, 1);
71,520✔
145
    }
146
}
71,520✔
147

148
static void __ort_alloc_default_shutdown(ort_alloc_t* allocator) {}
6,703✔
149

150
ORT_TLS ort_alloc_t __ort_allocator = {
151
    .alloc    = __ort_alloc_default_alloc,
152
    .memcpy   = __ort_alloc_default_memcpy,
153
    .free     = __ort_alloc_default_free,
154

155
    .startup  = __ort_alloc_default_startup,
156
    .shutdown = __ort_alloc_default_shutdown,
157

158
    .alignment = sizeof(void*) * 2,
159
};
160

161
void ort_alloc_align(size_t alignment) {
5,028✔
162
    assert(
5,028✔
163
        (alignment & (alignment - 1)) == 0 && /* power of 2 */
164
        alignment >= sizeof(void*));          /* minimum alignment */
165

166
    __ort_allocator.alignment = alignment;
5,028✔
167
}
5,028✔
168

UNCOV
169
size_t ort_alloc_alignment(void) {
×
UNCOV
170
    return __ort_allocator.alignment;
×
171
}
172

173
zend_bool ort_alloc_aligned(void* ptr) {
×
174
    return (zend_bool)
×
175
        ((uintptr_t)ptr % 
×
176
            __ort_allocator.alignment) == 0;
×
177
}
178

179
ort_memcpy_func_t ort_alloc_memcpy(ort_memcpy_func_t memcpy) {
5,028✔
180
    ort_memcpy_func_t fallback = __ort_allocator.memcpy;
5,028✔
181

182
    if (memcpy) {
5,028✔
183
        __ort_allocator.memcpy = memcpy;
5,028✔
184
    }
185

186
    return fallback;
5,028✔
187
}
188

189
void* ort_alloc(size_t size, size_t count) {
72,192✔
190
    return __ort_allocator.alloc(size, count,
72,192✔
191
        __ort_allocator.alignment);
192
}
193

194
void* ort_memcpy(void *dest, const void *src, size_t n) {
11,856✔
195
    return __ort_allocator.memcpy(dest, src, n);
11,856✔
196
}
197

198
void ort_free(void* ptr) {
71,520✔
199
    __ort_allocator.free(ptr);
71,520✔
200
}
71,520✔
201

202
void ort_alloc_startup()
6,704✔
203
{
204
    if (__ort_allocator.startup) {
6,704✔
205
        __ort_allocator.startup(&__ort_allocator);
6,704✔
206
    }
207
}
6,704✔
208

209
void ort_alloc_shutdown()
6,704✔
210
{
211
    if (__ort_allocator.shutdown) {
6,704✔
212
        __ort_allocator.shutdown(&__ort_allocator);
6,704✔
213
    }
214
}
6,702✔
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