From db2bf1e024b8bc082086f460e295e7a5ec93c204 Mon Sep 17 00:00:00 2001 From: Alexander Petrov Date: Wed, 29 Jul 2020 01:13:49 +0200 Subject: [PATCH] issue-433 increase the number of constructed Jsonnet handles. --- c-bindings/c-bindings.go | 12 ++++++------ c-bindings/handles.go | 16 ++++++++-------- c-bindings/internal.h | 8 ++++---- c-bindings/libjsonnet.cpp | 4 ++-- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/c-bindings/c-bindings.go b/c-bindings/c-bindings.go index 98e66fa..6a6a812 100644 --- a/c-bindings/c-bindings.go +++ b/c-bindings/c-bindings.go @@ -106,12 +106,12 @@ func jsonnet_make() *C.struct_JsonnetVm { os.Exit(1) } - return C.jsonnet_internal_make_vm_with_id(C.uint32_t(id)) + return C.jsonnet_internal_make_vm_with_id(C.uint64_t(id)) } //export jsonnet_destroy func jsonnet_destroy(vmRef *C.struct_JsonnetVm) { - if err := handles.free(uint32(vmRef.id)); err != nil { + if err := handles.free(uint64(vmRef.id)); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } @@ -120,7 +120,7 @@ func jsonnet_destroy(vmRef *C.struct_JsonnetVm) { } func getVM(vmRef *C.struct_JsonnetVm) *vm { - ref, err := handles.get(uint32(vmRef.id)) + ref, err := handles.get(uint64(vmRef.id)) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) @@ -411,7 +411,7 @@ func jsonnet_json_destroy(vmRef *C.struct_JsonnetVm, v *C.struct_JsonnetJsonValu jsonnet_json_destroy(vmRef, child) } - if err := handles.free(uint32(v.id)); err != nil { + if err := handles.free(uint64(v.id)); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } @@ -427,11 +427,11 @@ func createJSONValue(vmRef *C.struct_JsonnetVm, val interface{}) *C.struct_Jsonn os.Exit(1) } - return C.jsonnet_internal_make_json_with_id(C.uint32_t(id)) + return C.jsonnet_internal_make_json_with_id(C.uint64_t(id)) } func getJSONValue(jsonRef *C.struct_JsonnetJsonValue) *jsonValue { - ref, err := handles.get(uint32(jsonRef.id)) + ref, err := handles.get(uint64(jsonRef.id)) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) diff --git a/c-bindings/handles.go b/c-bindings/handles.go index 6d1d118..00da2c4 100644 --- a/c-bindings/handles.go +++ b/c-bindings/handles.go @@ -5,7 +5,7 @@ import ( "fmt" ) -const maxID = 100000 +const maxID = (1 << 63) - 1 // Because of Go GC, there are restrictions on keeping Go pointers in C. // We cannot just pass *jsonnet.VM/JsonValue to C. So instead we use "handle" structs in C @@ -18,7 +18,7 @@ const maxID = 100000 // handlesTable is the set of active, valid Jsonnet allocated handles type handlesTable struct { objects []interface{} - freedIDs []uint32 + freedIDs []uint64 } // errMaxNumberOfOpenHandles tells that there was an attempt to create more than maxID open handles @@ -28,14 +28,14 @@ var errMaxNumberOfOpenHandles = fmt.Errorf("maximum number of constructed Jsonne var errInvalidHandle = errors.New("invalid handle ID was provided") // make registers the new object as a handle and returns the corresponding ID -func (h *handlesTable) make(obj interface{}) (uint32, error) { - var id uint32 +func (h *handlesTable) make(obj interface{}) (uint64, error) { + var id uint64 if len(h.freedIDs) > 0 { id, h.freedIDs = h.freedIDs[len(h.freedIDs)-1], h.freedIDs[:len(h.freedIDs)-1] h.objects[id-1] = obj } else { - id = uint32(len(h.objects) + 1) + id = uint64(len(h.objects) + 1) if id > maxID { return 0, errMaxNumberOfOpenHandles @@ -48,7 +48,7 @@ func (h *handlesTable) make(obj interface{}) (uint32, error) { } // free marks the given handle ID as unused -func (h *handlesTable) free(id uint32) error { +func (h *handlesTable) free(id uint64) error { if err := h.ensureValidID(id); err != nil { return err } @@ -60,7 +60,7 @@ func (h *handlesTable) free(id uint32) error { } // get returns the corresponding object for the provided ID -func (h *handlesTable) get(id uint32) (interface{}, error) { +func (h *handlesTable) get(id uint64) (interface{}, error) { if err := h.ensureValidID(id); err != nil { return nil, err } @@ -69,7 +69,7 @@ func (h *handlesTable) get(id uint32) (interface{}, error) { } // ensureValidID returns an error if the given handle ID is invalid, otherwise returns nil -func (h *handlesTable) ensureValidID(id uint32) error { +func (h *handlesTable) ensureValidID(id uint64) error { if id == 0 || uint64(id) > uint64(len(h.objects)) { return errInvalidHandle } diff --git a/c-bindings/internal.h b/c-bindings/internal.h index d8c60ac..f8d54a2 100644 --- a/c-bindings/internal.h +++ b/c-bindings/internal.h @@ -3,17 +3,17 @@ #include struct JsonnetVm { - uint32_t id; + uint64_t id; }; -struct JsonnetVm *jsonnet_internal_make_vm_with_id(uint32_t id); +struct JsonnetVm *jsonnet_internal_make_vm_with_id(uint64_t id); void jsonnet_internal_free_vm(struct JsonnetVm *x); struct JsonnetJsonValue { - uint32_t id; + uint64_t id; }; -struct JsonnetJsonValue *jsonnet_internal_make_json_with_id(uint32_t id); +struct JsonnetJsonValue *jsonnet_internal_make_json_with_id(uint64_t id); void jsonnet_internal_free_json(struct JsonnetJsonValue *x); typedef struct JsonnetJsonValue *JsonnetNativeCallback(void *ctx, diff --git a/c-bindings/libjsonnet.cpp b/c-bindings/libjsonnet.cpp index 1669ce2..48b070b 100644 --- a/c-bindings/libjsonnet.cpp +++ b/c-bindings/libjsonnet.cpp @@ -6,7 +6,7 @@ extern "C" { #include "internal.h" } -struct JsonnetVm *jsonnet_internal_make_vm_with_id(uint32_t id) { +struct JsonnetVm *jsonnet_internal_make_vm_with_id(uint64_t id) { JsonnetVm *vm = new JsonnetVm(); vm->id = id; return vm; @@ -16,7 +16,7 @@ void jsonnet_internal_free_vm(struct JsonnetVm *x) { delete(x); } -struct JsonnetJsonValue *jsonnet_internal_make_json_with_id(uint32_t id) { +struct JsonnetJsonValue *jsonnet_internal_make_json_with_id(uint64_t id) { JsonnetJsonValue *json = new JsonnetJsonValue(); json->id = id; return json;