| 276 | } |
| 277 | |
| 278 | Error new_alloc(void** p, size_t bytes, bool host) { |
| 279 | Runtime& r = rt(); |
| 280 | const size_t n = bytes ? bytes : 1; |
| 281 | MTL::Buffer* b = r.dev->newBuffer(n, MTL::ResourceStorageModeShared | MTL::ResourceHazardTrackingModeUntracked); |
| 282 | if (!b) return fail(kErrorMemoryAllocation); |
| 283 | Alloc a{b, b->gpuAddress(), (uint8_t*) b->contents(), n, host}; |
| 284 | alloc_log(host ? "host alloc" : "device alloc", n); |
| 285 | { |
| 286 | std::lock_guard<std::mutex> lk(r.mu); |
| 287 | r.by_gpu[a.gpu] = a; |
| 288 | r.by_cpu[(uintptr_t) a.cpu] = a.gpu; |
| 289 | r.rs->addAllocation(b); |
| 290 | r.rs->commit(); |
| 291 | } |
| 292 | *p = host ? (void*) a.cpu : (void*) (uintptr_t) a.gpu; |
| 293 | return kSuccess; |
| 294 | } |
| 295 | |
| 296 | Error free_alloc(void* p) { |
| 297 | if (!p) return kSuccess; |
no test coverage detected