go-jsonnet/c-bindings/libjsonnet.cpp
mikehoyle eac7db4471 c-bindings: Add jsonnet_realloc export
This adds the realloc function as an export in c-bindings.go so
that it is included in the generated header and can be used by
dependent libraries.
2022-10-21 17:55:35 +01:00

97 lines
2.5 KiB
C++

#include <stdio.h>
#include <stdlib.h>
extern "C" {
#include "libjsonnet.h"
#include "internal.h"
}
struct JsonnetVm *jsonnet_internal_make_vm_with_id(uintptr_t id) {
JsonnetVm *vm = new JsonnetVm();
vm->id = id;
return vm;
}
void jsonnet_internal_free_vm(struct JsonnetVm *x) {
delete(x);
}
struct JsonnetJsonValue *jsonnet_internal_make_json_with_id(uintptr_t id) {
JsonnetJsonValue *json = new JsonnetJsonValue();
json->id = id;
return json;
}
void jsonnet_internal_free_json(struct JsonnetJsonValue *x) {
delete(x);
}
struct JsonnetJsonValue* jsonnet_internal_execute_native(JsonnetNativeCallback *cb,
void *ctx,
const struct JsonnetJsonValue *const *argv,
int *success)
{
return (cb)(ctx, argv, success);
}
char* jsonnet_internal_execute_import(JsonnetImportCallback *cb,
void *ctx,
const char *base,
const char *rel,
char **found_here,
int *success)
{
return (cb)(ctx, base, rel, found_here, success);
}
int jsonnet_internal_execute_writer(JsonnetIoWriterCallback *cb,
const void *buf,
size_t nbytes,
int *success)
{
return (cb)(buf, nbytes, success);
}
void jsonnet_internal_free_string(char *str) {
if (str != nullptr) {
::free(str);
}
}
void jsonnet_gc_min_objects(struct JsonnetVm *vm, unsigned v) {
// no-op
}
void jsonnet_gc_growth_trigger(struct JsonnetVm *vm, double v) {
// no-op
}
static void memory_panic(void)
{
fputs("FATAL ERROR: A memory allocation error occurred.\n", stderr);
abort();
}
char *jsonnet_internal_realloc(JsonnetVm *vm, char *str, size_t sz)
{
(void)vm;
if (str == nullptr) {
if (sz == 0)
return nullptr;
auto *r = static_cast<char *>(::malloc(sz));
if (r == nullptr)
memory_panic();
return r;
} else {
if (sz == 0) {
::free(str);
return nullptr;
} else {
auto *r = static_cast<char *>(::realloc(str, sz));
if (r == nullptr)
memory_panic();
return r;
}
}
}