github.com/moontrade/nogc@v0.1.7/collections/wormhole/wormhole_cgo_unsafe.go (about) 1 //go:build !tinygo && (amd64 || arm64) 2 // +build !tinygo 3 // +build amd64 arm64 4 5 package wormhole 6 7 /* 8 #cgo LDFLAGS: -L. -lstdc++ 9 #cgo CXXFLAGS: -std=c++20 -I. 10 #include <stdlib.h> 11 #include <pthread.h> 12 #include "lib.h" 13 #include "kv.h" 14 #include "wh.h" 15 16 typedef struct { 17 size_t ptr; 18 } qsbr_create_t; 19 20 void do_qsbr_create(size_t arg0, size_t arg1) { 21 qsbr_create_t* args = (qsbr_create_t*)(void*)arg0; 22 args->ptr = (size_t)qsbr_create(); 23 } 24 25 typedef struct { 26 size_t ptr; 27 } qsbr_destroy_t; 28 29 void do_qsbr_destroy(size_t arg0, size_t arg1) { 30 qsbr_destroy_t* args = (qsbr_destroy_t*)(void*)arg0; 31 qsbr_destroy((struct qsbr *)args->ptr); 32 } 33 34 typedef struct { 35 size_t ptr; 36 size_t ref; 37 size_t success; 38 } qsbr_register_t; 39 40 void do_qsbr_register(size_t arg0, size_t arg1) { 41 qsbr_register_t* args = (qsbr_register_t*)(void*)arg0; 42 args->success = qsbr_register((struct qsbr *)args->ptr, (struct qsbr_ref *)args->ref) ? 1 : 0; 43 } 44 45 typedef struct { 46 size_t ptr; 47 size_t ref; 48 } qsbr_unregister_t; 49 50 void do_qsbr_unregister(size_t arg0, size_t arg1) { 51 qsbr_unregister_t* args = (qsbr_unregister_t*)(void*)arg0; 52 qsbr_unregister((struct qsbr *)args->ptr, (struct qsbr_ref *)args->ref); 53 } 54 55 typedef struct { 56 size_t ptr; 57 u64 target; 58 } qsbr_wait_t; 59 60 void do_qsbr_wait(size_t arg0, size_t arg1) { 61 qsbr_wait_t* args = (qsbr_wait_t*)(void*)arg0; 62 qsbr_wait((struct qsbr *)args->ptr, args->target); 63 } 64 65 typedef struct { 66 size_t ref; 67 u64 target; 68 } qsbr_update_t; 69 70 void do_qsbr_update(size_t arg0, size_t arg1) { 71 qsbr_update_t* args = (qsbr_update_t*)(void*)arg0; 72 qsbr_update((struct qsbr_ref *)args->ref, args->target); 73 } 74 75 typedef struct { 76 size_t ref; 77 } qsbr_park_t; 78 79 void do_qsbr_park(size_t arg0, size_t arg1) { 80 qsbr_park_t* args = (qsbr_park_t*)(void*)arg0; 81 qsbr_park((struct qsbr_ref *)args->ref); 82 } 83 84 typedef struct { 85 size_t ref; 86 } qsbr_resume_t; 87 88 void do_qsbr_resume(size_t arg0, size_t arg1) { 89 qsbr_resume_t* args = (qsbr_resume_t*)(void*)arg0; 90 qsbr_resume((struct qsbr_ref *)args->ref); 91 } 92 93 typedef struct { 94 size_t ptr; 95 } wh_create_t; 96 97 void do_wh_create(size_t arg0, size_t arg1) { 98 wh_create_t* args = (wh_create_t*)(void*)arg0; 99 args->ptr = (size_t)wh_create(); 100 } 101 102 typedef struct { 103 size_t wh; 104 size_t ref; 105 } wh_ref_t; 106 107 void do_wh_ref(size_t arg0, size_t arg1) { 108 wh_ref_t* args = (wh_ref_t*)(void*)arg0; 109 args->ref = (size_t)wh_ref((struct wormhole *)args->wh); 110 } 111 112 typedef struct { 113 size_t ref; 114 size_t key; 115 size_t value; 116 size_t success; 117 u32 key_len; 118 u32 value_len; 119 } wh_put_t; 120 121 void do_wh_put(size_t arg0, size_t arg1) { 122 wh_put_t* args = (wh_put_t*)(void*)arg0; 123 args->success = wh_put((struct wormref *)args->ref, (const void *)args->key, args->key_len, (const void *)args->value, args->value_len) ? 1 : 0; 124 } 125 126 typedef struct { 127 size_t ref; 128 size_t key; 129 size_t success; 130 u32 key_len; 131 } wh_del_t; 132 133 void do_wh_del(size_t arg0, size_t arg1) { 134 wh_del_t* args = (wh_del_t*)(void*)arg0; 135 args->success = wh_del((struct wormref *)args->ref, (const void *)args->key, args->key_len) ? 1 : 0; 136 } 137 138 typedef struct { 139 size_t ref; 140 size_t key; 141 size_t value; 142 size_t success; 143 u32 key_len; 144 u32 value_len; 145 u32 value_len_out; 146 u32 padding; 147 } wh_get_t; 148 149 void do_wh_get(size_t arg0, size_t arg1) { 150 wh_get_t* args = (wh_get_t*)(void*)arg0; 151 args->success = wh_get((struct wormref *)args->ref, (const void *)args->key, args->key_len, (void *)args->value, args->value_len, &args->value_len_out) ? 1 : 0; 152 } 153 154 typedef struct { 155 size_t ptr; 156 } whunsafe_create_t; 157 158 void do_whunsafe_create(size_t arg0, size_t arg1) { 159 whunsafe_create_t* args = (whunsafe_create_t*)(void*)arg0; 160 args->ptr = (size_t)whunsafe_create(&kvmap_mm_ndf); 161 } 162 163 typedef struct { 164 size_t map; 165 size_t key; 166 size_t value; 167 u32 key_len; 168 u32 value_len; 169 } whunsafe_get_t; 170 171 void do_whunsafe_get(size_t arg0, size_t arg1) { 172 whunsafe_get_t* args = (whunsafe_get_t*)(void*)arg0; 173 struct kref key; 174 key.len = args->key_len; 175 key.ptr = (const u8 *)args->key; 176 struct kv out; 177 struct kv* result = whunsafe_get((struct wormhole *)args->map, &key, &out); 178 if (!result) { 179 return; 180 } 181 //args->value = (size_t)result->privptr; 182 //args->value_len = (u32)result->kvlen; 183 } 184 185 typedef struct { 186 size_t map; 187 size_t key; 188 size_t value; 189 size_t success; 190 u32 key_len; 191 u32 value_len; 192 } whunsafe_put_t; 193 194 void do_whunsafe_put(size_t arg0, size_t arg1) { 195 whunsafe_put_t* args = (whunsafe_put_t*)(void*)arg0; 196 struct kv * const newkv = kv_create((const void *)args->key, args->key_len, (const void *)args->value, args->value_len); 197 if (newkv == NULL) { 198 args->success = 0; 199 return; 200 } 201 args->success = whunsafe_put((struct wormhole *)args->map, newkv) ? 1 : 0; 202 } 203 204 */ 205 import "C" 206 import ( 207 "github.com/moontrade/nogc" 208 "github.com/moontrade/nogc/unsafecgo/cgo" 209 "unsafe" 210 ) 211 212 type QSBR C.struct_qsbr 213 type QSBRRef C.struct_qsbr_ref 214 type WH C.struct_wormhole 215 type Ref C.struct_wormref 216 type KV C.struct_kv 217 218 func NewQSBR() *QSBR { 219 args := struct { 220 ptr uintptr 221 }{} 222 p := uintptr(unsafe.Pointer(&args)) 223 cgo.NonBlocking((*byte)(C.do_qsbr_create), p, 0) 224 return (*QSBR)(nogc.Pointer(args.ptr).Unsafe()) 225 } 226 227 func (q *QSBR) Close() error { 228 args := struct { 229 ptr uintptr 230 }{ 231 ptr: uintptr(unsafe.Pointer(q)), 232 } 233 p := uintptr(unsafe.Pointer(&args)) 234 cgo.NonBlocking((*byte)(C.do_qsbr_destroy), p, 0) 235 return nil 236 } 237 238 func (q *QSBR) Register(ref *QSBRRef) bool { 239 args := struct { 240 ptr uintptr 241 ref uintptr 242 success uintptr 243 }{ 244 ptr: uintptr(unsafe.Pointer(q)), 245 ref: uintptr(unsafe.Pointer(ref)), 246 } 247 p := uintptr(unsafe.Pointer(&args)) 248 cgo.NonBlocking((*byte)(C.do_qsbr_register), p, 0) 249 return args.success == 1 250 } 251 252 func (q *QSBR) Unregister(ref *QSBRRef) { 253 args := struct { 254 ptr uintptr 255 ref uintptr 256 }{ 257 ptr: uintptr(unsafe.Pointer(q)), 258 ref: uintptr(unsafe.Pointer(ref)), 259 } 260 p := uintptr(unsafe.Pointer(&args)) 261 cgo.NonBlocking((*byte)(C.do_qsbr_unregister), p, 0) 262 } 263 264 func (q *QSBR) Update(ref *QSBRRef, v uint64) { 265 args := struct { 266 ref uintptr 267 v uint64 268 }{ 269 ref: uintptr(unsafe.Pointer(ref)), 270 v: v, 271 } 272 p := uintptr(unsafe.Pointer(&args)) 273 cgo.NonBlocking((*byte)(C.do_qsbr_update), p, 0) 274 } 275 276 func (q *QSBR) Park(ref *QSBRRef) { 277 args := struct { 278 ref uintptr 279 }{ 280 ref: uintptr(unsafe.Pointer(ref)), 281 } 282 p := uintptr(unsafe.Pointer(&args)) 283 cgo.NonBlocking((*byte)(C.do_qsbr_park), p, 0) 284 } 285 286 func (q *QSBR) Resume(ref *QSBRRef) { 287 args := struct { 288 ref uintptr 289 }{ 290 ref: uintptr(unsafe.Pointer(ref)), 291 } 292 p := uintptr(unsafe.Pointer(&args)) 293 cgo.NonBlocking((*byte)(C.do_qsbr_resume), p, 0) 294 } 295 296 func (q *QSBR) Wait(target uint64) { 297 args := struct { 298 ptr uintptr 299 target uint64 300 }{ 301 ptr: uintptr(unsafe.Pointer(q)), 302 target: target, 303 } 304 p := uintptr(unsafe.Pointer(&args)) 305 cgo.NonBlocking((*byte)(C.do_qsbr_wait), p, 0) 306 } 307 308 type Unsafe struct { 309 ptr uintptr 310 } 311 312 func NewUnsafe() Unsafe { 313 args := struct { 314 ptr uintptr 315 }{} 316 p := uintptr(unsafe.Pointer(&args)) 317 cgo.NonBlocking((*byte)(C.do_whunsafe_create), p, 0) 318 return Unsafe{ptr: args.ptr} 319 } 320 321 func (wh Unsafe) Get(key nogc.FatPointer) nogc.FatPointer { 322 /* 323 typedef struct { 324 size_t ref; 325 size_t key; 326 size_t value; 327 size_t success; 328 u32 key_len; 329 u32 value_len; 330 u32 value_len_out; 331 u32 padding; 332 } wh_get_t; 333 */ 334 args := struct { 335 ref uintptr 336 key uintptr 337 value uintptr 338 keyLen uint32 339 valueLen uint32 340 }{ 341 ref: wh.ptr, 342 key: uintptr(key.Pointer), 343 keyLen: uint32(key.Len()), 344 } 345 p := uintptr(unsafe.Pointer(&args)) 346 cgo.NonBlocking((*byte)(C.do_whunsafe_get), p, 0) 347 return nogc.FatPointerOf(nogc.Pointer(args.value), uintptr(args.valueLen)) 348 } 349 350 func (wh Unsafe) Put(key, value nogc.FatPointer) bool { 351 /* 352 typedef struct { 353 size_t ref; 354 size_t key; 355 size_t value; 356 size_t success; 357 u32 key_len; 358 u32 value_len; 359 } wh_put_t; 360 */ 361 args := struct { 362 ref uintptr 363 key uintptr 364 value uintptr 365 success uintptr 366 keyLen uint32 367 valueLen uint32 368 }{ 369 ref: wh.ptr, 370 key: uintptr(key.Pointer), 371 value: uintptr(value.Pointer), 372 keyLen: uint32(key.Len()), 373 valueLen: uint32(value.Len()), 374 } 375 p := uintptr(unsafe.Pointer(&args)) 376 cgo.NonBlocking((*byte)(C.do_whunsafe_put), p, 0) 377 return args.success == 1 378 } 379 380 //func (wh Unsafe) Delete(ref *Ref, key, value nogc.FatPointer) bool { 381 // /* 382 // typedef struct { 383 // size_t ref; 384 // size_t key; 385 // size_t success; 386 // u32 key_len; 387 // } wh_put_t; 388 // */ 389 // args := struct { 390 // ref uintptr 391 // key uintptr 392 // success uintptr 393 // keyLen uint32 394 // }{ 395 // ref: uintptr(unsafe.Pointer(ref)), 396 // key: uintptr(key.Pointer), 397 // keyLen: uint32(key.Len()), 398 // } 399 // p := uintptr(unsafe.Pointer(&args)) 400 // cgo.NonBlocking((*byte)(C.do_wh_del), p, 0) 401 // return args.success == 1 402 //} 403 404 type Wormhole struct { 405 ptr uintptr 406 } 407 408 func NewWormhole() Wormhole { 409 args := struct { 410 ptr uintptr 411 }{} 412 p := uintptr(unsafe.Pointer(&args)) 413 cgo.NonBlocking((*byte)(C.do_wh_create), p, 0) 414 return Wormhole{ptr: args.ptr} 415 } 416 417 func (wh Wormhole) Ref() *Ref { 418 args := struct { 419 wh uintptr 420 ref uintptr 421 }{ 422 wh: wh.ptr, 423 } 424 p := uintptr(unsafe.Pointer(&args)) 425 cgo.NonBlocking((*byte)(C.do_wh_ref), p, 0) 426 return (*Ref)(nogc.Pointer(args.ref).Unsafe()) 427 } 428 429 func (wh Wormhole) Get(ref *Ref, key, value nogc.FatPointer) bool { 430 /* 431 typedef struct { 432 size_t ref; 433 size_t key; 434 size_t value; 435 size_t success; 436 u32 key_len; 437 u32 value_len; 438 u32 value_len_out; 439 u32 padding; 440 } wh_get_t; 441 */ 442 args := struct { 443 ref uintptr 444 key uintptr 445 value uintptr 446 success uintptr 447 keyLen uint32 448 valueLen uint32 449 valueLenOut uint32 450 padding uint32 451 }{ 452 ref: uintptr(unsafe.Pointer(ref)), 453 key: uintptr(key.Pointer), 454 value: uintptr(value.Pointer), 455 keyLen: uint32(key.Len()), 456 valueLen: uint32(value.Len()), 457 } 458 p := uintptr(unsafe.Pointer(&args)) 459 cgo.NonBlocking((*byte)(C.do_wh_get), p, 0) 460 return args.success == 1 461 } 462 463 func (wh Wormhole) Put(ref *Ref, key, value nogc.FatPointer) bool { 464 /* 465 typedef struct { 466 size_t ref; 467 size_t key; 468 size_t value; 469 size_t success; 470 u32 key_len; 471 u32 value_len; 472 } wh_put_t; 473 */ 474 args := struct { 475 ref uintptr 476 key uintptr 477 value uintptr 478 success uintptr 479 keyLen uint32 480 valueLen uint32 481 }{ 482 ref: uintptr(unsafe.Pointer(ref)), 483 key: uintptr(key.Pointer), 484 value: uintptr(value.Pointer), 485 keyLen: uint32(key.Len()), 486 valueLen: uint32(value.Len()), 487 } 488 p := uintptr(unsafe.Pointer(&args)) 489 cgo.NonBlocking((*byte)(C.do_wh_put), p, 0) 490 return args.success == 1 491 } 492 493 func (wh Wormhole) Delete(ref *Ref, key, value nogc.FatPointer) bool { 494 /* 495 typedef struct { 496 size_t ref; 497 size_t key; 498 size_t success; 499 u32 key_len; 500 } wh_put_t; 501 */ 502 args := struct { 503 ref uintptr 504 key uintptr 505 success uintptr 506 keyLen uint32 507 }{ 508 ref: uintptr(unsafe.Pointer(ref)), 509 key: uintptr(key.Pointer), 510 keyLen: uint32(key.Len()), 511 } 512 p := uintptr(unsafe.Pointer(&args)) 513 cgo.NonBlocking((*byte)(C.do_wh_del), p, 0) 514 return args.success == 1 515 }